Abstract Class Yiisoft\Db\Driver\Pdo\AbstractPdoConnection
| Inheritance | Yiisoft\Db\Driver\Pdo\AbstractPdoConnection » Yiisoft\Db\Connection\AbstractConnection |
|---|---|
| Implements | Psr\Log\LoggerAwareInterface, Yiisoft\Db\Connection\ConnectionInterface, Yiisoft\Db\Driver\Pdo\PdoConnectionInterface, Yiisoft\Db\Profiler\ProfilerAwareInterface |
| Uses Traits | Psr\Log\LoggerAwareTrait, Yiisoft\Db\Profiler\ProfilerAwareTrait |
Represents a connection to a database using the PDO (PHP Data Objects) extension.
It provides a set of methods for interacting with a database using PDO, such as executing SQL statements, preparing and executing statements, and managing transactions.
The ConnectionPDO classes extend from this class, which is a base class for representing a connection to a database.
It implements the ConnectionInterface, which defines the interface for interacting with a database connection.
Protected Properties
Public Methods
Protected Methods
| Method | Description | Defined By |
|---|---|---|
| initConnection() | Initializes the DB connection. | Yiisoft\Db\Driver\Pdo\AbstractPdoConnection |
| rollbackTransactionOnLevel() | Yiisoft\Db\Driver\Pdo\AbstractPdoConnection |
Property Details
Method Details
Reset the connection after cloning.
| public mixed __clone ( ) |
public function __clone()
{
$this->transaction = null;
$this->pdo = null;
}
| public mixed __construct ( Yiisoft\Db\Driver\Pdo\PdoDriverInterface $driver, Yiisoft\Db\Cache\SchemaCache $schemaCache, Yiisoft\Db\Schema\Column\ColumnFactoryInterface|null $columnFactory = null ) | ||
| $driver | Yiisoft\Db\Driver\Pdo\PdoDriverInterface | |
| $schemaCache | Yiisoft\Db\Cache\SchemaCache | |
| $columnFactory | Yiisoft\Db\Schema\Column\ColumnFactoryInterface|null | |
public function __construct(
protected PdoDriverInterface $driver,
protected SchemaCache $schemaCache,
protected ?ColumnFactoryInterface $columnFactory = null,
) {}
Close the connection before serializing.
| public array __sleep ( ) |
public function __sleep(): array
{
$fields = (array) $this;
unset(
$fields["\000*\000" . 'pdo'],
$fields["\000*\000" . 'transaction'],
$fields["\000*\000" . 'schema'],
);
return array_keys($fields);
}
| public Yiisoft\Db\Transaction\TransactionInterface beginTransaction ( string|null $isolationLevel = null ) | ||
| $isolationLevel | string|null | |
public function beginTransaction(?string $isolationLevel = null): TransactionInterface
{
$transaction = parent::beginTransaction($isolationLevel);
if ($this->logger !== null && $transaction instanceof LoggerAwareInterface) {
$transaction->setLogger($this->logger);
}
return $transaction;
}
| public void close ( ) |
public function close(): void
{
if ($this->pdo !== null) {
$this->logger?->log(
LogLevel::DEBUG,
'Closing DB connection: ' . $this->driver->getDsn() . ' ' . __METHOD__,
['type' => LogType::CONNECTION],
);
$this->pdo = null;
$this->transaction = null;
}
}
| 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 PDO getActivePdo ( ) |
public function getActivePdo(): PDO
{
$this->open();
$pdo = $this->getPdo();
if ($pdo === null) {
throw new Exception('PDO cannot be initialized.');
}
return $pdo;
}
| 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;
| public Yiisoft\Db\Driver\Pdo\PdoDriverInterface getDriver ( ) |
public function getDriver(): PdoDriverInterface
{
return $this->driver;
}
| public string getDriverName ( ) |
public function getDriverName(): string
{
return $this->driver->getDriverName();
}
| public boolean|null getEmulatePrepare ( ) |
public function getEmulatePrepare(): ?bool
{
return $this->emulatePrepare;
}
| public string getLastInsertId ( string|null $sequenceName = null ) | ||
| $sequenceName | string|null | |
public function getLastInsertId(?string $sequenceName = null): string
{
if ($this->pdo !== null) {
return $this->pdo->lastInsertID($sequenceName ?? null);
}
throw new InvalidCallException('DB Connection is not active.');
}
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;
| public Yiisoft\Db\Connection\ServerInfoInterface getServerInfo ( ) |
public function getServerInfo(): ServerInfoInterface
{
return $this->serverInfo ??= new PdoServerInfo($this);
}
| 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;
}
Initializes the DB connection.
This method is invoked right after the DB connection is established.
The default implementation turns on PDO::ATTR_EMULATE_PREPARES, if {@see \Yiisoft\Db\Driver\Pdo\getEmulatePrepare()} is true.
| protected void initConnection ( ) |
protected function initConnection(): void
{
if ($this->getEmulatePrepare() !== null) {
$this->driver->attributes([PDO::ATTR_EMULATE_PREPARES => $this->getEmulatePrepare()]);
}
$this->pdo = $this->driver->createConnection();
}
| public boolean isSavepointEnabled ( ) |
public function isSavepointEnabled(): bool
{
return $this->enableSavepoint;
}
| public void open ( ) |
public function open(): void
{
if ($this->pdo instanceof PDO) {
return;
}
if ($this->driver->getDsn() === '') {
throw new InvalidConfigException('Connection::dsn cannot be empty.');
}
$token = 'Opening DB connection: ' . $this->driver->getDsn();
$connectionContext = new ConnectionContext(__METHOD__);
try {
$this->logger?->log(LogLevel::INFO, $token, ['type' => LogType::CONNECTION]);
$this->profiler?->begin($token, $connectionContext);
$this->initConnection();
$this->profiler?->end($token, $connectionContext);
} catch (PDOException $e) {
$this->profiler?->end($token, $connectionContext->setException($e));
$this->logger?->log(LogLevel::ERROR, $token, ['type' => LogType::CONNECTION]);
throw new Exception($e->getMessage(), (array) $e->errorInfo, $e);
}
}
| public mixed quoteValue ( mixed $value ) | ||
| $value | mixed | |
public function quoteValue(mixed $value): mixed
{
if (is_string($value) === false) {
return $value;
}
return $this->getActivePdo()->quote($value);
}
| protected void rollbackTransactionOnLevel ( Yiisoft\Db\Transaction\TransactionInterface $transaction, integer $level ) | ||
| $transaction | Yiisoft\Db\Transaction\TransactionInterface | |
| $level | integer | |
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 $e) {
$this->logger?->log(LogLevel::ERROR, (string) $e, [__METHOD__, 'type' => LogType::TRANSACTION]);
/** 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 setEmulatePrepare ( boolean $value ) | ||
| $value | boolean | |
public function setEmulatePrepare(bool $value): void
{
$this->emulatePrepare = $value;
}
| public void setEnableSavepoint ( boolean $value ) | ||
| $value | boolean | |
public function setEnableSavepoint(bool $value): void
{
$this->enableSavepoint = $value;
}
| public void setProfiler ( Yiisoft\Db\Profiler\ProfilerInterface|null $profiler ) | ||
| $profiler | Yiisoft\Db\Profiler\ProfilerInterface|null | |
public function setProfiler(?ProfilerInterface $profiler): void
{
$this->profiler = $profiler;
}
| public void setTablePrefix ( string $value ) | ||
| $value | string | |
public function setTablePrefix(string $value): void
{
parent::setTablePrefix($value);
if ($this->quoter !== null && method_exists($this->quoter, 'setTablePrefix')) {
$this->quoter->setTablePrefix($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.