0 follower

Abstract Class Yiisoft\Db\Connection\AbstractConnection

InheritanceYiisoft\Db\Connection\AbstractConnection
ImplementsYiisoft\Db\Connection\ConnectionInterface
SubclassesYiisoft\Db\Driver\Pdo\AbstractPdoConnection

Represents a connection to a database.

It provides methods for interacting with the database, such as executing SQL queries and performing data manipulation.

Public Methods

Hide inherited methods

Method Description Defined By
beginTransaction() Yiisoft\Db\Connection\AbstractConnection
close() Closes the currently active DB connection. Yiisoft\Db\Connection\ConnectionInterface
createBatchQueryResult() Yiisoft\Db\Connection\AbstractConnection
createCommand() Creates a command for execution. Yiisoft\Db\Connection\ConnectionInterface
createQuery() Yiisoft\Db\Connection\AbstractConnection
createTransaction() Creates Yiisoft\Db\Transaction\TransactionInterface instance. Yiisoft\Db\Connection\ConnectionInterface
getColumnBuilderClass() Yiisoft\Db\Connection\AbstractConnection
getColumnFactory() Returns the column factory for creating column instances. Yiisoft\Db\Connection\ConnectionInterface
getDriverName() Returns the name of the DB driver for the current dsn. Yiisoft\Db\Connection\ConnectionInterface
getLastInsertId() Returns the ID of the last inserted row or a sequence value. Yiisoft\Db\Connection\ConnectionInterface
getQueryBuilder() Returns the query builder for the current DB connection. Yiisoft\Db\Connection\ConnectionInterface
getQuoter() Returns quoter helper for current DB connection. Yiisoft\Db\Connection\ConnectionInterface
getSchema() Returns the schema information for the database opened by this connection. Yiisoft\Db\Connection\ConnectionInterface
getServerInfo() Returns Yiisoft\Db\Connection\ServerInfoInterface instance that provides information about the database server. Yiisoft\Db\Connection\ConnectionInterface
getTablePrefix() Yiisoft\Db\Connection\AbstractConnection
getTableSchema() Yiisoft\Db\Connection\AbstractConnection
getTransaction() Yiisoft\Db\Connection\AbstractConnection
isActive() Whether the DB connection is active. Yiisoft\Db\Connection\ConnectionInterface
isSavepointEnabled() Yiisoft\Db\Connection\AbstractConnection
open() Establishes a DB connection. Yiisoft\Db\Connection\ConnectionInterface
quoteValue() Quotes a value for use in a query. Yiisoft\Db\Connection\ConnectionInterface
select() Yiisoft\Db\Connection\AbstractConnection
setEnableSavepoint() Yiisoft\Db\Connection\AbstractConnection
setTablePrefix() Yiisoft\Db\Connection\AbstractConnection
transaction() Yiisoft\Db\Connection\AbstractConnection

Protected Methods

Hide inherited methods

Method Description Defined By
rollbackTransactionOnLevel() Rolls back given Yiisoft\Db\Transaction\TransactionInterface an object if it's still active and level match. Yiisoft\Db\Connection\AbstractConnection

Property Details

Method Details

Hide inherited methods

beginTransaction() public method

public beginTransaction( string|null $isolationLevel null ): Yiisoft\Db\Transaction\TransactionInterface
$isolationLevel string|null

                public function beginTransaction(?string $isolationLevel = null): TransactionInterface
{
    $this->open();
    $this->transaction = $this->getTransaction();
    if ($this->transaction === null) {
        $this->transaction = $this->createTransaction();
    }
    $this->transaction->begin($isolationLevel);
    return $this->transaction;
}

            
close() public abstract method

Defined in: Yiisoft\Db\Connection\ConnectionInterface::close()

Closes the currently active DB connection.

It does nothing if the connection is already closed.

public abstract close( ): void

                public function close(): void;

            
createBatchQueryResult() public method

public createBatchQueryResult( Yiisoft\Db\Query\QueryInterface $query ): Yiisoft\Db\Query\BatchQueryResultInterface
$query Yiisoft\Db\Query\QueryInterface

                public function createBatchQueryResult(QueryInterface $query): BatchQueryResultInterface
{
    return (new BatchQueryResult($query))
        ->indexBy($query->getIndexBy())
        ->resultCallback($query->getResultCallback());
}

            
createCommand() public abstract method

