1
|
|
|
<?php
|
2
|
|
|
/**
|
3
|
|
|
* This file is part of the O2System Framework package.
|
4
|
|
|
*
|
5
|
|
|
* For the full copyright and license information, please view the LICENSE
|
6
|
|
|
* file that was distributed with this source code.
|
7
|
|
|
*
|
8
|
|
|
* @author Steeve Andrian Salim
|
9
|
|
|
* @copyright Copyright (c) Steeve Andrian Salim
|
10
|
|
|
*/
|
11
|
|
|
// ------------------------------------------------------------------------
|
12
|
|
|
|
13
|
|
|
namespace O2System\Database\NoSql\Drivers\MongoDb;
|
14
|
|
|
|
15
|
|
|
// ------------------------------------------------------------------------
|
16
|
|
|
|
17
|
|
|
use O2System\Database\NoSql\Abstracts\AbstractQueryBuilder;
|
18
|
|
|
use O2System\Database\NoSql\DataStructures\Query\BuilderCache;
|
19
|
|
|
|
20
|
|
|
/**
|
21
|
|
|
* Class QueryBuilder
|
22
|
|
|
*
|
23
|
|
|
* @package O2System\Database\Drivers\MySql
|
24
|
|
|
*/
|
25
|
|
|
class QueryBuilder extends AbstractQueryBuilder
|
26
|
|
|
{
|
27
|
|
|
/**
|
28
|
|
|
* QueryBuilder::countAll
|
29
|
|
|
*
|
30
|
|
|
* Returns numbers of query result.
|
31
|
|
|
*
|
32
|
|
|
* @access public
|
33
|
|
|
* @return int|string
|
34
|
|
|
* @throws \O2System\Spl\Exceptions\RuntimeException
|
35
|
|
|
*/
|
36
|
|
|
public function countAll()
|
37
|
|
|
{
|
38
|
|
|
$totalDocuments = 0;
|
39
|
|
|
|
40
|
|
|
$result = $this->conn->query($this->builderCache);
|
41
|
|
|
|
42
|
|
|
if ($result->count()) {
|
43
|
|
|
$totalDocuments = $result->count();
|
44
|
|
|
}
|
45
|
|
|
|
46
|
|
|
return $totalDocuments;
|
47
|
|
|
}
|
48
|
|
|
|
49
|
|
|
//--------------------------------------------------------------------
|
50
|
|
|
|
51
|
|
|
/**
|
52
|
|
|
* QueryBuilder::countAllResult
|
53
|
|
|
*
|
54
|
|
|
* Returns numbers of total documents.
|
55
|
|
|
*
|
56
|
|
|
* @param bool $reset Whether perform reset Query Builder or not
|
57
|
|
|
*
|
58
|
|
|
* @return int
|
59
|
|
|
* @access public
|
60
|
|
|
*/
|
61
|
|
|
public function countAllResults($reset = true)
|
62
|
|
|
{
|
63
|
|
|
$cursor = $this->conn->server->executeCommand('neo_app',
|
64
|
|
|
new \MongoDb\Driver\Command(['count' => 'posts']));
|
65
|
|
|
|
66
|
|
|
$result = current($cursor->toArray());
|
67
|
|
|
|
68
|
|
|
$totalDocuments = 0;
|
69
|
|
|
|
70
|
|
|
if (isset($result->n)) {
|
71
|
|
|
$totalDocuments = (int)$result->n;
|
72
|
|
|
}
|
73
|
|
|
|
74
|
|
|
return $totalDocuments;
|
75
|
|
|
}
|
76
|
|
|
|
77
|
|
|
//--------------------------------------------------------------------
|
78
|
|
|
|
79
|
|
|
/**
|
80
|
|
|
* QueryBuilder::prepareWhereIn
|
81
|
|
|
*
|
82
|
|
|
* @param string|array $field
|
83
|
|
|
* @param null|mixed $value
|
84
|
|
|
* @param string $cacheKey
|
85
|
|
|
*/
|
86
|
|
|
protected function prepareWhere($field, $value = null, $cacheKey)
|
87
|
|
|
{
|
88
|
|
|
if (is_array($field)) {
|
89
|
|
|
foreach ($field as $name => $value) {
|
90
|
|
|
$this->prepareWhere($name, $value, $cacheKey);
|
91
|
|
|
}
|
92
|
|
|
} elseif (isset($value)) {
|
93
|
|
|
|
94
|
|
|
if ($field === '_id') {
|
95
|
|
|
$value = new \MongoDb\BSON\ObjectID($value);
|
96
|
|
|
}
|
97
|
|
|
|
98
|
|
|
$this->builderCache->store($cacheKey, [$field => $value]);
|
99
|
|
|
}
|
100
|
|
|
}
|
101
|
|
|
|
102
|
|
|
//--------------------------------------------------------------------
|
103
|
|
|
|
104
|
|
|
/**
|
105
|
|
|
* QueryBuilder::prepareWhereIn
|
106
|
|
|
*
|
107
|
|
|
* @param $field
|
108
|
|
|
* @param array $values
|
109
|
|
|
* @param $cacheKey
|
110
|
|
|
*/
|
111
|
|
|
protected function prepareWhereIn($field, array $values = [], $cacheKey)
|
112
|
|
|
{
|
113
|
|
|
if (is_array($field)) {
|
114
|
|
|
foreach ($field as $name => $values) {
|
115
|
|
|
$this->prepareWhereIn($name, $values, $cacheKey);
|
116
|
|
|
}
|
117
|
|
|
} elseif (count($values)) {
|
118
|
|
|
|
119
|
|
|
if ($field === '_id') {
|
120
|
|
|
foreach ($values as $key => $value) {
|
121
|
|
|
$values[ $key ] = new \MongoDb\BSON\ObjectID($value);
|
122
|
|
|
}
|
123
|
|
|
}
|
124
|
|
|
|
125
|
|
|
$this->builderCache->store($cacheKey, [$field => $values]);
|
126
|
|
|
}
|
127
|
|
|
}
|
128
|
|
|
|
129
|
|
|
//--------------------------------------------------------------------
|
130
|
|
|
|
131
|
|
|
/**
|
132
|
|
|
* QueryBuilder::platformInsertHandler
|
133
|
|
|
*
|
134
|
|
|
* @param BuilderCache $builderCache
|
135
|
|
|
*
|
136
|
|
|
* @return bool
|
137
|
|
|
* @throws \O2System\Spl\Exceptions\RuntimeException
|
138
|
|
|
*/
|
139
|
|
|
protected function platformInsertHandler(BuilderCache $builderCache)
|
140
|
|
|
{
|
141
|
|
|
if ($builderCache->from) {
|
|
|
|
|
142
|
|
|
return $this->conn->execute($builderCache, ['method' => 'insert']);
|
143
|
|
|
}
|
144
|
|
|
|
145
|
|
|
return false;
|
146
|
|
|
}
|
147
|
|
|
|
148
|
|
|
//--------------------------------------------------------------------
|
149
|
|
|
|
150
|
|
|
/**
|
151
|
|
|
* AbstractQueryBuilder::platformInsertBatchHandler
|
152
|
|
|
*
|
153
|
|
|
* @param BuilderCache $builderCache
|
154
|
|
|
*
|
155
|
|
|
* @return bool
|
156
|
|
|
* @throws \O2System\Spl\Exceptions\RuntimeException
|
157
|
|
|
*/
|
158
|
|
|
protected function platformInsertBatchHandler(BuilderCache $builderCache)
|
159
|
|
|
{
|
160
|
|
|
if ($builderCache->from) {
|
|
|
|
|
161
|
|
|
return $this->conn->execute($builderCache, ['method' => 'insert']);
|
162
|
|
|
}
|
163
|
|
|
|
164
|
|
|
return false;
|
165
|
|
|
}
|
166
|
|
|
|
167
|
|
|
//--------------------------------------------------------------------
|
168
|
|
|
|
169
|
|
|
/**
|
170
|
|
|
* QueryBuilder::platformUpdateHandler
|
171
|
|
|
*
|
172
|
|
|
* @param BuilderCache $builderCache
|
173
|
|
|
*
|
174
|
|
|
* @return bool
|
175
|
|
|
* @throws \O2System\Spl\Exceptions\RuntimeException
|
176
|
|
|
*/
|
177
|
|
|
protected function platformUpdateHandler(BuilderCache $builderCache)
|
178
|
|
|
{
|
179
|
|
|
if ($builderCache->from) {
|
|
|
|
|
180
|
|
|
|
181
|
|
|
// New sets document
|
182
|
|
|
$collection = $builderCache->from;
|
183
|
|
|
$sets = $builderCache->sets;
|
|
|
|
|
184
|
|
|
|
185
|
|
|
// Get old document
|
186
|
|
|
$document = $this->get()->first();
|
187
|
|
|
$builderCache = new BuilderCache();
|
188
|
|
|
$builderCache->store('from', $collection);
|
189
|
|
|
$builderCache->store('where', ['_id' => $document->_id]);
|
|
|
|
|
190
|
|
|
$document = $document->getArrayCopy();
|
191
|
|
|
unset($document[ '_id' ]);
|
192
|
|
|
|
193
|
|
|
$builderCache->store('sets', array_merge($document, $sets));
|
194
|
|
|
|
195
|
|
|
return $this->conn->execute($builderCache, ['method' => 'update']);
|
196
|
|
|
}
|
197
|
|
|
|
198
|
|
|
return false;
|
199
|
|
|
}
|
200
|
|
|
|
201
|
|
|
//--------------------------------------------------------------------
|
202
|
|
|
|
203
|
|
|
/**
|
204
|
|
|
* AbstractQueryBuilder::platformUpdateBatchHandler
|
205
|
|
|
*
|
206
|
|
|
* @param BuilderCache $builderCache
|
207
|
|
|
*
|
208
|
|
|
* @return bool
|
209
|
|
|
* @throws \O2System\Spl\Exceptions\RuntimeException
|
210
|
|
|
*/
|
211
|
|
|
protected function platformUpdateBatchHandler(BuilderCache $builderCache)
|
212
|
|
|
{
|
213
|
|
|
if ($builderCache->from) {
|
|
|
|
|
214
|
|
|
|
215
|
|
|
// New sets document
|
216
|
|
|
$collection = $builderCache->from;
|
217
|
|
|
$sets = $builderCache->sets;
|
|
|
|
|
218
|
|
|
|
219
|
|
|
// Get all old documents
|
220
|
|
|
$result = $this->get();
|
221
|
|
|
|
222
|
|
|
$builderCache = new BuilderCache();
|
223
|
|
|
$builderCache->store('from', $collection);
|
224
|
|
|
|
225
|
|
|
$documentIds = [];
|
226
|
|
|
$documents = [];
|
227
|
|
|
|
228
|
|
|
foreach ($result as $document) {
|
229
|
|
|
$document = $this->get()->first();
|
230
|
|
|
$documentIds[] = $document->_id;
|
|
|
|
|
231
|
|
|
$document = $document->getArrayCopy();
|
232
|
|
|
unset($document[ '_id' ]);
|
233
|
|
|
|
234
|
|
|
$documents[] = array_merge($document, $sets);
|
235
|
|
|
}
|
236
|
|
|
|
237
|
|
|
$builderCache->store('whereIn', ['_id' => $documentIds]);
|
238
|
|
|
$builderCache->store('sets', $documents);
|
239
|
|
|
|
240
|
|
|
return $this->conn->execute($builderCache, ['method' => 'update']);
|
241
|
|
|
}
|
242
|
|
|
|
243
|
|
|
return false;
|
244
|
|
|
}
|
245
|
|
|
|
246
|
|
|
//--------------------------------------------------------------------
|
247
|
|
|
|
248
|
|
|
/**
|
249
|
|
|
* QueryBuilder::platformReplaceHandler
|
250
|
|
|
*
|
251
|
|
|
* @param BuilderCache $builderCache
|
252
|
|
|
*
|
253
|
|
|
* @return bool
|
254
|
|
|
* @throws \O2System\Spl\Exceptions\RuntimeException
|
255
|
|
|
*/
|
256
|
|
|
protected function platformReplaceHandler(BuilderCache $builderCache)
|
257
|
|
|
{
|
258
|
|
|
if ($builderCache->from) {
|
|
|
|
|
259
|
|
|
|
260
|
|
|
return $this->conn->execute($builderCache, ['method' => 'update']);
|
261
|
|
|
}
|
262
|
|
|
|
263
|
|
|
return false;
|
264
|
|
|
}
|
265
|
|
|
|
266
|
|
|
//--------------------------------------------------------------------
|
267
|
|
|
|
268
|
|
|
/**
|
269
|
|
|
* AbstractQueryBuilder::platformReplaceBatchHandler
|
270
|
|
|
*
|
271
|
|
|
* @param BuilderCache $builderCache
|
272
|
|
|
*
|
273
|
|
|
* @return bool
|
274
|
|
|
* @throws \O2System\Spl\Exceptions\RuntimeException
|
275
|
|
|
*/
|
276
|
|
|
protected function platformReplaceBatchHandler(BuilderCache $builderCache)
|
277
|
|
|
{
|
278
|
|
|
if ($builderCache->from) {
|
|
|
|
|
279
|
|
|
|
280
|
|
|
return $this->conn->execute($builderCache, ['method' => 'update']);
|
281
|
|
|
}
|
282
|
|
|
|
283
|
|
|
return false;
|
284
|
|
|
}
|
285
|
|
|
|
286
|
|
|
//--------------------------------------------------------------------
|
287
|
|
|
|
288
|
|
|
/**
|
289
|
|
|
* QueryBuilder::platformDeleteHandler
|
290
|
|
|
*
|
291
|
|
|
* @param BuilderCache $builderCache
|
292
|
|
|
*
|
293
|
|
|
* @return bool
|
294
|
|
|
* @throws \O2System\Spl\Exceptions\RuntimeException
|
295
|
|
|
*/
|
296
|
|
|
protected function platformDeleteHandler(BuilderCache $builderCache)
|
297
|
|
|
{
|
298
|
|
|
if ($builderCache->from) {
|
|
|
|
|
299
|
|
|
return $this->conn->execute($builderCache, ['method' => 'delete']);
|
300
|
|
|
}
|
301
|
|
|
|
302
|
|
|
return false;
|
303
|
|
|
}
|
304
|
|
|
|
305
|
|
|
//--------------------------------------------------------------------
|
306
|
|
|
|
307
|
|
|
/**
|
308
|
|
|
* AbstractQueryBuilder::platformDeleteBatchHandler
|
309
|
|
|
*
|
310
|
|
|
* @param BuilderCache $builderCache
|
311
|
|
|
*
|
312
|
|
|
* @return bool
|
313
|
|
|
* @throws \O2System\Spl\Exceptions\RuntimeException
|
314
|
|
|
*/
|
315
|
|
|
protected function platformDeleteBatchHandler(BuilderCache $builderCache)
|
316
|
|
|
{
|
317
|
|
|
if ($builderCache->from) {
|
|
|
|
|
318
|
|
|
return $this->conn->execute($builderCache, ['method' => 'delete']);
|
319
|
|
|
}
|
320
|
|
|
|
321
|
|
|
return false;
|
322
|
|
|
}
|
323
|
|
|
}
|
324
|
|
|
|