forked from sanchezzzhak/kak-clickhouse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ActiveQuery.php
194 lines (177 loc) · 6.46 KB
/
ActiveQuery.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
<?php
namespace kak\clickhouse;
use yii\base\InvalidConfigException;
use yii\db\ActiveQueryInterface;
use yii\db\ActiveQueryTrait;
use yii\db\ActiveRecordInterface;
use yii\db\ActiveRelationTrait;
/**
* Class ActiveQuery
* @package kak\clickhouse
*/
class ActiveQuery extends Query implements ActiveQueryInterface
{
use ActiveQueryTrait;
use ActiveRelationTrait;
/**
* Constructor.
* @param array $modelClass the model class associated with this query
* @param array $config configurations to be applied to the newly created query object
*/
public function __construct($modelClass, $config = [])
{
$this->modelClass = $modelClass;
parent::__construct($config);
}
/**
* Creates a DB command that can be used to execute this query.
* @param Connection|null $db the DB connection used to create the DB command.
* If `null`, the DB connection returned by [[modelClass]] will be used.
* @return Command the created DB command instance.
*/
public function createCommand($db = null)
{
$modelClass = $this->modelClass;
return parent::createCommand($db ? $db : $modelClass::getDb());
}
/**
* Returns the number of records.
* @param string $q the COUNT expression. Defaults to ''. clickhouse not support
* Make sure you properly [quote](guide:db-dao#quoting-table-and-column-names) column names in the expression.
* @param Connection $db the database connection used to generate the SQL statement.
* If this parameter is not given (or null), the `db` application component will be used.
* @return integer|string number of records. The result may be a string depending on the
* underlying database engine and to support integer values higher than a 32bit PHP integer can handle.
*/
public function count($q = '', $db = null)
{
return parent::count($q, $db);
}
/**
* Executes query and returns all results as an array.
* @param Connection $db the DB connection used to create the DB command.
* If null, the DB connection returned by [[modelClass]] will be used.
* @return array|ActiveRecord[] the query results. If the query results in nothing, an empty array will be returned.
*/
public function all($db = null)
{
return parent::all($db);
}
/**
* @param null $db
* @return ActiveRecord|null
* @throws InvalidConfigException
*/
public function one($db = null)
{
$row = parent::one($db);
if ($row !== null) {
$models = $this->populate([$row]);
return reset($models) ?: null;
}
return null;
}
/**
* {@inheritdoc}
* @throws InvalidConfigException
*/
public function populate($rows)
{
if (empty($rows)) {
return [];
}
$models = $this->createModels($rows);
if (!empty($this->join) && $this->indexBy === null) {
$models = $this->removeDuplicatedModels($models);
}
if (!empty($this->with)) {
$this->findWith($this->with, $models);
}
if ($this->inverseOf !== null) {
$this->addInverseRelations($models);
}
if (!$this->asArray) {
foreach ($models as $model) {
$model->afterFind();
}
}
return parent::populate($models);
}
/**
* Removes duplicated models by checking their primary key values.
* This method is mainly called when a join query is performed, which may cause duplicated rows being returned.
* @param array $models the models to be checked
* @throws InvalidConfigException if model primary key is empty
* @return array the distinctive models
*/
private function removeDuplicatedModels($models)
{
$hash = [];
/* @var $class ActiveRecord */
$class = $this->modelClass;
$pks = $class::primaryKey();
if (count($pks) > 1) {
// composite primary key
foreach ($models as $i => $model) {
$key = [];
foreach ($pks as $pk) {
if (!isset($model[$pk])) {
// do not continue if the primary key is not part of the result set
break 2;
}
$key[] = $model[$pk];
}
$key = serialize($key);
if (isset($hash[$key])) {
unset($models[$i]);
} else {
$hash[$key] = true;
}
}
} elseif (empty($pks)) {
throw new InvalidConfigException("Primary key of '{$class}' can not be empty.");
} else {
// single column primary key
$pk = reset($pks);
foreach ($models as $i => $model) {
if (!isset($model[$pk])) {
// do not continue if the primary key is not part of the result set
break;
}
$key = $model[$pk];
if (isset($hash[$key])) {
unset($models[$i]);
} elseif ($key !== null) {
$hash[$key] = true;
}
}
}
return array_values($models);
}
/**
* If applicable, populate the query's primary model into the related records' inverse relationship.
* @param array $result the array of related records as generated by [[populate()]]
* @since 2.0.9
*/
private function addInverseRelations(&$result)
{
if ($this->inverseOf === null) {
return;
}
foreach ($result as $i => $relatedModel) {
if ($relatedModel instanceof ActiveRecordInterface) {
if (!isset($inverseRelation)) {
$inverseRelation = $relatedModel->getRelation($this->inverseOf);
}
$relatedModel->populateRelation($this->inverseOf, $inverseRelation->multiple ? [$this->primaryModel] : $this->primaryModel);
} else {
if (!isset($inverseRelation)) {
/* @var $modelClass ActiveRecordInterface */
$modelClass = $this->modelClass;
$inverseRelation = $modelClass::instance()->getRelation($this->inverseOf);
}
$result[$i][$this->inverseOf] = $inverseRelation->multiple ? [$this->primaryModel] : $this->primaryModel;
}
}
}
}