0 follower

Final Class Yiisoft\Db\Oracle\DMLQueryBuilder

InheritanceYiisoft\Db\Oracle\DMLQueryBuilder » Yiisoft\Db\QueryBuilder\AbstractDMLQueryBuilder

Implements a DML (Data Manipulation Language) SQL statements for Oracle Server.

Protected Methods

Hide inherited methods

Method Description Defined By
prepareInsertValues() Yiisoft\Db\Oracle\DMLQueryBuilder

Method Details

Hide inherited methods

insertBatch() public method

public string insertBatch ( string $table, iterable $rows, array $columns = [], array &$params = [] )
$table string
$rows iterable
$columns array
$params array

                public function insertBatch(string $table, iterable $rows, array $columns = [], array &$params = []): string
{
    if (!is_array($rows)) {
        $rows = $this->prepareTraversable($rows);
    }
    if (empty($rows)) {
        return '';
    }
    $columns = $this->extractColumnNames($rows, $columns);
    $values = $this->prepareBatchInsertValues($table, $rows, $columns, $params);
    if (empty($values)) {
        return '';
    }
    $query = 'INSERT INTO ' . $this->quoter->quoteTableName($table);
    if (count($columns) > 0) {
        $quotedColumnNames = array_map($this->quoter->quoteColumnName(...), $columns);
        $query .= ' (' . implode(', ', $quotedColumnNames) . ')';
    }
    return $query . "\nSELECT " . implode(" FROM DUAL UNION ALL\nSELECT ", $values) . ' FROM DUAL';
}

            
insertReturningPks() public method

public string insertReturningPks ( string $table, array|\Yiisoft\Db\Query\QueryInterface $columns, array &$params = [] )
$table string
$columns array|\Yiisoft\Db\Query\QueryInterface
$params array

                public function insertReturningPks(string $table, array|QueryInterface $columns, array &$params = []): string
{
    throw new NotSupportedException(__METHOD__ . ' is not supported by Oracle.');
}

            
prepareInsertValues() protected method

protected array prepareInsertValues ( string $table, array|\Yiisoft\Db\Query\QueryInterface $columns, array $params = [] )
$table string
$columns array|\Yiisoft\Db\Query\QueryInterface
$params array

                protected function prepareInsertValues(string $table, array|QueryInterface $columns, array $params = []): array
{
    if (empty($columns)) {
        $names = [];
        $placeholders = [];
        $tableSchema = $this->schema->getTableSchema($table);
        if ($tableSchema !== null) {
            if (!empty($tableSchema->getPrimaryKey())) {
                $names = $tableSchema->getPrimaryKey();
            } else {
                /**
                 * @psalm-suppress PossiblyNullArgument
                 * @var string[] $names
                 */
                $names = [array_key_first($tableSchema->getColumns())];
            }
            $placeholders = array_fill(0, count($names), 'DEFAULT');
        }
        return [$names, $placeholders, '', $params];
    }
    return parent::prepareInsertValues($table, $columns, $params);
}

            
resetSequence() public method

public string resetSequence ( string $table, integer|string|null $value null )
$table string
$value integer|string|null

                public function resetSequence(string $table, int|string|null $value = null): string
{
    $tableSchema = $this->schema->getTableSchema($table);
    if ($tableSchema === null) {
        throw new InvalidArgumentException("Table not found: '$table'.");
    }
    $sequenceName = $tableSchema->getSequenceName();
    if ($sequenceName === null) {
        throw new InvalidArgumentException("There is not sequence associated with table '$table'.");
    }
    if ($value === null && count($tableSchema->getPrimaryKey()) > 1) {
        throw new InvalidArgumentException("Can't reset sequence for composite primary key in table: $table");
    }
    /**
     * Oracle needs at least many queries to reset a sequence (see adding transactions and/or use an alter method to
     * avoid grant issue?)
     */
    return 'declare
lastSeq number' . ($value !== null ? (' := ' . $value) : '') . ';
n' . ($value === null ? '
SELECT MAX("' . $tableSchema->getPrimaryKey()[0] . '") + 1 INTO lastSeq FROM "' . $tableSchema->getName() . '";' : '') . '
if lastSeq IS NULL then lastSeq := 1; end if;
execute immediate \'DROP SEQUENCE "' . $sequenceName . '"\';
execute immediate \'CREATE SEQUENCE "' . $sequenceName . '" START WITH \' || lastSeq || \' INCREMENT BY 1 NOMAXVALUE NOCACHE\';
';
}

            
update() public method

