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() Create a transaction 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 sequence value. Yiisoft\Db\Connection\ConnectionInterface
getQueryBuilder() Returns the query builder for the current DB connection. Yiisoft\Db\Connection\ConnectionInterface
getQuoter() Return 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 {@see 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() Returns a value indicating 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 {@see 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 Yiisoft\Db\Transaction\TransactionInterface beginTransaction ( string|null $isolationLevel null )
$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 void close ( )

                public function close(): void;

            
createBatchQueryResult() public method

public Yiisoft\Db\Query\BatchQueryResultInterface createBatchQueryResult ( Yiisoft\Db\Query\QueryInterface $query )
$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 Yiisoft\Db\Command\CommandInterface createCommand ( string|null $sql null, array $params = [] )
$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 instance.

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

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

            
createQuery() public method

public Yiisoft\Db\Query\QueryInterface createQuery ( )

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

            
createTransaction() public abstract method

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

Create a transaction instance.

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

The transaction instance.

                public function createTransaction(): TransactionInterface;

            
getColumnBuilderClass() public method

public string getColumnBuilderClass ( )

                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 Yiisoft\Db\Schema\Column\ColumnFactoryInterface getColumnFactory ( )

                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 string getDriverName ( )
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 sequence value.

public abstract string getLastInsertId ( string|null $sequenceName null )
$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 Yiisoft\Db\QueryBuilder\QueryBuilderInterface getQueryBuilder ( )
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()

Return quoter helper for current DB connection.

public abstract Yiisoft\Db\Schema\QuoterInterface getQuoter ( )
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 Yiisoft\Db\Schema\SchemaInterface getSchema ( )
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 {@see ServerInfoInterface} instance that provides information about the database server.

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

                public function getServerInfo(): ServerInfoInterface;

            
getTablePrefix() public method

public string getTablePrefix ( )

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

            
getTableSchema() public method

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

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

            
getTransaction() public method

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

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

            
isActive() public abstract method

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

Returns a value indicating whether the DB connection is active.

public abstract boolean isActive ( )
return boolean

Whether the DB connection is active.

                public function isActive(): bool;

            
isSavepointEnabled() public method

public boolean isSavepointEnabled ( )

                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 void open ( )
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 mixed quoteValue ( mixed $value )
$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 {@see TransactionInterface} an object if it's still active and level match.

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

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

TransactionInterface object given from {@see \Yiisoft\Db\Connection\beginTransaction()}.

$level integer

TransactionInterface level just after {@see \Yiisoft\Db\Connection\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 Yiisoft\Db\Query\QueryInterface select ( array|boolean|float|integer|string|Yiisoft\Db\Expression\ExpressionInterface $columns = [], string|null $option null )
$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 void setEnableSavepoint ( boolean $value )
$value boolean

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

            
setTablePrefix() public method

public void setTablePrefix ( string $value )
$value string

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

            
transaction() public method

public mixed transaction ( Closure $closure, string|null $isolationLevel null )
$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;
}