PHP Класс Elgg\Database

Наследование: use trait Profilable
Показать файл Открыть проект Примеры использования класса

Открытые методы

Метод Описание
__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.

Защищенные методы

Метод Описание
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

Описание методов

__construct() публичный Метод

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

__get() публичный Метод

Handle magic property reads
public __get ( string $name ) : mixed
$name string Property name
Результат mixed

__set() публичный Метод

Handle magic property writes
public __set ( string $name, mixed $value ) : void
$name string Property name
$value mixed Value
Результат void

assertInstalled() публичный Метод

Test that the Elgg database is installed
public assertInstalled ( ) : void
Результат void

connect() публичный Метод

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".
Результат void

deleteData() публичный Метод

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']
Результат integer The number of affected rows

disableQueryCache() публичный Метод

This is useful for special scripts that pull large amounts of data back in single queries.
public disableQueryCache ( ) : void
Результат void

enableQueryCache() публичный Метод

This does not take precedence over the \Elgg\Database\Config setting.
public enableQueryCache ( ) : void
Результат void

executeDelayedQueries() публичный Метод

Trigger all queries that were registered as "delayed" queries. This is called by the system automatically on shutdown.
public executeDelayedQueries ( ) : void
Результат void

executeQuery() защищенный Метод

$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']
Результат Doctrine\DBAL\Driver\Statement The result of the query

fingerprintCallback() публичный Метод

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.
С версии: 1.9.4
public fingerprintCallback ( callable $callback ) : string
$callback callable The callable value to fingerprint
Результат string A string that is unique for each callable passed in

getConnection() защищенный Метод

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".
Результат Doctrine\DBAL\Connection

getData() публичный Метод

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']
Результат array An array of database result objects or callback function results. If the query returned nothing, an empty array.

getDataRow() публичный Метод

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']
Результат mixed A single database result object or the result of the callback function.

getQueryCount() публичный Метод

Get the number of queries made to the database
public getQueryCount ( ) : integer
Результат integer

getResults() защищенный Метод

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']
Результат array An array of database result objects or callback function results. If the query returned nothing, an empty array.

getServerVersion() публичный Метод

Get the server version number
public getServerVersion ( string $type ) : string
$type string Connection type (Config constants, e.g. Config::READ_WRITE)
Результат string Empty if version cannot be determined

insertData() публичный Метод

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']
Результат integer | false The database id of the inserted row if a AUTO_INCREMENT field is defined, 0 if not, and false on failure.

invalidateQueryCache() защищенный Метод

Invalidate the query cache
protected invalidateQueryCache ( ) : void
Результат void

registerDelayedQuery() публичный Метод

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']
Результат boolean Whether registering was successful.

runSqlScript() публичный Метод

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
Результат void

sanitizeInt() публичный Метод

Sanitizes an integer value for use in a query
Устаревший: 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)
Результат integer

sanitizeString() публичный Метод

Sanitizes a string for use in a query
Устаревший: Use query parameters where possible
public sanitizeString ( string $value ) : string
$value string Value to escape
Результат string

setLogger() публичный Метод

Set the logger object
public setLogger ( Logger $logger ) : void
$logger Logger The logger
Результат void

setupConnections() публичный Метод

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
Результат void

updateData() публичный Метод

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']
Результат boolean | integer