-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathQuery.php
321 lines (291 loc) · 9.16 KB
/
Query.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
<?php
/**
* Created by PhpStorm.
* User: kak
* Date: 03.05.2017
* Time: 11:53
*/
namespace kak\clickhouse;
use yii\db\Query as BaseQuery;
use yii\db\Exception as DbException;
/**
* Class Query
* @package kak\clickhouse
* @method getCountAll() int
* @method getTotals() array
* @method getData() array
* @method getExtremes() array
* @method getRows() int
* @method getMeta() array
*/
class Query extends BaseQuery
{
/** @var \kak\clickhouse\Command|null */
private $_command;
/** @var bool */
/** @depends-annotations this prop will be removed in 1.3.0 */
private $_withTotals = false;
public $sample = null;
public $preWhere = null;
public $limitBy = null;
public $withGroup = null;
/** @var array */
public $withQueries = [];
/**
* Creates a DB command that can be used to execute this query.
* @param \kak\clickhouse\Connection $db the database connection used to generate the SQL statement.
* If this parameter is not given, the `db` application component will be used.
* @return \kak\clickhouse\Command the created DB command instance.
*/
public function createCommand($db = null)
{
if ($db === null) {
$db = \Yii::$app->get('clickhouse');
}
list ($sql, $params) = $db->getQueryBuilder()->build($this);
$this->_command = $db->createCommand($sql, $params);
$this->setCommandCache($this->_command);
return $this->_command;
}
/**
* set section query SAMPLE
* @param $n float|int set value 0.1 .. 1 percent or int 1 .. 1m value
* @return $this the query object itself
*/
public function sample($n)
{
$this->sample = $n;
return $this;
}
/**
* Sets the PREWHERE part of the query.
*
* The method requires a `$condition` parameter, and optionally a `$params` parameter
* specifying the values to be bound to the query.
*
* The `$condition` parameter should be either a string (e.g. `'id=1'`) or an array.
*
* @inheritdoc
*
* @param string|array|Expression $condition the conditions that should be put in the WHERE part.
* @param array $params the parameters (name => value) to be bound to the query.
* @return $this the query object itself
*** see andWhere()
*** see orWhere()
*/
public function preWhere($condition, $params = [])
{
$this->preWhere = $condition;
$this->addParams($params);
return $this;
}
/**
* Adds an additional PREWHERE condition to the existing one.
* The new condition and the existing one will be joined using the 'AND' operator.
* @param string|array|Expression $condition the new WHERE condition. Please refer to [[where()]]
* on how to specify this parameter.
* @param array $params the parameters (name => value) to be bound to the query.
* @return $this the query object itself
* @see preWhere()
* @see orPreWhere()
*/
public function andPreWhere($condition, $params = [])
{
if ($this->preWhere === null) {
$this->preWhere = $condition;
} else {
$this->preWhere = ['and', $this->preWhere, $condition];
}
$this->addParams($params);
return $this;
}
/**
* Adds an additional PREWHERE condition to the existing one.
* The new condition and the existing one will be joined using the 'OR' operator.
* @param string|array|Expression $condition the new WHERE condition. Please refer to [[where()]]
* on how to specify this parameter.
* @param array $params the parameters (name => value) to be bound to the query.
* @return $this the query object itself
* @see preWhere()
* @see andPreWhere()
*/
public function orPreWhere($condition, $params = [])
{
if ($this->preWhere === null) {
$this->preWhere = $condition;
} else {
$this->preWhere = ['or', $this->preWhere, $condition];
}
$this->addParams($params);
return $this;
}
/**
* @return $this
*/
public function limitBy($n, $columns)
{
$this->limitBy = [$n , $columns];
return $this;
}
/**
* @param bool $active
* @return $this
*/
public function withTotals($active = true)
{
$this->withGroup = $active ? 'TOTALS' : null;
return $this;
}
/**
* @param bool $active
* @return $this
*/
public function withRollup($active = true)
{
$this->withGroup = $active ? 'ROLLUP' : null;
return $this;
}
/**
* @param bool $active
* @return $this
*/
public function withCube($active = true)
{
$this->withGroup = $active ? 'CUBE' : null;
return $this;
}
/**
* ```php
* $q = new Query();
* $q->select(['count() as cnt']);
* $q->withQuery(10, 'userAlias')
* $q->where('user_id > userAlias')
* ```
* @docs https://clickhouse.com/docs/en/sql-reference/statements/select/with/
*
* @param string|Query $query - expression or query
* @param string $alias - name expresion
* @param bool $recursive - this parameter is unused ClickHouse and ignored
* @return $this|Query
*/
public function withQuery($query, $alias, $recursive = false)
{
$this->withQueries[] = ['query' => $query, 'alias' => $alias, 'recursive' => $recursive];
return $this;
}
/**
* @return bool
* @deprecated
* @depends-annotations this method will be removed in 1.3.0
*/
public function hasWithTotals()
{
return $this->_withTotals;
}
/**
* check is first method executed
* @throws DbException
*/
private function ensureQueryExecuted()
{
if (null === $this->_command) {
throw new DbException('Query was not executed yet');
}
}
/**
* call method Command::{$name}
* @param $name
* @return mixed
*/
private function callSpecialCommand($name)
{
$this->ensureQueryExecuted();
return $this->_command->{$name}();
}
public function __call($name, $params)
{
$methods = ['getmeta', 'getdata', 'getextremes', 'gettotals', 'getcountall', 'getrows'];
if (in_array(strtolower($name), $methods)) {
return $this->callSpecialCommand($name);
}
return parent::__call($name, $params);
}
/**
* reset command
*/
public function __clone()
{
$this->_command = null;
parent::__clone();
}
/**
* Starts a batch query.
*
* A batch query supports fetching data in batches, which can keep the memory usage under a limit.
* This method will return a [[BatchQueryResult]] object which implements the [[\Iterator]] interface
* and can be traversed to retrieve the data in batches.
*
* For example,
*
* ```php
* $query = (new Query)->from('user');
* foreach ($query->batch() as $rows) {
* // $rows is an array of 100 or fewer rows from user table
* }
* ```
*
* @param int $batchSize the number of records to be fetched in each batch.
* @param Connection $db the database connection. If not set, the "db" application component will be used.
* @return BatchQueryResult the batch query result. It implements the [[\Iterator]] interface
* and can be traversed to retrieve the data in batches.
*/
public function batch($batchSize = 100, $db = null)
{
return \Yii::createObject([
'class' => BatchQueryResult::class,
'query' => $this,
'batchSize' => $batchSize,
'db' => $db,
'each' => false,
]);
}
/**
* Starts a batch query and retrieves data row by row.
*
* This method is similar to [[batch()]] except that in each iteration of the result,
* only one row of data is returned. For example,
*
* ```php
* $query = (new Query)->from('user');
* foreach ($query->each() as $row) {
* }
* ```
*
* @param int $batchSize the number of records to be fetched in each batch.
* @param Connection $db the database connection. If not set, the "db" application component will be used.
* @return BatchQueryResult the batch query result. It implements the [[\Iterator]] interface
* and can be traversed to retrieve the data in batches.
*/
public function each($batchSize = 100, $db = null)
{
return \Yii::createObject([
'class' => BatchQueryResult::class,
'query' => $this,
'batchSize' => $batchSize,
'db' => $db,
'each' => true,
]);
}
/**
* Appends a SQL statement using UNION operator.
* @param string|Query $sql the SQL statement to be appended using UNION
* @param mixed $all - is value TRUE if using UNION ALL and FALSE if using UNION
* or support string type ('ALL', 'DISTINCT')
* @return $this the query object itself
*/
public function union($sql, $all = false)
{
$this->union[] = ['query' => $sql, 'all' => $all];
return $this;
}
}