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) { // Benchmark this query for the current instance $benchmark = Profiler::start("Database ({$this->_instance})", $sql); } try { $result = $this->_connection->query($sql); // add query to PTB ProfilerToolbar::setSqlData($this->_instance, $sql, $result->rowCount()); } catch (Exception $e) { if (isset($benchmark)) { // This benchmark is worthless Profiler::delete($benchmark); } // Convert the exception in a database exception throw new Database_Exception(':error [ :query ]', array(':error' => $e->getMessage(), ':query' => $sql), $e->getCode()); } if (isset($benchmark)) { Profiler::stop($benchmark); } // Set the last query $this->last_query = $sql; if ($type === Database::SELECT) { // Convert the result into an array, as PDOStatement::rowCount is not reliable if ($as_object === FALSE) { $result->setFetchMode(PDO::FETCH_ASSOC); } elseif (is_string($as_object)) { $result->setFetchMode(PDO::FETCH_CLASS, $as_object, $params); } else { $result->setFetchMode(PDO::FETCH_CLASS, 'stdClass'); } $result = $result->fetchAll(); // Return an iterator of results return new Database_Result_Cached($result, $sql, $as_object, $params); } elseif ($type === Database::INSERT) { // Return a list of insert id and rows created return array($this->_connection->lastInsertId(), $result->rowCount()); } else { // Return the number of rows affected return $result->rowCount(); } }
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; } }
/** * @param string $method method of PDOStatement to be called * @param mixed $mode parameters to be passed to the method * @param array $params input parameters (name=>value) for the SQL execution. This is an alternative * to {@link bindParam} and {@link bindValue}. If you have multiple input parameters, passing * them in this way can improve the performance. Note that if you pass parameters in this way, * you cannot bind parameters or values using {@link bindParam} or {@link bindValue}, and vice versa. * Please also note that all values are treated as strings in this case, if you need them to be handled as * their real data types, you have to use {@link bindParam} or {@link bindValue} instead. * @throws Exception if CDbCommand failed to execute the SQL statement * @return mixed the method execution result */ private function queryInternal($method, $mode, $params = []) { $params = array_merge($this->params, $params); if ($this->_connection->enableParamLogging && ($pars = array_merge($this->_paramLog, $params)) !== []) { $p = []; foreach ($pars as $name => $value) { $p[$name] = $name . '=' . var_export($value, true); } $par = '. Bound with ' . implode(', ', $p); } else { $par = ''; } //var_echo('Querying SQL: ' . $this->getText() . $par, 'system.db.CDbCommand'); if ($this->_connection->queryCachingCount > 0 && $method !== '' && $this->_connection->queryCachingDuration > 0 && $this->_connection->queryCacheID !== false) { #TODO Сделать систему кеширования Sql запросов /*$this->_connection->queryCachingCount--; $cacheKey='yii:dbquery'.$this->_connection->connectionString.':'.$this->_connection->username; $cacheKey.=':'.$this->getText().':'.serialize(array_merge($this->_paramLog,$params)); if(($result=$cache->get($cacheKey))!==false) { var_echo('Query result found in cache','system.db.CDbCommand'); return $result[0]; }*/ } try { if (\Kohana::$profiling and $this->_connection->enableProfiling) { // Benchmark this query for the current instance //var_dump($this->getText()); foreach ($params as $item) { //var_echo($item); } //str_replace('') $benchmark = \Profiler::start("Database ('default')", $this->Debug($this->getText(), $this->_paramLog) . $par); } $this->prepare(); if ($params === []) { $this->_statement->execute(); } else { $this->_statement->execute($params); } if ($method === '') { $result = new DataReader($this); } else { $mode = (array) $mode; call_user_func_array([$this->_statement, 'setFetchMode'], $mode); $result = $this->_statement->{$method}(); $this->_statement->closeCursor(); } if (\Kohana::$profiling and $this->_connection->enableProfiling) { //var_dump($this->getText()); \ProfilerToolbar::setSqlData('default', $this->Debug($this->getText(), $this->_paramLog), sizeof($result)); } if (isset($cache, $cacheKey)) { \Cache::instance('database')->set($this->getText(), $result, $this->_connection->caching); } return $result; } catch (Exception $e) { if (isset($benchmark)) { // This benchmark is worthless \Profiler::delete($benchmark); } $errorInfo = $e instanceof \PDOException ? $e->errorInfo : null; $message = $e->getMessage(); \Kohana::$log->add(\Log::EMERGENCY, '\\Database\\Command::' . $method . ' failed ' . $message . '. The SQL statement executed was: ' . $this->getText() . $par); if (\Kohana::DEVELOPMENT) { $message .= '. The SQL statement executed was: ' . $this->getText() . $par; } throw new Exception('\\Database\\Command failed to execute the SQL statement: ' . $message, (int) $e->getCode(), $errorInfo); } }