Elgg  Version 4.x
Public Member Functions | Public Attributes | Protected Member Functions | Protected Attributes | List of all members
Elgg\Database Class Reference

The Elgg database. More...

Public Member Functions

 __construct (DbConfig $config, QueryCache $query_cache)
 Constructor. More...
 
 resetConnections (DbConfig $config)
 Reset the connections with new credentials. More...
 
 closeConnections ()
 Close all database connections. More...
 
 getConnection (string $type)
 Gets (if required, also creates) a DB connection. More...
 
 setupConnections ()
 Establish database connections. More...
 
 connect (string $type= 'readwrite')
 Establish a connection to the database server. More...
 
 getData ($query, $callback=null, array $params=[])
 Retrieve rows from the database. More...
 
 getDataRow ($query, $callback=null, array $params=[])
 Retrieve a single row from the database. More...
 
 insertData ($query, array $params=[])
 Insert a row into the database. More...
 
 updateData ($query, bool $get_num_rows=false, array $params=[])
 Update the database. More...
 
 deleteData ($query, array $params=[])
 Delete data from the database. More...
 
 trackQuery ($query, array $params, callable $callback)
 Tracks the query count and timers for a given query. More...
 
 registerDelayedQuery ($query, string $type, $callback=null, array $params=[])
 Queue a query for execution upon shutdown. More...
 
 executeDelayedQueries ()
 Trigger all queries that were registered as "delayed" queries. More...
 
 enableQueryCache ()
 Enable the query cache. More...
 
 disableQueryCache ()
 Disable the query cache. More...
 
 getQueryCount ()
 Get the number of queries made to the database. More...
 
 getServerVersion (string $type=DbConfig::READ_WRITE)
 Get the server version number. More...
 
 isMariaDB (string $type=DbConfig::READ_WRITE)
 Is the database MariaDB. More...
 
 __get ($name)
 Handle magic property reads. More...
 
 __set ($name, $value)
 Handle magic property writes. More...
 

Public Attributes

const DELAYED_QUERY = 'q'
 
const DELAYED_TYPE = 't'
 
const DELAYED_HANDLER = 'h'
 
const DELAYED_PARAMS = 'p'
 

Protected Member Functions

 fingerprintCallback ($callback)
 Get a string that uniquely identifies a callback during the current request. More...
 
 getResults ($query, $callback=null, bool $single=false, array $params=[])
 Handles queries that return results, running the results through a an optional callback function. More...
 
 executeQuery ($query, Connection $connection, array $params=[])
 Execute a query. More...
 

Protected Attributes

 $query_cache
 
 $delayed_queries = []
 

Detailed Description

The Elgg database.

Definition at line 24 of file Database.php.

Constructor & Destructor Documentation

Elgg\Database::__construct ( DbConfig  $config,
QueryCache  $query_cache 
)

Constructor.

Parameters
DbConfig$configDB configuration
QueryCache$query_cacheQuery Cache

Definition at line 76 of file Database.php.

Member Function Documentation

Elgg\Database::__get (   $name)

Handle magic property reads.

Parameters
string$nameProperty name
Returns
mixed
Exceptions

Definition at line 678 of file Database.php.

Elgg\Database::__set (   $name,
  $value 
)

Handle magic property writes.

Parameters
string$nameProperty name
mixed$valueValue
Returns
void
Exceptions

Definition at line 695 of file Database.php.

Elgg\Database::closeConnections ( )

Close all database connections.

Note: this is only meant to be used in the PHPUnit test suites

Returns
void
Since
4.1

Definition at line 106 of file Database.php.

Elgg\Database::connect ( string  $type = 'readwrite')

Establish a connection to the database server.

Connect to the database server and use the Elgg database for a particular database link

Parameters
string$typeThe type of database connection. "read", "write", or "readwrite".
Returns
void
Exceptions
DatabaseException

Definition at line 160 of file Database.php.

