Class yii\sphinx\QueryBuilder
Inheritance | yii\sphinx\QueryBuilder » yii\base\BaseObject |
---|---|
Implements | yii\base\Configurable |
Available since version | 2.0 |
Source Code | https://github.com/yiisoft/yii2-sphinx/blob/master/QueryBuilder.php |
QueryBuilder builds a SELECT SQL statement based on the specification given as a yii\sphinx\Query object.
QueryBuilder can also be used to build SQL statements such as INSERT, REPLACE, UPDATE, DELETE, from a yii\sphinx\Query object.
Public Properties
Property | Type | Description | Defined By |
---|---|---|---|
$db | yii\sphinx\Connection | The Sphinx connection. | yii\sphinx\QueryBuilder |
$matchBuilder | yii\sphinx\MatchBuilder | Match builder. | yii\sphinx\QueryBuilder |
$querySeparator | string | Separator between different SQL queries. | yii\sphinx\QueryBuilder |
$separator | string | The separator between different fragments of a SQL statement. | yii\sphinx\QueryBuilder |
Protected Properties
Property | Type | Description | Defined By |
---|---|---|---|
$conditionBuilders | array | Map of query condition to builder methods. | yii\sphinx\QueryBuilder |
Public Methods
Method | Description | Defined By |
---|---|---|
__call() | Calls the named method which is not a class method. | yii\base\BaseObject |
__construct() | Constructor. | yii\sphinx\QueryBuilder |
__get() | Returns the value of an object property. | yii\base\BaseObject |
__isset() | Checks if a property is set, i.e. defined and not null. | yii\base\BaseObject |
__set() | Sets value of an object property. | yii\base\BaseObject |
__unset() | Sets an object property to null. | yii\base\BaseObject |
batchInsert() | Generates a batch INSERT SQL statement. | yii\sphinx\QueryBuilder |
batchReplace() | Generates a batch REPLACE SQL statement. | yii\sphinx\QueryBuilder |
build() | Generates a SELECT SQL statement from a yii\sphinx\Query object. | yii\sphinx\QueryBuilder |
buildAndCondition() | Connects two or more SQL expressions with the AND or OR operator. |
yii\sphinx\QueryBuilder |
buildBetweenCondition() | Creates an SQL expressions with the BETWEEN operator. |
yii\sphinx\QueryBuilder |
buildColumns() | Processes columns and properly quote them if necessary. | yii\sphinx\QueryBuilder |
buildCondition() | Parses the condition specification and generates the corresponding SQL expression. | yii\sphinx\QueryBuilder |
buildFrom() | yii\sphinx\QueryBuilder | |
buildGroupBy() | yii\sphinx\QueryBuilder | |
buildHashCondition() | Creates a condition based on column-value pairs. | yii\sphinx\QueryBuilder |
buildHaving() | yii\sphinx\QueryBuilder | |
buildInCondition() | Creates an SQL expressions with the IN operator. |
yii\sphinx\QueryBuilder |
buildLikeCondition() | Creates an SQL expressions with the LIKE operator. |
yii\sphinx\QueryBuilder |
buildLimit() | yii\sphinx\QueryBuilder | |
buildMatch() | yii\sphinx\QueryBuilder | |
buildNotCondition() | Inverts an SQL expressions with NOT operator. |
yii\sphinx\QueryBuilder |
buildOption() | yii\sphinx\QueryBuilder | |
buildOrderBy() | yii\sphinx\QueryBuilder | |
buildOrderByAndLimit() | Builds the ORDER BY and LIMIT/OFFSET clauses and appends them to the given SQL. | yii\sphinx\QueryBuilder |
buildSelect() | yii\sphinx\QueryBuilder | |
buildSimpleCondition() | Creates an SQL expressions like "column" operator value . |
yii\sphinx\QueryBuilder |
buildWhere() | yii\sphinx\QueryBuilder | |
buildWithin() | yii\sphinx\QueryBuilder | |
callKeywords() | Builds a SQL statement for returning tokenized and normalized forms of the keywords, and, optionally, keyword statistics. | yii\sphinx\QueryBuilder |
callSnippets() | Builds a SQL statement for call snippet from provided data and query, using specified index settings. | yii\sphinx\QueryBuilder |
canGetProperty() | Returns a value indicating whether a property can be read. | yii\base\BaseObject |
canSetProperty() | Returns a value indicating whether a property can be set. | yii\base\BaseObject |
className() | Returns the fully qualified name of this class. | yii\base\BaseObject |
delete() | Creates a DELETE SQL statement. | yii\sphinx\QueryBuilder |
getMatchBuilder() | yii\sphinx\QueryBuilder | |
hasMethod() | Returns a value indicating whether a method is defined. | yii\base\BaseObject |
hasProperty() | Returns a value indicating whether a property is defined. | yii\base\BaseObject |
init() | Initializes the object. | yii\base\BaseObject |
insert() | Creates an INSERT SQL statement. | yii\sphinx\QueryBuilder |
replace() | Creates an REPLACE SQL statement. | yii\sphinx\QueryBuilder |
truncateIndex() | Builds a SQL statement for truncating an index. | yii\sphinx\QueryBuilder |
update() | Creates an UPDATE SQL statement. | yii\sphinx\QueryBuilder |
Protected Methods
Method | Description | Defined By |
---|---|---|
buildCompositeInCondition() | yii\sphinx\QueryBuilder | |
buildFacets() | yii\sphinx\QueryBuilder | |
buildShowMeta() | Builds SHOW META query. | yii\sphinx\QueryBuilder |
composeColumnValue() | Composes column value for SQL, taking in account the column type. | yii\sphinx\QueryBuilder |
generateBatchInsertReplace() | Generates a batch INSERT/REPLACE SQL statement. | yii\sphinx\QueryBuilder |
generateInsertReplace() | Generates INSERT/REPLACE SQL statement. | yii\sphinx\QueryBuilder |
Constants
Constant | Value | Description | Defined By |
---|---|---|---|
PARAM_PREFIX | ':qp' | The prefix for automatically generated query binding parameters. | yii\sphinx\QueryBuilder |
Property Details
Map of query condition to builder methods. These methods are used by buildCondition() to build SQL conditions from array syntax.
The Sphinx connection.
Match builder.
Separator between different SQL queries. This is mainly used by build() when generating a SQL statement.
The separator between different fragments of a SQL statement. Defaults to an empty space. This is mainly used by build() when generating a SQL statement.
Method Details
Constructor.
public void __construct ( $connection, $config = [] ) | ||
$connection | yii\sphinx\Connection | The Sphinx connection. |
$config | array | Name-value pairs that will be used to initialize the object properties |
Generates a batch INSERT SQL statement.
For example,
$sql = $queryBuilder->batchInsert('idx_user', ['id', 'name', 'age'], [
[1, 'Tom', 30],
[2, 'Jane', 20],
[3, 'Linda', 25],
], $params);
Note that the values in each row must match the corresponding column names.
public string batchInsert ( $index, $columns, $rows, &$params ) | ||
$index | string | The index that new rows will be inserted into. |
$columns | array | The column names |
$rows | array | The rows to be batch inserted into the index |
$params | array | The binding parameters that will be generated by this method. They should be bound to the Sphinx command later. |
return | string | The batch INSERT SQL statement |
---|
Generates a batch REPLACE SQL statement.
For example,
$sql = $queryBuilder->batchReplace('idx_user', ['id', 'name', 'age'], [
[1, 'Tom', 30],
[2, 'Jane', 20],
[3, 'Linda', 25],
], $params);
Note that the values in each row must match the corresponding column names.
public string batchReplace ( $index, $columns, $rows, &$params ) | ||
$index | string | The index that new rows will be replaced. |
$columns | array | The column names |
$rows | array | The rows to be batch replaced in the index |
$params | array | The binding parameters that will be generated by this method. They should be bound to the Sphinx command later. |
return | string | The batch INSERT SQL statement |
---|
Generates a SELECT SQL statement from a yii\sphinx\Query object.
public array build ( $query, $params = [] ) | ||
$query | yii\sphinx\Query | The yii\sphinx\Query object from which the SQL statement will be generated |
$params | array | The parameters to be bound to the generated SQL statement. These parameters will be included in the result with the additional parameters generated during the query building process. |
return | array | The generated SQL statement (the first array element) and the corresponding
parameters to be bound to the SQL statement (the second array element). The parameters returned
include those provided in |
---|---|---|
throws | yii\base\NotSupportedException | if query contains 'join' option. |
Connects two or more SQL expressions with the AND
or OR
operator.
public string buildAndCondition ( $indexes, $operator, $operands, &$params ) | ||
$indexes | yii\sphinx\IndexSchema[] | List of indexes, which affected by query |
$operator | string | The operator to use for connecting the given operands |
$operands | array | The SQL expressions to connect. |
$params | array | The binding parameters to be populated |
return | string | The generated SQL expression |
---|
Creates an SQL expressions with the BETWEEN
operator.
public string buildBetweenCondition ( $indexes, $operator, $operands, &$params ) | ||
$indexes | yii\sphinx\IndexSchema[] | List of indexes, which affected by query |
$operator | string | The operator to use (e.g. |
$operands | array | The first operand is the column name. The second and third operands describe the interval that column value should be in. |
$params | array | The binding parameters to be populated |
return | string | The generated SQL expression |
---|---|---|
throws | yii\db\Exception | if wrong number of operands have been given. |
Processes columns and properly quote them if necessary.
It will join all columns into a string with comma as separators.
public string buildColumns ( $columns ) | ||
$columns | string|array | The columns to be processed |
return | string | The processing result |
---|
protected string buildCompositeInCondition ( $indexes, $operator, $columns, $values, &$params ) | ||
$indexes | yii\sphinx\IndexSchema[] | List of indexes, which affected by query |
$operator | string | The operator to use (e.g. |
$columns | array | |
$values | array | |
$params | array | The binding parameters to be populated |
return | string | The generated SQL expression |
---|
Parses the condition specification and generates the corresponding SQL expression.
public string buildCondition ( $indexes, $condition, &$params ) | ||
$indexes | yii\sphinx\IndexSchema[] | List of indexes, which affected by query |
$condition | string|array | The condition specification. Please refer to yii\sphinx\Query::where() on how to specify a condition. |
$params | array | The binding parameters to be populated |
return | string | The generated SQL expression |
---|---|---|
throws | yii\db\Exception | if the condition is in bad format |
protected string buildFacets ( $facets, &$params ) | ||
$facets | array | Facet specifications |
$params | array | The binding parameters to be populated |
return | string | The FACET clause build from \yii\sphinx\query |
---|---|---|
throws | yii\base\InvalidConfigException | on invalid facet specification. |
public string buildFrom ( $indexes, &$params ) | ||
$indexes | array | |
$params | array | The binding parameters to be populated |
return | string | The FROM clause built from \yii\sphinx\query. |
---|
public string buildGroupBy ( $columns, $limit ) | ||
$columns | array | Group columns |
$limit | integer | Group limit |
return | string | The GROUP BY clause |
---|
Creates a condition based on column-value pairs.
public string buildHashCondition ( $indexes, $condition, &$params ) | ||
$indexes | yii\sphinx\IndexSchema[] | List of indexes, which affected by query |
$condition | array | The condition specification. |
$params | array | The binding parameters to be populated |
return | string | The generated SQL expression |
---|
public string buildHaving ( $indexes, $condition, &$params ) | ||
$indexes | string[] | List of index names, which affected by query |
$condition | string|array | |
$params | array | The binding parameters to be populated |
return | string | The HAVING clause built from yii\sphinx\Query::$having. |
---|
Creates an SQL expressions with the IN
operator.
public string buildInCondition ( $indexes, $operator, $operands, &$params ) | ||
$indexes | yii\sphinx\IndexSchema[] | List of indexes, which affected by query |
$operator | string | The operator to use (e.g. |
$operands | array | The first operand is the column name. If it is an array
a composite IN condition will be generated.
The second operand is an array of values that column value should be among.
If it is an empty array the generated expression will be a |
$params | array | The binding parameters to be populated |
return | string | The generated SQL expression |
---|---|---|
throws | yii\db\Exception | if wrong number of operands have been given. |
Creates an SQL expressions with the LIKE
operator.
public string buildLikeCondition ( $indexes, $operator, $operands, &$params ) | ||
$indexes | yii\sphinx\IndexSchema[] | List of indexes, which affected by query |
$operator | string | The operator to use (e.g. |
$operands | array | An array of two or three operands
|
$params | array | The binding parameters to be populated |
return | string | The generated SQL expression |
---|---|---|
throws | yii\base\InvalidParamException | if wrong number of operands have been given. |
public string buildLimit ( $limit, $offset ) | ||
$limit | integer | |
$offset | integer | |
return | string | The LIMIT and OFFSET clauses built from \yii\sphinx\query. |
---|
public string buildMatch ( $match, &$params ) | ||
$match | string|yii\db\Expression|yii\sphinx\MatchExpression | Match condition |
$params | array | The binding parameters to be populated |
return | string | Generated MATCH expression |
---|
Inverts an SQL expressions with NOT
operator.
public string buildNotCondition ( $indexes, $operator, $operands, &$params ) | ||
$indexes | yii\sphinx\IndexSchema[] | List of indexes, which affected by query |
$operator | string | The operator to use for connecting the given operands |
$operands | array | The SQL expressions to connect. |
$params | array | The binding parameters to be populated |
return | string | The generated SQL expression |
---|---|---|
throws | yii\base\InvalidParamException | if wrong number of operands have been given. |
public string buildOption ( $options, &$params ) | ||
$options | array | Query options in format: optionName => optionValue |
$params | array | The binding parameters to be populated |
return | string | The OPTION clause build from \yii\sphinx\query |
---|
public string buildOrderBy ( $columns ) | ||
$columns | array | |
return | string | The ORDER BY clause built from \yii\sphinx\query. |
---|
Builds the ORDER BY and LIMIT/OFFSET clauses and appends them to the given SQL.
public string buildOrderByAndLimit ( $sql, $orderBy, $limit, $offset ) | ||
$sql | string | The existing SQL (without ORDER BY/LIMIT/OFFSET) |
$orderBy | array | The order by columns. See yii\sphinx\Query::orderBy() for more details on how to specify this parameter. |
$limit | integer | The limit number. See yii\sphinx\Query::limit() for more details. |
$offset | integer | The offset number. See yii\sphinx\Query::offset() for more details. |
return | string | The SQL completed with ORDER BY/LIMIT/OFFSET (if any) |
---|
public string buildSelect ( $columns, &$params, $distinct = false, $selectOption = null ) | ||
$columns | array | |
$params | array | The binding parameters to be populated |
$distinct | boolean | |
$selectOption | string | |
return | string | The SELECT clause built from \yii\sphinx\query. |
---|
Builds SHOW META query.
protected string buildShowMeta ( $showMeta, &$params ) | ||
$showMeta | boolean|string|yii\db\Expression | Show meta specification. |
$params | array | The binding parameters to be populated |
return | string | SHOW META query, if it does not required - empty string. |
---|
Creates an SQL expressions like "column" operator value
.
public string buildSimpleCondition ( $indexes, $operator, $operands, &$params ) | ||
$indexes | yii\sphinx\IndexSchema[] | List of indexes, which affected by query |
$operator | string | The operator to use. Anything could be used e.g. |
$operands | array | Contains two column names. |
$params | array | The binding parameters to be populated |
return | string | The generated SQL expression |
---|---|---|
throws | yii\base\InvalidParamException | if count($operands) is not 2 |
public string buildWhere ( $indexes, $condition, &$params, $match = null ) | ||
$indexes | string[] | List of index names, which affected by query |
$condition | string|array | |
$params | array | The binding parameters to be populated |
$match | string|yii\db\Expression|null | |
return | string | The WHERE clause built from \yii\sphinx\query. |
---|
public string buildWithin ( $columns ) | ||
$columns | array | |
return | string | The ORDER BY clause built from \yii\sphinx\query. |
---|
Builds a SQL statement for returning tokenized and normalized forms of the keywords, and, optionally, keyword statistics.
public string callKeywords ( $index, $text, $fetchStatistic, &$params ) | ||
$index | string | The name of the index from which to take the text processing settings |
$text | string | The text to break down to keywords. |
$fetchStatistic | boolean | Whether to return document and hit occurrence statistics |
$params | array | The binding parameters that will be modified by this method so that they can be bound to the Sphinx command later. |
return | string | The SQL statement for call keywords. |
---|
Builds a SQL statement for call snippet from provided data and query, using specified index settings.
public string callSnippets ( $index, $source, $match, $options, &$params ) | ||
$index | string | Name of the index, from which to take the text processing settings. |
$source | string|array | Is the source data to extract a snippet from. It could be either a single string or array of strings. |
$match | string | The full-text query to build snippets for. |
$options | array | List of options in format: optionName => optionValue |
$params | array | The binding parameters that will be modified by this method so that they can be bound to the Sphinx command later. |
return | string | The SQL statement for call snippets. |
---|
Composes column value for SQL, taking in account the column type.
protected string composeColumnValue ( $indexes, $columnName, $value, &$params ) | ||
$indexes | yii\sphinx\IndexSchema[] | List of indexes, which affected by query |
$columnName | string | Name of the column |
$value | mixed | Raw column value |
$params | array | The binding parameters to be populated |
return | string | SQL expression, which represents column value |
---|
Creates a DELETE SQL statement.
For example,
$sql = $queryBuilder->delete('idx_user', 'status = 0');
The method will properly escape the index and column names.
public string delete ( $index, $condition, &$params ) | ||
$index | string | The index where the data will be deleted from. |
$condition | array|string | The condition that will be put in the WHERE part. Please refer to yii\sphinx\Query::where() on how to specify condition. |
$params | array | The binding parameters that will be modified by this method so that they can be bound to the Sphinx command later. |
return | string | The DELETE SQL |
---|
Generates a batch INSERT/REPLACE SQL statement.
protected string generateBatchInsertReplace ( $statement, $index, $columns, $rows, &$params ) | ||
$statement | string | Statement ot be generated. |
$index | string | The affected index name. |
$columns | array | The column data (name => value). |
$rows | array | The rows to be batch inserted into the index |
$params | array | The binding parameters that will be generated by this method. |
return | string | Generated SQL |
---|
Generates INSERT/REPLACE SQL statement.
protected string generateInsertReplace ( $statement, $index, $columns, &$params ) | ||
$statement | string | Statement ot be generated. |
$index | string | The affected index name. |
$columns | array | The column data (name => value). |
$params | array | The binding parameters that will be generated by this method. |
return | string | Generated SQL |
---|
public yii\sphinx\MatchBuilder getMatchBuilder ( ) | ||
return | yii\sphinx\MatchBuilder | Match builder. |
---|
Creates an INSERT SQL statement.
For example,
$sql = $queryBuilder->insert('idx_user', [
'name' => 'Sam',
'age' => 30,
'id' => 10,
], $params);
The method will properly escape the index and column names.
public string insert ( $index, $columns, &$params ) | ||
$index | string | The index that new rows will be inserted into. |
$columns | array | The column data (name => value) to be inserted into the index. |
$params | array | The binding parameters that will be generated by this method. They should be bound to the Sphinx command later. |
return | string | The INSERT SQL |
---|
Creates an REPLACE SQL statement.
For example,
$sql = $queryBuilder->replace('idx_user', [
'name' => 'Sam',
'age' => 30,
'id' => 10,
], $params);
The method will properly escape the index and column names.
public string replace ( $index, $columns, &$params ) | ||
$index | string | The index that new rows will be replaced. |
$columns | array | The column data (name => value) to be replaced in the index. |
$params | array | The binding parameters that will be generated by this method. They should be bound to the Sphinx command later. |
return | string | The INSERT SQL |
---|
Builds a SQL statement for truncating an index.
public string truncateIndex ( $index ) | ||
$index | string | The index to be truncated. The name will be properly quoted by the method. |
return | string | The SQL statement for truncating an index. |
---|
Creates an UPDATE SQL statement.
For example,
$params = [];
$sql = $queryBuilder->update('idx_user', ['status' => 1], 'age > 30', $params);
The method will properly escape the index and column names.
public string update ( $index, $columns, $condition, &$params, $options ) | ||
$index | string | The index to be updated. |
$columns | array | The column data (name => value) to be updated. |
$condition | array|string | The condition that will be put in the WHERE part. Please refer to yii\sphinx\Query::where() on how to specify condition. |
$params | array | The binding parameters that will be modified by this method so that they can be bound to the Sphinx command later. |
$options | array | List of options in format: optionName => optionValue |
return | string | The UPDATE SQL |
---|