From 5b5e7373e7347b680f5aa446eb8583737f5664db Mon Sep 17 00:00:00 2001 From: thinkphp Date: Thu, 12 Jan 2017 10:45:21 +0800 Subject: [PATCH 01/10] =?UTF-8?q?=E6=94=B9=E8=BF=9BQuery=E7=B1=BB=E6=9D=A1?= =?UTF-8?q?=E4=BB=B6=E6=9F=A5=E8=AF=A2=E4=B8=80=E5=A4=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/db/Query.php | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/library/think/db/Query.php b/library/think/db/Query.php index 88cbf6e2..e15a48fd 100644 --- a/library/think/db/Query.php +++ b/library/think/db/Query.php @@ -1175,18 +1175,18 @@ class Query $this->bind($param[2]); } // 记录一个字段多次查询条件 - $this->options['multi'][$field][] = $where[$field]; + $this->options['multi'][$logic][$field][] = $where[$field]; } if (!empty($where)) { if (!isset($this->options['where'][$logic])) { $this->options['where'][$logic] = []; } - if (is_string($field) && $this->checkMultiField($field)) { - $where[$field] = $this->options['multi'][$field]; + if (is_string($field) && $this->checkMultiField($field, $logic)) { + $where[$field] = $this->options['multi'][$logic][$field]; } elseif (is_array($field)) { foreach ($field as $key => $val) { - if ($this->checkMultiField($key)) { - $where[$key] = $this->options['multi'][$key]; + if ($this->checkMultiField($key, $logic)) { + $where[$key] = $this->options['multi'][$logic][$key]; } } } @@ -1194,10 +1194,16 @@ class Query } } - // 检查是否存在一个字段多次查询条件 - private function checkMultiField($field) + /** + * 检查是否存在一个字段多次查询条件 + * @access public + * @param string $field 查询字段 + * @param string $logic 查询逻辑 and or xor + * @return bool + */ + private function checkMultiField($field, $logic) { - return isset($this->options['multi'][$field]) && count($this->options['multi'][$field]) > 1; + return isset($this->options['multi'][$logic][$field]) && count($this->options['multi'][$logic][$field]) > 1; } /** From 1ab44ff8e7b1df128ef4ee09fb590ea327f5d0ee Mon Sep 17 00:00:00 2001 From: thinkphp Date: Thu, 12 Jan 2017 11:45:55 +0800 Subject: [PATCH 02/10] =?UTF-8?q?=E6=94=B9=E8=BF=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/db/Query.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/think/db/Query.php b/library/think/db/Query.php index e15a48fd..5ed6fb17 100644 --- a/library/think/db/Query.php +++ b/library/think/db/Query.php @@ -1154,7 +1154,7 @@ class Query // 数组批量查询 $where = $field; foreach ($where as $k => $val) { - $this->options['multi'][$k][] = $val; + $this->options['multi'][$logic][$k][] = $val; } } elseif ($field && is_string($field)) { // 字符串查询 From b47fe5553b71c54fdbfce067e293275ea7cacbf1 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Thu, 12 Jan 2017 12:17:53 +0800 Subject: [PATCH 03/10] =?UTF-8?q?=E4=BF=AE=E6=AD=A3null=E6=9F=A5=E8=AF=A2?= =?UTF-8?q?=E7=9A=84=E6=9D=A1=E4=BB=B6=E5=90=88=E5=B9=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/db/Query.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/library/think/db/Query.php b/library/think/db/Query.php index 5ed6fb17..9c796081 100644 --- a/library/think/db/Query.php +++ b/library/think/db/Query.php @@ -1158,13 +1158,15 @@ class Query } } elseif ($field && is_string($field)) { // 字符串查询 - $where[$field] = ['null', '']; + $where[$field] = ['null', '']; + $this->options['multi'][$logic][$field][] = $where[$field]; } } elseif (is_array($op)) { $where[$field] = $param; } elseif (in_array(strtolower($op), ['null', 'notnull', 'not null'])) { // null查询 - $where[$field] = [$op, '']; + $where[$field] = [$op, '']; + $this->options['multi'][$logic][$field][] = $where[$field]; } elseif (is_null($condition)) { // 字段相等查询 $where[$field] = ['eq', $op]; From b54442a42fbfa78590173f7d638f61357316ec87 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Thu, 12 Jan 2017 12:43:58 +0800 Subject: [PATCH 04/10] =?UTF-8?q?=E6=94=B9=E8=BF=9B=E5=8D=95=E4=B8=AA?= =?UTF-8?q?=E5=AD=97=E6=AE=B5=E5=A4=9A=E6=AC=A1Or=E6=9F=A5=E8=AF=A2?= =?UTF-8?q?=E6=83=85=E5=86=B5=E7=9A=84=E6=9F=A5=E8=AF=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/db/Query.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/library/think/db/Query.php b/library/think/db/Query.php index 9c796081..1ef97571 100644 --- a/library/think/db/Query.php +++ b/library/think/db/Query.php @@ -1169,7 +1169,8 @@ class Query $this->options['multi'][$logic][$field][] = $where[$field]; } elseif (is_null($condition)) { // 字段相等查询 - $where[$field] = ['eq', $op]; + $where[$field] = ['eq', $op]; + $this->options['multi'][$logic][$field][] = $where[$field]; } else { $where[$field] = [$op, $condition, isset($param[2]) ? $param[2] : null]; if ('exp' == strtolower($op) && isset($param[2]) && is_array($param[2])) { From 28747aa5dcc59d7957fb61476fbb023be0e96352 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Thu, 12 Jan 2017 13:29:46 +0800 Subject: [PATCH 05/10] =?UTF-8?q?=E6=94=B9=E8=BF=9BValidate=E7=B1=BB?= =?UTF-8?q?=E7=9A=84=E9=94=99=E8=AF=AF=E4=BF=A1=E6=81=AF=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Validate.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/library/think/Validate.php b/library/think/Validate.php index e3bc9db6..e91ee338 100644 --- a/library/think/Validate.php +++ b/library/think/Validate.php @@ -1222,9 +1222,9 @@ class Validate $msg = Lang::get(substr($msg, 2, -1)); } - if (is_string($msg) && is_string($rule) && false !== strpos($msg, ':')) { + if (is_string($msg) && is_scalar($rule) && false !== strpos($msg, ':')) { // 变量替换 - if (strpos($rule, ',')) { + if (is_string($rule) && strpos($rule, ',')) { $array = array_pad(explode(',', $rule), 3, ''); } else { $array = array_pad([], 3, ''); From f2a82bcf82dfaaec76eff25db1525c41a48650dd Mon Sep 17 00:00:00 2001 From: thinkphp Date: Thu, 12 Jan 2017 13:53:09 +0800 Subject: [PATCH 06/10] =?UTF-8?q?=E6=94=B9=E8=BF=9Btoday=E7=9A=84=E6=97=A5?= =?UTF-8?q?=E6=9C=9F=E6=9F=A5=E8=AF=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/db/Query.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/think/db/Query.php b/library/think/db/Query.php index 1ef97571..3ba42b2f 100644 --- a/library/think/db/Query.php +++ b/library/think/db/Query.php @@ -1644,7 +1644,7 @@ class Query switch (strtolower($op)) { case 'today': case 'd': - $range = 'today'; + $range = ['today', 'tomorrow']; break; case 'week': case 'w': From bd007e81e9ca9a97694a0135b6617a13982c7477 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Thu, 12 Jan 2017 14:47:20 +0800 Subject: [PATCH 07/10] =?UTF-8?q?Query=E7=B1=BB=E5=A2=9E=E5=8A=A0getPdo?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/db/Query.php | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/library/think/db/Query.php b/library/think/db/Query.php index 3ba42b2f..e2c38d2f 100644 --- a/library/think/db/Query.php +++ b/library/think/db/Query.php @@ -409,7 +409,7 @@ class Query if (isset($this->options['field'])) { unset($this->options['field']); } - $pdo = $this->field($field)->fetchPdo(true)->find(); + $pdo = $this->field($field)->limit(1)->getPdo(); if (is_string($pdo)) { // 返回SQL语句 return $pdo; @@ -459,7 +459,7 @@ class Query if ($key && '*' != $field) { $field = $key . ',' . $field; } - $pdo = $this->field($field)->fetchPdo(true)->select(); + $pdo = $this->field($field)->getPdo(); if (is_string($pdo)) { // 返回SQL语句 return $pdo; @@ -2188,6 +2188,27 @@ class Query } } + /** + * 执行查询但只返回PDOStatement对象 + * @access public + * @return \PDOStatement|string + */ + public function getPdo() + { + // 分析查询表达式 + $options = $this->parseExpress(); + // 生成查询SQL + $sql = $this->builder->select($options); + // 获取参数绑定 + $bind = $this->getBind(); + if ($options['fetch_sql']) { + // 获取实际执行的SQL语句 + return $this->connection->getRealSql($sql, $bind); + } + // 执行查询操作 + return $this->query($sql, $bind, $options['master'], true); + } + /** * 查找记录 * @access public From 508ff846c33338e14e540db4093f96b9499ceba8 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Thu, 12 Jan 2017 16:03:19 +0800 Subject: [PATCH 08/10] =?UTF-8?q?=E5=8F=96=E6=B6=88min=20max=20sum=20avg?= =?UTF-8?q?=E6=96=B9=E6=B3=95=E7=9A=84=E5=8F=82=E6=95=B0=E9=BB=98=E8=AE=A4?= =?UTF-8?q?=E5=80=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/db/Query.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/library/think/db/Query.php b/library/think/db/Query.php index e2c38d2f..47d53670 100644 --- a/library/think/db/Query.php +++ b/library/think/db/Query.php @@ -522,7 +522,7 @@ class Query * @param string $field 字段名 * @return float|int */ - public function sum($field = '*') + public function sum($field) { return $this->value('SUM(' . $field . ') AS tp_sum', 0, true); } @@ -533,7 +533,7 @@ class Query * @param string $field 字段名 * @return mixed */ - public function min($field = '*') + public function min($field) { return $this->value('MIN(' . $field . ') AS tp_min', 0, true); } @@ -544,7 +544,7 @@ class Query * @param string $field 字段名 * @return mixed */ - public function max($field = '*') + public function max($field) { return $this->value('MAX(' . $field . ') AS tp_max', 0, true); } @@ -555,7 +555,7 @@ class Query * @param string $field 字段名 * @return float|int */ - public function avg($field = '*') + public function avg($field) { return $this->value('AVG(' . $field . ') AS tp_avg', 0, true); } From f37a5e8f55687ac9782f37f3b398f4f7e0cf0f86 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Thu, 12 Jan 2017 17:07:40 +0800 Subject: [PATCH 09/10] =?UTF-8?q?=E6=94=B9=E8=BF=9BQuery=E7=B1=BB=E7=9A=84?= =?UTF-8?q?whereTime=E6=96=B9=E6=B3=95=E6=94=AF=E6=8C=81=E6=9B=B4=E5=A4=9A?= =?UTF-8?q?=E7=9A=84=E6=97=B6=E9=97=B4=E6=97=A5=E6=9C=9F=E8=A1=A8=E8=BE=BE?= =?UTF-8?q?=E5=BC=8F=EF=BC=88=E9=BB=98=E8=AE=A4=E6=9F=A5=E8=AF=A2=E6=9D=A1?= =?UTF-8?q?=E4=BB=B6=E4=B8=BA=E5=A4=A7=E4=BA=8E=E6=8C=87=E5=AE=9A=E6=97=B6?= =?UTF-8?q?=E9=97=B4=E8=A1=A8=E8=BE=BE=E5=BC=8F=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/db/Query.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/library/think/db/Query.php b/library/think/db/Query.php index 47d53670..24f5ce2a 100644 --- a/library/think/db/Query.php +++ b/library/think/db/Query.php @@ -1670,6 +1670,8 @@ class Query case 'last year': $range = [mktime(0, 0, 0, 1, 1, $date['year'] - 1), mktime(0, 0, 0, 1, 1, $date['year'])]; break; + default: + $range = $op; } $op = is_array($range) ? 'between' : '>'; } From 1e8e169b0a4229cb5a49aa6f162d3430dd1d1e9e Mon Sep 17 00:00:00 2001 From: thinkphp Date: Fri, 13 Jan 2017 17:53:11 +0800 Subject: [PATCH 10/10] =?UTF-8?q?=E6=94=B9=E8=BF=9Bwhere=E6=96=B9=E6=B3=95?= =?UTF-8?q?=E7=9A=84=E7=9B=B8=E5=90=8C=E5=AD=97=E6=AE=B5=E5=A4=9A=E6=AC=A1?= =?UTF-8?q?=E6=9F=A5=E8=AF=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/db/Query.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/library/think/db/Query.php b/library/think/db/Query.php index 24f5ce2a..45c66ccf 100644 --- a/library/think/db/Query.php +++ b/library/think/db/Query.php @@ -1169,8 +1169,10 @@ class Query $this->options['multi'][$logic][$field][] = $where[$field]; } elseif (is_null($condition)) { // 字段相等查询 - $where[$field] = ['eq', $op]; - $this->options['multi'][$logic][$field][] = $where[$field]; + $where[$field] = ['eq', $op]; + if ('AND' != $logic) { + $this->options['multi'][$logic][$field][] = $where[$field]; + } } else { $where[$field] = [$op, $condition, isset($param[2]) ? $param[2] : null]; if ('exp' == strtolower($op) && isset($param[2]) && is_array($param[2])) {