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 {@see TransactionInterface} an object if it's still active and level match. | Yiisoft\Db\Connection\AbstractConnection |
Property Details
Method Details
| 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;
}
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;
| 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());
}
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;
| public Yiisoft\Db\Query\QueryInterface createQuery ( ) |
public function createQuery(): QueryInterface
{
return new Query($this);
}
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;
| public string getColumnBuilderClass ( ) |
public function getColumnBuilderClass(): string
{
return ColumnBuilder::class;
}
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;
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 |
|---|---|---|
public function getDriverName(): string;
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;
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;
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;
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;
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;
| public string getTablePrefix ( ) |
public function getTablePrefix(): string
{
return $this->tablePrefix;
}
| 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);
}
| public Yiisoft\Db\Transaction\TransactionInterface|null getTransaction ( ) |
public function getTransaction(): ?TransactionInterface
{
return $this->transaction && $this->transaction->isActive() ? $this->transaction : null;
}
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;
| public boolean isSavepointEnabled ( ) |
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 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;
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;
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 */
}
}
}
| 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);
}
| public void setEnableSavepoint ( boolean $value ) | ||
| $value | boolean | |
public function setEnableSavepoint(bool $value): void
{
$this->enableSavepoint = $value;
}
| public void setTablePrefix ( string $value ) | ||
| $value | string | |
public function setTablePrefix(string $value): void
{
$this->tablePrefix = $value;
}
| 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;
}
Signup or Login in order to comment.