|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391 |
- <?php
-
-
- namespace yii\db;
-
- use yii\base\NotSupportedException;
-
-
- trait QueryTrait
- {
-
-
- public $where;
-
-
- public $limit;
-
-
- public $offset;
-
-
- public $orderBy;
-
-
- public $indexBy;
-
-
-
-
- public function indexBy($column)
- {
- $this->indexBy = $column;
- return $this;
- }
-
-
-
- public function where($condition)
- {
- $this->where = $condition;
- return $this;
- }
-
-
-
- public function andWhere($condition)
- {
- if ($this->where === null) {
- $this->where = $condition;
- } else {
- $this->where = ['and', $this->where, $condition];
- }
- return $this;
- }
-
-
-
- public function orWhere($condition)
- {
- if ($this->where === null) {
- $this->where = $condition;
- } else {
- $this->where = ['or', $this->where, $condition];
- }
- return $this;
- }
-
-
-
- public function filterWhere(array $condition)
- {
- $condition = $this->filterCondition($condition);
- if ($condition !== []) {
- $this->where($condition);
- }
- return $this;
- }
-
-
-
- public function andFilterWhere(array $condition)
- {
- $condition = $this->filterCondition($condition);
- if ($condition !== []) {
- $this->andWhere($condition);
- }
- return $this;
- }
-
-
-
- public function orFilterWhere(array $condition)
- {
- $condition = $this->filterCondition($condition);
- if ($condition !== []) {
- $this->orWhere($condition);
- }
- return $this;
- }
-
-
-
- protected function filterCondition($condition)
- {
- if (!is_array($condition)) {
- return $condition;
- }
-
- if (!isset($condition[0])) {
-
- foreach ($condition as $name => $value) {
- if ($this->isEmpty($value)) {
- unset($condition[$name]);
- }
- }
- return $condition;
- }
-
-
-
- $operator = array_shift($condition);
-
- switch (strtoupper($operator)) {
- case 'NOT':
- case 'AND':
- case 'OR':
- foreach ($condition as $i => $operand) {
- $subCondition = $this->filterCondition($operand);
- if ($this->isEmpty($subCondition)) {
- unset($condition[$i]);
- } else {
- $condition[$i] = $subCondition;
- }
- }
-
- if (empty($condition)) {
- return [];
- }
- break;
- case 'BETWEEN':
- case 'NOT BETWEEN':
- if (array_key_exists(1, $condition) && array_key_exists(2, $condition)) {
- if ($this->isEmpty($condition[1]) || $this->isEmpty($condition[2])) {
- return [];
- }
- }
- break;
- default:
- if (array_key_exists(1, $condition) && $this->isEmpty($condition[1])) {
- return [];
- }
- }
-
- array_unshift($condition, $operator);
-
- return $condition;
- }
-
-
-
- protected function isEmpty($value)
- {
- return $value === '' || $value === [] || $value === null || is_string($value) && trim($value) === '';
- }
-
-
-
- public function orderBy($columns)
- {
- $this->orderBy = $this->normalizeOrderBy($columns);
- return $this;
- }
-
-
-
- public function addOrderBy($columns)
- {
- $columns = $this->normalizeOrderBy($columns);
- if ($this->orderBy === null) {
- $this->orderBy = $columns;
- } else {
- $this->orderBy = array_merge($this->orderBy, $columns);
- }
- return $this;
- }
-
-
-
- protected function normalizeOrderBy($columns)
- {
- if ($columns instanceof Expression) {
- return [$columns];
- } elseif (is_array($columns)) {
- return $columns;
- } else {
- $columns = preg_split('/\s*,\s*/', trim($columns), -1, PREG_SPLIT_NO_EMPTY);
- $result = [];
- foreach ($columns as $column) {
- if (preg_match('/^(.*?)\s+(asc|desc)$/i', $column, $matches)) {
- $result[$matches[1]] = strcasecmp($matches[2], 'desc') ? SORT_ASC : SORT_DESC;
- } else {
- $result[$column] = SORT_ASC;
- }
- }
- return $result;
- }
- }
-
-
-
- public function limit($limit)
- {
- $this->limit = $limit;
- return $this;
- }
-
-
-
- public function offset($offset)
- {
- $this->offset = $offset;
- return $this;
- }
- }
|