Defined in: Yiisoft\Db\Connection\ConnectionInterface::createCommand()

Creates a command for execution.

public abstract createCommand( string|null $sql null, array $params = [] ): Yiisoft\Db\Command\CommandInterface
$sql string|null

The SQL statement to execute.

$params array

The parameters to bind to the SQL statement.

return Yiisoft\Db\Command\CommandInterface

The database command.

throws Yiisoft\Db\Exception\Exception
throws Yiisoft\Db\Exception\InvalidConfigException

                public function createCommand(?string $sql = null, array $params = []): CommandInterface;

            
createQuery() public method

public createQuery( ): Yiisoft\Db\Query\QueryInterface

                public function createQuery(): QueryInterface
{
    return new Query($this);
}

            
createTransaction() public abstract method
public abstract createTransaction( ): Yiisoft\Db\Transaction\TransactionInterface
return Yiisoft\Db\Transaction\TransactionInterface

The transaction.

                public function createTransaction(): TransactionInterface;

            
getColumnBuilderClass() public method

public getColumnBuilderClass( ): string

                public function getColumnBuilderClass(): string
{
    return ColumnBuilder::class;
}

            
getColumnFactory() public abstract method

Defined in: Yiisoft\Db\Connection\ConnectionInterface::getColumnFactory()

Returns the column factory for creating column instances.

public abstract getColumnFactory( ): Yiisoft\Db\Schema\Column\ColumnFactoryInterface

                public function getColumnFactory(): ColumnFactoryInterface;

            
getDriverName() public abstract method

Defined in: Yiisoft\Db\Connection\ConnectionInterface::getDriverName()

Returns the name of the DB driver for the current dsn.

Use this method for information only.

public abstract getDriverName( ): string
return string

The name of the DB driver for the current dsn.

                public function getDriverName(): string;

            
getLastInsertId() public abstract method

Defined in: Yiisoft\Db\Connection\ConnectionInterface::getLastInsertId()

Returns the ID of the last inserted row or a sequence value.

public abstract getLastInsertId( string|null $sequenceName null ): string
$sequenceName string|null

The name of the sequence object (required by some DBMS).

return string

The row ID of the last row inserted, or the last value retrieved from the sequence object.

throws Yiisoft\Db\Exception\Exception
throws Yiisoft\Db\Exception\InvalidCallException

                public function getLastInsertId(?string $sequenceName = null): string;

            
getQueryBuilder() public abstract method

Defined in: Yiisoft\Db\Connection\ConnectionInterface::getQueryBuilder()

Returns the query builder for the current DB connection.

public abstract getQueryBuilder( ): Yiisoft\Db\QueryBuilder\QueryBuilderInterface
return Yiisoft\Db\QueryBuilder\QueryBuilderInterface

The query builder for the current DB connection.

                public function getQueryBuilder(): QueryBuilderInterface;

            
getQuoter() public abstract method

Defined in: Yiisoft\Db\Connection\ConnectionInterface::getQuoter()

Returns quoter helper for current DB connection.

public abstract getQuoter( ): Yiisoft\Db\Schema\QuoterInterface
return Yiisoft\Db\Schema\QuoterInterface

The quoter helper for the current DB connection.

                public function getQuoter(): QuoterInterface;

            
getSchema() public abstract method

Defined in: Yiisoft\Db\Connection\ConnectionInterface::getSchema()

Returns the schema information for the database opened by this connection.

public abstract getSchema( ): Yiisoft\Db\Schema\SchemaInterface
return Yiisoft\Db\Schema\SchemaInterface

The schema information for the database opened by this connection.

                public function getSchema(): SchemaInterface;

            
getServerInfo() public abstract method

Defined in: Yiisoft\Db\Connection\ConnectionInterface::getServerInfo()

Returns Yiisoft\Db\Connection\ServerInfoInterface instance that provides information about the database server.

public abstract getServerInfo( ): Yiisoft\Db\Connection\ServerInfoInterface

                public function getServerInfo(): ServerInfoInterface;

            
getTablePrefix() public method

public getTablePrefix( ): string

                public function getTablePrefix(): string
{
    return $this->tablePrefix;
}

            
getTableSchema() public method

