From a853000904ad910b29610ccc725cc69ac62ec6eb Mon Sep 17 00:00:00 2001 From: hepeichun <946599101@qq.com> Date: Mon, 18 Jan 2021 17:36:23 +0800 Subject: [PATCH 1/4] =?UTF-8?q?add:=E5=A2=9E=E5=8A=A0where=E7=9A=84?= =?UTF-8?q?=E6=9D=A1=E4=BB=B6=E5=88=A4=E6=96=AD=E8=83=BD=E5=8A=9B,?= =?UTF-8?q?=E9=A3=8E=E6=A0=BC=E7=B1=BB=E4=BC=BCTP?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/db/Query.php | 89 +++++++++++++++++++++++++++--------------------- 1 file changed, 51 insertions(+), 38 deletions(-) diff --git a/src/db/Query.php b/src/db/Query.php index e0f5675..a0e56c9 100644 --- a/src/db/Query.php +++ b/src/db/Query.php @@ -17,23 +17,23 @@ class Query //db参数 protected $options = [ - 'table' => '', - 'alias' => [], - 'where' => [], - 'whereNum' => 0, - 'field' => '*', - 'order' => [], - 'distinct' => false, - 'join' => '', - 'union' => '', - 'group' => '', - 'having' => '', - 'limit' => '', - 'lock' => false, + 'table' => '', + 'alias' => [], + 'where' => [], + 'whereNum' => 0, + 'field' => '*', + 'order' => [], + 'distinct' => false, + 'join' => '', + 'union' => '', + 'group' => '', + 'having' => '', + 'limit' => '', + 'lock' => false, 'fetch_sql' => false, - 'data' => [], - 'prefix' => '', - 'setDefer' => true + 'data' => [], + 'prefix' => '', + 'setDefer' => true ]; @@ -49,7 +49,8 @@ class Query * * @return string */ - protected function class_info (){ + protected function class_info() + { return json_encode(debug_backtrace()); } @@ -59,12 +60,13 @@ class Query * * @param $class_info */ - protected function dumpError($class_info){ - echo PHP_EOL.PHP_EOL.PHP_EOL; - echo "=================================================================".PHP_EOL; - echo "时间:".date('Y-m-d H:m:i',time()).PHP_EOL.PHP_EOL; - echo "报错信息:(格式为json,请格式化后分析)".PHP_EOL; - echo $class_info.PHP_EOL; + protected function dumpError($class_info) + { + echo PHP_EOL . PHP_EOL . PHP_EOL; + echo "=================================================================" . PHP_EOL; + echo "时间:" . date('Y-m-d H:m:i', time()) . PHP_EOL . PHP_EOL; + echo "报错信息:(格式为json,请格式化后分析)" . PHP_EOL; + echo $class_info . PHP_EOL; } @@ -76,8 +78,8 @@ class Query */ public function init(MysqlPool $MysqlPool) { - $this->MysqlPool = $MysqlPool; - $this->options['prefix'] = $MysqlPool->config['prefix']; + $this->MysqlPool = $MysqlPool; + $this->options['prefix'] = $MysqlPool->config['prefix']; $this->options['setDefer'] = $MysqlPool->config['setDefer']; return $this; } @@ -197,8 +199,19 @@ class Query * @param array $whereArray * @return $this */ - public function where($whereArray = []) + public function where($field, $op = null, $condition = null) { + if (is_array($field)) { + $whereArray = $field; + } elseif (is_object($field)){ + $field($this); + return $this; + } else { + $whereArray = [ + $field => [$op != null ?: '=', $condition] + ]; + } + $this->options['where'][$this->options['whereNum']] = $whereArray; $this->options['whereNum']++; return $this; @@ -362,8 +375,8 @@ class Query $chan = new \chan(1); $class_info = $this->class_info(); - go(function () use ($chan, $result,$class_info) { - try{ + go(function () use ($chan, $result, $class_info) { + try { $dumpError = false; $mysql = $this->MysqlPool->get(); @@ -371,7 +384,7 @@ class Query if (is_string($result)) { $rs = $mysql->query($result); - if($rs === false){ + if ($rs === false) { $dumpError = true; } @@ -381,22 +394,22 @@ class Query } else { $stmt = $mysql->prepare($result['sql']); - if($stmt === false){ + if ($stmt === false) { $dumpError = true; } if ($stmt) { $rs = $stmt->execute($result['sethinkBind']); - if($rs === false){ + if ($rs === false) { $dumpError = true; } if ($this->options['setDefer']) { if ($this->options['limit'] == 1) { - if(count($rs) > 0){ + if (count($rs) > 0) { $chan->push($rs[0]); - }else{ + } else { $chan->push(null); } } else { @@ -411,12 +424,12 @@ class Query } $this->put($mysql); - if($dumpError){ + if ($dumpError) { $this->dumpError($class_info); - echo PHP_EOL."mysql_error : {$mysql->error}".PHP_EOL; - echo "mysql_errno : {$mysql->errno}".PHP_EOL; + echo PHP_EOL . "mysql_error : {$mysql->error}" . PHP_EOL; + echo "mysql_errno : {$mysql->errno}" . PHP_EOL; } - }catch (\Exception $e){ + } catch (\Exception $e) { $this->dumpError($class_info); var_dump($e); if ($this->options['setDefer']) { @@ -426,7 +439,7 @@ class Query }); - if($this->options['setDefer']){ + if ($this->options['setDefer']) { return $chan->pop(); } } -- Gitee From b801906f68a0adf401373de6caf18973c043562e Mon Sep 17 00:00:00 2001 From: hepeichun <946599101@qq.com> Date: Mon, 18 Jan 2021 17:44:23 +0800 Subject: [PATCH 2/4] =?UTF-8?q?fix:=E4=BF=AE=E5=A4=8D=E5=8F=AA=E6=9C=89?= =?UTF-8?q?=E4=B8=A4=E4=B8=AA=E5=8F=82=E6=95=B0=EF=BC=8C=E4=BC=9A=E8=A2=AB?= =?UTF-8?q?=E5=BC=BA=E5=88=B6=E6=94=B9=E5=8F=98=E6=88=90'=3Dnull'=EF=BC=8C?= =?UTF-8?q?=E5=BA=94=E5=8F=82=E7=85=A7TP=E9=A3=8E=E6=A0=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/db/Query.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/db/Query.php b/src/db/Query.php index a0e56c9..fb86d62 100644 --- a/src/db/Query.php +++ b/src/db/Query.php @@ -207,6 +207,10 @@ class Query $field($this); return $this; } else { + if ($op != null && $condition == null){ + $condition = $op; + $op = '='; + } $whereArray = [ $field => [$op != null ?: '=', $condition] ]; -- Gitee From c866a2ebd38a67969483887eec7848a314824034 Mon Sep 17 00:00:00 2001 From: hepeichun <946599101@qq.com> Date: Tue, 19 Jan 2021 11:45:53 +0800 Subject: [PATCH 3/4] =?UTF-8?q?fix:=E6=B7=BB=E5=8A=A0table=E8=A1=A8?= =?UTF-8?q?=E6=96=B9=E6=B3=95=EF=BC=8C=E4=BC=98=E5=8C=96=E5=BC=82=E5=B8=B8?= =?UTF-8?q?=E5=A4=84=E7=90=86=EF=BC=8C=E5=B0=86=E5=BC=82=E5=B8=B8=E4=B8=8A?= =?UTF-8?q?=E6=8A=9B=EF=BC=8C=E7=AC=A6=E5=90=88=E7=8E=B0=E4=BB=A3=E5=8C=96?= =?UTF-8?q?PHP=E8=A7=84=E8=8C=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/db/Query.php | 35 +++++++++++++---------------------- 1 file changed, 13 insertions(+), 22 deletions(-) diff --git a/src/db/Query.php b/src/db/Query.php index fb86d62..1be9cc6 100644 --- a/src/db/Query.php +++ b/src/db/Query.php @@ -51,25 +51,9 @@ class Query */ protected function class_info() { - return json_encode(debug_backtrace()); + return debug_backtrace(); } - - /** - * @错误信息格式 - * - * @param $class_info - */ - protected function dumpError($class_info) - { - echo PHP_EOL . PHP_EOL . PHP_EOL; - echo "=================================================================" . PHP_EOL; - echo "时间:" . date('Y-m-d H:m:i', time()) . PHP_EOL . PHP_EOL; - echo "报错信息:(格式为json,请格式化后分析)" . PHP_EOL; - echo $class_info . PHP_EOL; - } - - /** * @初始化 * @@ -97,6 +81,16 @@ class Query return $this; } + /** + * @param string $tableName + * @return $this + */ + public function table($tableName = '') + { + $this->options['table'] = $tableName; + return $this; + } + //暂未实现 // public function alias() // { @@ -429,16 +423,13 @@ class Query $this->put($mysql); if ($dumpError) { - $this->dumpError($class_info); - echo PHP_EOL . "mysql_error : {$mysql->error}" . PHP_EOL; - echo "mysql_errno : {$mysql->errno}" . PHP_EOL; + throw new \ErrorException($mysql->error,$mysql->errno); } } catch (\Exception $e) { - $this->dumpError($class_info); - var_dump($e); if ($this->options['setDefer']) { $chan->push(null); } + throw new \Exception($e->getMessage(),$e->getCode(),$e->getPrevious()); } }); -- Gitee From 2fb610860762df62df530442d045dc593abe3ec0 Mon Sep 17 00:00:00 2001 From: hepeichun <946599101@qq.com> Date: Tue, 19 Jan 2021 11:48:43 +0800 Subject: [PATCH 4/4] =?UTF-8?q?style:=E5=AE=8C=E5=96=84=E6=B3=A8=E9=87=8A?= =?UTF-8?q?=E9=81=97=E4=BA=A7=E5=A4=84=E7=90=86=E7=B1=BB=E7=9A=84=E5=A3=B0?= =?UTF-8?q?=E6=98=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/db/Query.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/db/Query.php b/src/db/Query.php index 1be9cc6..aa46b82 100644 --- a/src/db/Query.php +++ b/src/db/Query.php @@ -364,9 +364,9 @@ class Query /** * @执行sql - * * @param $result * @return mixed + * @throws \Exception */ public function query($result) { -- Gitee