diff --git a/src/db/Query.php b/src/db/Query.php index e0f56751ffd4c8966c6a8af0bd4c37be2f346429..aa46b8231f30abeaf8820ea30a8c3e363641993f 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,25 +49,11 @@ class Query * * @return string */ - protected function class_info (){ - return json_encode(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; + protected function class_info() + { + return debug_backtrace(); } - /** * @初始化 * @@ -76,8 +62,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; } @@ -95,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() // { @@ -197,8 +193,23 @@ 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 { + if ($op != null && $condition == null){ + $condition = $op; + $op = '='; + } + $whereArray = [ + $field => [$op != null ?: '=', $condition] + ]; + } + $this->options['where'][$this->options['whereNum']] = $whereArray; $this->options['whereNum']++; return $this; @@ -353,17 +364,17 @@ class Query /** * @执行sql - * * @param $result * @return mixed + * @throws \Exception */ public function query($result) { $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 +382,7 @@ class Query if (is_string($result)) { $rs = $mysql->query($result); - if($rs === false){ + if ($rs === false) { $dumpError = true; } @@ -381,22 +392,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,22 +422,19 @@ 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; + if ($dumpError) { + throw new \ErrorException($mysql->error,$mysql->errno); } - }catch (\Exception $e){ - $this->dumpError($class_info); - var_dump($e); + } catch (\Exception $e) { if ($this->options['setDefer']) { $chan->push(null); } + throw new \Exception($e->getMessage(),$e->getCode(),$e->getPrevious()); } }); - if($this->options['setDefer']){ + if ($this->options['setDefer']) { return $chan->pop(); } }