PHP Class Elgg\Database

Inheritance: use trait Profilable
Exibir arquivo Open project: elgg/elgg Class Usage Examples

Public Methods

Method Description
__construct ( Elgg\Database\Config $config, Logger $logger = null ) Constructor
__get ( string $name ) : mixed Handle magic property reads
__set ( string $name, mixed $value ) : void Handle magic property writes
assertInstalled ( ) : void Test that the Elgg database is installed
connect ( string $type = "readwrite" ) : void Establish a connection to the database server
deleteData ( string $query, array $params = [] ) : integer Delete data from the database
disableQueryCache ( ) : void Disable the query cache
enableQueryCache ( ) : void Enable the query cache
executeDelayedQueries ( ) : void Trigger all queries that were registered as "delayed" queries. This is called by the system automatically on shutdown.
fingerprintCallback ( callable $callback ) : string Get a string that uniquely identifies a callback during the current request.
getData ( string $query, callable $callback = null, array $params = [] ) : array Retrieve rows from the database.
getDataRow ( string $query, callable $callback = null, array $params = [] ) : mixed Retrieve a single row from the database.
getQueryCount ( ) : integer Get the number of queries made to the database
getServerVersion ( string $type ) : string Get the server version number
insertData ( string $query, array $params = [] ) : integer | false Insert a row into the database.
registerDelayedQuery ( string $query, string $type, callable $callback = null, array $params = [] ) : boolean Queue a query for execution upon shutdown.
runSqlScript ( string $scriptlocation ) : void Runs a full database script from disk.
sanitizeInt ( integer $value, boolean $signed = true ) : integer Sanitizes an integer value for use in a query
sanitizeString ( string $value ) : string Sanitizes a string for use in a query
setLogger ( Logger $logger ) : void Set the logger object
setupConnections ( ) : void Establish database connections
updateData ( string $query, boolean $get_num_rows = false, array $params = [] ) : boolean | integer Update the database.

Protected Methods

Method Description
executeQuery ( string $query, Doctrine\DBAL\Connection $connection, array $params = [] ) : Doctrine\DBAL\Driver\Statement Execute a query.
getConnection ( string $type ) : Doctrine\DBAL\Connection Gets (if required, also creates) a DB connection.
getResults ( string $query, string $callback = null, boolean $single = false, array $params = [] ) : array Handles queries that return results, running the results through a an optional callback function. This is for R queries (from CRUD).
invalidateQueryCache ( ) : void Invalidate the query cache

Method Details

__construct() public method

Constructor
public __construct ( Elgg\Database\Config $config, Logger $logger = null )
$config Elgg\Database\Config Database configuration
$logger Logger The logger

__get() public method

Handle magic property reads
public __get ( string $name ) : mixed
$name string Property name
return mixed

__set() public method

Handle magic property writes
public __set ( string $name, mixed $value ) : void
$name string Property name
$value mixed Value
return void

assertInstalled() public method

Test that the Elgg database is installed
public assertInstalled ( ) : void
return void

connect() public method

Connect to the database server and use the Elgg database for a particular database link
public connect ( string $type = "readwrite" ) : void
$type string The type of database connection. "read", "write", or "readwrite".
return void

deleteData() public method

Delete data from the database
public deleteData ( string $query, array $params = [] ) : integer
$query string The SQL query to run
$params array Query params. E.g. [1, 'steve'] or [':id' => 1, ':name' => 'steve']
return integer The number of affected rows

disableQueryCache() public method

This is useful for special scripts that pull large amounts of data back in single queries.
public disableQueryCache ( ) : void
return void

enableQueryCache() public method

This does not take precedence over the \Elgg\Database\Config setting.
public enableQueryCache ( ) : void
return void

executeDelayedQueries() public method

Trigger all queries that were registered as "delayed" queries. This is called by the system automatically on shutdown.
public executeDelayedQueries ( ) : void
return void

executeQuery() protected method

$query is executed via {@link Connection::query}. If there is an SQL error, a {@link DatabaseException} is thrown.
protected executeQuery ( string $query, Doctrine\DBAL\Connection $connection, array $params = [] ) : Doctrine\DBAL\Driver\Statement
$query string The query
$connection Doctrine\DBAL\Connection The DB connection
$params array Query params. E.g. [1, 'steve'] or [':id' => 1, ':name' => 'steve']
return Doctrine\DBAL\Driver\Statement The result of the query

