Index Documentation

DbTools
in package

FinalYes

Common database actions.

DSN documentation:

DbTools only handles the scheme part of the URL, the rest of the URL is described in the documentation of the parseDsn implementation of the respective backend.

The scheme can be a PHP classpath to an implementation of IDbBackend, or any of these aliases:

  • null: maps to NullDb\NullDbBackend or Index-Data-NullDb-NullDbBackend, provides a fallback blackhole database backend.
  • mariadb, mysql: maps to MariaDB\MariaDBBackend or Index-Data-MariaDB-MariaDBBackend, provides a backend based on the mysqli extension.
  • sqlite, sqlite3: maps to SQLite\SQLiteBackend or Index-Data-SQLite-SQLiteBackend, provides a backend based on the sqlite3 extension.

Short names are currently hardcoded and cannot be expanded.

Table of Contents

Methods

backend()  : IDbBackend
Retrieves database backend based on DSN URL.
create()  : IDbConnection
Uses a DSN URL to create a database instance.
detectType()  : int
Detects the DbType of the passed argument. Should be used for DbType::AUTO.
parse()  : IDbConnectionInfo
Parses a DSN URL.
prepareListString()  : string
Constructs a partial query for prepared statements of lists.
transaction()  : void
Transaction wrapper.

Methods

backend()

Retrieves database backend based on DSN URL.

public static backend(string $dsn) : IDbBackend

Format of the DSN URLs are described in the documentation of the DbTools class as a whole.

Parameters
$dsn : string

URL to create database connection from.

Tags
throws
InvalidArgumentException

if $dsn is not a valid URL.

throws
DataException

if no database connection can be made using the URL.

Return values
IDbBackend

Database backend instance.

create()

Uses a DSN URL to create a database instance.

public static create(string $dsn) : IDbConnection

Format of the DSN URLs are described in the documentation of the DbTools class as a whole.

Parameters
$dsn : string

URL to create database connection from.

Tags
throws
InvalidArgumentException

if $dsn is not a valid URL.

throws
DataException

if no database connection can be made using the URL.

Return values
IDbConnection

An active database connection.

detectType()

Detects the DbType of the passed argument. Should be used for DbType::AUTO.

public static detectType(mixed $value) : int
Parameters
$value : mixed

A value of unknown type.

Return values
int

DbType of the value passed in the argument.

parse()

Parses a DSN URL.

public static parse(string $dsn) : IDbConnectionInfo

Format of the DSN URLs are described in the documentation of the DbTools class as a whole.

Parameters
$dsn : string

URL to create database connection from.

Tags
throws
InvalidArgumentException

if $dsn is not a valid URL.

throws
DataException

if no database connection can be made using the URL.

Return values
IDbConnectionInfo

Database connection info.

prepareListString()

Constructs a partial query for prepared statements of lists.

public static prepareListString(Countable|array<string|int, mixed>|int $count[, string $repeat = '?' ][, string $glue = ', ' ]) : string
Parameters
$count : Countable|array<string|int, mixed>|int

Amount of times to repeat the string in the $repeat parameter.

$repeat : string = '?'

String to repeat.

$glue : string = ', '

Glue character.

Return values
string

Glued string ready for being thrown into your prepared statement.

transaction()

Transaction wrapper.

public static transaction(IDbTransactions $connection, callable $callable) : void

Takes a database connection with transaction support and a callable that may return a boolean based on the success of the actions. If the callable returns nothing, nothing will happen. If the callable returns true, commit will be called. If the callable returns false, rollback will be called.

Parameters
$connection : IDbTransactions

A database connection with transaction support.

$callable : callable

A callable that handles the transaction, may return a bool.


        
On this page

Search results