get(); if ($conn === false) { throw new DbException("无法连接SQL!" . $line); } $result = $conn->query($line) === false ? false : ($conn->errno != 0 ? false : true); ZMBuf::$sql_pool->put($conn); return $result; } catch (DBException $e) { if (ZMBuf::get("sql_log") === true) { $log = "[" . date("Y-m-d H:i:s") . " " . round(microtime(true) - $starttime, 5) . "] " . $line . " (Error:" . $e->getMessage() . ")\n"; Coroutine::writeFile(CRASH_DIR . "sql.log", $log, FILE_APPEND); } Console::warning($e->getMessage()); throw $e; } } /** * @param string $line * @param array $params * @return mixed * @throws DbException */ public static function rawQuery(string $line, $params = []) { if (ZMBuf::get("sql_log") === true) { $starttime = microtime(true); } Console::debug("MySQL: ".$line); try { $conn = ZMBuf::$sql_pool->get(); if ($conn === false) { throw new DbException("无法连接SQL!" . $line); } $ps = $conn->prepare($line); if ($ps === false) { ZMBuf::$sql_pool->connect_cnt -= 1; throw new DbException("SQL语句查询错误," . $line . ",错误信息:" . $conn->error); } else { if (!($ps instanceof PDOStatement)) { throw new DbException("语句查询错误!" . $line); } if ($params == []) $result = $ps->execute(); elseif (!is_array($params)) { $result = $ps->execute([$params]); } else $result = $ps->execute($params); ZMBuf::$sql_pool->put($conn); if ($result !== true) { throw new DBException("语句[$line]错误!" . $ps->errorInfo()[2]); //echo json_encode(debug_backtrace(), 128 | 256); } if (ZMBuf::get("sql_log") === true) { $log = "[" . date("Y-m-d H:i:s") . " " . round(microtime(true) - $starttime, 4) . "] " . $line . " " . json_encode($params, JSON_UNESCAPED_UNICODE) . "\n"; Coroutine::writeFile(CRASH_DIR . "sql.log", $log, FILE_APPEND); } return $ps->fetchAll(); } } catch (DBException $e) { if (ZMBuf::get("sql_log") === true) { $log = "[" . date("Y-m-d H:i:s") . " " . round(microtime(true) - $starttime, 4) . "] " . $line . " " . json_encode($params, JSON_UNESCAPED_UNICODE) . " (Error:" . $e->getMessage() . ")\n"; Coroutine::writeFile(CRASH_DIR . "sql.log", $log, FILE_APPEND); } Console::warning($e->getMessage()); throw $e; } } public static function isTableExists($table) { return in_array($table, self::$table_list); } }