ObjectDatabase
class ObjectDatabase extends Database
Provides the basic interfaces between BaseObject and the underlying PDO database
Basic functions include loading (caching), updating, creating, and deleting objects. This class should only be used internally for BaseObjects.
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 |
from Database |
| protected array<string,mixed> | $config | from Database | |
| protected | $driver | The enum value of the driver being used |
from Database |
| private | $read_only | if true, don't allow writes |
from Database |
| private DBStats[] | $stats_stack | from Database | |
| private | $queries | global history of SQL queries sent to the DB (not a stack) |
from Database |
| private T>> | $objects | ||
| private array | $loaded |
Methods
Constructs the database and initializes the PDO connection
Returns a string with the primary CLI usage for Install()
returns the array of config that was loaded from the config file
Imports the appropriate SQL template file for an app
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 expects using public. as a prefix for table names
Returns the driver-specific SQL UPSERT DO NOTHING query
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
Ends timing a query (performance metrics)
Pop the current performance metrics context off of the stack
No description
Loops through every objects and saves them to the DB
Return the database table name for a class
Converts database rows into objects
Attempt to fetch an object from the cache by its ID
Attempt to load a unique object by the value of a field
Loads an array of objects using the given query
Deletes a single object from the database (only to be called by the object itself)
UPDATEs an object in the database with the given data
INSERTs a new object into the database with the given data
Generates a new blank object of the given class
Details
in
Database at line 90
int
getDriver()
No description
in
Database at line 98
__construct(string|null $config = null)
Constructs the database and initializes the PDO connection
in
Database at line 155
static string
GetInstallUsage()
Returns a string with the primary CLI usage for Install()
in
Database at line 161
static array
GetInstallUsages()
Returns the CLI usages specific to each driver
in
Database at line 177
static string|null
Install(Input $input)
Creates and tests a new database config from the given user input
in
Database at line 237
array
GetClientObject()
returns the array of config that was loaded from the config file
in
Database at line 250
array
getInfo()
returns an array with some PDO attributes for debugging
in
Database at line 265
Database
setReadOnly(bool $ro = true)
Sets the database as writeable or readonly
in
Database at line 268
bool
isReadOnly()
Returns true if the database is read-only
in
Database at line 274
Database
importTemplate(string $path)
Imports the appropriate SQL template file for an app
in
Database at line 280
Database
importFile(string $path)
Parses and imports an SQL file into the database
in
Database at line 288
protected bool
SupportsRETURNING()
Whether or not the DB supports the RETURNING keyword
in
Database at line 295
protected bool
RequiresSAVEPOINT()
Whether or not the DB aborts transactions after an error and requires use of SAVEPOINTs
in
Database at line 298
protected bool
BinaryAsStreams()
Whether or not the DB fetches binary/blob fields as streams rather than scalars
in
Database at line 301
protected bool
BinaryEscapeInput()
Whether or not the DB requires binary input to be escaped
in
Database at line 304
protected bool
UsePublicSchema()
Whether or not the DB expects using public. as a prefix for table names
in
Database at line 307
string
SQLConcat(string ...$args)
Returns the given arguments concatenated in SQL
in
Database at line 321
string
SQLUpsert(string $query)
Returns the driver-specific SQL UPSERT DO NOTHING query
in
Database at line 331
bool
inTransaction()
Returns true if the DB is currently in a transaction
in
Database 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
in
Database 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
in
Database at line 433
private void
fetchStreams(array $rows)
Loops through an array of row results and replaces streams with their values
in
Database at line 446
void
beginTransaction()
Begins a new database transaction
in
Database at line 459
rollback()
Rolls back the current database transaction
in
Database at line 472
commit()
Commits the current database transaction
in
Database at line 485
private void
startTimingQuery()
Begins timing a query (performance metrics)
in
Database at line 492
private void
stopTimingQuery(string $sql, int $type, bool $count = true)
Ends timing a query (performance metrics)
in
Database at line 499
Database
pushStatsContext()
Add a new performance metrics context on to the stack
in
Database at line 505
DBStats|null
popStatsContext()
Pop the current performance metrics context off of the stack
in
Database at line 514
array
getAllQueries()
Returns the array of query history
at line 40
array
getLoadedObjectIDs()
No description
at line 46
ObjectDatabase
saveObjects(bool $onlyMandatory = false)
Loops through every objects and saves them to the DB
at line 59
string
GetClassTableName(string $class)
Return the database table name for a class
at line 74
private array
Rows2Objects(array $rows, string $class)
Converts database rows into objects
at line 114
private BaseObject|null
TryPreloadObjectByID(string $class, string $id)
Attempt to fetch an object from the cache by its ID
at line 141
BaseObject|null
TryLoadObjectByUniqueKey(string $class, string $field, string $value)
Attempt to load a unique object by the value of a field
Will try to fetch the object from the cache if loading by ID
at line 162
int
CountObjectsByQuery(string $class, QueryBuilder $query)
Counts objects using the given query
at line 178
array
LoadObjectsByQuery(string $class, QueryBuilder $query)
Loads an array of objects using the given query
at line 191
private void
UnsetObject(BaseObject $object)
No description
at line 205
int
DeleteObjectsByQuery(string $class, QueryBuilder $query)
Delete objects matching the given query
The objects will be loaded when deleted and their Delete() will run
at line 244
ObjectDatabase
DeleteObject(BaseObject $object)
Deletes a single object from the database (only to be called by the object itself)
The delete query happens immediately, not waiting for the object to be saved.
at line 264
ObjectDatabase
SaveObject(BaseObject $object, array $values, array $counters)
UPDATEs an object in the database with the given data
at line 297
ObjectDatabase
InsertObject(BaseObject $object, array $values)
INSERTs a new object into the database with the given data
at line 332
BaseObject
GenerateObject(string $class)
Generates a new blank object of the given class
The object will not actually exist in the DB until Save() is called, and its fields will have only the defaults given in its field template