示例#1
0
 public function query($type, $sql, $as_object = FALSE, array $params = NULL)
 {
     // Make sure the database is connected
     $this->_connection or $this->connect();
     if (Kohana::$profiling && stripos($sql, 'explain') !== 0) {
         // Benchmark this query for the current instance
         $benchmark = Profiler::start("Database ({$this->_instance})", $sql);
     }
     if (!empty($this->_config['connection']['persistent']) and $this->_config['connection']['database'] !== Database_MySQL::$_current_databases[$this->_connection_id]) {
         // Select database on persistent connections
         $this->_select_db($this->_config['connection']['database']);
     }
     // Execute the query
     if (($result = mysql_query($sql, $this->_connection)) === FALSE) {
         if (isset($benchmark)) {
             // This benchmark is worthless
             Profiler::delete($benchmark);
         }
         throw new Database_Exception(':error [ :query ]', array(':error' => mysql_error($this->_connection), ':query' => $sql), mysql_errno($this->_connection));
     }
     if (isset($benchmark)) {
         Profiler::stop($benchmark);
     }
     // Set the last query
     $this->last_query = $sql;
     if ($type === Database::SELECT) {
         // Return an iterator of results
         $res = new Database_MySQL_Result($result, $sql, $as_object, $params);
         if (stripos($sql, 'explain') !== 0) {
             ProfilerToolbar::setSqlData($this->_instance, $sql, $res->count());
         }
         return $res;
     } elseif ($type === Database::INSERT) {
         // Return a list of insert id and rows created
         $res = array(mysql_insert_id($this->_connection), mysql_affected_rows($this->_connection));
         ProfilerToolbar::setSqlData($this->_instance, $sql, $res[1]);
         return $res;
     } else {
         // Return the number of rows affected
         $res = mysql_affected_rows($this->_connection);
         ProfilerToolbar::setSqlData($this->_instance, $sql, $res);
         return $res;
     }
 }
示例#2
0
 /**
  * (non-PHPdoc)
  * @see modules/auth/classes/kohana/Kohana_Database_Mysql#query()
  */
 public function query($type, $sql, $as_object = FALSE, array $params = NULL)
 {
     if (isset($params['caching'])) {
         $this->_allow_caching = $params['caching'];
     }
     if ($this->_allow_caching === TRUE) {
         list($auto_type, $tags) = $this->_get_tables($sql);
     }
     $cache_key = $this->get_prefix('Database::query("' . $sql . '")');
     $result = FALSE;
     // Make sure the database is connected
     $this->_connection or $this->connect();
     if (!empty($this->_config['connection']['persistent']) and $this->_config['connection']['database'] !== Database_MySQL::$_current_databases[$this->_connection_id]) {
         // Select database on persistent connections
         $this->_select_db($this->_config['connection']['database']);
     }
     $from_cache = TRUE;
     if ($this->_allow_caching !== TRUE or $type != Database::SELECT or ($result = $this->_cache_instance->get($cache_key)) === NULL) {
         $from_cache = FALSE;
         if (!empty($this->_config['profiling'])) {
             // Benchmark this query for the current instance
             $benchmark = Profiler::start("Database ({$this->_instance})", $sql);
         }
         $result = mysql_query($sql, $this->_connection);
         // Execute the query
         if ($result === FALSE) {
             if (isset($benchmark)) {
                 // This benchmark is worthless
                 Profiler::delete($benchmark);
             }
             throw new Database_Exception(mysql_errno($this->_connection), '[:code] :error ( :query )', array(':code' => mysql_errno($this->_connection), ':error' => mysql_error($this->_connection), ':query' => $sql));
         }
     }
     if (isset($benchmark)) {
         Profiler::stop($benchmark);
     }
     // Set the last query
     $this->last_query = $sql;
     if ($type === Database::SELECT) {
         if (is_resource($result)) {
             $returned = new Database_MySQL_Result($result, $sql, $as_object, $params);
             if ($this->_allow_caching === TRUE) {
                 $this->_cache_instance->set_with_tags($cache_key, $returned->as_array(), Kohana::$cache_life, $tags);
             }
         } else {
             $returned = new Database_Result_Cached($result, $sql, $as_object);
         }
         return $returned;
     } else {
         if ($this->_allow_caching === TRUE) {
             if (!empty($tags)) {
                 // Сброс кэша
                 foreach ($tags as $tag) {
                     $this->_cache_instance->delete_tag($tag, 0);
                 }
             }
         }
         if ($type === Database::INSERT or $type === Database::REPLACE) {
             // Return a list of insert id and rows created
             return array(mysql_insert_id($this->_connection), mysql_affected_rows($this->_connection));
         } else {
             // Return the number of rows affected
             return mysql_affected_rows($this->_connection);
         }
     }
     return $result;
 }