Elgg  Version 2.2
 All Classes Namespaces Files Functions Variables Pages
Public Member Functions | Public Attributes | Protected Member Functions | List of all members
Elgg\Database Class Reference
Inheritance diagram for Elgg\Database:
Elgg\Application\Database

Public Member Functions

 __construct (\Elgg\Database\Config $config,\Elgg\Logger $logger=null)
 Constructor. More...
 
 setLogger (Logger $logger)
 Set the logger object. More...
 
 setupConnections ()
 Establish database connections. More...
 
 connect ($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, $get_num_rows=false, array $params=[])
 Update the database. More...
 
 deleteData ($query, array $params=[])
 Delete data from the database. More...
 
 fingerprintCallback ($callback)
 Get a string that uniquely identifies a callback during the current request. More...
 
 runSqlScript ($scriptlocation)
 Runs a full database script from disk. More...
 
 registerDelayedQuery ($query, $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...
 
 assertInstalled ()
 Test that the Elgg database is installed. More...
 
 getQueryCount ()
 Get the number of queries made to the database. More...
 
 getTablePrefix ()
 Get the prefix for Elgg's tables. More...
 
 sanitizeInt ($value, $signed=true)
 Sanitizes an integer value for use in a query. More...
 
 sanitizeString ($value)
 Sanitizes a string for use in a query. More...
 
 getServerVersion ($type)
 Get the server version number. More...
 

Public Attributes

global $USERNAME_TO_GUID_MAP_CACHE
 
 $USERNAME_TO_GUID_MAP_CACHE = array()
 
const DELAYED_QUERY = 'q'
 
const DELAYED_TYPE = 't'
 
const DELAYED_HANDLER = 'h'
 
const DELAYED_PARAMS = 'p'
 

Protected Member Functions

 getConnection ($type)
 Gets (if required, also creates) a DB connection. More...
 
 getResults ($query, $callback=null, $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...
 
 invalidateQueryCache ()
 Invalidate the query cache. More...
 

Detailed Description

Definition at line 19 of file Database.php.

Constructor & Destructor Documentation

Elgg\Database::__construct ( \Elgg\Database\Config  $config,
\Elgg\Logger  $logger = null 
)

Constructor.

Parameters
\Elgg\Database\Config$configDatabase configuration
\Elgg\Logger$loggerThe logger

Definition at line 90 of file Database.php.

Member Function Documentation

Elgg\Database::assertInstalled ( )

Test that the Elgg database is installed.

Returns
void
Exceptions
\InstallationExceptionprivate

Definition at line 630 of file Database.php.

Elgg\Database::connect (   $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
\DatabaseExceptionprivate

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
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
Exceptions
\DatabaseException

Definition at line 298 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 private

Definition at line 605 of file Database.php.

Elgg\Database::enableQueryCache ( )

Enable the query cache.

This does not take precedence over the setting.

Returns
void private

Definition at line 589 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 private

Definition at line 557 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
string$queryThe query
Connection$connectionThe DB connection
array$paramsQuery params. E.g. [1, 'steve'] or [':id' => 1, ':name' => 'steve']
Returns
Statement The result of the query
Exceptions
\DatabaseException

Definition at line 427 of file Database.php.

Elgg\Database::fingerprintCallback (   $callback)

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 private

Definition at line 325 of file Database.php.

Elgg\Database::getConnection (   $type)
protected

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

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

Definition at line 119 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.@paramstring$queryThequerybeingpassed.@paramcallable$callbackOptionally,thenameofafunctiontocallbacktooneachrow@paramarray$paramsQueryparams.E.g.[1,'steve']or[':id'=>1,':name'=>'steve']@returnarrayAnarrayofdatabaseresultobjectsorcallbackfunctionresults.Ifthequeryreturnednothing,anemptyarray.@throws\DatabaseException

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.@paramstring$queryThequerytoexecute.@paramcallable$callbackAcallbackfunctiontoapplytotherow@paramarray$paramsQueryparams.E.g.[1,'steve']or[':id'=>1,':name'=>'steve']@returnmixedAsingledatabaseresultobjectortheresultofthecallbackfunction.@throws\DatabaseException

Definition at line 227 of file Database.php.

Elgg\Database::getQueryCount ( )

Get the number of queries made to the database.

Returns
int private

Definition at line 652 of file Database.php.

Elgg\Database::getResults (   $query,
  $callback = null,
  $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
string$queryThe select query to execute
string$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
\DatabaseException

Definition at line 355 of file Database.php.

Elgg\Database::getServerVersion (   $type)

Get the server version number.

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

Definition at line 709 of file Database.php.

Elgg\Database::getTablePrefix ( )

Get the prefix for Elgg's tables.

Returns
string

Definition at line 661 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
string$queryThe query to execute.
array$paramsQuery params. E.g. [1, 'steve'] or [':id' => 1, ':name' => 'steve']
Returns
int|false The database id of the inserted row if a AUTO_INCREMENT field is defined, 0 if not, and false on failure.
Exceptions
\DatabaseException

Definition at line 243 of file Database.php.

Elgg\Database::invalidateQueryCache ( )
protected

Invalidate the query cache.

Returns
void

Definition at line 614 of file Database.php.

Elgg\Database::registerDelayedQuery (   $query,
  $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
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
boolean Whether registering was successful. private

Definition at line 534 of file Database.php.

Elgg\Database::runSqlScript (   $scriptlocation)

Runs a full database script from disk.

The file specified should be a standard SQL file as created by mysqldump or similar. Statements must be terminated with ; and a newline character (
or
).

The special string 'prefix_' is replaced with the database prefix as defined in ->tablePrefix.

Warning
Only single line comments are supported. A comment must start with '– ' or '# ', where the comment sign is at the very beginning of each line.
Errors do not halt execution of the script. If a line generates an error, the error message is saved and the next line is executed. After the file is run, any errors are displayed as a DatabaseException
Parameters
string$scriptlocationThe full path to the script
Returns
void
Exceptions
\DatabaseExceptionprivate

Definition at line 482 of file Database.php.

Elgg\Database::sanitizeInt (   $value,
  $signed = true 
)

Sanitizes an integer value for use in a query.

Parameters
int$valueValue to sanitize
bool$signedWhether negative values are allowed (default: true)
Returns
int

Definition at line 673 of file Database.php.

Elgg\Database::sanitizeString (   $value)

Sanitizes a string for use in a query.

Parameters
string$valueValue to escape
Returns
string
Exceptions
\DatabaseException

Definition at line 693 of file Database.php.

Elgg\Database::setLogger ( Logger  $logger)

Set the logger object.

Parameters
Logger$loggerThe logger
Returns
void private

Definition at line 107 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
Exceptions
\DatabaseExceptionprivate

Definition at line 140 of file Database.php.

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

Update the database.

Note
Altering the DB invalidates all queries in the query cache.
WARNING! update_data() has the 2nd and 3rd arguments reversed.
Parameters
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
Exceptions
\DatabaseException

Definition at line 271 of file Database.php.

Member Data Documentation

global Elgg\Database::$USERNAME_TO_GUID_MAP_CACHE

Definition at line 9 of file UsersTable.php.

Elgg\Database::$USERNAME_TO_GUID_MAP_CACHE = array()

Definition at line 10 of file UsersTable.php.

const Elgg\Database::DELAYED_HANDLER = 'h'

Definition at line 24 of file Database.php.

const Elgg\Database::DELAYED_PARAMS = 'p'

Definition at line 25 of file Database.php.

const Elgg\Database::DELAYED_QUERY = 'q'

Definition at line 22 of file Database.php.

const Elgg\Database::DELAYED_TYPE = 't'

Definition at line 23 of file Database.php.


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