public string update ( string $table, array $columns, array|string|\Yiisoft\Db\Expression\ExpressionInterface $condition, array|string|\Yiisoft\Db\Expression\ExpressionInterface|null $from null, array &$params = [] )
$table string
$columns array
$condition array|string|\Yiisoft\Db\Expression\ExpressionInterface
$from array|string|\Yiisoft\Db\Expression\ExpressionInterface|null
$params array

                public function update(
    string $table,
    array $columns,
    array|string|ExpressionInterface $condition,
    array|string|ExpressionInterface|null $from = null,
    array &$params = [],
): string {
    if ($from !== null) {
        throw new NotSupportedException('Oracle does not support FROM clause in UPDATE statement.');
    }
    return parent::update($table, $columns, $condition, null, $params);
}

            
upsert() public method

public string upsert ( string $table, array|\Yiisoft\Db\Query\QueryInterface $insertColumns, array|boolean $updateColumns true, array &$params = [] )
$table string
$insertColumns array|\Yiisoft\Db\Query\QueryInterface
$updateColumns array|boolean
$params array

                public function upsert(
    string $table,
    array|QueryInterface $insertColumns,
    array|bool $updateColumns = true,
    array &$params = [],
): string {
    $constraints = [];
    [$uniqueNames, $insertNames, $updateNames] = $this->prepareUpsertColumns(
        $table,
        $insertColumns,
        $updateColumns,
        $constraints,
    );
    if (empty($uniqueNames)) {
        return $this->insert($table, $insertColumns, $params);
    }
    $onCondition = ['or'];
    $quotedTableName = $this->quoter->quoteTableName($table);
    foreach ($constraints as $constraint) {
        $columnNames = $constraint->columnNames;
        $constraintCondition = ['and'];
        foreach ($columnNames as $name) {
            $quotedName = $this->quoter->quoteColumnName($name);
            $constraintCondition[] = "$quotedTableName.$quotedName=EXCLUDED.$quotedName";
        }
        $onCondition[] = $constraintCondition;
    }
    $on = $this->queryBuilder->buildCondition($onCondition, $params);
    [, $placeholders, $values, $params] = $this->prepareInsertValues($table, $insertColumns, $params);
    if (!empty($placeholders)) {
        $values = $this->buildSimpleSelect(array_combine($insertNames, $placeholders)) . ' FROM "DUAL"';
    }
    $insertValues = [];
    $quotedInsertNames = array_map($this->quoter->quoteColumnName(...), $insertNames);
    foreach ($quotedInsertNames as $quotedName) {
        $insertValues[] = 'EXCLUDED.' . $quotedName;
    }
    $mergeSql = 'MERGE INTO ' . $quotedTableName . ' USING (' . $values . ') EXCLUDED ON (' . $on . ')';
    $insertSql = 'INSERT (' . implode(', ', $quotedInsertNames) . ')'
        . ' VALUES (' . implode(', ', $insertValues) . ')';
    if (empty($updateColumns) || $updateNames === []) {
        /** there are no columns to update */
        return "$mergeSql WHEN NOT MATCHED THEN $insertSql";
    }
    $updates = $this->prepareUpsertSets($table, $updateColumns, $updateNames, $params);
    $updateSql = 'UPDATE SET ' . implode(', ', $updates);
    return "$mergeSql WHEN MATCHED THEN $updateSql WHEN NOT MATCHED THEN $insertSql";
}

            
upsertReturning() public method

public string upsertReturning ( string $table, array|\Yiisoft\Db\Query\QueryInterface $insertColumns, array|boolean $updateColumns true, array|null $returnColumns null, array &$params = [] )
$table string
$insertColumns array|\Yiisoft\Db\Query\QueryInterface
$updateColumns array|boolean
$returnColumns array|null
$params array

                public function upsertReturning(
    string $table,
    array|QueryInterface $insertColumns,
    array|bool $updateColumns = true,
    ?array $returnColumns = null,
    array &$params = [],
): string {
    throw new NotSupportedException(__METHOD__ . '() is not supported by Oracle.');
}