class Database implements Transactions

This class implements the PDO database abstraction.

Manages connecting to the database, installing config, abstracting some driver differences, and logging queries and performance statistics. Queries are always made as part of a transaction, and always used as prepared statements. Performance statistics and queries are tracked as a stack, but transactions cannot be nested. Queries made must always be compatible with all supported drivers.

Constants

private CONFIG_PATHS

the default path for storing the config file

DRIVER_MYSQL

DRIVER_SQLITE

DRIVER_POSTGRESQL

private DRIVERS

QUERY_READ

QUERY_WRITE

Properties

private $connection

the PDO database connection

protected array<string,mixed> $config
protected $driver

The enum value of the driver being used

private $read_only

if true, don't allow writes

private DBStats[] $stats_stack
private $queries

global history of SQL queries sent to the DB (not a stack)

Methods

int
getDriver()

No description

__construct(string|null $config = null)

Constructs the database and initializes the PDO connection

static string
GetInstallUsage()

Returns a string with the primary CLI usage for Install()

static array
GetInstallUsages()

Returns the CLI usages specific to each driver

static string|null
Install(Input $input)

Creates and tests a new database config from the given user input

array
GetClientObject()

returns the array of config that was loaded from the config file

array
getInfo()

returns an array with some PDO attributes for debugging

setReadOnly(bool $ro = true)

Sets the database as writeable or readonly

bool
isReadOnly()

Returns true if the database is read-only

importTemplate(string $path)

Imports the appropriate SQL template file for an app

importFile(string $path)

Parses and imports an SQL file into the database

bool
SupportsRETURNING()

Whether or not the DB supports the RETURNING keyword

bool
RequiresSAVEPOINT()

Whether or not the DB aborts transactions after an error and requires use of SAVEPOINTs

bool
BinaryAsStreams()

Whether or not the DB fetches binary/blob fields as streams rather than scalars

bool
BinaryEscapeInput()

Whether or not the DB requires binary input to be escaped

bool
UsePublicSchema()

Whether or not the DB expects using public. as a prefix for table names

string
SQLConcat(string ...$args)

Returns the given arguments concatenated in SQL

string
SQLUpsert(string $query)

Returns the driver-specific SQL UPSERT DO NOTHING query

bool
inTransaction()

Returns true if the DB is currently in a transaction

array|int|null
query(string $sql, int $type, array|null $data = null)

Sends an SQL query down to the database, possibly beginning a transaction

string
logQuery(string $sql, array|null $data)

Logs a query to the internal query history, logging the actual data values if debug allows

void
fetchStreams(array $rows)

Loops through an array of row results and replaces streams with their values

void
beginTransaction()

Begins a new database transaction

rollback()

Rolls back the current database transaction

commit()

Commits the current database transaction

void
startTimingQuery()

Begins timing a query (performance metrics)

void
stopTimingQuery(string $sql, int $type, bool $count = true)

Ends timing a query (performance metrics)

pushStatsContext()

Add a new performance metrics context on to the stack

DBStats|null
popStatsContext()

Pop the current performance metrics context off of the stack

array
getAllQueries()

Returns the array of query history

Details

at line 90
int getDriver()

No description

Return Value

int

See also

Database::$driver

at line 98
__construct(string|null $config = null)

Constructs the database and initializes the PDO connection

Parameters

string|null $config

the path to the config file to use, generated by Install()

Exceptions

DatabaseConfigException

if the given path does not exist

InvalidDriverException

if the driver in the config is invalid

at line 155
static string GetInstallUsage()

Returns a string with the primary CLI usage for Install()

Return Value

string

at line 161
static array GetInstallUsages()

Returns the CLI usages specific to each driver

Return Value

array

at line 177
static string|null Install(Input $input)

Creates and tests a new database config from the given user input

Parameters

Input $input

input parameters

Return Value

string|null

the database config file contents

Exceptions

Throwable

if the database config is not valid and PDO fails

See also

Database::GetInstallUsage

at line 237
array GetClientObject()

returns the array of config that was loaded from the config file

Return Value

array

{driver:string, connect:string, ?username:string, ?password:true, ?persistent:bool}

at line 250
array getInfo()

returns an array with some PDO attributes for debugging

Return Value

array

{driver:string, cversion:string, sversion:string, info:string}

at line 265
Database setReadOnly(bool $ro = true)

Sets the database as writeable or readonly

Parameters

bool $ro

if true, set as readonly

Return Value

Database

at line 268
bool isReadOnly()

Returns true if the database is read-only

Return Value

bool

at line 274
Database importTemplate(string $path)

Imports the appropriate SQL template file for an app

Parameters

string $path

the base path containing the templates

Return Value

Database

at line 280
Database importFile(string $path)

Parses and imports an SQL file into the database

Parameters

string $path

the path of the SQL file

Return Value

Database

at line 288
protected bool SupportsRETURNING()

Whether or not the DB supports the RETURNING keyword

Return Value

bool

at line 295
protected bool RequiresSAVEPOINT()

Whether or not the DB aborts transactions after an error and requires use of SAVEPOINTs

Return Value

bool

at line 298
protected bool BinaryAsStreams()

Whether or not the DB fetches binary/blob fields as streams rather than scalars

Return Value

bool

at line 301
protected bool BinaryEscapeInput()

Whether or not the DB requires binary input to be escaped

Return Value

bool

at line 304
protected bool UsePublicSchema()

Whether or not the DB expects using public. as a prefix for table names

Return Value

bool

at line 307
string SQLConcat(string ...$args)

Returns the given arguments concatenated in SQL

Parameters

string ...$args

Return Value

string

at line 321
string SQLUpsert(string $query)

Returns the driver-specific SQL UPSERT DO NOTHING query

Parameters

string $query

the query after INSERT INTO (e.g. table (columns) VALUES (vals)

Return Value

string

compiled query

at line 331
bool inTransaction()

Returns true if the DB is currently in a transaction

Return Value

bool

at line 343
array|int|null query(string $sql, int $type, array|null $data = null)

Sends an SQL query down to the database, possibly beginning a transaction

Parameters

string $sql

the SQL query string, with placeholder data values

int $type

whether the query is a read, a write, or both (bitset)

array|null $data

associative array of data replacements for the prepared statement

Return Value

array|int|null

an associative array of the query results for reads, row count for writes

Exceptions

DatabaseReadOnlyException

if the query is a write and the DB is read-only

at line 411
private string logQuery(string $sql, array|null $data)

Logs a query to the internal query history, logging the actual data values if debug allows

Parameters

string $sql
array|null $data

Return Value

string

at line 433
private void fetchStreams(array $rows)

Loops through an array of row results and replaces streams with their values

Parameters

array $rows

reference to an array of rows from the DB

Return Value

void

at line 446
void beginTransaction()

Begins a new database transaction

Return Value

void

at line 459
rollback()

Rolls back the current database transaction

at line 472
commit()

Commits the current database transaction

at line 485
private void startTimingQuery()

Begins timing a query (performance metrics)

Return Value

void

at line 492
private void stopTimingQuery(string $sql, int $type, bool $count = true)

Ends timing a query (performance metrics)

Parameters

string $sql
int $type
bool $count

Return Value

void

at line 499
Database pushStatsContext()

Add a new performance metrics context on to the stack

Return Value

Database

at line 505
DBStats|null popStatsContext()

Pop the current performance metrics context off of the stack

Return Value

DBStats|null

at line 514
array getAllQueries()

Returns the array of query history

Return Value

array

string array