Elgg  Version 1.9
Public Member Functions | Protected Member Functions | List of all members
Elgg_Database Class Reference

Public Member Functions

 __construct (Elgg_Database_Config $config, Elgg_Logger $logger)
 Constructor. More...
 
 getLink ($type)
 Gets (if required, also creates) a database link resource. More...
 
 setupConnections ()
 Establish database connections. More...
 
 establishLink ($dblinkname="readwrite")
 Establish a connection to the database server. More...
 
 getData ($query, $callback= '')
 Retrieve rows from the database. More...
 
 getDataRow ($query, $callback= '')
 Retrieve a single row from the database. More...
 
 insertData ($query)
 Insert a row into the database. More...
 
 updateData ($query, $getNumRows=false)
 Update the database. More...
 
 deleteData ($query)
 Delete data from the database. More...
 
 fingerprintCallback ($callback)
 Get a string that uniquely identifies a callback during the current request. More...
 
 executeQuery ($query, $dblink)
 Execute a query. More...
 
 runSqlScript ($scriptlocation)
 Runs a full database script from disk. More...
 
 registerDelayedQuery ($query, $type, $handler="")
 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...
 

Protected Member Functions

 getResults ($query, $callback=null, $single=false)
 Handles queries that return results, running the results through a an optional callback function. More...
 
 invalidateQueryCache ()
 Invalidate the query cache. More...
 

Detailed Description

Definition at line 13 of file Database.php.

Constructor & Destructor Documentation

Elgg_Database::__construct ( Elgg_Database_Config  $config,
Elgg_Logger  $logger 
)

Constructor.

Parameters
Elgg_Database_Config$configDatabase configuration
Elgg_Logger$loggerThe logger

Definition at line 74 of file Database.php.

Member Function Documentation

Elgg_Database::assertInstalled ( )

Test that the Elgg database is installed.

Returns
void
Exceptions
InstallationException

Definition at line 580 of file Database.php.

Elgg_Database::deleteData (   $query)

Delete data from the database.

Note
Altering the DB invalidates all queries in query cache.
Parameters
string$queryThe SQL query to run
Returns
int|false The number of affected rows or false on failure
Exceptions
DatabaseException

Definition at line 260 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 558 of file Database.php.

Elgg_Database::enableQueryCache ( )

Enable the query cache.

This does not take precedence over the Elgg_Database_Config setting.

Returns
void

Definition at line 543 of file Database.php.

Elgg_Database::establishLink (   $dblinkname = "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$dblinknameThe type of database connection. Used to identify the resource: "read", "write", or "readwrite".
Returns
void
Exceptions
DatabaseException

Definition at line 137 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 511 of file Database.php.

Elgg_Database::executeQuery (   $query,
  $dblink 
)

Execute a query.

$query is executed via mysql_query(). If there is an SQL error, a DatabaseException is thrown.

Parameters
string$queryThe query
resource$dblinkThe DB link
Returns
resource|bool The result of mysql_query()
Exceptions
DatabaseException

Definition at line 389 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 288 of file Database.php.

Elgg_Database::getData (   $query,
  $callback = '' 
)

Retrieve rows from the database.

Queries are executed with Elgg_Database::executeQuery() and results are retrieved with mysql_fetch_object(). If a callback function $callback is defined, each row will be passed as a single argument to $callback. If no callback function is defined, the entire result set is returned as an array.

Parameters
mixed$queryThe query being passed.
string$callbackOptionally, the name of a function to call back to on each row
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 173 of file Database.php.

Elgg_Database::getDataRow (   $query,
  $callback = '' 
)

Retrieve a single row from the database.

Similar to Elgg_Database::getData() but returns only the first row matched. If a callback function $callback is specified, the row will be passed as the only argument to $callback.

Parameters
mixed$queryThe query to execute.
string$callbackA callback function
Returns
mixed A single database result object or the result of the callback function.
Exceptions
DatabaseException

Definition at line 190 of file Database.php.

Elgg_Database::getLink (   $type)

Gets (if required, also creates) a database link resource.

The database link resources are created by Elgg_Database::setupConnections(), which is called if no links exist.

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

Definition at line 96 of file Database.php.

Elgg_Database::getQueryCount ( )

Get the number of queries made to the database.

Returns
int

Definition at line 604 of file Database.php.

Elgg_Database::getResults (   $query,
  $callback = null,
  $single = false 
)
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?
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 317 of file Database.php.

Elgg_Database::getTablePrefix ( )

Get the prefix for Elgg's tables.

Returns
string

Definition at line 613 of file Database.php.

Elgg_Database::insertData (   $query)

Insert a row into the database.

Note
Altering the DB invalidates all queries in the query cache.
Parameters
mixed$queryThe query to execute.
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 205 of file Database.php.

Elgg_Database::invalidateQueryCache ( )
protected

Invalidate the query cache.

Returns
void

Definition at line 567 of file Database.php.

Elgg_Database::registerDelayedQuery (   $query,
  $type,
  $handler = "" 
)

Queue a query for execution upon shutdown.

You can specify a handler function if you care about the result. This function will accept the raw result from mysql_query().

Parameters
string$queryThe query to execute
string$typeThe query type ('read' or 'write')
string$handlerA callback function to pass the results array to
Returns
boolean Whether registering was successful.

Definition at line 485 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
DatabaseException

Definition at line 434 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 624 of file Database.php.

Elgg_Database::sanitizeString (   $value)

Sanitizes a string for use in a query.

Parameters
string$valueValue to escape
Returns
string

Definition at line 642 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
DatabaseException

Definition at line 116 of file Database.php.

Elgg_Database::updateData (   $query,
  $getNumRows = false 
)

Update the database.

Note
Altering the DB invalidates all queries in the query cache.
Parameters
string$queryThe query to run.
bool$getNumRowsReturn the number of rows affected (default: false)
Returns
bool|int
Exceptions
DatabaseException

Definition at line 231 of file Database.php.


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