Elgg  Version 1.8
Namespaces | Functions | Variables
engine/lib/database.php File Reference

Go to the source code of this file.

Namespaces

namespace  Elgg\Core
 

Activate a plugin or plugins.


Functions

 establish_db_link ($dblinkname="readwrite")
 Establish a connection to the database servser.
 setup_db_connections ()
 Establish database connections.
 db_profiling_shutdown_hook ()
 Display profiling information about db at NOTICE debug level upon shutdown.
 db_delayedexecution_shutdown_hook ()
 Execute any delayed queries upon shutdown.
 get_db_link ($dblinktype)
 Returns (if required, also creates) a database link resource.
 explain_query ($query, $link)
 Execute an EXPLAIN for $query.
 execute_query ($query, $dblink)
 Execute a query.
 execute_delayed_query ($query, $dblink, $handler="")
 Queue a query for execution upon shutdown.
 execute_delayed_write_query ($query, $handler="")
 Write wrapper for execute_delayed_query()
 execute_delayed_read_query ($query, $handler="")
 Read wrapper for execute_delayed_query()
 get_data ($query, $callback="")
 Retrieve rows from the database.
 get_data_row ($query, $callback="")
 Retrieve a single row from the database.
 elgg_query_runner ($query, $callback=null, $single=false)
 Handles returning data from a query, running it through a callback function, and caching the results.
 insert_data ($query)
 Insert a row into the database.
 update_data ($query)
 Update the database.
 delete_data ($query)
 Remove data from the database.
 _elgg_invalidate_query_cache ()
 Invalidate the query cache.
 optimize_table ($table)
 Optimise a table.
 get_db_error ($dblink)
 Get the last database error for a particular database link.
 run_sql_script ($scriptlocation)
 Runs a full database script from disk.
 elgg_format_query ($query)
 Format a query string for logging.
 sanitise_string_special ($string, $extra_escapeable= '')
 Sanitise a string for database use, but with the option of escaping extra characters.
 sanitise_string ($string)
 Sanitise a string for database use.
 sanitize_string ($string)
 Wrapper function for alternate English spelling.
 sanitise_int ($int, $signed=true)
 Sanitises an integer for database use.
 sanitize_int ($int, $signed=true)
 Sanitizes an integer for database use.
 init_db ()
 Registers shutdown functions for database profiling and delayed queries.

Variables

global $DB_QUERY_CACHE = null
 Query cache for all queries.
global $DB_DELAYED_QUERIES = array()
 Queries to be executed upon shutdown.
global $dblink = array()
 Database connection resources.
global $dbcalls = 0
 Database call count.

Function Documentation

Invalidate the query cache.

private

Definition at line 530 of file database.php.

Execute any delayed queries upon shutdown.

Returns:
void private

Definition at line 175 of file database.php.

Display profiling information about db at NOTICE debug level upon shutdown.

Returns:
void private

Definition at line 162 of file database.php.

delete_data ( query)

Remove data from the database.

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

Definition at line 510 of file database.php.

elgg_format_query ( query)

Format a query string for logging.

Parameters:
string$queryQuery string
Returns:
string private

Definition at line 676 of file database.php.

elgg_query_runner ( query,
callback = null,
single = false 
)

Handles returning data from a query, running it through a callback function, and caching the results.

This is for R queries (from CRUD).

private

Parameters:
string$queryThe 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.
Since:
1.8.0 private

Definition at line 397 of file database.php.

establish_db_link ( dblinkname = "readwrite")

Establish a connection to the database servser.

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. eg "read", "write", or "readwrite".
Returns:
void
Exceptions:
DatabaseExceptionprivate

Definition at line 85 of file database.php.

