Database
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
No description
Constructs the database and initializes the PDO connection
Returns a string with the primary CLI usage for Install()
Returns the CLI usages specific to each driver
returns the array of config that was loaded from the config file
returns an array with some PDO attributes for debugging
Sets the database as writeable or readonly
Returns true if the database is read-only
Imports the appropriate SQL template file for an app
Parses and imports an SQL file into the database
Whether or not the DB supports the RETURNING keyword
Whether or not the DB aborts transactions after an error and requires use of SAVEPOINTs
Whether or not the DB fetches binary/blob fields as streams rather than scalars
Whether or not the DB requires binary input to be escaped
Whether or not the DB expects using public. as a prefix for table names
Returns the given arguments concatenated in SQL
Returns the driver-specific SQL UPSERT DO NOTHING query
Returns true if the DB is currently in a transaction
Sends an SQL query down to the database, possibly beginning a transaction
Logs a query to the internal query history, logging the actual data values if debug allows
Loops through an array of row results and replaces streams with their values
Begins a new database transaction
Rolls back the current database transaction
Commits the current database transaction
Begins timing a query (performance metrics)
Ends timing a query (performance metrics)
Add a new performance metrics context on to the stack
Pop the current performance metrics context off of the stack
Returns the array of query history
Details
at line 90
int
getDriver()
No description
at line 98
__construct(string|null $config = null)
Constructs the database and initializes the PDO connection
at line 155
static string
GetInstallUsage()
Returns a string with the primary CLI usage for Install()
at line 161
static array
GetInstallUsages()
Returns the CLI usages specific to each driver
at line 177
static string|null
Install(Input $input)
Creates and tests a new database config from the given user input
at line 237
array
GetClientObject()
returns the array of config that was loaded from the config file
at line 250
array
getInfo()
returns an array with some PDO attributes for debugging
at line 265
Database
setReadOnly(bool $ro = true)
Sets the database as writeable or readonly
at line 268
bool
isReadOnly()
Returns true if the database is read-only
at line 274
Database
importTemplate(string $path)
Imports the appropriate SQL template file for an app
at line 280
Database
importFile(string $path)
Parses and imports an SQL file into the database
at line 288
protected bool
SupportsRETURNING()
Whether or not the DB supports the RETURNING keyword
at line 295
protected bool
RequiresSAVEPOINT()
Whether or not the DB aborts transactions after an error and requires use of SAVEPOINTs
at line 298
protected bool
BinaryAsStreams()
Whether or not the DB fetches binary/blob fields as streams rather than scalars
at line 301
protected bool
BinaryEscapeInput()
Whether or not the DB requires binary input to be escaped
at line 304
protected bool
UsePublicSchema()
Whether or not the DB expects using public. as a prefix for table names
at line 307
string
SQLConcat(string ...$args)
Returns the given arguments concatenated in SQL
at line 321
string
SQLUpsert(string $query)
Returns the driver-specific SQL UPSERT DO NOTHING query
at line 331
bool
inTransaction()
Returns true if the DB is currently in a transaction
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
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
at line 433
private void
fetchStreams(array $rows)
Loops through an array of row results and replaces streams with their values
at line 446
void
beginTransaction()
Begins a new database transaction
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)
at line 492
private void
stopTimingQuery(string $sql, int $type, bool $count = true)
Ends timing a query (performance metrics)
at line 499
Database
pushStatsContext()
Add a new performance metrics context on to the stack
at line 505
DBStats|null
popStatsContext()
Pop the current performance metrics context off of the stack
at line 514
array
getAllQueries()
Returns the array of query history