QueryBuilder
API reference › yii › db › sqlite › QueryBuilder
QueryBuilder is the query builder for SQLite databases.
Properties
Section titled “Properties”| Property | Type | Description | Defined by |
|---|---|---|---|
$db | yii\ | The database connection. Default: null. | yii\ |
$separator | string | The separator between different fragments of a SQL statement. Default: ' '. | yii\ |
$typeMap | array | Mapping from abstract column types (keys) to physical column types (values). Default: [\. | QueryBuilder |
$conditionClasses protected | array | Map of condition aliases to condition classes. Default: []. Available since 2.0.14. | yii\ |
$expressionBuilders protected | string[]|yii\[] | Maps expression class to expression builder class. Default: []. Available since 2.0.14. | yii\ |
Methods
Section titled “Methods”| Method | Description | Defined by |
|---|---|---|
__construct() | Constructor. | yii\ |
__call() | Calls the named method which is not a class method. | yii\ |
__get() | Returns the value of an object property. | yii\ |
__isset() | Checks if a property is set, i.e. defined and not null. | yii\ |
__set() | Sets value of an object property. | yii\ |
__unset() | Sets an object property to null. | yii\ |
addCheck() | Creates a SQL command for adding a check constraint to an existing table. | QueryBuilder |
addColumn() | Builds a SQL statement for adding a new DB column. | yii\ |
addCommentOnColumn() | Builds a SQL command for adding comment to column. | QueryBuilder |
addCommentOnTable() | Builds a SQL command for adding comment to table. | QueryBuilder |
addDefaultValue() | Creates a SQL command for adding a default value constraint to an existing table. | QueryBuilder |
addForeignKey() | Builds a SQL statement for adding a foreign key constraint to an existing table. | QueryBuilder |
addPrimaryKey() | Builds a SQL statement for adding a primary key constraint to an existing table. | QueryBuilder |
addUnique() | Creates a SQL command for adding an unique constraint to an existing table. | QueryBuilder |
alterColumn() | Builds a SQL statement for changing the definition of a column. | QueryBuilder |
batchInsert() | Generates a batch INSERT SQL statement. | yii\ |
bindParam() | Helper method to add $value to $params array using PARAM_PREFIX. | yii\ |
build() | Generates a SELECT SQL statement from a Query object. | yii\ |
buildColumns() | Processes columns and properly quotes them if necessary. | yii\ |
buildCondition() | Parses the condition specification and generates the corresponding SQL expression. | yii\ |
buildExpression() | Builds given $expression | yii\ |
buildFrom() | yii\ | |
buildGroupBy() | yii\ | |
buildHaving() | yii\ | |
buildJoin() | yii\ | |
buildLimit() | QueryBuilder | |
buildOrderBy() | yii\ | |
buildOrderByAndLimit() | Builds the ORDER BY and LIMIT/OFFSET clauses and appends them to the given SQL. | yii\ |
buildSelect() | yii\ | |
buildUnion() | QueryBuilder | |
buildUnionOrderByAndLimit() | Builds the global ORDER BY and LIMIT/OFFSET clauses for a UNION query. | yii\ |
buildWhere() | yii\ | |
buildWithQueries() | yii\ | |
canGetProperty() | Returns a value indicating whether a property can be read. | yii\ |
canSetProperty() | Returns a value indicating whether a property can be set. | yii\ |
checkIntegrity() | Enables or disables integrity check. | QueryBuilder |
className() static | Returns the fully qualified name of this class. | yii\ |
createConditionFromArray() | Transforms $condition defined in array format (as described in Query::where() to instance of ConditionInterface according to $conditionClasses map. | yii\ |
createIndex() | Builds a SQL statement for creating a new index. | QueryBuilder |
createTable() | Builds a SQL statement for creating a new DB table. | yii\ |
createView() | Creates a SQL View. | yii\ |
delete() | Creates a DELETE SQL statement. | yii\ |
dropCheck() | Creates a SQL command for dropping a check constraint. | QueryBuilder |
dropColumn() | Builds a SQL statement for dropping a DB column. | QueryBuilder |
dropCommentFromColumn() | Builds a SQL command for adding comment to column. | QueryBuilder |
dropCommentFromTable() | Builds a SQL command for adding comment to table. | QueryBuilder |
dropDefaultValue() | Creates a SQL command for dropping a default value constraint. | QueryBuilder |
dropForeignKey() | Builds a SQL statement for dropping a foreign key constraint. | QueryBuilder |
dropIndex() | Builds a SQL statement for dropping an index. | QueryBuilder |
dropPrimaryKey() | Builds a SQL statement for removing a primary key constraint to an existing table. | QueryBuilder |
dropTable() | Builds a SQL statement for dropping a DB table. | yii\ |
dropUnique() | Creates a SQL command for dropping an unique constraint. | QueryBuilder |
dropView() | Drops a SQL View. | yii\ |
executeResetSequence() | Execute a SQL statement for resetting the sequence value of a table’s primary key. | yii\ |
getColumnType() | Converts an abstract column type into a physical column type. | yii\ |
getExpressionBuilder() | Gets object of ExpressionBuilderInterface that is suitable for $expression. | yii\ |
hasMethod() | Returns a value indicating whether a method is defined. | yii\ |
hasProperty() | Returns a value indicating whether a property is defined. | yii\ |
init() | Initializes the object. | yii\ |
insert() | Creates an INSERT SQL statement. | yii\ |
renameColumn() | Builds a SQL statement for renaming a column. | QueryBuilder |
renameTable() | Builds a SQL statement for renaming a DB table. | QueryBuilder |
resetSequence() | Creates a SQL statement for resetting the sequence value of a table’s primary key. | QueryBuilder |
selectExists() | Creates a SELECT EXISTS() SQL statement. | yii\ |
setConditionClasses() | Setter for $conditionClasses property. | yii\ |
setExpressionBuilders() | Setter for $expressionBuilders property. | yii\ |
truncateTable() | Builds a SQL statement for truncating a DB table. | QueryBuilder |
update() | Creates an UPDATE SQL statement. | yii\ |
upsert() | Creates an SQL statement to insert rows into a database table if they do not already exist (matching unique constraints), or update them if they do. | QueryBuilder |
concatUnionSql() protected | Concatenates the first SELECT statement with the UNION clauses built by buildUnion(). | QueryBuilder |
defaultConditionClasses() protected | Contains array of default condition classes. Extend this method, if you want to change default condition classes for the query builder. See $conditionClasses docs for details. | yii\ |
defaultExpressionBuilders() protected | Contains array of default expression builders. Extend this method and override it, if you want to change default expression builders for this query builder. See $expressionBuilders docs for details. | QueryBuilder |
extractAlias() protected | Extracts table alias if there is one or returns false | yii\ |
hasLimit() protected | Checks to see if the given limit is effective. | yii\ |
hasOffset() protected | Checks to see if the given offset is effective. | yii\ |
prepareInsertSelectSubQuery() protected | Prepare select-subquery and field names for INSERT INTO … SELECT SQL statement. | yii\ |
prepareInsertValues() protected | Prepares a VALUES part for an INSERT SQL statement. | yii\ |
prepareSelectQuery() protected | Performs driver-specific preparation after Query::prepare(). | yii\ |
prepareUpdateSets() protected | Prepares a SET parts for an UPDATE SQL statement. | yii\ |
prepareUpsertColumns() protected | yii\ | |
prepareUpsertSets() protected | Prepares the SET assignments for the update branch of an upsert statement. | yii\ |
Constants
Section titled “Constants”| Constant | Value | Description | Defined by |
|---|---|---|---|
PARAM_PREFIX | ':qp' | The prefix for automatically generated query binding parameters. | yii\ |
Method details
Section titled “Method details”addCheck()
Section titled “addCheck() ”public function addCheck(mixed $name, mixed $table, mixed $expression): stringCreates a SQL command for adding a check constraint to an existing table.
Parameters
| Parameter | Type | Description |
|---|---|---|
$name | mixed | The name of the check constraint. The name will be properly quoted by the method. |
$table | mixed | The table that the check constraint will be added to. The name will be properly quoted by the method. |
$expression | mixed | The SQL of the CHECK constraint. |
Returns
string: The SQL statement for adding a check constraint to an existing table.
Throws
yii\base\NotSupportedException: this is not supported by SQLite.
Since
2.0.13
addCommentOnColumn()
Section titled “addCommentOnColumn() ”public function addCommentOnColumn(mixed $table, mixed $column, mixed $comment): stringBuilds a SQL command for adding comment to column.
Parameters
| Parameter | Type | Description |
|---|---|---|
$table | mixed | The table whose column is to be commented. The table name will be properly quoted by the method. |
$column | mixed | The name of the column to be commented. The column name will be properly quoted by the method. |
$comment | mixed | The text of the comment to be added. The comment will be properly quoted by the method. |
Returns
string: The SQL statement for adding comment on column
Throws
Since
2.0.8
addCommentOnTable()
Section titled “addCommentOnTable() ”public function addCommentOnTable(mixed $table, mixed $comment): stringBuilds a SQL command for adding comment to table.
Parameters
| Parameter | Type | Description |
|---|---|---|
$table | mixed | The table whose column is to be commented. The table name will be properly quoted by the method. |
$comment | mixed | The text of the comment to be added. The comment will be properly quoted by the method. |
Returns
string: The SQL statement for adding comment on table
Throws
Since
2.0.8
addDefaultValue()
Section titled “addDefaultValue() ”public function addDefaultValue(mixed $name, mixed $table, mixed $column, mixed $value): stringCreates a SQL command for adding a default value constraint to an existing table.
Parameters
| Parameter | Type | Description |
|---|---|---|
$name | mixed | The name of the default value constraint. The name will be properly quoted by the method. |
$table | mixed | The table that the default value constraint will be added to. The name will be properly quoted by the method. |
$column | mixed | The name of the column to that the constraint will be added on. The name will be properly quoted by the method. |
$value | mixed | Default value. |
Returns
string: The SQL statement for adding a default value constraint to an existing table.
Throws
yii\base\NotSupportedException: this is not supported by SQLite.
Since
2.0.13
addForeignKey()
Section titled “addForeignKey() ”public function addForeignKey(string $name, string $table, string|array $columns, string $refTable, string|array $refColumns, string|null $delete = null, string|null $update = null): stringBuilds a SQL statement for adding a foreign key constraint to an existing table.
The method will properly quote the table and column names.
Parameters
| Parameter | Type | Description |
|---|---|---|
$name | string | The name of the foreign key constraint. |
$table | string | The table that the foreign key constraint will be added to. |
$columns | string| | 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| | 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 |
Returns
string: The SQL statement for adding a foreign key constraint to an existing table.
Throws
yii\base\NotSupportedException: this is not supported by SQLite
addPrimaryKey()
Section titled “addPrimaryKey() ”public function addPrimaryKey(string $name, string $table, string|array $columns): stringBuilds a SQL statement for adding a primary key constraint to an existing table.
Parameters
| Parameter | Type | Description |
|---|---|---|
$name | string | The name of the primary key constraint. |
$table | string | The table that the primary key constraint will be added to. |
$columns | string| | Comma separated string or array of columns that the primary key will consist of. |
Returns
string: The SQL statement for adding a primary key constraint to an existing table.
Throws
yii\base\NotSupportedException: this is not supported by SQLite
addUnique()
Section titled “addUnique() ”public function addUnique(mixed $name, mixed $table, mixed $columns): stringCreates a SQL command for adding an unique constraint to an existing table.
Parameters
| Parameter | Type | Description |
|---|---|---|
$name | mixed | The name of the unique constraint. The name will be properly quoted by the method. |
$table | mixed | The table that the unique constraint will be added to. The name will be properly quoted by the method. |
$columns | mixed | The name of the column to that the constraint will be added on. If there are multiple columns, separate them with commas. The name will be properly quoted by the method. |
Returns
string: The SQL statement for adding an unique constraint to an existing table.
Throws
yii\base\NotSupportedException: this is not supported by SQLite.
Since
2.0.13
alterColumn()
Section titled “alterColumn() ”public function alterColumn(string $table, string $column, string $type): stringBuilds a SQL statement for changing the definition of a column.
Parameters
| Parameter | Type | Description |
|---|---|---|
$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’. |
Returns
string: The SQL statement for changing the definition of a column.
Throws
yii\base\NotSupportedException: this is not supported by SQLite
buildLimit()
Section titled “buildLimit() ”public function buildLimit(mixed $limit, mixed $offset): stringParameters
| Parameter | Type | Description |
|---|---|---|
$limit | mixed | |
$offset | mixed |
Returns
string: The LIMIT and OFFSET clauses
buildUnion()
Section titled “buildUnion() ”public function buildUnion(mixed $unions, mixed &$params): stringParameters
| Parameter | Type | Description |
|---|---|---|
$unions | mixed | |
$params | mixed | The binding parameters to be populated |
Returns
string: The UNION clause built from Query::$union.
checkIntegrity()
Section titled “checkIntegrity() ”public function checkIntegrity(bool $check = true, string $schema = '', string $table = ''): stringEnables or disables integrity check.
Parameters
| Parameter | Type | Description |
|---|---|---|
$check | bool | Whether to turn on or off the integrity check. |
$schema | string | The schema of the tables. Meaningless for SQLite. |
$table | string | The table name. Meaningless for SQLite. |
Returns
string: The SQL statement for checking integrity.
createIndex()
Section titled “createIndex() ”public function createIndex(mixed $name, mixed $table, mixed $columns, mixed $unique = false): stringBuilds a SQL statement for creating a new index.
Parameters
| Parameter | Type | Description |
|---|---|---|
$name | mixed | The name of the index. The name will be properly quoted by the method. |
$table | mixed | The table that the new index will be created for. The table name will be properly quoted by the method. |
$columns | mixed | 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 | mixed | Whether to add UNIQUE constraint on the created index. |
Returns
string: The SQL statement for creating a new index.
dropCheck()
Section titled “dropCheck() ”public function dropCheck(mixed $name, mixed $table): stringCreates a SQL command for dropping a check constraint.
Parameters
| Parameter | Type | Description |
|---|---|---|
$name | mixed | The name of the check constraint to be dropped. The name will be properly quoted by the method. |
$table | mixed | The table whose check constraint is to be dropped. The name will be properly quoted by the method. |
Returns
string: The SQL statement for dropping a check constraint.
Throws
yii\base\NotSupportedException: this is not supported by SQLite.
Since
2.0.13
dropColumn()
Section titled “dropColumn() ”public function dropColumn(string $table, string $column): stringBuilds a SQL statement for dropping a DB column.
Parameters
| Parameter | Type | Description |
|---|---|---|
$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. |
Returns
string: The SQL statement for dropping a DB column.
Throws
yii\base\NotSupportedException: this is not supported by SQLite
dropCommentFromColumn()
Section titled “dropCommentFromColumn() ”public function dropCommentFromColumn(mixed $table, mixed $column): stringBuilds a SQL command for adding comment to column.
Parameters
| Parameter | Type | Description |
|---|---|---|
$table | mixed | The table whose column is to be commented. The table name will be properly quoted by the method. |
$column | mixed | The name of the column to be commented. The column name will be properly quoted by the method. |
Returns
string: The SQL statement for adding comment on column
Throws
Since
2.0.8
dropCommentFromTable()
Section titled “dropCommentFromTable() ”public function dropCommentFromTable(mixed $table): stringBuilds a SQL command for adding comment to table.
Parameters
| Parameter | Type | Description |
|---|---|---|
$table | mixed | The table whose column is to be commented. The table name will be properly quoted by the method. |
Returns
string: The SQL statement for adding comment on column
Throws
Since
2.0.8
dropDefaultValue()
Section titled “dropDefaultValue() ”public function dropDefaultValue(mixed $name, mixed $table): stringCreates a SQL command for dropping a default value constraint.
Parameters
| Parameter | Type | Description |
|---|---|---|
$name | mixed | The name of the default value constraint to be dropped. The name will be properly quoted by the method. |
$table | mixed | The table whose default value constraint is to be dropped. The name will be properly quoted by the method. |
Returns
string: The SQL statement for dropping a default value constraint.
Throws
yii\base\NotSupportedException: this is not supported by SQLite.
Since
2.0.13
dropForeignKey()
Section titled “dropForeignKey() ”public function dropForeignKey(string $name, string $table): stringBuilds a SQL statement for dropping a foreign key constraint.
Parameters
| Parameter | Type | Description |
|---|---|---|
$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. |
Returns
string: The SQL statement for dropping a foreign key constraint.
Throws
yii\base\NotSupportedException: this is not supported by SQLite
dropIndex()
Section titled “dropIndex() ”public function dropIndex(string $name, string $table): stringBuilds a SQL statement for dropping an index.
Parameters
| Parameter | Type | Description |
|---|---|---|
$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. |
Returns
string: The SQL statement for dropping an index.
dropPrimaryKey()
Section titled “dropPrimaryKey() ”public function dropPrimaryKey(string $name, string $table): stringBuilds a SQL statement for removing a primary key constraint to an existing table.
Parameters
| Parameter | Type | Description |
|---|---|---|
$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. |
Returns
string: The SQL statement for removing a primary key constraint from an existing table.
Throws
yii\base\NotSupportedException: this is not supported by SQLite
dropUnique()
Section titled “dropUnique() ”public function dropUnique(mixed $name, mixed $table): stringCreates a SQL command for dropping an unique constraint.
Parameters
| Parameter | Type | Description |
|---|---|---|
$name | mixed | The name of the unique constraint to be dropped. The name will be properly quoted by the method. |
$table | mixed | The table whose unique constraint is to be dropped. The name will be properly quoted by the method. |
Returns
string: The SQL statement for dropping an unique constraint.
Throws
yii\base\NotSupportedException: this is not supported by SQLite.
Since
2.0.13
renameColumn()
Section titled “renameColumn() ”public function renameColumn(string $table, string $oldName, string $newName): stringBuilds a SQL statement for renaming a column.
Parameters
| Parameter | Type | Description |
|---|---|---|
$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. |
Returns
string: The SQL statement for renaming a DB column.
Throws
yii\base\NotSupportedException: this is not supported by SQLite
renameTable()
Section titled “renameTable() ”public function renameTable(string $table, string $newName): stringBuilds a SQL statement for renaming a DB table.
Parameters
| Parameter | Type | Description |
|---|---|---|
$table | string | The table to be renamed. The name will be properly quoted by the method. |
$newName | string | The new table name. The name will be properly quoted by the method. |
Returns
string: The SQL statement for renaming a DB table.
resetSequence()
Section titled “resetSequence() ”public function resetSequence(string $tableName, int|null $value = null): stringCreates a SQL statement for resetting the sequence value of a table’s primary key.
The sequence will be reset such that the primary key of the next new row inserted will have the specified value
or 1.
Parameters
| Parameter | Type | Description |
|---|---|---|
$tableName | string | The name of the table whose primary key sequence will be reset. |
$value | int| | The integer 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. |
Returns
string: The SQL statement for resetting sequence.
Throws
yii\base\InvalidArgumentException: if the table does not exist or there is no sequence associated with the table.
truncateTable()
Section titled “truncateTable() ”public function truncateTable(string $table): stringBuilds a SQL statement for truncating a DB table.
Parameters
| Parameter | Type | Description |
|---|---|---|
$table | string | The table to be truncated. The name will be properly quoted by the method. |
Returns
string: The SQL statement for truncating a DB table.
upsert()
Section titled “upsert() ”public function upsert(mixed $table, mixed $insertColumns, mixed $updateColumns, mixed &$params): stringCreates an SQL statement to insert rows into a database table if they do not already exist (matching unique constraints), or update them if they do.
For example,
$sql = $queryBuilder->upsert('pages', [ 'name' => 'Front page', 'url' => 'https://example.com/', // url is unique 'visits' => 0,], [ 'visits' => new \yii\db\Expression('visits + 1'),], $params);The method will properly escape the table and column names.
Parameters
| Parameter | Type | Description |
|---|---|---|
$table | mixed | The table that new rows will be inserted into/updated in. |
$insertColumns | mixed | The column data (name => value) to be inserted into the table or instance of Query to perform INSERT INTO ... SELECT SQL statement. |
$updateColumns | mixed | The column data (name => value) to be updated if they already exist. If true is passed, the column data will be updated to match the insert column data. If false is passed, no update will be performed if the column data already exists. |
$params | mixed | The binding parameters that will be generated by this method. They should be bound to the DB command later. |
Returns
string: The resulting SQL.
Throws
yii\base\NotSupportedException: if this is not supported by the underlying DBMS.
Since
2.0.14
See also
concatUnionSql()
Section titled “concatUnionSql() ”protected function concatUnionSql(string $sql, string $union): stringConcatenates the first SELECT statement with the UNION clauses built by buildUnion().
SQLite does not support parenthesized UNION operands, so the first SELECT statement is left unwrapped.
Parameters
| Parameter | Type | Description |
|---|---|---|
$sql | string | The SQL of the first SELECT statement. |
$union | string | The UNION clauses. |
Returns
string: The compound SQL with the first statement parenthesized.
Since
22.0
defaultExpressionBuilders()
Section titled “defaultExpressionBuilders() ”protected function defaultExpressionBuilders(): arrayContains array of default expression builders. Extend this method and override it, if you want to change
default expression builders for this query builder. See $expressionBuilders docs for details.
Returns
array
Since
2.0.14