/** * Execute an sql query, Cache the result if needed, Load the result * if available. * * @global object $cache Cache object included for sql caching * @param string $sql The sql query to be executed * @param boolean $transact Transact the query? * @param string $cache_name The cache handle for this query * @return mixed */ function sql_query($sql, $transact = false, $cache_name = false) { global $cache; // Increment the query counter. $this->stats['queries']++; // Check for caching if ( $cache_name != false ) { // Allow the query to be cached without a handle if ( gettype($cache_name) == 'boolean' ) { // The handle becomes an md5 string of the sql $cache_name = md5($sql); } // If not, execute and handle bad results. if ( ! $result = $this->layer->sql_query($sql, $transact) ) { // Report a query error. trigger_error('Error_DB_Query', ERROR_DB); } // Attempt to load cache if ( ! $this->cache[$cache_name] = $cache->sql->load($cache_name) ) { // If load fails, free var $this->sql_freeresult($result); } // Only execute if not already set if ( ! isset ( $this->cache[$cache_name] ) ) { // Handle caching if needed $cache[$cache_name] = $this->sql_fetchrowset($result); $cache->sql->store($cache_name, $cache[$cache_name]); // Free the result $this->sql_freeresult($result); } // Return as cache handle $result = $cache_name; // Increment cached queries $this->stats['queries']++; } // Check if cache loaded if ( ! $result ) { // If not, execute and handle bad results. if ( ! $result = $this->layer->sql_query($sql, $transact) ) { // Report a query error. trigger_error('Error_DB_Query', ERROR_DB); } } // Return Identifier return $result; }