public function query($sql) { if (Pix_Table::$_log_groups[Pix_Table::LOG_QUERY]) { Pix_Table::debug(sprintf("[%s]\t%40s", $this->_path . $this->_name, $sql)); } $starttime = microtime(true); $statement = $this->_pdo->prepare($sql); if (!$statement) { if ($errno = $this->_pdo->errorCode()) { $errorInfo = $this->_pdo->errorInfo(); } if ($errorInfo[2] == 'PRIMARY KEY must be unique' or preg_match('/duplicate key value violates unique constraint/', $errorInfo[2])) { throw new Pix_Table_DuplicateException(); } throw new Exception("SQL Error: ({$errorInfo[0]}:{$errorInfo[1]}) {$errorInfo[2]} (SQL: {$sql})"); } $res = $statement->execute(); if ($t = Pix_Table::getLongQueryTime() and ($delta = microtime(true) - $starttime) > $t) { Pix_Table::debug(sprintf("[%s]\t%s\t%40s", $this->_pdo->getAttribute(PDO::ATTR_SERVER_INFO), $delta, $sql)); } if ($res === false) { if ($errno = $this->_pdo->errorCode()) { $errorInfo = $this->_pdo->errorInfo(); } if ($errorInfo[2] == 'PRIMARY KEY must be unique' or preg_match('/duplicate key value violates unique constraint/', $errorInfo[2])) { throw new Pix_Table_DuplicateException(); } throw new Exception("SQL Error: ({$errorInfo[0]}:{$errorInfo[1]}) {$errorInfo[2]} (SQL: {$sql})"); } return new Pix_Table_Db_Adapter_PDO_Result($statement); }
/** * query 對 db 下 SQL query * * @param mixed $sql * @access protected * @return Mysqli result */ public function query($sql, $table = null) { if (Pix_Table::$_log_groups[Pix_Table::LOG_QUERY]) { Pix_Table::debug(sprintf("[%s]\t%40s", $this->_link->host_info, $sql)); } // TODO 需要 log SQL Query 功能 if ($comment = Pix_Table::getQueryComment()) { $sql = trim($sql, '; ') . ' #' . $comment; } $starttime = microtime(true); $res = $this->_link->query($sql); if ($t = Pix_Table::getLongQueryTime() and ($delta = microtime(true) - $starttime) > $t) { Pix_Table::debug(sprintf("[%s]\t%s\t%40s", $this->_link->host_info, $delta, $sql)); } if ($res === false) { if ($errno = $this->_link->errno) { switch ($errno) { case 1062: throw new Pix_Table_DuplicateException($this->_link->error, $errno); case 1406: throw new Pix_Table_DataTooLongException($this->_link->error, $errno); default: throw new Exception("SQL Error: {$this->_link->error} SQL: {$sql}"); } } } return $res; }
public function query($sql) { if (Pix_Table::$_log_groups[Pix_Table::LOG_QUERY]) { Pix_Table::debug(sprintf("[%s]\t%40s", $this->_path . $this->_name, $sql)); } $starttime = microtime(true); $statement = $this->_pdo->prepare($sql); if (!$statement) { if ($errno = $this->_pdo->errorCode()) { $errorInfo = $this->_pdo->errorInfo(); } if ($errorInfo[2] == 'PRIMARY KEY must be unique' or preg_match('/columns? .+ (are|is) not unique/', $errorInfo[2]) or strpos($errorInfo[2], 'UNIQUE constraint failed:') === 0) { throw new Pix_Table_DuplicateException(); } throw new Exception("SQL Error: ({$errorInfo[0]}:{$errorInfo[1]}) {$errorInfo[2]} (SQL: {$sql})"); } $res = $statement->execute(); if ($t = Pix_Table::getLongQueryTime() and ($delta = microtime(true) - $starttime) > $t) { Pix_Table::debug(sprintf("[%s]\t%s\t%40s", $this->_path, $delta, $sql)); } if ($res === false) { if ($errno = $statement->errorCode()) { $errorInfo = $statement->errorInfo(); } if ($errorInfo[2] == 'PRIMARY KEY must be unique' or preg_match('/columns? .+ (are|is) not unique/', $errorInfo[2]) or strpos($errorInfo[2], 'UNIQUE constraint failed:') === 0) { throw new Pix_Table_DuplicateException(); } throw new Exception("SQL Error: ({$errorInfo[0]}:{$errorInfo[1]}) {$errorInfo[2]} (SQL: {$sql})"); } return new Pix_Table_Db_Adapter_PDO_Result($statement); }
/** * query * * @param string $sql * @access protected * @return mssql result */ public function query($sql, $table = null) { if (Pix_Table::$_log_groups[Pix_Table::LOG_QUERY]) { Pix_Table::debug(sprintf("[%s]\t%40s", strval($this->_link), $sql)); } // TODO: log sql query if ($comment = Pix_Table::getQueryComment()) { $sql = trim($sql, '; ') . ' #' . $comment; } $starttime = microtime(true); $res = mssql_query($sql, $this->_link); if ($t = Pix_Table::getLongQueryTime() and ($delta = microtime(true) - $starttime) > $t) { Pix_Table::debug(sprintf("[%s]\t%s\t%40s", strval($this->_link), $delta, $sql)); } if ($res === false) { throw new Exception("SQL Error: {$this->_link} SQL: {$sql}"); } return new Pix_Table_Db_Adapter_MsSQL_Result($res); }
public function query($sql, $table = null) { // 判斷要用 Master 還是 Slave $type = 'master'; if (!Pix_Table::$_force_master and preg_match('#^SELECT #', strtoupper($sql))) { $type = 'slave'; } if (Pix_Setting::get('Table:ExplainFileSortEnable')) { if (preg_match('#^SELECT #', strtoupper($sql))) { $res = $this->_getLink($type)->query("EXPLAIN {$sql}"); $row = $res->fetch_assoc(); if (preg_match('#Using filesort#', $row['Extra'])) { trigger_error("Using Filesort Query {$sql}", E_USER_WARNING); } $res->free_result(); } } if (Pix_Setting::get('Table:SQLNoCache')) { if (preg_match('#^SELECT #', strtoupper($sql))) { $sql = 'SELECT SQL_NO_CACHE ' . substr($sql, 7); } } // 加上 Query Comment if ($comment = Pix_Table::getQueryComment()) { $sql = trim($sql, '; ') . ' #' . $comment; } for ($i = 0; $i < 3; $i++) { if (!($link = $this->_getLink($type))) { throw new Exception('找不到 Link'); } $starttime = microtime(true); $res = $link->query($sql); $this->insert_id = $link->insert_id; $delta = microtime(true) - $starttime; if (array_key_exists(Pix_Table::LOG_QUERY, Pix_Table::$_log_groups) and Pix_Table::$_log_groups[Pix_Table::LOG_QUERY]) { Pix_Table::debug(sprintf("[%s-%s](%f)%s", strval($link->host_info), $type, $delta, $sql)); } elseif ($t = Pix_Table::getLongQueryTime() and $delta > $t) { Pix_Table::debug(sprintf("[%s-%s](%f)%s", strval($link->host_info), $type, $delta, $sql)); } if ($res === false) { if ($errno = $link->errno) { $message = (is_null($table) ? '' : "Table: {$table->getClass()}") . "SQL Error: ({$errno}){$link->error} " . substr($sql, 0, 128); switch ($errno) { case 1146: throw new Pix_Table_TableNotFoundException($message); case 1062: throw new Pix_Table_DuplicateException((is_null($table) ? '' : "(Table: {$table->getClass()})") . $link->error, $errno); case 1406: throw new Pix_Table_DataTooLongException($message); case 2006: // MySQL server gone away // MySQL server gone away case 2013: // Lost connection to MySQL server during query trigger_error("Pix_Table " . $message, E_USER_WARNING); $this->resetConnect(); continue 2; } } throw new Pix_Table_Exception($message); } if ($link->warning_count) { $e = $link->get_warnings(); do { if (1592 == $e->errno) { continue; } if (Pix_Table::$_throw_incorrect_string_exception and 1366 == $e->errno) { throw new Pix_Table_IncorrectStringException($e->message); } trigger_error("Pix_Table " . (is_null($table) ? '' : "Table: {$table->getClass()}") . "SQL Warning: ({$e->errno}){$e->message} " . substr($sql, 0, 128), E_USER_WARNING); } while ($e->next()); } return $res; } throw new Pix_Table_Exception("query 三次失敗"); }
/** * 測試 LongQueryTime 是否能正常讀取寫入 */ public function testSetLongQueryTime() { Pix_Table::setLongQueryTime(); $this->assertEquals(Pix_Table::getLongQueryTime(), 1); Pix_Table::setLongQueryTime(100); $this->assertEquals(Pix_Table::getLongQueryTime(), 100); Pix_Table::setLongQueryTime(0); $this->assertEquals(Pix_Table::getLongQueryTime(), 0); Pix_Table::setLongQueryTime(); $this->assertEquals(Pix_Table::getLongQueryTime(), 1); }