Skip to content
PHP Ecosystem
Explore

QueryBuilder

API reference › yii › db › oci › QueryBuilder

classyii\db\oci\QueryBuilder
Extendsyii\db\QueryBuilder » yii\base\BaseObject
Implementsyii\base\Configurable
Since2.0
Sourceframework/db/oci/QueryBuilder.php

QueryBuilder is the query builder for Oracle databases (version 12.1 and above).

PropertyTypeDescriptionDefined by
$dbyii\db\ConnectionThe database connection. Default: null.yii\db\QueryBuilder
$separatorstringThe separator between different fragments of a SQL statement. Default: ' '.yii\db\QueryBuilder
$typeMaparrayMapping from abstract column types (keys) to physical column types (values). Default: [\yii\db\oci\Schema::TYPE_PK => 'NUMBER(10) GENERATED BY DEFAULT ON NULL AS IDENTITY NOT NULL PRIMARY KEY', \yii\db\oci\Schema::TYPE_UPK => 'NUMBER(10) GENERATED BY DEFAULT ON NULL AS IDENTITY NOT NULL PRIMARY KEY', \yii\db\oci\Schema::TYPE_BIGPK => 'NUMBER(20) GENERATED BY DEFAULT ON NULL AS IDENTITY NOT NULL PRIMARY KEY', \yii\db\oci\Schema::TYPE_UBIGPK => 'NUMBER(20) GENERATED BY DEFAULT ON NULL AS IDENTITY NOT NULL PRIMARY KEY', \yii\db\oci\Schema::TYPE_CHAR => 'CHAR(1)', \yii\db\oci\Schema::TYPE_STRING => 'VARCHAR2(255)', \yii\db\oci\Schema::TYPE_TEXT => 'CLOB', \yii\db\oci\Schema::TYPE_TINYINT => 'NUMBER(3)', \yii\db\oci\Schema::TYPE_SMALLINT => 'NUMBER(5)', \yii\db\oci\Schema::TYPE_INTEGER => 'NUMBER(10)', \yii\db\oci\Schema::TYPE_BIGINT => 'NUMBER(20)', \yii\db\oci\Schema::TYPE_FLOAT => 'NUMBER', \yii\db\oci\Schema::TYPE_DOUBLE => 'NUMBER', \yii\db\oci\Schema::TYPE_DECIMAL => 'NUMBER', \yii\db\oci\Schema::TYPE_DATETIME => 'TIMESTAMP', \yii\db\oci\Schema::TYPE_TIMESTAMP => 'TIMESTAMP', \yii\db\oci\Schema::TYPE_TIME => 'TIMESTAMP', \yii\db\oci\Schema::TYPE_DATE => 'DATE', \yii\db\oci\Schema::TYPE_BINARY => 'BLOB', \yii\db\oci\Schema::TYPE_BOOLEAN => 'NUMBER(1)', \yii\db\oci\Schema::TYPE_MONEY => 'NUMBER(19,4)'].QueryBuilder
$conditionClasses protectedarrayMap of condition aliases to condition classes. Default: []. Available since 2.0.14.yii\db\QueryBuilder
$expressionBuilders protectedstring[]|yii\db\ExpressionBuilderInterface[]Maps expression class to expression builder class. Default: []. Available since 2.0.14.yii\db\QueryBuilder
MethodDescriptionDefined by
__construct()Constructor.yii\db\QueryBuilder
__call()Calls the named method which is not a class method.yii\base\BaseObject
__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
addCheck()Creates a SQL command for adding a check constraint to an existing table.yii\db\QueryBuilder
addColumn()Builds a SQL statement for adding a new DB column.yii\db\QueryBuilder
addCommentOnColumn()Builds a SQL command for adding comment to column.yii\db\QueryBuilder
addCommentOnTable()Builds a SQL command for adding comment to table.yii\db\QueryBuilder
addDefaultValue()Creates a SQL command for adding a default value constraint to an existing table.yii\db\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.yii\db\QueryBuilder
addUnique()Creates a SQL command for adding an unique constraint to an existing table.yii\db\QueryBuilder
alterColumn()Builds a SQL statement for changing the definition of a column.QueryBuilder
batchInsert()Generates a batch INSERT SQL statement.QueryBuilder
bindParam()Helper method to add $value to $params array using PARAM_PREFIX.yii\db\QueryBuilder
build()Generates a SELECT SQL statement from a Query object.yii\db\QueryBuilder
buildColumns()Processes columns and properly quotes them if necessary.yii\db\QueryBuilder
buildCondition()Parses the condition specification and generates the corresponding SQL expression.yii\db\QueryBuilder
buildExpression()Builds given $expressionyii\db\QueryBuilder
buildFrom()yii\db\QueryBuilder
buildGroupBy()yii\db\QueryBuilder
buildHaving()yii\db\QueryBuilder
buildJoin()yii\db\QueryBuilder
buildLimit()yii\db\QueryBuilder
buildOrderBy()yii\db\QueryBuilder
buildOrderByAndLimit()Builds the ORDER BY and LIMIT/OFFSET clauses and appends them to the given SQL.QueryBuilder
buildSelect()yii\db\QueryBuilder
buildUnion()yii\db\QueryBuilder
buildUnionOrderByAndLimit()Builds the global ORDER BY and LIMIT/OFFSET clauses for a UNION query.yii\db\QueryBuilder
buildWhere()yii\db\QueryBuilder
buildWithQueries()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
checkIntegrity()Builds a SQL statement for enabling or disabling integrity check.yii\db\QueryBuilder
className() staticReturns the fully qualified name of this class.yii\base\BaseObject
createConditionFromArray()Transforms $condition defined in array format (as described in Query::where() to instance of ConditionInterface according to $conditionClasses map.yii\db\QueryBuilder
createIndex()Builds a SQL statement for creating a new index.yii\db\QueryBuilder
createTable()Builds a SQL statement for creating a new DB table.yii\db\QueryBuilder
createView()Creates a SQL View.yii\db\QueryBuilder
delete()Creates a DELETE SQL statement.yii\db\QueryBuilder
dropCheck()Creates a SQL command for dropping a check constraint.yii\db\QueryBuilder
dropColumn()Builds a SQL statement for dropping a DB column.yii\db\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.yii\db\QueryBuilder
dropForeignKey()Builds a SQL statement for dropping a foreign key constraint.yii\db\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.yii\db\QueryBuilder
dropTable()Builds a SQL statement for dropping a DB table.yii\db\QueryBuilder
dropUnique()Creates a SQL command for dropping an unique constraint.yii\db\QueryBuilder
dropView()Drops a SQL View.yii\db\QueryBuilder
executeResetSequence()Execute a SQL statement for resetting the sequence value of a table’s primary key.QueryBuilder
getColumnType()Converts an abstract column type into a physical column type.yii\db\QueryBuilder
getExpressionBuilder()Gets object of ExpressionBuilderInterface that is suitable for $expression.yii\db\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\db\QueryBuilder
insert()Creates an INSERT SQL statement.QueryBuilder
mergeReturning()Appends a RETURNING clause for the given OUT placeholders, merging any locator clause already present.QueryBuilder
renameColumn()Builds a SQL statement for renaming a column.yii\db\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.yii\db\QueryBuilder
selectExists()Creates a SELECT EXISTS() SQL statement.QueryBuilder
setConditionClasses()Setter for $conditionClasses property.yii\db\QueryBuilder
setExpressionBuilders()Setter for $expressionBuilders property.yii\db\QueryBuilder
truncateTable()Builds a SQL statement for truncating a DB table.yii\db\QueryBuilder
update()Creates an UPDATE SQL statement.QueryBuilder
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() protectedConcatenates the first SELECT statement with the UNION clauses built by buildUnion().yii\db\QueryBuilder
defaultConditionClasses() protectedContains 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\db\QueryBuilder
defaultExpressionBuilders() protectedContains 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() protectedExtracts table alias if there is one or returns falseyii\db\QueryBuilder
hasLimit() protectedChecks to see if the given limit is effective.yii\db\QueryBuilder
hasOffset() protectedChecks to see if the given offset is effective.yii\db\QueryBuilder
prepareInsertSelectSubQuery() protectedPrepare select-subquery and field names for INSERT INTO … SELECT SQL statement.yii\db\QueryBuilder
prepareInsertValues() protectedPrepares a VALUES part for an INSERT SQL statement.QueryBuilder
prepareSelectQuery() protectedPerforms driver-specific preparation after Query::prepare().yii\db\QueryBuilder
prepareUpdateSets() protectedPrepares a SET parts for an UPDATE SQL statement.yii\db\QueryBuilder
prepareUpsertColumns() protectedyii\db\QueryBuilder
prepareUpsertSets() protectedPrepares the SET assignments for the update branch of an upsert statement.yii\db\QueryBuilder
ConstantValueDescriptionDefined by
UPSERT_AFFECTED_PARAM_PREFIX':yii_upsert_affected'Parameter name prefix for the affected-row OUT bind registered by a BLOB upsert.QueryBuilder
PARAM_PREFIX':qp'The prefix for automatically generated query binding parameters.yii\db\QueryBuilder
public function addForeignKey(mixed $name, mixed $table, mixed $columns, mixed $refTable, mixed $refColumns, mixed $delete = null, mixed $update = null): string

Builds a SQL statement for adding a foreign key constraint to an existing table.

The method will properly quote the table and column names.

Parameters

ParameterTypeDescription
$namemixedThe name of the foreign key constraint.
$tablemixedThe table that the foreign key constraint will be added to.
$columnsmixedThe 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.
$refTablemixedThe table that the foreign key references to.
$refColumnsmixedThe 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.
$deletemixedThe ON DELETE option. Most DBMS support these options: RESTRICT, CASCADE, NO ACTION, SET DEFAULT, SET NULL
$updatemixedThe 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.

public function alterColumn(string $table, string $column, string $type): string

Builds a SQL statement for changing the definition of a column.

Parameters

ParameterTypeDescription
$tablestringThe table whose column is to be changed. The table name will be properly quoted by the method.
$columnstringThe name of the column to be changed. The name will be properly quoted by the method.
$typestringThe 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.

public function batchInsert(string $table, array $columns, array|Generator $rows, array &$params = []): string

Generates a batch INSERT SQL statement.

For example,

$sql = $queryBuilder->batchInsert('user', ['name', 'age'], [
['Tom', 30],
['Jane', 20],
['Linda', 25],
]);

Note that the values in each row must match the corresponding column names.

Parameters

ParameterTypeDescription
$tablestringThe table that new rows will be inserted into.
$columnsarrayThe column names.
$rowsarray|GeneratorThe rows to be batch inserted into the table.
$paramsarrayThe parameters to be bound to the batch INSERT SQL statement. This parameter is passed by reference and will be modified by this method.

Returns

string: The batch INSERT SQL statement.

public function buildOrderByAndLimit(mixed $sql, mixed $orderBy, mixed $limit, mixed $offset): string

Builds the ORDER BY and LIMIT/OFFSET clauses and appends them to the given SQL.

Parameters

ParameterTypeDescription
$sqlmixedThe existing SQL (without ORDER BY/LIMIT/OFFSET)
$orderBymixedThe order by columns. See Query::orderBy() for more details on how to specify this parameter.
$limitmixedThe limit number. See Query::limit() for more details.
$offsetmixedThe offset number. See Query::offset() for more details.

Returns

string: The SQL completed with ORDER BY/LIMIT/OFFSET (if any)

public function buildWithQueries(mixed $withs, mixed &$params): string

Oracle does not support the RECURSIVE keyword for CTEs. Recursion is implicit when a CTE references itself.

Parameters

ParameterTypeDescription
$withsmixedOf configurations for each WITH query
$paramsmixedThe binding parameters to be populated

Returns

string: Compiled WITH prefix of query including nested queries

Since

2.0.35

public function dropCommentFromColumn(mixed $table, mixed $column): string

Builds a SQL command for adding comment to column.

Parameters

ParameterTypeDescription
$tablemixedThe table whose column is to be commented. The table name will be properly quoted by the method.
$columnmixedThe 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

Since

2.0.8

public function dropCommentFromTable(mixed $table): string

Builds a SQL command for adding comment to table.

Parameters

ParameterTypeDescription
$tablemixedThe 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

Since

2.0.8

public function dropIndex(string $name, string $table): string

Builds a SQL statement for dropping an index.

Parameters

ParameterTypeDescription
$namestringThe name of the index to be dropped. The name will be properly quoted by the method.
$tablestringThe 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.

public function executeResetSequence(mixed $table, mixed $value = null): mixed

Execute a SQL statement for resetting the sequence value of a table’s primary key.

IDENTITY columns reset via ALTER TABLE ... MODIFY because system sequences (ISEQ$$_*) are not addressable by name.

The column’s current generation clause is read from the data dictionary and preserved when resetting the identity; Oracle retains the identity options the statement does not specify. User-named sequences use the original DROP + CREATE recipe.

Reason for execute is that some databases (Oracle) need several queries to do so.

The sequence is reset such that the primary key of the next new row inserted will have the specified value or the maximum existing value +1.

Parameters

ParameterTypeDescription
$tablemixedThe name of the table whose primary key sequence is reset.
$valuemixedThe 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 the maximum existing value +1.

Returns

mixed

Throws

Since

2.0.16

public function insert(mixed $table, mixed $columns, mixed &$params): string

Creates an INSERT SQL statement.

Appends the locator RETURNING clause required by Oracle BLOB values.

For example,

$sql = $queryBuilder->insert('user', [
'name' => 'Sam',
'age' => 30,
], $params);

The method will properly escape the table and column names.

Parameters

ParameterTypeDescription
$tablemixedThe table that new rows will be inserted into.
$columnsmixedThe column data (name => value) to be inserted into the table or instance of Query to perform INSERT INTO … SELECT SQL statement. Passing of Query is available since version 2.0.11.
$paramsmixedThe binding parameters that will be generated by this method. They should be bound to the DB command later.

Returns

string: The INSERT SQL

public function mergeReturning(string $sql, array $params, array<string, string> $placeholders): string

Appends a RETURNING clause for the given OUT placeholders, merging any locator clause already present.

A DML statement built by insert() or update() may already end with a locator RETURNING clause; its entries fold into the new clause because Oracle allows a single RETURNING clause per statement.

Parameters

ParameterTypeDescription
$sqlstringDML statement built by this query builder.
$paramsarrayThe parameters bound to the SQL statement.
$placeholdersarray<string, string>OUT placeholders indexed by unquoted column name.

Returns

string: The SQL statement with the merged RETURNING clause.

public function renameTable(string $table, string $newName): string

Builds a SQL statement for renaming a DB table.

Parameters

ParameterTypeDescription
$tablestringThe table to be renamed. The name will be properly quoted by the method.
$newNamestringThe new table name. The name will be properly quoted by the method.

Returns

string: The SQL statement for renaming a DB table.

public function selectExists(mixed $rawSql): string

Creates a SELECT EXISTS() SQL statement.

Parameters

ParameterTypeDescription
$rawSqlmixedThe subquery in a raw form to select from.

Returns

string: The SELECT EXISTS() SQL statement.

Since

2.0.8

public function update(mixed $table, mixed $columns, mixed $condition, mixed &$params): string

Creates an UPDATE SQL statement.

Oracle BLOB updates require a condition that guarantees at most one matched row.

For example,

$params = [];
$sql = $queryBuilder->update('user', ['status' => 1], 'age > 30', $params);

The method will properly escape the table and column names.

Parameters

ParameterTypeDescription
$tablemixedThe table to be updated.
$columnsmixedThe column data (name => value) to be updated.
$conditionmixedThe condition that will be put in the WHERE part. Please refer to Query::where() on how to specify condition.
$paramsmixedThe binding parameters that will be modified by this method so that they can be bound to the DB command later.

Returns

string: The UPDATE SQL

Throws

public function upsert(mixed $table, mixed $insertColumns, mixed $updateColumns, mixed &$params): string

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.

A BLOB value routes to a single anonymous PL/SQL block through upsertWithLob(), because Oracle MERGE cannot return the LOB locators the payload binds require; every other case builds the regular MERGE.

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

ParameterTypeDescription
$tablemixedThe table that new rows will be inserted into/updated in.
$insertColumnsmixedThe column data (name => value) to be inserted into the table or instance of Query to perform INSERT INTO ... SELECT SQL statement.
$updateColumnsmixedThe 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.
$paramsmixedThe binding parameters that will be generated by this method. They should be bound to the DB command later.

Returns

string: The resulting SQL.

Throws

Since

2.0.14

See also

protected function defaultExpressionBuilders(): array

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.

Returns

array

Since

2.0.14

protected function prepareInsertValues(mixed $table, mixed $columns, mixed $params = []): array

Prepares a VALUES part for an INSERT SQL statement.

Parameters

ParameterTypeDescription
$tablemixedThe table that new rows will be inserted into.
$columnsmixedThe column data (name => value) to be inserted into the table or instance of Query to perform INSERT INTO … SELECT SQL statement.
$paramsmixedThe binding parameters that will be generated by this method. They should be bound to the DB command later.

Returns

array: Array of column names, placeholders, values and params.

Since

2.0.14