public function query($sql, $link = null)
 {
     if (self::$debug) {
         echo "/* " . $sql . " */\n";
     }
     if ($link === null) {
         if (preg_match('/^INSERT|^UPDATE|^REPLACE|^SET|^DELETE|^TRUNCATE|^OPTIMIZE/i', $sql)) {
             $link = $this->getConnectionForWrite();
         } else {
             $link = $this->getConnectionForRead();
         }
     }
     if (Mysql::$debug) {
         //var_dump($this->get_tags(get_object_vars($this)));
         if (Config::getSafe('query_cache', false) && $this->allow_caching) {
             if (!empty($this->cache_tags)) {
                 $tags = $this->cache_tags;
                 if (Mysql::$debug) {
                     var_dump('new_tags', $tags);
                 }
             } else {
                 $tags = $this->get_tags(get_object_vars($this));
             }
             if (!preg_match('/^INSERT|^UPDATE|^REPLACE|^SET|^DELETE|^TRUNCATE/i', $sql)) {
                 $key = $this->get_cache_key($sql);
                 if (($result = Cache::getInstance(true)->get($key)) === false) {
                     if (substr($sql, 0, 4) != 'SET ') {
                         self::$num_queries++;
                     }
                     $result = new MysqlResult(mysqli_query($link, $sql), $sql, $link);
                     $cache_data = $result->as_array(true);
                     self::$cache_misses++;
                     if ($this->allow_empty_cache_result && empty($cache_data) || !empty($cache_data)) {
                         Cache::getInstance(true)->set($key, $cache_data, $tags);
                         return $result;
                     }
                 } else {
                     if ($this->allow_empty_cache_result && empty($result) || !empty($result)) {
                         self::$cache_hits++;
                         $result = new CacheResult($result, $sql);
                         return $result;
                     }
                 }
             } else {
                 Cache::getInstance(true)->setInvalidTags($tags);
             }
         }
     }
     if (substr($sql, 0, 4) != 'SET ') {
         self::$num_queries++;
     }
     $result = mysqli_query($link, $sql);
     if ($result === false) {
         error_log('Query failed by reason : ' . mysqli_error($link) . ' (' . $sql . ')');
         throw new MysqlException('Query failed by reason : ' . mysqli_error($link) . ' (' . $sql . ')');
     }
     return new MysqlResult($result, $sql, $link);
 }
Exemple #2
0
 public function query($sql, $link = null)
 {
     if (self::$debug) {
         echo "/* " . $sql . " */\n";
     }
     if ($link === null) {
         if (preg_match('/^INSERT|^UPDATE|^REPLACE|^SET|^DELETE|^TRUNCATE|^OPTIMIZE/i', $sql)) {
             $link = $this->getConnectionForWrite();
         } else {
             $link = $this->getConnectionForRead();
         }
     }
     if (Config::getSafe('query_cache', false) && $this->allow_caching) {
         $tags = $this->get_tags(get_object_vars($this));
         if (!preg_match('/^INSERT|^UPDATE|^REPLACE|^SET|^DELETE|^TRUNCATE/i', $sql)) {
             $key = $this->get_cache_key($sql);
             if (($result = $this->cache->get($key)) === false) {
                 self::$num_queries++;
                 $result = new MysqlResult(mysqli_query($link, $sql), $sql, $link);
                 $this->cache->set($key, $result->as_array(true), $tags);
                 self::$cache_misses++;
                 return $result;
             } else {
                 self::$cache_hits++;
                 $result = new CacheResult($result, $sql);
                 return $result;
             }
         } else {
             $this->cache->setInvalidTags($tags);
         }
     }
     $this->enable_caching();
     self::$num_queries++;
     $result = mysqli_query($link, $sql);
     if ($result === false) {
         throw new MysqlException('Query failed by reason : ' . mysqli_error($link) . ' (' . $sql . ')');
     }
     return new MysqlResult($result, $sql, $link);
 }