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
|
1 |
|
public static function instantiate($action, $from, array $options = []) |
51
|
|
|
{ |
52
|
1 |
|
$query = new static(); |
53
|
|
|
|
54
|
1 |
|
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->searchOne($db); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function searchOne($db = null) |
|
|
|
|
74
|
|
|
{ |
75
|
|
|
return $this->limit(1)->addOption('batch', false)->search(); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function all($db = null) |
79
|
|
|
{ |
80
|
|
|
$rows = $this->searchAll(); |
81
|
|
|
|
82
|
|
|
if (!empty($rows) && $this->indexBy !== null) { |
83
|
|
|
$result = []; |
84
|
|
|
foreach ($rows as $row) { |
85
|
|
View Code Duplication |
if ($this->indexBy instanceof \Closure) { |
|
|
|
|
86
|
|
|
$key = call_user_func($this->indexBy, $row); |
87
|
|
|
} else { |
88
|
|
|
$key = $row[$this->indexBy]; |
89
|
|
|
} |
90
|
|
|
$result[$key] = $row; |
91
|
|
|
} |
92
|
|
|
$rows = $result; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
return $rows; |
96
|
|
|
} |
97
|
|
|
|
98
|
2 |
|
public function searchAll($db = null) |
|
|
|
|
99
|
|
|
{ |
100
|
2 |
|
return $this->addOption('batch', true)->search(); |
101
|
|
|
} |
102
|
|
|
|
103
|
2 |
|
public function search($db = null) |
104
|
|
|
{ |
105
|
2 |
|
return $this->createCommand($db)->search(); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function delete($db = null, $options = []) |
109
|
|
|
{ |
110
|
|
|
return $this->createCommand($db)->deleteByQuery($options); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public function count($q = '*', $db = null) |
114
|
|
|
{ |
115
|
|
|
$this->count = $q; |
116
|
|
|
|
117
|
|
|
return (int) $this->searchAll(); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public function exists($db = null) |
121
|
|
|
{ |
122
|
|
|
return !empty(self::one($db)); |
123
|
|
|
} |
124
|
|
|
|
125
|
1 |
|
public function action($action) |
126
|
|
|
{ |
127
|
1 |
|
$this->action = $action; |
128
|
|
|
|
129
|
1 |
|
return $this; |
130
|
|
|
} |
131
|
|
|
|
132
|
2 |
|
public function addAction($action) |
133
|
|
|
{ |
134
|
2 |
|
if (empty($this->action)) { |
135
|
2 |
|
$this->action = $action; |
136
|
2 |
|
} |
137
|
|
|
|
138
|
2 |
|
return $this; |
139
|
|
|
} |
140
|
|
|
|
141
|
2 |
|
public function addOption($name, $value) |
142
|
|
|
{ |
143
|
2 |
|
if (!isset($this->options[$name])) { |
144
|
2 |
|
$this->options[$name] = $value; |
145
|
2 |
|
} |
146
|
|
|
|
147
|
2 |
|
return $this; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
public function getOption($name) |
151
|
|
|
{ |
152
|
|
|
return isset($this->options[$name]) ? $this->options[$name] : null; |
153
|
|
|
} |
154
|
|
|
|
155
|
1 |
|
public function options($options) |
156
|
|
|
{ |
157
|
1 |
|
$this->options = $options; |
158
|
|
|
|
159
|
1 |
|
return $this; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
public function addOptions($options) |
163
|
|
|
{ |
164
|
|
|
if (!empty($options)) { |
165
|
|
|
$this->options = array_merge($this->options, $options); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
return $this; |
169
|
|
|
} |
170
|
|
|
|
171
|
1 |
|
public function body($body) |
172
|
|
|
{ |
173
|
1 |
|
$this->body = $body; |
174
|
|
|
|
175
|
1 |
|
return $this; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
public function innerJoin($table, $on = '', $params = []) |
179
|
|
|
{ |
180
|
|
|
$this->join[] = (array) $table; |
181
|
|
|
|
182
|
|
|
return $this; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
View Code Duplication |
public function fields($fields) |
|
|
|
|
186
|
|
|
{ |
187
|
|
|
if (is_array($fields) || $fields === null) { |
188
|
|
|
$this->fields = $fields; |
|
|
|
|
189
|
|
|
} else { |
190
|
|
|
$this->fields = func_get_args(); |
|
|
|
|
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
return $this; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
View Code Duplication |
public function source($source) |
|
|
|
|
197
|
|
|
{ |
198
|
|
|
if (is_array($source) || $source === null) { |
199
|
|
|
$this->source = $source; |
|
|
|
|
200
|
|
|
} else { |
201
|
|
|
$this->source = func_get_args(); |
|
|
|
|
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
return $this; |
205
|
|
|
} |
206
|
|
|
} |
207
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.