public getTableSchema( string $name, boolean $refresh false ): Yiisoft\Db\Schema\TableSchemaInterface|null
$name string
$refresh boolean

                public function getTableSchema(string $name, bool $refresh = false): ?TableSchemaInterface
{
    return $this->getSchema()->getTableSchema($name, $refresh);
}

            
getTransaction() public method

public getTransaction( ): Yiisoft\Db\Transaction\TransactionInterface|null

                public function getTransaction(): ?TransactionInterface
{
    return $this->transaction && $this->transaction->isActive() ? $this->transaction : null;
}

            
isActive() public abstract method

Defined in: Yiisoft\Db\Connection\ConnectionInterface::isActive()

Whether the DB connection is active.

public abstract isActive( ): boolean
return boolean

Whether the DB connection is active.

                public function isActive(): bool;

            
isSavepointEnabled() public method

public isSavepointEnabled( ): boolean

                public function isSavepointEnabled(): bool
{
    return $this->enableSavepoint;
}

            
open() public abstract method

Defined in: Yiisoft\Db\Connection\ConnectionInterface::open()

Establishes a DB connection.

It does nothing if a DB connection is active.

public abstract open( ): void
throws Yiisoft\Db\Exception\Exception

If connection fails.

throws Yiisoft\Db\Exception\InvalidConfigException

If a connection can't be established because of incomplete configuration.

                public function open(): void;

            
quoteValue() public abstract method

Defined in: Yiisoft\Db\Connection\ConnectionInterface::quoteValue()

Quotes a value for use in a query.

public abstract quoteValue( mixed $value ): mixed
$value mixed
return mixed

The quoted string.

throws Yiisoft\Db\Exception\Exception
throws Yiisoft\Db\Exception\InvalidConfigException

                public function quoteValue(mixed $value): mixed;

            
rollbackTransactionOnLevel() protected method

Rolls back given Yiisoft\Db\Transaction\TransactionInterface an object if it's still active and level match.

Sometimes, rollback can fail, so this method is fail-safe.

protected rollbackTransactionOnLevel( Yiisoft\Db\Transaction\TransactionInterface $transaction, integer $level ): void
$transaction Yiisoft\Db\Transaction\TransactionInterface

TransactionInterface object given from beginTransaction().

$level integer

TransactionInterface level just after beginTransaction() call.

throws Throwable

If transaction wasn't rolled back.

                protected function rollbackTransactionOnLevel(TransactionInterface $transaction, int $level): void
{
    if ($transaction->isActive() && $transaction->getLevel() === $level) {
        /**
         * @link https://github.com/yiisoft/yii2/pull/13347
         */
        try {
            $transaction->rollBack();
        } catch (Throwable) {
            /** hide this exception to be able to continue throwing original exception outside */
        }
    }
}

            
select() public method

public select( array|boolean|float|integer|string|Yiisoft\Db\Expression\ExpressionInterface $columns = [], string|null $option null ): Yiisoft\Db\Query\QueryInterface
$columns array|boolean|float|integer|string|Yiisoft\Db\Expression\ExpressionInterface
$option string|null

                public function select(
    array|bool|float|int|string|ExpressionInterface $columns = [],
    ?string $option = null,
): QueryInterface {
    return $this->createQuery()->select($columns, $option);
}

            
setEnableSavepoint() public method

public setEnableSavepoint( boolean $value ): void
$value boolean

                public function setEnableSavepoint(bool $value): void
{
    $this->enableSavepoint = $value;
}

            
setTablePrefix() public method

public setTablePrefix( string $value ): void
$value string

                public function setTablePrefix(string $value): void
{
    $this->tablePrefix = $value;
}

            
transaction() public method

public transaction( Closure $closure, string|null $isolationLevel null ): mixed
$closure Closure
$isolationLevel string|null

                public function transaction(Closure $closure, ?string $isolationLevel = null): mixed
{
    $transaction = $this->beginTransaction($isolationLevel);
    $level = $transaction->getLevel();
    try {
        $result = $closure($this);
        if ($transaction->isActive() && $transaction->getLevel() === $level) {
            $transaction->commit();
        }
    } catch (Throwable $e) {
        $this->rollbackTransactionOnLevel($transaction, $level);
        throw $e;
    }
    return $result;
}