Elgg  Version 2.3
Public Member Functions | List of all members
Elgg\Application\Database Class Reference
Inheritance diagram for Elgg\Application\Database:
Elgg\Database

Public Member Functions

 __construct (ElggDb $db)
 Constructor. More...
 
 getData ($query, $callback='', array $params=[])
 {Retrieve rows from the database.Queries are executed with () and results are retrieved with (). 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
string$queryThe query being passed.
callable$callbackOptionally, the name of a function to call back to on each row
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
} More...
 
 getDataRow ($query, $callback='', array $params=[])
 {Retrieve a single row from the database.Similar to () 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
string$queryThe query to execute.
callable$callbackA callback function to apply to the row
array$paramsQuery params. E.g. [1, 'steve'] or [':id' => 1, ':name' => 'steve']
Returns
mixed A single database result object or the result of the callback function.
Exceptions
} More...
 
 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
} More...
 
 updateData ($query, $getNumRows=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
} More...
 
 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
} More...
 
 getTablePrefix ()
 {Get the value of the "prefix" property.
See also
Database::$prefix
Returns
string
} More...
 
 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
} More...
 
 sanitizeString ($value)
 {Sanitizes a string for use in a query.
Parameters
string$valueValue to escape
Returns
string
Exceptions
} More...
 
 __get ($name)
 Handle magic property reads. More...
 
 __set ($name, $value)
 Handle magic property writes. More...
 
 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 @access private
} More...
 
 setTimer (Timer $timer)
 {} More...
 
 setLogger (Logger $logger)
 {Set the logger object.
Parameters
Logger$loggerThe logger
Returns
void @access private
} More...
 
 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
} More...
 
 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
} More...
 
 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 \r
).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
} More...
 
 registerDelayedQuery ($query, $type, $handler="", 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 \Doctrine\DBAL\Driver\Statement.
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. @access private
} More...
 
 executeDelayedQueries ()
 {Trigger all queries that were registered as "delayed" queries.This is called by the system automatically on shutdown.
Returns
void @access private
} More...
 
 enableQueryCache ()
 {Enable the query cache.This does not take precedence over the \Elgg\Database\Config setting.
Returns
void @access private
} More...
 
 disableQueryCache ()
 {Disable the query cache.This is useful for special scripts that pull large amounts of data back in single queries.
Returns
void @access private
} More...
 
 assertInstalled ()
 {Test that the Elgg database is installed.
Returns
void
Exceptions
} More...
 
 getQueryCount ()
 {Get the number of queries made to the database.
Returns
int @access private
} More...
 
 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 @access private
} More...
 
- Public Member Functions inherited from Elgg\Database
 __construct (\Elgg\Database\Config $config, \Elgg\Logger $logger=null)
 Constructor. More...
 

Additional Inherited Members

- Public Attributes inherited from Elgg\Database
const DELAYED_QUERY = 'q'
 
const DELAYED_TYPE = 't'
 
const DELAYED_HANDLER = 'h'
 
const DELAYED_PARAMS = 'p'
 
- Protected Member Functions inherited from Elgg\Database
 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 22 of file Database.php.

Constructor & Destructor Documentation

◆ __construct()

Elgg\Application\Database::__construct ( ElggDb  $db)

Constructor.

Parameters
ElggDb$dbThe Elgg database @access private

Definition at line 37 of file Database.php.

Member Function Documentation

◆ __get()

Elgg\Application\Database::__get (   $name)

Handle magic property reads.

Parameters
string$nameProperty name
Returns
mixed

Reimplemented from Elgg\Database.

Definition at line 107 of file Database.php.

◆ __set()

Elgg\Application\Database::__set (   $name,
  $value 
)

Handle magic property writes.

Parameters
string$nameProperty name
mixed$valueValue
Returns
void

Reimplemented from Elgg\Database.

Definition at line 122 of file Database.php.

◆ assertInstalled()

Elgg\Application\Database::assertInstalled ( )

{Test that the Elgg database is installed.

Returns
void
Exceptions
}

Reimplemented from Elgg\Database.

Definition at line 231 of file Database.php.

◆ connect()

Elgg\Application\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
}

Reimplemented from Elgg\Database.

Definition at line 171 of file Database.php.

◆ deleteData()

Elgg\Application\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
}

Reimplemented from Elgg\Database.