Elgg\Database::deleteData (   $query,
array  $params = [] 
)

Delete data from the database.

Note
Altering the DB invalidates all queries in query cache.
Parameters
QueryBuilder | string$queryThe SQL query to run
array$paramsQuery params. E.g. [1, 'steve'] or ['id' => 1, 'name' => 'steve']
Returns
int The number of affected rows

Definition at line 313 of file Database.php.

Elgg\Database::disableQueryCache ( )

Disable the query cache.

This is useful for special scripts that pull large amounts of data back in single queries.

Returns
void

Definition at line 615 of file Database.php.

Elgg\Database::enableQueryCache ( )

Enable the query cache.

This does not take precedence over the setting.

Returns
void

Definition at line 603 of file Database.php.

Elgg\Database::executeDelayedQueries ( )

Trigger all queries that were registered as "delayed" queries.

This is called by the system automatically on shutdown.

Returns
void

Definition at line 573 of file Database.php.

Elgg\Database::executeQuery (   $query,
Connection  $connection,
array  $params = [] 
)
protected

Execute a query.

$query is executed via Connection::query. If there is an SQL error, a DatabaseException is thrown.

Parameters
QueryBuilder | string$queryThe query
Connection$connectionThe DB connection
array$paramsQuery params. E.g. [1, 'steve'] or ['id' => 1, 'name' => 'steve']
Returns
Result|int The result of the query
Exceptions
DatabaseException

Definition at line 448 of file Database.php.

Elgg\Database::fingerprintCallback (   $callback)
protected

Get a string that uniquely identifies a callback during the current request.

This is used to cache queries whose results were transformed by the callback. If the callback involves object method calls of the same class, different instances will return different values.

Parameters
callable$callbackThe callable value to fingerprint
Returns
string A string that is unique for each callable passed in
Since
1.9.4

Definition at line 343 of file Database.php.

Elgg\Database::getConnection ( string  $type)

Gets (if required, also creates) a DB connection.

Parameters
string$typeThe type of link we want: "read", "write" or "readwrite".
Returns
Connection

Definition at line 121 of file Database.php.

Elgg\Database::getData (   $query,
  $callback = null,
array  $params = [] 
)

Retrieve rows from the database.

Queries are executed with Elgg\Database::executeQuery()andresultsareretrievedwith@link\PDO::fetchObject().Ifacallbackfunction$callbackisdefined,eachrowwillbepassedasasingleargumentto$callback.Ifnocallbackfunctionisdefined,theentireresultsetisreturnedasanarray.@paramQueryBuilder|string$queryThequerybeingpassed.@paramcallable$callbackOptionally,thenameofafunctiontocallbacktooneachrow@paramarray$paramsQueryparams.E.g.[1,'steve']or['id'=>1,'name'=>'steve']@returnarrayAnarrayofdatabaseresultobjectsorcallbackfunctionresults.Ifthequeryreturnednothing,anemptyarray.

Definition at line 209 of file Database.php.

Elgg\Database::getDataRow (   $query,
  $callback = null,
array  $params = [] 
)

Retrieve a single row from the database.

Similar to Elgg\Database::getData()butreturnsonlythefirstrowmatched.Ifacallbackfunction$callbackisspecified,therowwillbepassedastheonlyargumentto$callback.@paramQueryBuilder|string$queryThequerytoexecute.@paramcallable$callbackAcallbackfunctiontoapplytotherow@paramarray$paramsQueryparams.E.g.[1,'steve']or['id'=>1,'name'=>'steve']@returnmixedAsingledatabaseresultobjectortheresultofthecallbackfunction.

Definition at line 230 of file Database.php.

Elgg\Database::getQueryCount ( )

Get the number of queries made to the database.

Returns
int

Definition at line 624 of file Database.php.

Elgg\Database::getResults (   $query,
  $callback = null,
bool  $single = false,
array  $params = [] 
)
protected

Handles queries that return results, running the results through a an optional callback function.

