mirror of
https://github.com/zhamao-robot/zhamao-framework.git
synced 2026-07-21 15:45:36 +08:00
initial 2.0.0-a4 commit
This commit is contained in:
@@ -7,7 +7,7 @@ namespace ZM\DB;
|
||||
use Exception;
|
||||
use ZM\Config\ZMConfig;
|
||||
use ZM\Console\Console;
|
||||
use ZM\Store\ZMBuf;
|
||||
use ZM\Store\MySQL\SqlPoolStorage;
|
||||
use PDOException;
|
||||
use PDOStatement;
|
||||
use Swoole\Database\PDOStatementProxy;
|
||||
@@ -35,11 +35,11 @@ class DB
|
||||
* @return Table
|
||||
* @throws DbException
|
||||
*/
|
||||
public static function table($table_name, $enable_cache = null) {
|
||||
public static function table($table_name) {
|
||||
if (Table::getTableInstance($table_name) === null) {
|
||||
if (in_array($table_name, self::$table_list))
|
||||
return new Table($table_name, $enable_cache ?? ZMConfig::get("global", "sql_config")["sql_enable_cache"]);
|
||||
elseif(ZMBuf::$sql_pool !== null){
|
||||
return new Table($table_name);
|
||||
elseif(SqlPoolStorage::$sql_pool !== null){
|
||||
throw new DbException("Table " . $table_name . " not exist in database.");
|
||||
} else {
|
||||
throw new DbException("Database connection not exist or connect failed. Please check sql configuration");
|
||||
@@ -63,13 +63,13 @@ class DB
|
||||
*/
|
||||
public static function unprepared($line) {
|
||||
try {
|
||||
$conn = ZMBuf::$sql_pool->get();
|
||||
$conn = SqlPoolStorage::$sql_pool->get();
|
||||
if ($conn === false) {
|
||||
ZMBuf::$sql_pool->put(null);
|
||||
SqlPoolStorage::$sql_pool->put(null);
|
||||
throw new DbException("无法连接SQL!" . $line);
|
||||
}
|
||||
$result = $conn->query($line) === false ? false : true;
|
||||
ZMBuf::$sql_pool->put($conn);
|
||||
SqlPoolStorage::$sql_pool->put($conn);
|
||||
return $result;
|
||||
} catch (DBException $e) {
|
||||
Console::warning($e->getMessage());
|
||||
@@ -87,19 +87,19 @@ class DB
|
||||
public static function rawQuery(string $line, $params = [], $fetch_mode = ZM_DEFAULT_FETCH_MODE) {
|
||||
Console::debug("MySQL: ".$line." | ". implode(", ", $params));
|
||||
try {
|
||||
$conn = ZMBuf::$sql_pool->get();
|
||||
$conn = SqlPoolStorage::$sql_pool->get();
|
||||
if ($conn === false) {
|
||||
ZMBuf::$sql_pool->put(null);
|
||||
SqlPoolStorage::$sql_pool->put(null);
|
||||
throw new DbException("无法连接SQL!" . $line);
|
||||
}
|
||||
$ps = $conn->prepare($line);
|
||||
if ($ps === false) {
|
||||
ZMBuf::$sql_pool->put(null);
|
||||
SqlPoolStorage::$sql_pool->put(null);
|
||||
throw new DbException("SQL语句查询错误," . $line . ",错误信息:" . $conn->error);
|
||||
} else {
|
||||
if (!($ps instanceof PDOStatement) && !($ps instanceof PDOStatementProxy)) {
|
||||
var_dump($ps);
|
||||
ZMBuf::$sql_pool->put(null);
|
||||
SqlPoolStorage::$sql_pool->put(null);
|
||||
throw new DbException("语句查询错误!返回的不是 PDOStatement" . $line);
|
||||
}
|
||||
if ($params == []) $result = $ps->execute();
|
||||
@@ -107,11 +107,11 @@ class DB
|
||||
$result = $ps->execute([$params]);
|
||||
} else $result = $ps->execute($params);
|
||||
if ($result !== true) {
|
||||
ZMBuf::$sql_pool->put(null);
|
||||
SqlPoolStorage::$sql_pool->put(null);
|
||||
throw new DBException("语句[$line]错误!" . $ps->errorInfo()[2]);
|
||||
//echo json_encode(debug_backtrace(), 128 | 256);
|
||||
}
|
||||
ZMBuf::$sql_pool->put($conn);
|
||||
SqlPoolStorage::$sql_pool->put($conn);
|
||||
return $ps->fetchAll($fetch_mode);
|
||||
}
|
||||
} catch (DbException $e) {
|
||||
|
||||
@@ -4,7 +4,6 @@
|
||||
namespace ZM\DB;
|
||||
|
||||
|
||||
use ZM\Console\Console;
|
||||
use ZM\Exception\DbException;
|
||||
|
||||
class SelectBody
|
||||
@@ -46,17 +45,7 @@ class SelectBody
|
||||
* @throws DbException
|
||||
*/
|
||||
public function fetchAll($fetch_mode = ZM_DEFAULT_FETCH_MODE) {
|
||||
if ($this->table->isCacheEnabled()) {
|
||||
$rr = md5(implode(",", $this->select_thing) . serialize($this->where_thing));
|
||||
if (array_key_exists($rr, $this->table->cache)) {
|
||||
Console::debug('SQL query cached: ' . $rr);
|
||||
return $this->table->cache[$rr]->getResult();
|
||||
}
|
||||
}
|
||||
$this->execute($fetch_mode);
|
||||
if ($this->table->isCacheEnabled() && !in_array($rr, $this->table->cache)) {
|
||||
$this->table->cache[$rr] = $this;
|
||||
}
|
||||
return $this->getResult();
|
||||
}
|
||||
|
||||
|
||||
@@ -14,10 +14,7 @@ class Table
|
||||
|
||||
private static $table_instance = [];
|
||||
|
||||
private $enable_cache;
|
||||
|
||||
public function __construct($table_name, $enable_cache) {
|
||||
$this->enable_cache = $enable_cache;
|
||||
public function __construct($table_name) {
|
||||
$this->table_name = $table_name;
|
||||
self::$table_instance[$table_name] = $this;
|
||||
}
|
||||
@@ -75,5 +72,4 @@ class Table
|
||||
*/
|
||||
public function getTableName() { return $this->table_name; }
|
||||
|
||||
public function isCacheEnabled() { return $this->enable_cache; }
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user