fingerprintCallback() public method

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.
Since: 1.9.4
public fingerprintCallback ( callable $callback ) : string
$callback callable The callable value to fingerprint
return string A string that is unique for each callable passed in

getConnection() protected method

Gets (if required, also creates) a DB connection.
protected getConnection ( string $type ) : Doctrine\DBAL\Connection
$type string The type of link we want: "read", "write" or "readwrite".
return Doctrine\DBAL\Connection

getData() public method

Queries are executed with {@link \Elgg\Database::executeQuery()} and results are retrieved with {@link \PDO::fetchObject()}. 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.
public getData ( string $query, callable $callback = null, array $params = [] ) : array
$query string The query being passed.
$callback callable Optionally, the name of a function to call back to on each row
$params array Query params. E.g. [1, 'steve'] or [':id' => 1, ':name' => 'steve']
return array An array of database result objects or callback function results. If the query returned nothing, an empty array.

getDataRow() public method

Similar to {@link \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.
public getDataRow ( string $query, callable $callback = null, array $params = [] ) : mixed
$query string The query to execute.
$callback callable A callback function to apply to the row
$params array Query params. E.g. [1, 'steve'] or [':id' => 1, ':name' => 'steve']
return mixed A single database result object or the result of the callback function.

getQueryCount() public method

Get the number of queries made to the database
public getQueryCount ( ) : integer
return integer

getResults() protected method

Handles queries that return results, running the results through a an optional callback function. This is for R queries (from CRUD).
protected getResults ( string $query, string $callback = null, boolean $single = false, array $params = [] ) : array
$query string The select query to execute
$callback string An optional callback function to run on each row
$single boolean Return only a single result?
$params array Query params. E.g. [1, 'steve'] or [':id' => 1, ':name' => 'steve']
return array An array of database result objects or callback function results. If the query returned nothing, an empty array.

getServerVersion() public method

Get the server version number
public getServerVersion ( string $type ) : string
$type string Connection type (Config constants, e.g. Config::READ_WRITE)
return string Empty if version cannot be determined

insertData() public method

Insert a row into the database.
public insertData ( string $query, array $params = [] ) : integer | false
$query string The query to execute.
$params array Query params. E.g. [1, 'steve'] or [':id' => 1, ':name' => 'steve']
return integer | false The database id of the inserted row if a AUTO_INCREMENT field is defined, 0 if not, and false on failure.

invalidateQueryCache() protected method

Invalidate the query cache
protected invalidateQueryCache ( ) : void
return void

registerDelayedQuery() public method

You can specify a callback if you care about the result. This function will always be passed a \Doctrine\DBAL\Driver\Statement.
public registerDelayedQuery ( string $query, string $type, callable $callback = null, array $params = [] ) : boolean
$query string The query to execute
$type string The query type ('read' or 'write')
$callback callable A callback function to pass the results array to
$params array Query params. E.g. [1, 'steve'] or [':id' => 1, ':name' => 'steve']
return boolean Whether registering was successful.

runSqlScript() public method

The file specified should be a standard SQL file as created by mysqldump or similar. Statements must be terminated with ; and a newline character (\n or \r\n). The special string 'prefix_' is replaced with the database prefix as defined in {@link $this->tablePrefix}.
public runSqlScript ( string $scriptlocation ) : void
$scriptlocation string The full path to the script
return void

sanitizeInt() public method

Sanitizes an integer value for use in a query
Deprecation: Use query parameters where possible
public sanitizeInt ( integer $value, boolean $signed = true ) : integer
$value integer Value to sanitize
$signed boolean Whether negative values are allowed (default: true)
return integer

sanitizeString() public method

Sanitizes a string for use in a query
Deprecation: Use query parameters where possible
public sanitizeString ( string $value ) : string
$value string Value to escape
return string

setLogger() public method

Set the logger object
public setLogger ( Logger $logger ) : void
$logger Logger The logger
return void

setupConnections() public method

If the configuration has been set up for multiple read/write databases, set those links up separately; otherwise just create the one database link.
public setupConnections ( ) : void
return void

updateData() public method

Update the database.
public updateData ( string $query, boolean $get_num_rows = false, array $params = [] ) : boolean | integer
$query string The query to run.
$get_num_rows boolean Return the number of rows affected (default: false).
$params array Query params. E.g. [1, 'steve'] or [':id' => 1, ':name' => 'steve']
return boolean | integer