execute_delayed_query ( query,
dblink,
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
resource | string$dblinkThe database link to use or the link type (read | write)
string$handlerA callback function to pass the results array to
Returns:
true private

Definition at line 293 of file database.php.

execute_delayed_read_query ( query,
handler = "" 
)

Read wrapper for execute_delayed_query()

Parameters:
string$queryThe query to execute
string$handlerThe handler if you care about the result.
Returns:
true execute_delayed_query() get_db_link() private

Definition at line 341 of file database.php.

execute_delayed_write_query ( query,
handler = "" 
)

Write wrapper for execute_delayed_query()

Parameters:
string$queryThe query to execute
string$handlerThe handler if you care about the result.
Returns:
true execute_delayed_query() get_db_link() private

Definition at line 326 of file database.php.

execute_query ( query,
dblink 
)

Execute a query.

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

Definition at line 258 of file database.php.

explain_query ( query,
link 
)

Execute an EXPLAIN for $query.

Parameters:
string$queryThe query to explain
mixed$linkThe database link resource to user.
Returns:
mixed An object of the query's result, or FALSE private

Definition at line 234 of file database.php.

get_data ( query,
callback = "" 
)

Retrieve rows from the database.

Queries are executed with execute_query() and results are retrieved with mysql_fetch_object(). If a callback function $callback is defined, each row will be passed as the 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. private

Definition at line 361 of file database.php.

get_data_row ( query,
callback = "" 
)

Retrieve a single row from the database.

Similar to get_data() 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. private

Definition at line 378 of file database.php.

get_db_error ( dblink)

Get the last database error for a particular database link.

Parameters:
resource$dblinkThe DB link
Returns:
string Database error message private

Definition at line 606 of file database.php.

get_db_link ( dblinktype)

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

Database link resources are stored in the $dblink global. These resources are created by setup_db_connections(), which is called if no links exist.

Parameters:
string$dblinktypeThe type of link we want: "read", "write" or "readwrite".
Returns:
resource Database link private

Definition at line 212 of file database.php.

init_db ( )

Registers shutdown functions for database profiling and delayed queries.

private

Definition at line 759 of file database.php.

insert_data ( query)

Insert a row into the database.

Note:
Altering the DB invalidates all queries in $DB_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. private

Definition at line 460 of file database.php.

optimize_table ( table)

Optimise a table.

Executes an OPTIMIZE TABLE query on $table. Useful after large DB changes.

Parameters:
string$tableThe name of the table to optimise
Returns:
bool private

Definition at line 593 of file database.php.

run_sql_script ( 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
) with only one statement per line.

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

Warning:
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 631 of file database.php.

sanitise_int ( int,
signed = true 
)

Sanitises an integer for database use.

Parameters:
int$intValue to be sanitized
bool$signedWhether negative values should be allowed (true)
Returns:
int

Definition at line 730 of file database.php.

sanitise_string ( string)

Sanitise a string for database use.

Parameters:
string$stringThe string to sanitise
Returns:
string Sanitised string

Definition at line 706 of file database.php.

sanitise_string_special ( string,
extra_escapeable = '' 
)

Sanitise a string for database use, but with the option of escaping extra characters.

Parameters:
string$stringThe string to sanitise
string$extra_escapeableExtra characters to escape with '\'
Returns:
string The escaped string

Definition at line 689 of file database.php.

sanitize_int ( int,
signed = true 
)

Sanitizes an integer for database use.

Wrapper function for alternate English spelling (

See also:
sanitise_int)
Parameters:
int$intValue to be sanitized
bool$signedWhether negative values should be allowed (true)
Returns:
int

Definition at line 750 of file database.php.

sanitize_string ( string)

Wrapper function for alternate English spelling.

Parameters:
string$stringThe string to sanitise
Returns:
string Sanitised string

Definition at line 719 of file database.php.

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 private

Definition at line 145 of file database.php.

update_data ( query)

Update the database.

Note:
Altering the DB invalidates all queries in $DB_QUERY_CACHE.
Parameters:
string$queryThe query to run.
Returns:
bool private

Definition at line 485 of file database.php.


Variable Documentation

$DB_DELAYED_QUERIES = array()

Queries to be executed upon shutdown.

These queries are saved to an array and executed using a function registered by register_shutdown_function().

Queries are saved as an array in the format: $DB_DELAYED_QUERIES[] = array( 'q' => str $query, 'l' => resource $dblink, 'h' => str $handler // a callback function );

array $DB_DELAYED_QUERIES private

Definition at line 47 of file database.php.

$DB_QUERY_CACHE = null

Query cache for all queries.

Each query and its results are stored in this cache as: $DB_QUERY_CACHE[query hash] => array(result1, result2, ... resultN)

See also:
elgg_query_runner() for details on the hash.
Warning:
Elgg used to set this as an empty array to turn off the cache

ElggLRUCache|null $DB_QUERY_CACHE private

Definition at line 26 of file database.php.

$dbcalls = 0

Database call count.

Each call to the database increments this counter.

integer $dbcalls private

Definition at line 70 of file database.php.

$dblink = array()

Database connection resources.

Each database link created with establish_db_link($name) is stored in $dblink as $dblink[$name] => resource. Use get_db_link($name) to retrieve it.

resource[] $dblink private

Definition at line 59 of file database.php.

 All Classes Namespaces Files Functions Variables Enumerations