Example #1
0
 /**
  * Run a query.
  *
  * @param DB\Query $query Current query object.
  *
  * @return mixed
  */
 public function run(DB\Query $query)
 {
     $query->appendTablesPrefix(Core\Config()->DB['tables_prefix']);
     $sql = $this->buildSql($query);
     $query_hash = md5(serialize(array('query' => $sql, 'bind_params' => $query->bind_params)));
     $query_cache_name = implode(',', array($query->table, implode(',', array_map(function ($item) {
         return $item['table'];
     }, $query->join))));
     if (array_key_exists($query_hash, Core\DbCache()->getCache($query_cache_name))) {
         return Core\DbCache()->getCache($query_cache_name, $query_hash);
     }
     $this->storeQueries($sql, $query->bind_params);
     if ($query->type === 'select') {
         $res = $this->query($sql, $query->bind_params);
         Core\DbCache()->setCache($query_cache_name, $query_hash, $res);
         return $res;
     }
     foreach (Core\DbCache()->cache as $table => $value) {
         if (in_array($query->table, explode(',', $table), true)) {
             Core\DbCache()->clearCache($table);
         }
     }
     return $this->execute($sql, $query->bind_params);
 }