Documentation

No matching pages.

API

Config

Facade

Lifecycle

Class: Database

Full name: Azera\Db\Database

Class Database

Public methods

__construct() ·

public function __construct(string $dsn, string $user = '', string $pass = '', array $options = []): mixed

Create a new database connection using the provided DSN, credentials and options.

Parameters

Name Type Default Description
$dsn string -
$user string ''
$pass string ''
$options array []

Return value

  • Type: mixed

Throws

  • Exception

connect() ·

public function connect(): mixed

Establish a new PDO connection using the current configuration

Return value

  • Type: mixed

Throws

  • Exception

events() ·

public function events(): Psr\EventDispatcher\EventDispatcherInterface

Resolve the event dispatcher from AppContext, cached on first call.

Returns a NullEventDispatcher when no real dispatcher is registered, so dispatch() is always safe and cheap (single no-op method call).

Return value

  • Type: Psr\EventDispatcher\EventDispatcherInterface

setAutoReconnect() ·

public function setAutoReconnect(bool $enabled = true, int $maxAttempts = 0, float $retryDelay = 1, float $backoffMultiplier = 2, float $maxRetryDelay = 30, bool $jitter = true, callable|null $onReconnect = null): static

Configure automatic reconnection behavior with detailed options

Parameters

Name Type Default Description
$enabled bool true Enable or disable auto-reconnect
$maxAttempts int 0 Maximum number of retry attempts (0 for unlimited)
$retryDelay float 1 Initial delay between retries in seconds
$backoffMultiplier float 2 Multiplier for exponential backoff
$maxRetryDelay float 30 Maximum delay between retries in seconds
$jitter bool true Whether to add random jitter to retry delays
$onReconnect callable|null null Optional callback invoked on successful reconnect (receives attempt number and db instance)

Return value

  • Type: static

getAutoReconnect() ·

public function getAutoReconnect(): array|bool

Get auto-reconnect configuration

Return value

  • Type: array|bool

query() ·

public function query(string $statement, array|null $params = null): PDOStatement|bool

Execute a SQL statement with optional parameters and return the resulting statement or success status.

Parameters

Name Type Default Description
$statement string - SQL statement to execute
$params array|null null Optional parameters for prepared statements

Return value

  • Type: PDOStatement|bool

Throws

  • Exception

useStatementCache() ·

public function useStatementCache(bool $enabled = true, int $limit = 64): static

Enable or disable reuse of prepared statements between calls.

Enable it where the win has been measured. On SQLite it is worth about 3.8 µs of a 12.5 µs query, because PDO::query() re-parses and re-plans the SQL text on every call (sqlite3_prepare_v2), while a reused statement only re-runs the compiled program. The correctness rule is independent of the driver: see {@see \Database::$idleStatements} for why taking a statement removes it from the pool, which is what keeps two live ResultSets from sharing a cursor.

Parameters

Name Type Default Description
$enabled bool true Whether to reuse statements.
$limit int 64 Maximum idle statements held per connection.

Return value

  • Type: static

usesStatementCache() ·

public function usesStatementCache(): bool

Whether statements are reused between calls. See Database::useStatementCache().

Return value

  • Type: bool

releaseStatement() ·

public function releaseStatement(string $statement, PDOStatement $stmt): void

Offer an exhausted statement back to the pool.

Called through Database::releaseStatement(), which a ResultSet reaches on closeCursor() and on destruction. A returned statement is always closeCursor()ed first: on SQLite in WAL mode an open cursor holds a read snapshot, and handing one back to the pool would keep that snapshot alive on the connection, blocking writers from other connections.

Releasing is best-effort and idempotent - a slot that is already occupied keeps what it has rather than replacing it, so a double release cannot pin two statements in one slot and lose one of them.

Parameters

Name Type Default Description
$statement string -
$stmt PDOStatement -

Return value

  • Type: void

flushStatementCache() ·

public function flushStatementCache(): void

Drop every pooled statement.

Called when the connection is replaced (see Database::connect()), because statements prepared on the previous handle cannot be executed on the new one.

Return value

  • Type: void

prepare() ·

public function prepare(string $statement): Azera\Db\Statement

Prepare a SQL statement and return a Statement wrapper.

Each call returns an independent Statement that owns its PDO statement, so any number of statements can be prepared and executed concurrently without clobbering a single shared slot.

Parameters

Name Type Default Description
$statement string - SQL statement to prepare

Return value

Throws

  • Exception

processPdoException() ·

