| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540 |
- <?php
- // +----------------------------------------------------------------------
- // | ThinkPHP [ WE CAN DO IT JUST THINK ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2006~2019 http://thinkphp.cn All rights reserved.
- // +----------------------------------------------------------------------
- // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
- // +----------------------------------------------------------------------
- // | Author: liu21st <liu21st@gmail.com>
- // +----------------------------------------------------------------------
- declare (strict_types = 1);
- namespace think\db\concern;
- use Closure;
- use think\db\BaseQuery;
- use think\db\Raw;
- trait WhereQuery
- {
- /**
- * 指定AND查询条件
- * @access public
- * @param mixed $field 查询字段
- * @param mixed $op 查询表达式
- * @param mixed $condition 查询条件
- * @return $this
- */
- public function where($field, $op = null, $condition = null)
- {
- if ($field instanceof $this) {
- $this->parseQueryWhere($field);
- return $this;
- } elseif (true === $field || 1 === $field) {
- $this->options['where']['AND'][] = true;
- return $this;
- }
- $param = func_get_args();
- array_shift($param);
- return $this->parseWhereExp('AND', $field, $op, $condition, $param);
- }
- /**
- * 解析Query对象查询条件
- * @access public
- * @param BaseQuery $query 查询对象
- * @return void
- */
- protected function parseQueryWhere(BaseQuery $query): void
- {
- $this->options['where'] = $query->getOptions('where');
- if ($query->getOptions('via')) {
- $via = $query->getOptions('via');
- foreach ($this->options['where'] as $logic => &$where) {
- foreach ($where as $key => &$val) {
- if (is_array($val) && !strpos($val[0], '.')) {
- $val[0] = $via . '.' . $val[0];
- }
- }
- }
- }
- $this->bind($query->getBind(false));
- }
- /**
- * 指定OR查询条件
- * @access public
- * @param mixed $field 查询字段
- * @param mixed $op 查询表达式
- * @param mixed $condition 查询条件
- * @return $this
- */
- public function whereOr($field, $op = null, $condition = null)
- {
- $param = func_get_args();
- array_shift($param);
- return $this->parseWhereExp('OR', $field, $op, $condition, $param);
- }
- /**
- * 指定XOR查询条件
- * @access public
- * @param mixed $field 查询字段
- * @param mixed $op 查询表达式
- * @param mixed $condition 查询条件
- * @return $this
- */
- public function whereXor($field, $op = null, $condition = null)
- {
- $param = func_get_args();
- array_shift($param);
- return $this->parseWhereExp('XOR', $field, $op, $condition, $param);
- }
- /**
- * 指定Null查询条件
- * @access public
- * @param mixed $field 查询字段
- * @param string $logic 查询逻辑 and or xor
- * @return $this
- */
- public function whereNull(string $field, string $logic = 'AND')
- {
- return $this->parseWhereExp($logic, $field, 'NULL', null, [], true);
- }
- /**
- * 指定NotNull查询条件
- * @access public
- * @param mixed $field 查询字段
- * @param string $logic 查询逻辑 and or xor
- * @return $this
- */
- public function whereNotNull(string $field, string $logic = 'AND')
- {
- return $this->parseWhereExp($logic, $field, 'NOTNULL', null, [], true);
- }
- /**
- * 指定Exists查询条件
- * @access public
- * @param mixed $condition 查询条件
- * @param string $logic 查询逻辑 and or xor
- * @return $this
- */
- public function whereExists($condition, string $logic = 'AND')
- {
- if (is_string($condition)) {
- $condition = new Raw($condition);
- }
- $this->options['where'][strtoupper($logic)][] = ['', 'EXISTS', $condition];
- return $this;
- }
- /**
- * 指定NotExists查询条件
- * @access public
- * @param mixed $condition 查询条件
- * @param string $logic 查询逻辑 and or xor
- * @return $this
- */
- public function whereNotExists($condition, string $logic = 'AND')
- {
- if (is_string($condition)) {
- $condition = new Raw($condition);
- }
- $this->options['where'][strtoupper($logic)][] = ['', 'NOT EXISTS', $condition];
- return $this;
- }
- /**
- * 指定In查询条件
- * @access public
- * @param mixed $field 查询字段
- * @param mixed $condition 查询条件
- * @param string $logic 查询逻辑 and or xor
- * @return $this
- */
- public function whereIn(string $field, $condition, string $logic = 'AND')
- {
- return $this->parseWhereExp($logic, $field, 'IN', $condition, [], true);
- }
- /**
- * 指定NotIn查询条件
- * @access public
- * @param mixed $field 查询字段
- * @param mixed $condition 查询条件
- * @param string $logic 查询逻辑 and or xor
- * @return $this
- */
- public function whereNotIn(string $field, $condition, string $logic = 'AND')
- {
- return $this->parseWhereExp($logic, $field, 'NOT IN', $condition, [], true);
- }
- /**
- * 指定Like查询条件
- * @access public
- * @param mixed $field 查询字段
- * @param mixed $condition 查询条件
- * @param string $logic 查询逻辑 and or xor
- * @return $this
- */
- public function whereLike(string $field, $condition, string $logic = 'AND')
- {
- return $this->parseWhereExp($logic, $field, 'LIKE', $condition, [], true);
- }
- /**
- * 指定NotLike查询条件
- * @access public
- * @param mixed $field 查询字段
- * @param mixed $condition 查询条件
- * @param string $logic 查询逻辑 and or xor
- * @return $this
- */
- public function whereNotLike(string $field, $condition, string $logic = 'AND')
- {
- return $this->parseWhereExp($logic, $field, 'NOT LIKE', $condition, [], true);
- }
- /**
- * 指定Between查询条件
- * @access public
- * @param mixed $field 查询字段
- * @param mixed $condition 查询条件
- * @param string $logic 查询逻辑 and or xor
- * @return $this
- */
- public function whereBetween(string $field, $condition, string $logic = 'AND')
- {
- return $this->parseWhereExp($logic, $field, 'BETWEEN', $condition, [], true);
- }
- /**
- * 指定NotBetween查询条件
- * @access public
- * @param mixed $field 查询字段
- * @param mixed $condition 查询条件
- * @param string $logic 查询逻辑 and or xor
- * @return $this
- */
- public function whereNotBetween(string $field, $condition, string $logic = 'AND')
- {
- return $this->parseWhereExp($logic, $field, 'NOT BETWEEN', $condition, [], true);
- }
- /**
- * 指定FIND_IN_SET查询条件
- * @access public
- * @param mixed $field 查询字段
- * @param mixed $condition 查询条件
- * @param string $logic 查询逻辑 and or xor
- * @return $this
- */
- public function whereFindInSet(string $field, $condition, string $logic = 'AND')
- {
- return $this->parseWhereExp($logic, $field, 'FIND IN SET', $condition, [], true);
- }
- /**
- * 比较两个字段
- * @access public
- * @param string $field1 查询字段
- * @param string $operator 比较操作符
- * @param string $field2 比较字段
- * @param string $logic 查询逻辑 and or xor
- * @return $this
- */
- public function whereColumn(string $field1, string $operator, string $field2 = null, string $logic = 'AND')
- {
- if (is_null($field2)) {
- $field2 = $operator;
- $operator = '=';
- }
- return $this->parseWhereExp($logic, $field1, 'COLUMN', [$operator, $field2], [], true);
- }
- /**
- * 设置软删除字段及条件
- * @access public
- * @param string $field 查询字段
- * @param mixed $condition 查询条件
- * @return $this
- */
- public function useSoftDelete(string $field, $condition = null)
- {
- if ($field) {
- $this->options['soft_delete'] = [$field, $condition];
- }
- return $this;
- }
- /**
- * 指定Exp查询条件
- * @access public
- * @param mixed $field 查询字段
- * @param string $where 查询条件
- * @param array $bind 参数绑定
- * @param string $logic 查询逻辑 and or xor
- * @return $this
- */
- public function whereExp(string $field, string $where, array $bind = [], string $logic = 'AND')
- {
- if (!empty($bind)) {
- $this->bindParams($where, $bind);
- }
- $this->options['where'][$logic][] = [$field, 'EXP', new Raw($where)];
- return $this;
- }
- /**
- * 指定字段Raw查询
- * @access public
- * @param string $field 查询字段表达式
- * @param mixed $op 查询表达式
- * @param string $condition 查询条件
- * @param string $logic 查询逻辑 and or xor
- * @return $this
- */
- public function whereFieldRaw(string $field, $op, $condition = null, string $logic = 'AND')
- {
- if (is_null($condition)) {
- $condition = $op;
- $op = '=';
- }
- $this->options['where'][$logic][] = [new Raw($field), $op, $condition];
- return $this;
- }
- /**
- * 指定表达式查询条件
- * @access public
- * @param string $where 查询条件
- * @param array $bind 参数绑定
- * @param string $logic 查询逻辑 and or xor
- * @return $this
- */
- public function whereRaw(string $where, array $bind = [], string $logic = 'AND')
- {
- if (!empty($bind)) {
- $this->bindParams($where, $bind);
- }
- $this->options['where'][$logic][] = new Raw($where);
- return $this;
- }
- /**
- * 指定表达式查询条件 OR
- * @access public
- * @param string $where 查询条件
- * @param array $bind 参数绑定
- * @return $this
- */
- public function whereOrRaw(string $where, array $bind = [])
- {
- return $this->whereRaw($where, $bind, 'OR');
- }
- /**
- * 分析查询表达式
- * @access protected
- * @param string $logic 查询逻辑 and or xor
- * @param mixed $field 查询字段
- * @param mixed $op 查询表达式
- * @param mixed $condition 查询条件
- * @param array $param 查询参数
- * @param bool $strict 严格模式
- * @return $this
- */
- protected function parseWhereExp(string $logic, $field, $op, $condition, array $param = [], bool $strict = false)
- {
- $logic = strtoupper($logic);
- if (is_string($field) && !empty($this->options['via']) && false === strpos($field, '.')) {
- $field = $this->options['via'] . '.' . $field;
- }
- if ($field instanceof Raw) {
- return $this->whereRaw($field, is_array($op) ? $op : [], $logic);
- } elseif ($strict) {
- // 使用严格模式查询
- if ('=' == $op) {
- $where = $this->whereEq($field, $condition);
- } else {
- $where = [$field, $op, $condition, $logic];
- }
- } elseif (is_array($field)) {
- // 解析数组批量查询
- return $this->parseArrayWhereItems($field, $logic);
- } elseif ($field instanceof Closure) {
- $where = $field;
- } elseif (is_string($field)) {
- if (preg_match('/[,=\<\'\"\(\s]/', $field)) {
- return $this->whereRaw($field, is_array($op) ? $op : [], $logic);
- } elseif (is_string($op) && strtolower($op) == 'exp') {
- $bind = isset($param[2]) && is_array($param[2]) ? $param[2] : [];
- return $this->whereExp($field, $condition, $bind, $logic);
- }
- $where = $this->parseWhereItem($logic, $field, $op, $condition, $param);
- }
- if (!empty($where)) {
- $this->options['where'][$logic][] = $where;
- }
- return $this;
- }
- /**
- * 分析查询表达式
- * @access protected
- * @param string $logic 查询逻辑 and or xor
- * @param mixed $field 查询字段
- * @param mixed $op 查询表达式
- * @param mixed $condition 查询条件
- * @param array $param 查询参数
- * @return array
- */
- protected function parseWhereItem(string $logic, $field, $op, $condition, array $param = []): array
- {
- if (is_array($op)) {
- // 同一字段多条件查询
- array_unshift($param, $field);
- $where = $param;
- } elseif ($field && is_null($condition)) {
- if (is_string($op) && in_array(strtoupper($op), ['NULL', 'NOTNULL', 'NOT NULL'], true)) {
- // null查询
- $where = [$field, $op, ''];
- } elseif ('=' === $op || is_null($op)) {
- $where = [$field, 'NULL', ''];
- } elseif ('<>' === $op) {
- $where = [$field, 'NOTNULL', ''];
- } else {
- // 字段相等查询
- $where = $this->whereEq($field, $op);
- }
- } elseif (is_string($op) && in_array(strtoupper($op), ['EXISTS', 'NOT EXISTS', 'NOTEXISTS'], true)) {
- $where = [$field, $op, is_string($condition) ? new Raw($condition) : $condition];
- } else {
- $where = $field ? [$field, $op, $condition, $param[2] ?? null] : [];
- }
- return $where;
- }
- /**
- * 相等查询的主键处理
- * @access protected
- * @param string $field 字段名
- * @param mixed $value 字段值
- * @return array
- */
- protected function whereEq(string $field, $value): array
- {
- if ($this->getPk() == $field) {
- $this->options['key'] = $value;
- }
- return [$field, '=', $value];
- }
- /**
- * 数组批量查询
- * @access protected
- * @param array $field 批量查询
- * @param string $logic 查询逻辑 and or xor
- * @return $this
- */
- protected function parseArrayWhereItems(array $field, string $logic)
- {
- if (key($field) !== 0) {
- $where = [];
- foreach ($field as $key => $val) {
- if ($val instanceof Raw) {
- $where[] = [$key, 'exp', $val];
- } else {
- $where[] = is_null($val) ? [$key, 'NULL', ''] : [$key, is_array($val) ? 'IN' : '=', $val];
- }
- }
- } else {
- // 数组批量查询
- $where = $field;
- }
- if (!empty($where)) {
- $this->options['where'][$logic] = isset($this->options['where'][$logic]) ?
- array_merge($this->options['where'][$logic], $where) : $where;
- }
- return $this;
- }
- /**
- * 去除某个查询条件
- * @access public
- * @param string $field 查询字段
- * @param string $logic 查询逻辑 and or xor
- * @return $this
- */
- public function removeWhereField(string $field, string $logic = 'AND')
- {
- $logic = strtoupper($logic);
- if (isset($this->options['where'][$logic])) {
- foreach ($this->options['where'][$logic] as $key => $val) {
- if (is_array($val) && $val[0] == $field) {
- unset($this->options['where'][$logic][$key]);
- }
- }
- }
- return $this;
- }
- /**
- * 条件查询
- * @access public
- * @param mixed $condition 满足条件(支持闭包)
- * @param Closure|array $query 满足条件后执行的查询表达式(闭包或数组)
- * @param Closure|array $otherwise 不满足条件后执行
- * @return $this
- */
- public function when($condition, $query, $otherwise = null)
- {
- if ($condition instanceof Closure) {
- $condition = $condition($this);
- }
- if ($condition) {
- if ($query instanceof Closure) {
- $query($this, $condition);
- } elseif (is_array($query)) {
- $this->where($query);
- }
- } elseif ($otherwise) {
- if ($otherwise instanceof Closure) {
- $otherwise($this, $condition);
- } elseif (is_array($otherwise)) {
- $this->where($otherwise);
- }
- }
- return $this;
- }
- }
|