diff options
Diffstat (limited to 'engine/lib/database.php')
-rw-r--r-- | engine/lib/database.php | 40 |
1 files changed, 24 insertions, 16 deletions
diff --git a/engine/lib/database.php b/engine/lib/database.php index 7d90b30b8..2b348366d 100644 --- a/engine/lib/database.php +++ b/engine/lib/database.php @@ -17,7 +17,9 @@ * $DB_QUERY_CACHE[$query] => array(result1, result2, ... resultN) * </code> * - * @global array $DB_QUERY_CACHE + * @warning be array this var may be an array or ElggStaticVariableCache depending on when called :( + * + * @global ElggStaticVariableCache|array $DB_QUERY_CACHE */ global $DB_QUERY_CACHE; $DB_QUERY_CACHE = array(); @@ -48,7 +50,7 @@ $DB_DELAYED_QUERIES = array(); * 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. * - * @global array $dblink + * @global resource[] $dblink */ global $dblink; $dblink = array(); @@ -72,11 +74,12 @@ $dbcalls = 0; * resource. eg "read", "write", or "readwrite". * * @return void + * @throws DatabaseException * @access private */ function establish_db_link($dblinkname = "readwrite") { // Get configuration, and globalise database link - global $CONFIG, $dblink, $DB_QUERY_CACHE, $dbcalls; + global $CONFIG, $dblink, $DB_QUERY_CACHE; if ($dblinkname != "readwrite" && isset($CONFIG->db[$dblinkname])) { if (is_array($CONFIG->db[$dblinkname])) { @@ -120,6 +123,8 @@ function establish_db_link($dblinkname = "readwrite") { // Set up cache if global not initialized and query cache not turned off if ((!$DB_QUERY_CACHE) && (!$db_cache_off)) { + // @todo everywhere else this is assigned to array(), making it dangerous to call + // object methods on this. We should consider making this an plain array $DB_QUERY_CACHE = new ElggStaticVariableCache('db_query_cache'); } } @@ -134,7 +139,7 @@ function establish_db_link($dblinkname = "readwrite") { * @access private */ function setup_db_connections() { - global $CONFIG, $dblink; + global $CONFIG; if (!empty($CONFIG->db->split)) { establish_db_link('read'); @@ -197,7 +202,7 @@ function db_delayedexecution_shutdown_hook() { * * @param string $dblinktype The type of link we want: "read", "write" or "readwrite". * - * @return object Database link + * @return resource Database link * @access private */ function get_db_link($dblinktype) { @@ -216,7 +221,7 @@ function get_db_link($dblinktype) { /** * Execute an EXPLAIN for $query. * - * @param str $query The query to explain + * @param string $query The query to explain * @param mixed $link The database link resource to user. * * @return mixed An object of the query's result, or FALSE @@ -240,14 +245,14 @@ function explain_query($query, $link) { * {@link $dbcalls} is incremented and the query is saved into the {@link $DB_QUERY_CACHE}. * * @param string $query The query - * @param link $dblink The DB link + * @param resource $dblink The DB link * - * @return The result of mysql_query() + * @return resource result of mysql_query() * @throws DatabaseException * @access private */ function execute_query($query, $dblink) { - global $CONFIG, $dbcalls; + global $dbcalls; if ($query == NULL) { throw new DatabaseException(elgg_echo('DatabaseException:InvalidQuery')); @@ -275,7 +280,7 @@ function execute_query($query, $dblink) { * the raw result from {@link mysql_query()}. * * @param string $query The query to execute - * @param resource $dblink The database link to use or the link type (read | write) + * @param resource|string $dblink The database link to use or the link type (read | write) * @param string $handler A callback function to pass the results array to * * @return true @@ -386,7 +391,7 @@ function get_data_row($query, $callback = "") { * @access private */ function elgg_query_runner($query, $callback = null, $single = false) { - global $CONFIG, $DB_QUERY_CACHE; + global $DB_QUERY_CACHE; // Since we want to cache results of running the callback, we need to // need to namespace the query with the callback and single result request. @@ -410,7 +415,7 @@ function elgg_query_runner($query, $callback = null, $single = false) { // test for callback once instead of on each iteration. // @todo check profiling to see if this needs to be broken out into - // explicit cases instead of checking in the interation. + // explicit cases instead of checking in the iteration. $is_callable = is_callable($callback); while ($row = mysql_fetch_object($result)) { if ($is_callable) { @@ -451,7 +456,7 @@ function elgg_query_runner($query, $callback = null, $single = false) { * @access private */ function insert_data($query) { - global $CONFIG, $DB_QUERY_CACHE; + global $DB_QUERY_CACHE; elgg_log("DB query $query", 'NOTICE'); @@ -459,6 +464,7 @@ function insert_data($query) { // Invalidate query cache if ($DB_QUERY_CACHE) { + /* @var ElggStaticVariableCache $DB_QUERY_CACHE */ $DB_QUERY_CACHE->clear(); } @@ -482,7 +488,7 @@ function insert_data($query) { * @access private */ function update_data($query) { - global $CONFIG, $DB_QUERY_CACHE; + global $DB_QUERY_CACHE; elgg_log("DB query $query", 'NOTICE'); @@ -490,6 +496,7 @@ function update_data($query) { // Invalidate query cache if ($DB_QUERY_CACHE) { + /* @var ElggStaticVariableCache $DB_QUERY_CACHE */ $DB_QUERY_CACHE->clear(); elgg_log("Query cache invalidated", 'NOTICE'); } @@ -512,7 +519,7 @@ function update_data($query) { * @access private */ function delete_data($query) { - global $CONFIG, $DB_QUERY_CACHE; + global $DB_QUERY_CACHE; elgg_log("DB query $query", 'NOTICE'); @@ -520,6 +527,7 @@ function delete_data($query) { // Invalidate query cache if ($DB_QUERY_CACHE) { + /* @var ElggStaticVariableCache $DB_QUERY_CACHE */ $DB_QUERY_CACHE->clear(); elgg_log("Query cache invalidated", 'NOTICE'); } @@ -638,7 +646,7 @@ function run_sql_script($scriptlocation) { $statement = str_replace("prefix_", $CONFIG->dbprefix, $statement); if (!empty($statement)) { try { - $result = update_data($statement); + update_data($statement); } catch (DatabaseException $e) { $errors[] = $e->getMessage(); } |