0 follower

Final Class Yiisoft\Db\Mysql\Command

InheritanceYiisoft\Db\Mysql\Command » Yiisoft\Db\Driver\Pdo\AbstractPdoCommand

Implements a database command that can be executed with a PDO (PHP Data Object) database connection for MySQL, MariaDB.

Method Details

Hide inherited methods

insertReturningPks() public method

public array insertReturningPks ( string $table, array|\Yiisoft\Db\Query\QueryInterface $columns )
$table string
$columns array|\Yiisoft\Db\Query\QueryInterface

                public function insertReturningPks(string $table, array|QueryInterface $columns): array
{
    $tableSchema = $this->db->getSchema()->getTableSchema($table);
    $primaryKeys = $tableSchema?->getPrimaryKey() ?? [];
    $tableColumns = $tableSchema?->getColumns() ?? [];
    foreach ($primaryKeys as $name) {
        $column = $tableColumns[$name];
        if ($column->isAutoIncrement()) {
            continue;
        }
        if ($columns instanceof QueryInterface) {
            throw new NotSupportedException(
                __METHOD__ . '() is not supported by MySQL for tables without auto increment when inserting sub-query.',
            );
        }
        break;
    }
    $params = [];
    $insertSql = $this->db->getQueryBuilder()->insert($table, $columns, $params);
    $this->setSql($insertSql)->bindValues($params);
    $this->execute();
    if (empty($primaryKeys)) {
        return [];
    }
    $result = [];
    foreach ($primaryKeys as $name) {
        $column = $tableColumns[$name];
        if ($column->isAutoIncrement()) {
            $value = $this->db->getLastInsertId();
        } else {
            /** @var array $columns */
            $value = $columns[$name] ?? $column->getDefaultValue();
        }
        if ($this->phpTypecasting) {
            $value = $column->phpTypecast($value);
        }
        $result[$name] = $value;
    }
    return $result;
}

            
pdoStatementExecute() protected method

protected void pdoStatementExecute ( )

                protected function pdoStatementExecute(): void
{
    set_error_handler(
        static fn(int $errorNumber, string $errorString): bool
            => str_starts_with($errorString, 'Packets out of order. Expected '),
        E_WARNING,
    );
    try {
        $this->pdoStatement?->execute();
    } finally {
        restore_error_handler();
    }
}

            
queryInternal() protected method

protected mixed queryInternal ( integer $queryMode )
$queryMode integer

                protected function queryInternal(int $queryMode): mixed
{
    try {
        return parent::queryInternal($queryMode);
    } catch (IntegrityException $e) {
        if (
            str_starts_with($e->getMessage(), 'SQLSTATE[HY000]: General error: ')
            && in_array(substr($e->getMessage(), 32, 5), ['2006 ', '4031 '], true)
            && $this->db->getTransaction() === null
        ) {
            $this->cancel();
            $this->db->close();
            return parent::queryInternal($queryMode);
        }
        throw $e;
    }
}

            
showDatabases() public method

public array showDatabases ( )

                public function showDatabases(): array
{
    $sql = <<<SQL
    SHOW DATABASES WHERE `Database` NOT IN ('information_schema', 'mysql', 'performance_schema', 'sys')
    SQL;
    return $this->setSql($sql)->queryColumn();
}

            
upsertReturning() public method

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

                public function upsertReturning(
    string $table,
    array|QueryInterface $insertColumns,
    array|bool $updateColumns = true,
    ?array $returnColumns = null,
): array {
    $returnColumns ??= $this->db->getTableSchema($table)?->getColumnNames();
    if (empty($returnColumns)) {
        $this->upsert($table, $insertColumns, $updateColumns)->execute();
        return [];
    }
    $params = [];
    $sql = $this->getQueryBuilder()
        ->upsertReturning($table, $insertColumns, $updateColumns, $returnColumns, $params);
    $this->setSql($sql)->bindValues($params);
    $this->queryInternal(self::QUERY_MODE_EXECUTE);
    /** @psalm-var PDOStatement $this->pdoStatement */
    $this->pdoStatement->nextRowset();
    /** @psalm-var array<string,mixed> $result */
    $result = $this->pdoStatement->fetch(PDO::FETCH_ASSOC);
    $this->pdoStatement->closeCursor();
    if (!$this->phpTypecasting) {
        return $result;
    }
    $columns = $this->db->getTableSchema($table)?->getColumns();
    if (empty($columns)) {
        return $result;
    }
    foreach ($result as $name => &$value) {
        $value = $columns[$name]->phpTypecast($value);
    }
    return $result;
}