1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Tools to use API as ActiveRecord for Yii2 |
4
|
|
|
* |
5
|
|
|
* @link https://github.com/hiqdev/yii2-hiart |
6
|
|
|
* @package yii2-hiart |
7
|
|
|
* @license BSD-3-Clause |
8
|
|
|
* @copyright Copyright (c) 2015-2017, HiQDev (http://hiqdev.com/) |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace hiqdev\hiart; |
12
|
|
|
|
13
|
|
|
use yii\db\QueryInterface; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Query represents API query in a way that is independent from a concrete API. |
17
|
|
|
* Holds API query information: |
18
|
|
|
* - general query data |
19
|
|
|
* - action: action to be performed with this query, e.g. search, insert, update, delete |
20
|
|
|
* - options: other additional options, like |
21
|
|
|
* - raw: do not decode response |
22
|
|
|
* - batch: batch(bulk) request |
23
|
|
|
* - timeout, ... |
24
|
|
|
* - insert/update query data |
25
|
|
|
* - body: insert or update data |
26
|
|
|
* - select query data |
27
|
|
|
* - select: fields to select |
28
|
|
|
* - count: marks count query |
29
|
|
|
* - from: entity being queried, e.g. user |
30
|
|
|
* - join: data how to join with other entities |
31
|
|
|
* - other standard query options provided with QueryTrait: |
32
|
|
|
* - where, limit, offset, orderBy, indexBy. |
33
|
|
|
*/ |
34
|
|
|
class Query extends \yii\db\Query implements QueryInterface |
35
|
|
|
{ |
36
|
|
|
/** |
37
|
|
|
* @var string action that this query performs |
38
|
|
|
*/ |
39
|
|
|
public $action; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var array query options e.g. raw, batch |
43
|
|
|
*/ |
44
|
|
|
public $options = []; |
45
|
|
|
|
46
|
|
|
public $count; |
47
|
|
|
|
48
|
|
|
public $body = []; |
49
|
|
|
|
50
|
|
|
public static function instantiate($action, $from, array $options = []) |
51
|
|
|
{ |
52
|
|
|
$query = new static(); |
53
|
|
|
|
54
|
|
|
return $query->action($action)->from($from)->options($options); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function createCommand($db = null) |
58
|
|
|
{ |
59
|
|
|
if ($db === null) { |
60
|
|
|
throw new \Exception('no db given to Query::createCommand'); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$commandConfig = $db->getQueryBuilder()->build($this); |
64
|
|
|
|
65
|
|
|
return $db->createCommand($commandConfig); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function one($db = null) |
69
|
|
|
{ |
70
|
|
|
return $this->limit(1)->addOption('batch', false)->search(); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function all($db = null) |
74
|
|
|
{ |
75
|
|
|
$rows = $this->searchAll(); |
76
|
|
|
|
77
|
|
|
if (!empty($rows) && $this->indexBy !== null) { |
78
|
|
|
$result = []; |
79
|
|
|
foreach ($rows as $row) { |
80
|
|
View Code Duplication |
if ($this->indexBy instanceof \Closure) { |
|
|
|
|
81
|
|
|
$key = call_user_func($this->indexBy, $row); |
82
|
|
|
} else { |
83
|
|
|
$key = $row[$this->indexBy]; |
84
|
|
|
} |
85
|
|
|
$result[$key] = $row; |
86
|
|
|
} |
87
|
|
|
$rows = $result; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return $rows; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function searchAll($db = null) |
|
|
|
|
94
|
|
|
{ |
95
|
|
|
return $this->addOption('batch', true)->search(); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function search($db = null) |
99
|
|
|
{ |
100
|
|
|
return $this->createCommand($db)->search(); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function delete($db = null, $options = []) |
104
|
|
|
{ |
105
|
|
|
return $this->createCommand($db)->deleteByQuery($options); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function count($q = '*', $db = null) |
109
|
|
|
{ |
110
|
|
|
$this->count = $q; |
111
|
|
|
|
112
|
|
|
return (int) $this->searchAll(); |
|
|
|
|
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
public function exists($db = null) |
116
|
|
|
{ |
117
|
|
|
return !empty(self::one($db)); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public function action($action) |
121
|
|
|
{ |
122
|
|
|
$this->action = $action; |
123
|
|
|
|
124
|
|
|
return $this; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
public function addAction($action) |
128
|
|
|
{ |
129
|
|
|
if (empty($this->action)) { |
130
|
|
|
$this->action = $action; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
return $this; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
public function addOption($name, $value) |
137
|
|
|
{ |
138
|
|
|
if (!isset($this->options[$name])) { |
139
|
|
|
$this->options[$name] = $value; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
return $this; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
public function getOption($name) |
146
|
|
|
{ |
147
|
|
|
return isset($this->options[$name]) ? $this->options[$name] : null; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
public function options($options) |
151
|
|
|
{ |
152
|
|
|
$this->options = $options; |
153
|
|
|
|
154
|
|
|
return $this; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
public function addOptions($options) |
158
|
|
|
{ |
159
|
|
|
if (!empty($options)) { |
160
|
|
|
$this->options = array_merge($this->options, $options); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
return $this; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
public function body($body) |
167
|
|
|
{ |
168
|
|
|
$this->body = $body; |
169
|
|
|
|
170
|
|
|
return $this; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
public function innerJoin($table, $on = '', $params = []) |
174
|
|
|
{ |
175
|
|
|
$this->join[] = (array) $table; |
176
|
|
|
|
177
|
|
|
return $this; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
View Code Duplication |
public function fields($fields) |
|
|
|
|
181
|
|
|
{ |
182
|
|
|
if (is_array($fields) || $fields === null) { |
183
|
|
|
$this->fields = $fields; |
|
|
|
|
184
|
|
|
} else { |
185
|
|
|
$this->fields = func_get_args(); |
|
|
|
|
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
return $this; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
View Code Duplication |
public function source($source) |
|
|
|
|
192
|
|
|
{ |
193
|
|
|
if (is_array($source) || $source === null) { |
194
|
|
|
$this->source = $source; |
|
|
|
|
195
|
|
|
} else { |
196
|
|
|
$this->source = func_get_args(); |
|
|
|
|
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
return $this; |
200
|
|
|
} |
201
|
|
|
} |
202
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.