示例#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;
     }
 }