Definition at line 72 of file Database.php.

◆ disableQueryCache()

Elgg\Application\Database::disableQueryCache ( )

{Disable the query cache.This is useful for special scripts that pull large amounts of data back in single queries.

Returns
void @access private
}

Reimplemented from Elgg\Database.

Definition at line 221 of file Database.php.

◆ enableQueryCache()

Elgg\Application\Database::enableQueryCache ( )

{Enable the query cache.This does not take precedence over the \Elgg\Database\Config setting.

Returns
void @access private
}

Reimplemented from Elgg\Database.

Definition at line 211 of file Database.php.

◆ executeDelayedQueries()

Elgg\Application\Database::executeDelayedQueries ( )

{Trigger all queries that were registered as "delayed" queries.This is called by the system automatically on shutdown.

Returns
void @access private
}

Reimplemented from Elgg\Database.

Definition at line 201 of file Database.php.

◆ fingerprintCallback()

Elgg\Application\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 @access private
}

Reimplemented from Elgg\Database.

Definition at line 131 of file Database.php.

◆ getData()

Elgg\Application\Database::getData (   $query,
  $callback = '',
array  $params = [] 
)

{Retrieve rows from the database.Queries are executed with () and results are retrieved with (). 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
string$queryThe query being passed.
callable$callbackOptionally, the name of a function to call back to on each row
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
}

Reimplemented from Elgg\Database.

Definition at line 44 of file Database.php.

◆ getDataRow()

Elgg\Application\Database::getDataRow (   $query,
  $callback = '',
array  $params = [] 
)

{Retrieve a single row from the database.Similar to () 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
string$queryThe query to execute.
callable$callbackA callback function to apply to the row
array$paramsQuery params. E.g. [1, 'steve'] or [':id' => 1, ':name' => 'steve']
Returns
mixed A single database result object or the result of the callback function.
Exceptions
}

Reimplemented from Elgg\Database.

Definition at line 51 of file Database.php.

◆ getQueryCount()

Elgg\Application\Database::getQueryCount ( )

{Get the number of queries made to the database.

Returns
int @access private
}

Reimplemented from Elgg\Database.

Definition at line 241 of file Database.php.

◆ getServerVersion()

Elgg\Application\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 @access private
}

Reimplemented from Elgg\Database.

Definition at line 251 of file Database.php.

◆ getTablePrefix()

Elgg\Application\Database::getTablePrefix ( )

{Get the value of the "prefix" property.

See also
Database::$prefix
Returns
string
}

Reimplemented from Elgg\Database.

Definition at line 80 of file Database.php.

◆ insertData()

Elgg\Application\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
}

Reimplemented from Elgg\Database.

Definition at line 58 of file Database.php.

◆ registerDelayedQuery()

Elgg\Application\Database::registerDelayedQuery (   $query,
  $type,
  $handler = "",
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 \Doctrine\DBAL\Driver\Statement.

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. @access private
}

Reimplemented from Elgg\Database.

Definition at line 191 of file Database.php.

◆ runSqlScript()

Elgg\Application\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 \r
).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
}

Reimplemented from Elgg\Database.

Definition at line 181 of file Database.php.

◆ sanitizeInt()

Elgg\Application\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
}

Reimplemented from Elgg\Database.

Definition at line 90 of file Database.php.

◆ sanitizeString()

Elgg\Application\Database::sanitizeString (   $value)

{Sanitizes a string for use in a query.

Parameters
string$valueValue to escape
Returns
string
Exceptions
}

Reimplemented from Elgg\Database.

Definition at line 97 of file Database.php.

◆ setLogger()

Elgg\Application\Database::setLogger ( Logger  $logger)

{Set the logger object.

Parameters
Logger$loggerThe logger
Returns
void @access private
}

Reimplemented from Elgg\Database.

Definition at line 151 of file Database.php.

◆ setTimer()

Elgg\Application\Database::setTimer ( Timer  $timer)

{}

Definition at line 141 of file Database.php.

◆ setupConnections()

Elgg\Application\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
}

Reimplemented from Elgg\Database.

Definition at line 161 of file Database.php.

◆ updateData()

Elgg\Application\Database::updateData (   $query,
  $getNumRows = 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
}

Reimplemented from Elgg\Database.

Definition at line 65 of file Database.php.


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