Abstract Class Yiisoft\Db\Connection\AbstractConnection
| Inheritance | Yiisoft\Db\Connection\AbstractConnection |
|---|---|
| Implements | Yiisoft\Db\Connection\ConnectionInterface |
| Subclasses | Yiisoft\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.
Protected Properties
| Property | Type | Description | Defined By |
|---|---|---|---|
| $transaction | Yiisoft\Db\Transaction\TransactionInterface|null | Yiisoft\Db\Connection\AbstractConnection |
Public Methods
Protected 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
| 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;
}
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;
| 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());
}
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;
| public createQuery( ): Yiisoft\Db\Query\QueryInterface |
public function createQuery(): QueryInterface
{
return new Query($this);
}
Defined in: Yiisoft\Db\Connection\ConnectionInterface::createTransaction()
Creates Yiisoft\Db\Transaction\TransactionInterface instance.
| public abstract createTransaction( ): Yiisoft\Db\Transaction\TransactionInterface | ||
| return | Yiisoft\Db\Transaction\TransactionInterface |
The transaction. |
|---|---|---|
public function createTransaction(): TransactionInterface;
| public getColumnBuilderClass( ): string |
public function getColumnBuilderClass(): string
{
return ColumnBuilder::class;
}
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;
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 |
|---|---|---|
public function getDriverName(): string;
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;
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;
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;
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;
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;
| public getTablePrefix( ): string |
public function getTablePrefix(): string
{
return $this->tablePrefix;
}
| 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);
}
| public getTransaction( ): Yiisoft\Db\Transaction\TransactionInterface|null |
public function getTransaction(): ?TransactionInterface
{
return $this->transaction && $this->transaction->isActive() ? $this->transaction : null;
}
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;
| public isSavepointEnabled( ): boolean |
public function isSavepointEnabled(): bool
{
return $this->enableSavepoint;
}
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;
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;
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 */
}
}
}
| 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);
}
| public setEnableSavepoint( boolean $value ): void | ||
| $value | boolean | |
public function setEnableSavepoint(bool $value): void
{
$this->enableSavepoint = $value;
}
| public setTablePrefix( string $value ): void | ||
| $value | string | |
public function setTablePrefix(string $value): void
{
$this->tablePrefix = $value;
}
| 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;
}
Signup or Login in order to comment.