toolkit

Db

PDO connection handling and SQL execution, used by the registry.

Db:: Phlat\Toolkit\Db 13 methods
  • connect Opens a SQLite database under a connection name; a no-op when it is already open.
  • connection The PDO behind a connection name; throws when it isn't open.
  • disconnect Closes a connection.
  • disconnectAll Closes every connection.
  • isConnected Whether a connection name is open.
  • lastId The row id of the last insert.
  • like Escapes %, _ and \ so a value matches literally inside `LIKE ?
  • listen Registers a callback fired after every sql() call with (sql, params, duration in ms, connection name).
  • snapshot Writes the database at $path to $target as one consistent file, however busy it is - copying the file instead would miss what is still in its WAL. $target must not exist.
  • sql Runs a prepared statement with $params and returns it.
  • supported Whether the linked SQLite is at least MIN_VERSION.
  • transaction Nested calls (same $conn) join the outer transaction - only the outermost call begins/commits.
  • version The version of the SQLite library pdo_sqlite links against, read from an in-memory database so no site file is opened.

Methods

Db::connect(string $path, string $name = 'default'): void

Opens a SQLite database under a connection name; a no-op when it is already open.

Parameters (2)
$path string
$name string = 'default'
phlat/src/Toolkit/Db.php:37
Db::connection(string $name = 'default'): PDO

The PDO behind a connection name; throws when it isn't open.

Parameters (1)
$name string = 'default'

Returns PDO

phlat/src/Toolkit/Db.php:54
Db::disconnect(string $name = 'default'): void

Closes a connection.

Parameters (1)
$name string = 'default'
phlat/src/Toolkit/Db.php:122
Db::disconnectAll(): void

Closes every connection.

phlat/src/Toolkit/Db.php:150
Db::isConnected(string $name = 'default'): bool

Whether a connection name is open.

Parameters (1)
$name string = 'default'

Returns bool

phlat/src/Toolkit/Db.php:61
Db::lastId(string $conn = 'default'): string

The row id of the last insert.

Parameters (1)
$conn string = 'default'

Returns string

phlat/src/Toolkit/Db.php:89
Db::like(string $value): string

Escapes %, _ and \ so a value matches literally inside `LIKE ?

ESCAPE ''`.

Parameters (1)
$value string

Returns string

phlat/src/Toolkit/Db.php:83
Db::listen(callable $listener): void

Registers a callback fired after every sql() call with (sql, params, duration in ms, connection name).

Parameters (1)
$listener callable
phlat/src/Toolkit/Db.php:31
Db::snapshot(string $path, string $target): void

Writes the database at $path to $target as one consistent file, however busy it is - copying the file instead would miss what is still in its WAL. $target must not exist.

Parameters (2)
$path string
$target string
phlat/src/Toolkit/Db.php:128

sql

Db::sql(string $sql, array $params = [], string $conn = 'default'): PDOStatement

Runs a prepared statement with $params and returns it.

Parameters (3)
$sql string
$params array = []
$conn string = 'default'

Returns PDOStatement

phlat/src/Toolkit/Db.php:67
Db::supported(): bool

Whether the linked SQLite is at least MIN_VERSION.

Returns bool

phlat/src/Toolkit/Db.php:144
Db::transaction(callable $fn, string $conn = 'default', bool $immediate = false): mixed

Nested calls (same $conn) join the outer transaction - only the outermost call begins/commits.

An exception at any depth rolls back the whole thing. $immediate takes the write lock as the outermost transaction begins: needed when it reads before it writes, since a transaction that reads first can no longer write once another writer has committed.

Parameters (3)
$fn callable
$conn string = 'default'
$immediate bool = false

Returns mixed

phlat/src/Toolkit/Db.php:98
Db::version(): string

The version of the SQLite library pdo_sqlite links against, read from an in-memory database so no site file is opened.

Returns string

phlat/src/Toolkit/Db.php:138