Property | Type | Description | |
---|---|---|---|
$db | the database connection. | ||
$separator | the separator between different fragments of a SQL statement. Defaults to an empty space. This is mainly used by QueryBuilder::build when generating a SQL statement. | ||
$typeMap | the abstract column types mapped to physical column types. This is mainly used to support creating/modifying tables using DB-independent data type specifications. Child classes should override this property to declare supported type mappings. |
Property | Type | Description | |
---|---|---|---|
$conditionBuilders | map of query condition to builder methods. These methods are used by [[buildCondition]] to build SQL conditions from array syntax. |
Method | Description | |
---|---|---|
__construct ( |
Constructor. | |
addColumn ( string $table, string $column, string $type ) : string | Builds a SQL statement for adding a new DB column. | |
addCommentOnColumn ( string $table, string $column, string $comment ) : string | Builds a SQL command for adding comment to column | |
addCommentOnTable ( string $table, string $comment ) : string | Builds a SQL command for adding comment to table | |
addForeignKey ( string $name, string $table, string | array $columns, string $refTable, string | array $refColumns, string $delete = null, string $update = null ) : string | Builds a SQL statement for adding a foreign key constraint to an existing table. | |
addPrimaryKey ( string $name, string $table, string | array $columns ) : string | Builds a SQL statement for adding a primary key constraint to an existing table. | |
alterColumn ( string $table, string $column, string $type ) : string | Builds a SQL statement for changing the definition of a column. | |
batchInsert ( string $table, array $columns, array $rows ) : string | Generates a batch INSERT SQL statement. | |
build ( |
Generates a SELECT SQL statement from a Query object. | |
buildAndCondition ( string $operator, array $operands, array &$params ) : string | Connects two or more SQL expressions with the AND or OR operator. | |
buildBetweenCondition ( string $operator, array $operands, array &$params ) : string | Creates an SQL expressions with the BETWEEN operator. | |
buildColumns ( string | array $columns ) : string | Processes columns and properly quotes them if necessary. | |
buildCondition ( string | array | yii\db\Expression $condition, array &$params ) : string | Parses the condition specification and generates the corresponding SQL expression. | |
buildExistsCondition ( string $operator, array $operands, array &$params ) : string | Creates an SQL expressions with the EXISTS operator. | |
buildFrom ( array $tables, array &$params ) : string | ||
buildGroupBy ( array $columns ) : string | ||
buildHashCondition ( array $condition, array &$params ) : string | Creates a condition based on column-value pairs. | |
buildHaving ( string | array $condition, array &$params ) : string | ||
buildInCondition ( string $operator, array $operands, array &$params ) : string | Creates an SQL expressions with the IN operator. | |
buildJoin ( array $joins, array &$params ) : string | ||
buildLikeCondition ( string $operator, array $operands, array &$params ) : string | Creates an SQL expressions with the LIKE operator. | |
buildLimit ( integer $limit, integer $offset ) : string | ||
buildNotCondition ( string $operator, array $operands, array &$params ) : string | Inverts an SQL expressions with NOT operator. | |
buildOrderBy ( array $columns ) : string | ||
buildOrderByAndLimit ( string $sql, array $orderBy, integer $limit, integer $offset ) : string | Builds the ORDER BY and LIMIT/OFFSET clauses and appends them to the given SQL. | |
buildSelect ( array $columns, array &$params, boolean $distinct = false, string $selectOption = null ) : string | ||
buildSimpleCondition ( string $operator, array $operands, array &$params ) : string | Creates an SQL expressions like "column" operator value. | |
buildUnion ( array $unions, array &$params ) : string | ||
buildWhere ( string | array $condition, array &$params ) : string | ||
checkIntegrity ( boolean $check = true, string $schema = '', string $table = '' ) : string | Builds a SQL statement for enabling or disabling integrity check. | |
createIndex ( string $name, string $table, string | array $columns, boolean $unique = false ) : string | Builds a SQL statement for creating a new index. | |
createTable ( string $table, array $columns, string $options = null ) : string | Builds a SQL statement for creating a new DB table. | |
delete ( string $table, array | string $condition, array &$params ) : string | Creates a DELETE SQL statement. | |
dropColumn ( string $table, string $column ) : string | Builds a SQL statement for dropping a DB column. | |
dropCommentFromColumn ( string $table, string $column ) : string | Builds a SQL command for adding comment to column | |
dropCommentFromTable ( string $table ) : string | Builds a SQL command for adding comment to table | |
dropForeignKey ( string $name, string $table ) : string | Builds a SQL statement for dropping a foreign key constraint. | |
dropIndex ( string $name, string $table ) : string | Builds a SQL statement for dropping an index. | |
dropPrimaryKey ( string $name, string $table ) : string | Builds a SQL statement for removing a primary key constraint to an existing table. | |
dropTable ( string $table ) : string | Builds a SQL statement for dropping a DB table. | |
getColumnType ( string | |
Converts an abstract column type into a physical column type. | |
insert ( string $table, array $columns, array &$params ) : string | Creates an INSERT SQL statement. | |
renameColumn ( string $table, string $oldName, string $newName ) : string | Builds a SQL statement for renaming a column. | |
renameTable ( string $oldName, string $newName ) : string | Builds a SQL statement for renaming a DB table. | |
resetSequence ( string $table, array | string $value = null ) : string | Creates a SQL statement for resetting the sequence value of a table's primary key. | |
selectExists ( string $rawSql ) : string | Creates a SELECT EXISTS() SQL statement. | |
truncateTable ( string $table ) : string | Builds a SQL statement for truncating a DB table. | |
update ( string $table, array $columns, array | string $condition, array &$params ) : string | Creates an UPDATE SQL statement. |
Method | Description | |
---|---|---|
buildCompositeInCondition ( string $operator, array | Traversable $columns, array $values, array &$params ) : string | Builds SQL for IN condition | |
buildSubqueryInCondition ( string $operator, array $columns, |
Builds SQL for IN condition | |
hasLimit ( mixed $limit ) : boolean | Checks to see if the given limit is effective. | |
hasOffset ( mixed $offset ) : boolean | Checks to see if the given offset is effective. |
Method | Description | |
---|---|---|
quoteTableNames ( array $tables, array &$params ) : array | Quotes table names passed |
public __construct ( |
||
$connection | the database connection. | |
$config | array | name-value pairs that will be used to initialize the object properties |
public addColumn ( string $table, string $column, string $type ) : string | ||
$table | string | the table that the new column will be added to. The table name will be properly quoted by the method. |
$column | string | the name of the new column. The name will be properly quoted by the method. |
$type | string | the column type. The [[getColumnType()]] method will be invoked to convert abstract column type (if any) into the physical one. Anything that is not recognized as abstract type will be kept in the generated SQL. For example, 'string' will be turned into 'varchar(255)', while 'string not null' will become 'varchar(255) not null'. |
return | string | the SQL statement for adding a new column. |
public addCommentOnColumn ( string $table, string $column, string $comment ) : string | ||
$table | string | the table whose column is to be commented. The table name will be properly quoted by the method. |
$column | string | the name of the column to be commented. The column name will be properly quoted by the method. |
$comment | string | the text of the comment to be added. The comment will be properly quoted by the method. |
return | string | the SQL statement for adding comment on column |
public addCommentOnTable ( string $table, string $comment ) : string | ||
$table | string | the table whose column is to be commented. The table name will be properly quoted by the method. |
$comment | string | the text of the comment to be added. The comment will be properly quoted by the method. |
return | string | the SQL statement for adding comment on table |
public addForeignKey ( string $name, string $table, string | array $columns, string $refTable, string | array $refColumns, string $delete = null, string $update = null ) : string | ||
$name | string | the name of the foreign key constraint. |
$table | string | the table that the foreign key constraint will be added to. |
$columns | string | array | the name of the column to that the constraint will be added on. If there are multiple columns, separate them with commas or use an array to represent them. |
$refTable | string | the table that the foreign key references to. |
$refColumns | string | array | the name of the column that the foreign key references to. If there are multiple columns, separate them with commas or use an array to represent them. |
$delete | string | the ON DELETE option. Most DBMS support these options: RESTRICT, CASCADE, NO ACTION, SET DEFAULT, SET NULL |
$update | string | the ON UPDATE option. Most DBMS support these options: RESTRICT, CASCADE, NO ACTION, SET DEFAULT, SET NULL |
return | string | the SQL statement for adding a foreign key constraint to an existing table. |
public addPrimaryKey ( string $name, string $table, string | array $columns ) : string | ||
$name | string | the name of the primary key constraint. |
$table | string | the table that the primary key constraint will be added to. |
$columns | string | array | comma separated string or array of columns that the primary key will consist of. |
return | string | the SQL statement for adding a primary key constraint to an existing table. |
public alterColumn ( string $table, string $column, string $type ) : string | ||
$table | string | the table whose column is to be changed. The table name will be properly quoted by the method. |
$column | string | the name of the column to be changed. The name will be properly quoted by the method. |
$type | string | the new column type. The [[getColumnType()]] method will be invoked to convert abstract column type (if any) into the physical one. Anything that is not recognized as abstract type will be kept in the generated SQL. For example, 'string' will be turned into 'varchar(255)', while 'string not null' will become 'varchar(255) not null'. |
return | string | the SQL statement for changing the definition of a column. |
public build ( |
||
$query | the [[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 `$params`. |
public buildAndCondition ( string $operator, array $operands, array &$params ) : string | ||
$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 |
public buildBetweenCondition ( string $operator, array $operands, array &$params ) : string | ||
$operator | string | the operator to use (e.g. `BETWEEN` or `NOT BETWEEN`) |
$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 |
protected buildCompositeInCondition ( string $operator, array | Traversable $columns, array $values, array &$params ) : string | ||
$operator | string | |
$columns | array | Traversable | |
$values | array | |
$params | array | |
return | string | SQL |
public buildCondition ( string | array | yii\db\Expression $condition, array &$params ) : string | ||
$condition | string | array | yii\db\Expression | the condition specification. Please refer to [[Query::where()]] on how to specify a condition. |
$params | array | the binding parameters to be populated |
return | string | the generated SQL expression |
public buildExistsCondition ( string $operator, array $operands, array &$params ) : string | ||
$operator | string | the operator to use (e.g. `EXISTS` or `NOT EXISTS`) |
$operands | array | contains only one element which is a [[Query]] object representing the sub-query. |
$params | array | the binding parameters to be populated |
return | string | the generated SQL expression |
public buildGroupBy ( array $columns ) : string | ||
$columns | array | |
return | string | the GROUP BY clause |
public buildInCondition ( string $operator, array $operands, array &$params ) : string | ||
$operator | string | the operator to use (e.g. `IN` or `NOT IN`) |
$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 `false` value if operator is `IN` and empty if operator is `NOT IN`. |
$params | array | the binding parameters to be populated |
return | string | the generated SQL expression |
public buildLikeCondition ( string $operator, array $operands, array &$params ) : string | ||
$operator | string | the operator to use (e.g. `LIKE`, `NOT LIKE`, `OR LIKE` or `OR NOT LIKE`) |
$operands | array | an array of two or three operands - The first operand is the column name. - The second operand is a single value or an array of values that column value should be compared with. If it is an empty array the generated expression will be a `false` value if operator is `LIKE` or `OR LIKE`, and empty if operator is `NOT LIKE` or `OR NOT LIKE`. - An optional third operand can also be provided to specify how to escape special characters in the value(s). The operand should be an array of mappings from the special characters to their escaped counterparts. If this operand is not provided, a default escape mapping will be used. You may use `false` or an empty array to indicate the values are already escaped and no escape should be applied. Note that when using an escape mapping (or the third operand is not provided), the values will be automatically enclosed within a pair of percentage characters. |
$params | array | the binding parameters to be populated |
return | string | the generated SQL expression |
public buildNotCondition ( string $operator, array $operands, array &$params ) : string | ||
$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 |
public buildOrderBy ( array $columns ) : string | ||
$columns | array | |
return | string | the ORDER BY clause built from [[Query::$orderBy]]. |
public buildOrderByAndLimit ( string $sql, array $orderBy, integer $limit, integer $offset ) : string | ||
$sql | string | the existing SQL (without ORDER BY/LIMIT/OFFSET) |
$orderBy | array | the order by columns. See [[Query::orderBy]] for more details on how to specify this parameter. |
$limit | integer | the limit number. See [[Query::limit]] for more details. |
$offset | integer | the offset number. See [[Query::offset]] for more details. |
return | string | the SQL completed with ORDER BY/LIMIT/OFFSET (if any) |
public buildSimpleCondition ( string $operator, array $operands, array &$params ) : string | ||
$operator | string | the operator to use. Anything could be used e.g. `>`, `<=`, etc. |
$operands | array | contains two column names. |
$params | array | the binding parameters to be populated |
return | string | the generated SQL expression |
public checkIntegrity ( boolean $check = true, string $schema = '', string $table = '' ) : string | ||
$check | boolean | whether to turn on or off the integrity check. |
$schema | string | the schema of the tables. Defaults to empty string, meaning the current or default schema. |
$table | string | the table name. Defaults to empty string, meaning that no table will be changed. |
return | string | the SQL statement for checking integrity |
public createIndex ( string $name, string $table, string | array $columns, boolean $unique = false ) : string | ||
$name | string | the name of the index. The name will be properly quoted by the method. |
$table | string | the table that the new index will be created for. The table name will be properly quoted by the method. |
$columns | string | array | the column(s) that should be included in the index. If there are multiple columns, separate them with commas or use an array to represent them. Each column name will be properly quoted by the method, unless a parenthesis is found in the name. |
$unique | boolean | whether to add UNIQUE constraint on the created index. |
return | string | the SQL statement for creating a new index. |
public createTable ( string $table, array $columns, string $options = null ) : string | ||
$table | string | the name of the table to be created. The name will be properly quoted by the method. |
$columns | array | the columns (name => definition) in the new table. |
$options | string | additional SQL fragment that will be appended to the generated SQL. |
return | string | the SQL statement for creating a new DB table. |
public delete ( string $table, array | string $condition, array &$params ) : string | ||
$table | string | the table where the data will be deleted from. |
$condition | array | string | the condition that will be put in the WHERE part. Please refer to [[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 DB command later. |
return | string | the DELETE SQL |
public dropColumn ( string $table, string $column ) : string | ||
$table | string | the table whose column is to be dropped. The name will be properly quoted by the method. |
$column | string | the name of the column to be dropped. The name will be properly quoted by the method. |
return | string | the SQL statement for dropping a DB column. |
public dropCommentFromColumn ( string $table, string $column ) : string | ||
$table | string | the table whose column is to be commented. The table name will be properly quoted by the method. |
$column | string | the name of the column to be commented. The column name will be properly quoted by the method. |
return | string | the SQL statement for adding comment on column |
public dropCommentFromTable ( string $table ) : string | ||
$table | string | the table whose column is to be commented. The table name will be properly quoted by the method. |
return | string | the SQL statement for adding comment on column |
public dropForeignKey ( string $name, string $table ) : string | ||
$name | string | the name of the foreign key constraint to be dropped. The name will be properly quoted by the method. |
$table | string | the table whose foreign is to be dropped. The name will be properly quoted by the method. |
return | string | the SQL statement for dropping a foreign key constraint. |
public dropIndex ( string $name, string $table ) : string | ||
$name | string | the name of the index to be dropped. The name will be properly quoted by the method. |
$table | string | the table whose index is to be dropped. The name will be properly quoted by the method. |
return | string | the SQL statement for dropping an index. |
public dropPrimaryKey ( string $name, string $table ) : string | ||
$name | string | the name of the primary key constraint to be removed. |
$table | string | the table that the primary key constraint will be removed from. |
return | string | the SQL statement for removing a primary key constraint from an existing table. |
public getColumnType ( string | |
||
$type | string | |
abstract column type |
return | string | physical column type. |
public insert ( string $table, array $columns, array &$params ) : string | ||
$table | string | the table that new rows will be inserted into. |
$columns | array | the column data (name => value) to be inserted into the table. |
$params | array | the binding parameters that will be generated by this method. They should be bound to the DB command later. |
return | string | the INSERT SQL |
public renameColumn ( string $table, string $oldName, string $newName ) : string | ||
$table | string | the table whose column is to be renamed. The name will be properly quoted by the method. |
$oldName | string | the old name of the column. The name will be properly quoted by the method. |
$newName | string | the new name of the column. The name will be properly quoted by the method. |
return | string | the SQL statement for renaming a DB column. |
public resetSequence ( string $table, array | string $value = null ) : string | ||
$table | string | the name of the table whose primary key sequence will be reset |
$value | array | string | the value for the primary key of the next new row inserted. If this is not set, the next new row's primary key will have a value 1. |
return | string | the SQL statement for resetting sequence |
public selectExists ( string $rawSql ) : string | ||
$rawSql | string | the subquery in a raw form to select from. |
return | string | the SELECT EXISTS() SQL statement. |
public truncateTable ( string $table ) : string | ||
$table | string | the table to be truncated. The name will be properly quoted by the method. |
return | string | the SQL statement for truncating a DB table. |
public update ( string $table, array $columns, array | string $condition, array &$params ) : string | ||
$table | string | the table 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 [[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 DB command later. |
return | string | the UPDATE SQL |
protected $conditionBuilders |
public $separator |