public function processPdoException(PDOException $exception, string $operation, string|null $sql = null, array|null $params = null): mixed

Parameters

Name Type Default Description
$exception PDOException -
$operation string - The database operation that failed.
$sql string|null null The SQL statement that failed, if known.
$params array|null null Bound parameters for the failed statement, if known.

Return value

  • Type: mixed

Throws

  • Exception

selectRow() ·

public function selectRow(string $query, array|null $params = null, int $fetchMode = 0): array|bool

Fetch a single row from the database as object, associative array, or numeric array depending on the specified fetch mode.

Parameters

Name Type Default Description
$query string -
$params array|null null
$fetchMode int 0

Return value

  • Type: array|bool

selectAll() ·

public function selectAll(string $query, array|null $params = null, int $fetchMode = 0): array

Fetch all rows from the database as an array of objects, associative arrays, or numeric arrays depending on the specified fetch mode.

Parameters

Name Type Default Description
$query string -
$params array|null null
$fetchMode int 0

Return value

  • Type: array

rowCount() ·

public function rowCount(): int

Return the number of rows affected by the last executed statement.

Return value

  • Type: int
  • Description: Number of affected rows, or 0 if no statement has been executed.

getDefaultFetchMode() ·

public function getDefaultFetchMode(): int

Driver's default row fetch mode — the mode ResultSet uses when no explicit mode is given. The value is fixed by the constructor options, so it is resolved once and cached; getAttribute() is not re-read for every result set.

Return value

  • Type: int

inTransaction() ·

public function inTransaction(): bool

Whether a transaction is currently active on this connection (including nested savepoint levels).

Return value

  • Type: bool

lastInsertId() ·

public function lastInsertId(string|null $table = null, string|null $field = null): string|bool

Parameters

Name Type Default Description
$table string|null null
$field string|null null

Return value

  • Type: string|bool

begin() ·

public function begin(bool $nesting = true): int|bool

Begin a new transaction, or create a savepoint if nested transactions are enabled and a transaction is already active.

Parameters

Name Type Default Description
$nesting bool true Whether to use savepoints for nested transactions (if supported by the driver).

Return value

  • Type: int|bool
  • Description: True or the number of affected rows on success.

Throws

  • RuntimeException If the transaction cannot be started.

commit() ·

public function commit(bool $nesting = true): int|bool

Commit the current transaction or release the current savepoint (for nested transactions).

Parameters

Name Type Default Description
$nesting bool true Whether to use savepoints for nested transactions (if supported by the driver).

Return value

  • Type: int|bool
  • Description: True or the number of affected rows on success.

Throws

  • RuntimeException If there is no active transaction.

rollback() ·

public function rollback(bool $nesting = true): int|bool

Rollback the current transaction or to a savepoint if nesting is enabled and supported by the driver.

Parameters

Name Type Default Description
$nesting bool true Whether to use savepoints for nested transactions (if supported by the driver)

Return value

  • Type: int|bool

Throws

  • Exception

quote() ·

public function quote(string|null $str): string|bool

Quote a string for use in a query.

Parameters

Name Type Default Description
$str string|null -

Return value

  • Type: string|bool

quoteIdentifier() ·

public function quoteIdentifier(string|null ...$args): string

Quote one or more identifier parts (schema, table, column) using the driver-appropriate quote character.

Parts are joined with a dot separator. NULL parts are skipped. "*" is passed through unquoted.

Parameters

Name Type Default Description
$args string|null - Identifier parts to quote and join (e.g. schema, table, column).

Return value

  • Type: string
  • Description: Fully quoted identifier string.

getInternalConnection() ·

public function getInternalConnection(): PDO|null

Return the underlying PDO connection instance.

Return value

  • Type: PDO|null
  • Description: The PDO instance, or null if not connected.

builder() ·

public function builder(): Azera\Db\Query

Create a new Query builder instance associated with this database connection.

Return value


supportsReturning() ·

public function supportsReturning(): bool

Whether the connected server supports the RETURNING clause on INSERT/UPDATE/DELETE.

PostgreSQL supports it natively. MySQL 8.0.27+, MariaDB 10.5.0+ and SQLite 3.35+ also support it. Older servers must fall back to lastInsertId() for ID backfilling.

Return value

  • Type: bool

getDriver() ·

public function getDriver(): string

Return the lowercase database driver name extracted from the DSN (e.g. "mysql", "pgsql", "sqlite").

Return value

  • Type: string
  • Description: Driver name.

Back to the Index