19 private $dbLinks = array();
22 private $queryCount = 0;
35 private $queryCache = null;
40 private $queryCacheSize = 50;
57 private $delayedQueries = array();
60 private $installed =
false;
76 $this->logger = $logger;
77 $this->config = $config;
97 if (isset($this->dbLinks[
$type])) {
98 return $this->dbLinks[
$type];
99 }
else if (isset($this->dbLinks[
'readwrite'])) {
100 return $this->dbLinks[
'readwrite'];
117 if ($this->config->isDatabaseSplit()) {
139 $conf = $this->config->getConnectionConfig($dblinkname);
142 $this->dbLinks[$dblinkname] = mysql_connect($conf[
'host'], $conf[
'user'], $conf[
'password'],
true);
143 if (!$this->dbLinks[$dblinkname]) {
144 $msg =
"Elgg couldn't connect to the database using the given credentials. Check the settings file.";
148 if (!mysql_select_db($conf[
'database'], $this->dbLinks[$dblinkname])) {
149 $msg =
"Elgg couldn't select the database '{$conf['database']}'. Please check that the database is created and you have access to it.";
154 mysql_query(
"SET NAMES utf8");
173 public function getData($query, $callback =
'') {
174 return $this->
getResults($query, $callback,
false);
191 return $this->
getResults($query, $callback,
true);
209 $dblink = $this->
getLink(
'write');
214 return mysql_insert_id($dblink);
235 $dblink = $this->
getLink(
'write');
241 return mysql_affected_rows($dblink);
264 $dblink = $this->
getLink(
'write');
269 return mysql_affected_rows($dblink);
289 if (is_string($callback)) {
292 if (is_object($callback)) {
293 return spl_object_hash($callback) .
"::__invoke";
295 if (is_array($callback)) {
296 if (is_string($callback[0])) {
297 return "{$callback[0]}::{$callback[1]}";
299 return spl_object_hash($callback[0]) .
"::{$callback[1]}";
317 protected function getResults($query, $callback = null, $single =
false) {
322 $query_id = (int)$single . $query .
'|';
324 $is_callable = is_callable($callback);
332 $is_callable =
false;
335 $hash = md5($query_id);
338 if ($this->queryCache) {
339 if (isset($this->queryCache[$hash])) {
340 $this->logger->log(
"DB query $query results returned from cache (hash: $hash)",
Elgg_Logger::INFO);
341 return $this->queryCache[$hash];
345 $dblink = $this->
getLink(
'read');
351 $row = call_user_func($callback,
$row);
368 if ($this->queryCache) {
369 $this->queryCache[$hash] =
$return;
370 $this->logger->log(
"DB query $query results cached (hash: $hash)",
Elgg_Logger::INFO);
391 if ($query == null) {
395 if (!is_resource($dblink)) {
401 $result = mysql_query($query, $dblink);
403 if (mysql_errno($dblink)) {
435 $script = file_get_contents($scriptlocation);
441 $script = preg_replace(
'/^(?:--|#) .*$/m',
'', $script);
444 $sql_statements = preg_split(
'/;[\n\r]+/',
"$script\n");
446 foreach ($sql_statements as $statement) {
447 $statement = trim($statement);
448 $statement = str_replace(
"prefix_", $this->tablePrefix, $statement);
449 if (!empty($statement)) {
460 $errortxt .=
" {$error};";
463 $msg =
"There were a number of issues: " . $errortxt;
467 $msg =
"Elgg couldn't find the requested database script at " . $scriptlocation .
".";
492 $delayed_query = array();
493 $delayed_query[
'q'] = $query;
494 $delayed_query[
'l'] =
$type;
497 $this->delayedQueries[] = $delayed_query;
513 foreach ($this->delayedQueries as $query_details) {
515 $link = $query_details[
'l'];
517 if ($link ==
'read' || $link ==
'write') {
519 } elseif (!is_resource($link)) {
520 $msg =
"Link for delayed query not valid resource or db_link type. Query: {$query_details['q']}";
526 if ((isset($query_details[
'h'])) && (is_callable($query_details[
'h']))) {
544 if ($this->config->isQueryCacheEnabled() && $this->queryCache === null) {
559 $this->queryCache = null;
568 if ($this->queryCache) {
569 $this->queryCache->clear();
587 $dblink = $this->
getLink(
'read');
588 mysql_query(
"SELECT value FROM {$this->tablePrefix}datalists WHERE name = 'installed'", $dblink);
589 if (mysql_errno($dblink) > 0) {
593 throw new InstallationException(
"Unable to handle this request. This site is not configured or the database is down.");
605 return $this->queryCount;
614 return $this->tablePrefix;
627 if ($signed ===
false) {
643 return mysql_real_escape_string(
$value);
getResults($query, $callback=null, $single=false)
Handles queries that return results, running the results through a an optional callback function...
getDataRow($query, $callback= '')
Retrieve a single row from the database.
if(!$owner||!($owner instanceof ElggUser)||!$owner->canEdit()) $error
setupConnections()
Establish database connections.
$CONFIG installed
Is the site fully installed.
__construct(Elgg_Database_Config $config, Elgg_Logger $logger)
Constructor.
executeDelayedQueries()
Trigger all queries that were registered as "delayed" queries.
updateData($query, $getNumRows=false)
Update the database.
sanitizeString($value)
Sanitizes a string for use in a query.
deleteData($query)
Delete data from the database.
getQueryCount()
Get the number of queries made to the database.
getLink($type)
Gets (if required, also creates) a database link resource.
runSqlScript($scriptlocation)
Runs a full database script from disk.
fingerprintCallback($callback)
Get a string that uniquely identifies a callback during the current request.
sanitizeInt($value, $signed=true)
Sanitizes an integer value for use in a query.
enableQueryCache()
Enable the query cache.
insertData($query)
Insert a row into the database.
getData($query, $callback= '')
Retrieve rows from the database.
disableQueryCache()
Disable the query cache.
getTablePrefix()
Get the database table prefix.
assertInstalled()
Test that the Elgg database is installed.
registerDelayedQuery($query, $type, $handler="")
Queue a query for execution upon shutdown.
establishLink($dblinkname="readwrite")
Establish a connection to the database server.
getTablePrefix()
Get the prefix for Elgg's tables.
executeQuery($query, $dblink)
Execute a query.
invalidateQueryCache()
Invalidate the query cache.