Database
in package
Wraps an active connection to the database.
Primarily provides query, escaping, and locking methods.
Table of Contents
Properties
Methods
- __construct() : mixed
- Not intended to be constructed by hand. If you need an instance of Database, use Database::getInstance();
- connectionFactory() : Connection
- Used by the DI container to construct the underlying connection object.
- delete() : int
- DELETE row(s) from $table that meet $criteria.
- escapeArray() : T
- escapeBoolean() : string
- escapeNullableObject() : string|null
- escapeNullableString() : string|null
- escapeNumber() : T
- escapeObject() : string
- escapeString() : string
- getDbBytes() : int
- Returns the size of the current database in bytes.
- getInstance() : self
- Returns the instance of this class from the DI container.
- insert() : void
- INSERT a row into $table.
- insertAutoIncrement() : int
- INSERT a row into a $table with an auto-increment column.
- lockTable() : void
- Acquire a write lock for $table.
- read() : DatabaseResult
- Perform a read-only query on the database.
- replace() : void
- REPLACE a row into $table.
- replaceAutoIncrement() : int
- REPLACE a row into $table with an auto-increment column.
- resetInstance() : void
- This should not be needed except perhaps by persistent services (such as Dicord/IRC clients) to prevent connection timeouts between callbacks.
- switchDatabases() : void
- This method will switch the connection to the specified database.
- switchDatabaseToLive() : void
- Switch back to the configured live database
- unlock() : void
- update() : int
- UPDATE $fields in $table for rows that meet $criteria.
- write() : int
- Perform a write-only query on the database.
- getInsertID() : int
- getParamTypes() : array<string, ParameterType|ArrayParameterType>
- Determine Doctrine\DBAL types automatically based on the passed in type.
Properties
$dbConn read-only
private
Connection
$dbConn
$dbName read-only
private
string
$dbName
Methods
__construct()
Not intended to be constructed by hand. If you need an instance of Database, use Database::getInstance();
public
__construct(Connection $dbConn, string $dbName) : mixed
Parameters
- $dbConn : Connection
- $dbName : string
connectionFactory()
Used by the DI container to construct the underlying connection object.
public
static connectionFactory(DatabaseProperties $dbProperties) : Connection
Not intended to be used outside the DI context.
Parameters
- $dbProperties : DatabaseProperties
Return values
Connectiondelete()
DELETE row(s) from $table that meet $criteria.
public
delete(string $table, array<string, mixed> $criteria) : int
Parameters
- $table : string
- $criteria : array<string, mixed>
Return values
int —Number of deleted rows
escapeArray()
public
escapeArray(T $array) : T
Parameters
- $array : T
Tags
Return values
TescapeBoolean()
public
escapeBoolean(bool $bool) : string
Parameters
- $bool : bool
Return values
stringescapeNullableObject()
public
escapeNullableObject(object|array<string|int, mixed>|string|null $object[, bool $compress = false ]) : string|null
Parameters
- $object : object|array<string|int, mixed>|string|null
- $compress : bool = false
Return values
string|nullescapeNullableString()
public
escapeNullableString(string|null $string) : string|null
Parameters
- $string : string|null
Return values
string|nullescapeNumber()
public
escapeNumber(T $num) : T
Parameters
- $num : T
Tags
Return values
TescapeObject()
public
escapeObject(object|array<string|int, mixed>|string $object[, bool $compress = false ]) : string
Parameters
- $object : object|array<string|int, mixed>|string
- $compress : bool = false
Return values
stringescapeString()
public
escapeString(string $string) : string
Parameters
- $string : string
Return values
stringgetDbBytes()
Returns the size of the current database in bytes.
public
getDbBytes() : int
Return values
intgetInstance()
Returns the instance of this class from the DI container.
public
static getInstance() : self
If one does not exist yet, it will be created. This is the intended way to construct this class.
Return values
selfinsert()
INSERT a row into $table.
public
insert(string $table, array<string, mixed> $fields) : void
Parameters
- $table : string
- $fields : array<string, mixed>
insertAutoIncrement()
INSERT a row into a $table with an auto-increment column.
public
insertAutoIncrement(string $table, array<string, mixed> $fields) : int
Parameters
- $table : string
- $fields : array<string, mixed>
Return values
int —Insert ID of auto-incrementing column
lockTable()
Acquire a write lock for $table.
public
lockTable(string $table[, array<string|int, string> $readTables = [] ]) : void
Parameters
- $table : string
- $readTables : array<string|int, string> = []
-
Additional read locks
read()
Perform a read-only query on the database.
public
read(string $query[, array<string|int, mixed> $params = [] ]) : DatabaseResult
Used for SELECT queries, for example.
Parameters
- $query : string
- $params : array<string|int, mixed> = []
Return values
DatabaseResultreplace()
REPLACE a row into $table.
public
replace(string $table, array<string, mixed> $fields) : void
Parameters
- $table : string
- $fields : array<string, mixed>
replaceAutoIncrement()
REPLACE a row into $table with an auto-increment column.
public
replaceAutoIncrement(string $table, array<string, mixed> $fields) : int
Parameters
- $table : string
- $fields : array<string, mixed>
Return values
int —Insert ID of auto-incrementing column
resetInstance()
This should not be needed except perhaps by persistent services (such as Dicord/IRC clients) to prevent connection timeouts between callbacks.
public
static resetInstance() : void
Closes the underlying connection and removes it, along with the Database instance that wraps it, from the DI container. A new Database instance will be made, along with a fresh database connection, the next time that Database::getInstance() is called.
This function is safe to use even if the DI container or the Database instances have not been initialized yet.
switchDatabases()
This method will switch the connection to the specified database.
public
switchDatabases(string $databaseName) : void
Useful for switching back and forth between historical, and live databases.
Parameters
- $databaseName : string
-
The name of the database to switch to
switchDatabaseToLive()
Switch back to the configured live database
public
switchDatabaseToLive() : void
unlock()
public
unlock() : void
update()
UPDATE $fields in $table for rows that meet $criteria.
public
update(string $table, array<string, mixed> $fields, array<string, mixed> $criteria) : int
Parameters
- $table : string
- $fields : array<string, mixed>
- $criteria : array<string, mixed>
Return values
int —Number of updated rows
write()
Perform a write-only query on the database.
public
write(string $query[, array<string|int, mixed> $params = [] ]) : int
Used for UPDATE, DELETE, REPLACE and INSERT queries, for example.
Parameters
- $query : string
- $params : array<string|int, mixed> = []
Return values
int —Number of affected rows
getInsertID()
private
getInsertID() : int
Return values
intgetParamTypes()
Determine Doctrine\DBAL types automatically based on the passed in type.
private
static getParamTypes(array<string|int, mixed> $params) : array<string, ParameterType|ArrayParameterType>
Parameters
- $params : array<string|int, mixed>