This is for R queries (from CRUD).

Parameters
QueryBuilder | string$queryThe select query to execute
callable$callbackAn optional callback function to run on each row
bool$singleReturn only a single result?
array$paramsQuery params. E.g. [1, 'steve'] or ['id' => 1, 'name' => 'steve']
Returns
array| An array of database result objects or callback function results. If the query returned nothing, an empty array.
Exceptions

Definition at line 377 of file Database.php.

Elgg\Database::getServerVersion ( string  $type = DbConfig::READ_WRITE)

Get the server version number.

Parameters
string$typeConnection type (Config constants, e.g. Config::READ_WRITE)
Returns
string Empty if version cannot be determined

Definition at line 635 of file Database.php.

Elgg\Database::insertData (   $query,
array  $params = [] 
)

Insert a row into the database.

Note
Altering the DB invalidates all queries in the query cache.
Parameters
QueryBuilder | string$queryThe query to execute.
array$paramsQuery params. E.g. [1, 'steve'] or ['id' => 1, 'name' => 'steve']
Returns
int The database id of the inserted row if a AUTO_INCREMENT field is defined, 0 if not

Definition at line 248 of file Database.php.

Elgg\Database::isMariaDB ( string  $type = DbConfig::READ_WRITE)

Is the database MariaDB.

Parameters
string$typeConnection type (Config constants, e.g. Config::READ_WRITE)
Returns
bool if MariaDB is detected

Definition at line 659 of file Database.php.

Elgg\Database::registerDelayedQuery (   $query,
string  $type,
  $callback = null,
array  $params = [] 
)

Queue a query for execution upon shutdown.

You can specify a callback if you care about the result. This function will always be passed a .

Parameters
QueryBuilder | string$queryThe query to execute
string$typeThe query type ('read' or 'write')
callable$callbackA callback function to pass the results array to
array$paramsQuery params. E.g. [1, 'steve'] or [':id' => 1, ':name' => 'steve']
Returns
bool Whether registering was successful

Definition at line 548 of file Database.php.

Elgg\Database::resetConnections ( DbConfig  $config)

Reset the connections with new credentials.

Parameters
DbConfig$configDB config
Returns
void

Definition at line 89 of file Database.php.

Elgg\Database::setupConnections ( )

Establish database connections.

If the configuration has been set up for multiple read/write databases, set those links up separately; otherwise just create the one database link.

Returns
void

Definition at line 141 of file Database.php.

Elgg\Database::trackQuery (   $query,
array  $params,
callable  $callback 
)

Tracks the query count and timers for a given query.

Parameters
QueryBuilder | string$queryThe query
array$paramsOptional query params
callable$callbackCallback to execyte during query execution
Returns
mixed

Definition at line 505 of file Database.php.

Elgg\Database::updateData (   $query,
bool  $get_num_rows = false,
array  $params = [] 
)

Update the database.

Note
Altering the DB invalidates all queries in the query cache.
Parameters
QueryBuilder | string$queryThe query to run.
bool$get_num_rowsReturn the number of rows affected (default: false).
array$paramsQuery params. E.g. [1, 'steve'] or ['id' => 1, 'name' => 'steve']
Returns
bool|int

Definition at line 280 of file Database.php.

Member Data Documentation

Elgg\Database::$delayed_queries = []
protected

Definition at line 63 of file Database.php.

Elgg\Database::$query_cache
protected

Definition at line 54 of file Database.php.

const Elgg\Database::DELAYED_HANDLER = 'h'

Definition at line 31 of file Database.php.

const Elgg\Database::DELAYED_PARAMS = 'p'

Definition at line 32 of file Database.php.

const Elgg\Database::DELAYED_QUERY = 'q'

Definition at line 29 of file Database.php.

const Elgg\Database::DELAYED_TYPE = 't'

Definition at line 30 of file Database.php.


The documentation for this class was generated from the following file: