forked from sanchezzzhak/kak-clickhouse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Query.php
228 lines (205 loc) · 6.06 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
<?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 */
private $_withTotals = false;
/**
* @var null
*/
public $sample = null;
public $preWhere = null;
/**
* 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);
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 withTotals()
{
$this->_withTotals = true;
return $this;
}
/**
* @return bool
*/
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();
}
/**
* @inheritdoc
*/
protected function queryScalar($selectExpression, $db)
{
if ($this->emulateExecution) {
return null;
}
if (
!$this->distinct
&& empty($this->groupBy)
&& empty($this->having)
&& empty($this->union)
) {
$select = $this->select;
$order = $this->orderBy;
$limit = $this->limit;
$offset = $this->offset;
$this->select = [$selectExpression];
$this->orderBy = null;
$this->limit = null;
$this->offset = null;
$command = $this->createCommand($db);
$this->select = $select;
$this->orderBy = $order;
$this->limit = $limit;
$this->offset = $offset;
return $command->queryScalar();
}
return (new Query)
->select([$selectExpression])
->from(['c' => $this])
->createCommand($db)
->queryScalar();
}
}