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
|
|
|
|
14
|
|
|
namespace O2System\Database\NoSql\DataStructures;
|
15
|
|
|
|
16
|
|
|
// ------------------------------------------------------------------------
|
17
|
|
|
|
18
|
|
|
use O2System\Spl\Traits\Collectors\ErrorCollectorTrait;
|
19
|
|
|
|
20
|
|
|
/**
|
21
|
|
|
* Class QueryStatement
|
22
|
|
|
*
|
23
|
|
|
* @package O2System\Database\Sql\DataStructures
|
24
|
|
|
*/
|
25
|
|
|
class QueryStatement
|
26
|
|
|
{
|
27
|
|
|
use ErrorCollectorTrait;
|
28
|
|
|
|
29
|
|
|
/**
|
30
|
|
|
* QueryStatement::$builderCache
|
31
|
|
|
*
|
32
|
|
|
* @var \O2System\Database\NoSql\DataStructures\QueryBuilderCache
|
33
|
|
|
*/
|
34
|
|
|
private $builderCache;
|
35
|
|
|
|
36
|
|
|
/**
|
37
|
|
|
* QueryStatement::$collection
|
38
|
|
|
*
|
39
|
|
|
* Query collection name.
|
40
|
|
|
*
|
41
|
|
|
* @var string
|
42
|
|
|
*/
|
43
|
|
|
private $collection;
|
44
|
|
|
|
45
|
|
|
/**
|
46
|
|
|
* QueryStatement::$document
|
47
|
|
|
*
|
48
|
|
|
* Query document.
|
49
|
|
|
*
|
50
|
|
|
* @var array
|
51
|
|
|
*/
|
52
|
|
|
private $document = [];
|
53
|
|
|
|
54
|
|
|
/**
|
55
|
|
|
* QueryStatement::$filter
|
56
|
|
|
*
|
57
|
|
|
* Query filter array.
|
58
|
|
|
*
|
59
|
|
|
* @var array
|
60
|
|
|
*/
|
61
|
|
|
private $filter = [];
|
62
|
|
|
|
63
|
|
|
/**
|
64
|
|
|
* QueryStatement::$options
|
65
|
|
|
*
|
66
|
|
|
* Query options array.
|
67
|
|
|
*
|
68
|
|
|
* @var array
|
69
|
|
|
*/
|
70
|
|
|
private $options = [];
|
71
|
|
|
|
72
|
|
|
/**
|
73
|
|
|
* QueryStatement::$startExecutionTime
|
74
|
|
|
*
|
75
|
|
|
* The start time in seconds with microseconds
|
76
|
|
|
* for when this query was executed.
|
77
|
|
|
*
|
78
|
|
|
* @var float
|
79
|
|
|
*/
|
80
|
|
|
private $startExecutionTime;
|
81
|
|
|
|
82
|
|
|
/**
|
83
|
|
|
* QueryStatement::$endExecutionTime
|
84
|
|
|
*
|
85
|
|
|
* The end time in seconds with microseconds
|
86
|
|
|
* for when this query was executed.
|
87
|
|
|
*
|
88
|
|
|
* @var float
|
89
|
|
|
*/
|
90
|
|
|
private $endExecutionTime;
|
91
|
|
|
|
92
|
|
|
/**
|
93
|
|
|
* QueryStatement::$affectedDocuments
|
94
|
|
|
*
|
95
|
|
|
* The numbers of affected documents.
|
96
|
|
|
*
|
97
|
|
|
* @var int
|
98
|
|
|
*/
|
99
|
|
|
private $affectedDocuments;
|
100
|
|
|
|
101
|
|
|
/**
|
102
|
|
|
* QueryStatement::$lastInsertId
|
103
|
|
|
*
|
104
|
|
|
* The last insert id.
|
105
|
|
|
*
|
106
|
|
|
* @var string|int
|
107
|
|
|
*/
|
108
|
|
|
private $lastInsertId;
|
109
|
|
|
|
110
|
|
|
//--------------------------------------------------------------------
|
111
|
|
|
|
112
|
|
|
/**
|
113
|
|
|
* QueryStatement::__construct
|
114
|
|
|
*
|
115
|
|
|
* @param \O2System\Database\NoSql\DataStructures\QueryBuilderCache $queryBuilderCache
|
116
|
|
|
*/
|
117
|
|
|
public function __construct(QueryBuilderCache $queryBuilderCache)
|
118
|
|
|
{
|
119
|
|
|
$this->builderCache = $queryBuilderCache;
|
120
|
|
|
$this->setCollection($queryBuilderCache->from);
|
|
|
|
|
121
|
|
|
|
122
|
|
|
if (count($queryBuilderCache->sets)) {
|
|
|
|
|
123
|
|
|
$this->document = $queryBuilderCache->sets;
|
124
|
|
|
}
|
125
|
|
|
}
|
126
|
|
|
|
127
|
|
|
//--------------------------------------------------------------------
|
128
|
|
|
|
129
|
|
|
/**
|
130
|
|
|
* QueryStatement::getBuilderCache
|
131
|
|
|
*
|
132
|
|
|
* @return \O2System\Database\NoSql\DataStructures\QueryBuilderCache
|
133
|
|
|
*/
|
134
|
|
|
public function getBuilderCache()
|
135
|
|
|
{
|
136
|
|
|
return $this->builderCache;
|
137
|
|
|
}
|
138
|
|
|
|
139
|
|
|
//--------------------------------------------------------------------
|
140
|
|
|
|
141
|
|
|
/**
|
142
|
|
|
* QueryStatement::getCollection
|
143
|
|
|
*
|
144
|
|
|
* Get Query Collection name
|
145
|
|
|
*
|
146
|
|
|
* @return string
|
147
|
|
|
*/
|
148
|
|
|
public function getCollection()
|
149
|
|
|
{
|
150
|
|
|
return $this->collection;
|
151
|
|
|
}
|
152
|
|
|
|
153
|
|
|
//--------------------------------------------------------------------
|
154
|
|
|
|
155
|
|
|
/**
|
156
|
|
|
* QueryStatement::setCollection
|
157
|
|
|
*
|
158
|
|
|
* Set Query Collection name
|
159
|
|
|
*
|
160
|
|
|
* @param string $collection
|
161
|
|
|
*
|
162
|
|
|
* @return static
|
163
|
|
|
*/
|
164
|
|
|
public function setCollection($collection)
|
165
|
|
|
{
|
166
|
|
|
$this->collection = trim($collection);
|
167
|
|
|
|
168
|
|
|
return $this;
|
169
|
|
|
}
|
170
|
|
|
|
171
|
|
|
//--------------------------------------------------------------------
|
172
|
|
|
|
173
|
|
|
/**
|
174
|
|
|
* QueryStatement::addFilter
|
175
|
|
|
*
|
176
|
|
|
* Add Query Filter
|
177
|
|
|
*
|
178
|
|
|
* @param string $field
|
179
|
|
|
* @param int $value
|
180
|
|
|
*
|
181
|
|
|
* @return static
|
182
|
|
|
*/
|
183
|
|
|
public function addFilter($field, $value)
|
184
|
|
|
{
|
185
|
|
|
$this->filter[ $field ] = $value;
|
186
|
|
|
|
187
|
|
|
return $this;
|
188
|
|
|
}
|
189
|
|
|
|
190
|
|
|
//--------------------------------------------------------------------
|
191
|
|
|
|
192
|
|
|
/**
|
193
|
|
|
* QueryStatement::getFilter
|
194
|
|
|
*
|
195
|
|
|
* Get Query Filter
|
196
|
|
|
*
|
197
|
|
|
* @return array
|
198
|
|
|
*/
|
199
|
|
|
public function getFilter()
|
200
|
|
|
{
|
201
|
|
|
return $this->filter;
|
202
|
|
|
}
|
203
|
|
|
|
204
|
|
|
//--------------------------------------------------------------------
|
205
|
|
|
|
206
|
|
|
/**
|
207
|
|
|
* QueryStatement::setFilter
|
208
|
|
|
*
|
209
|
|
|
* Set Query Filter Array
|
210
|
|
|
*
|
211
|
|
|
* @param array $filter
|
212
|
|
|
*
|
213
|
|
|
* @return static
|
214
|
|
|
*/
|
215
|
|
|
public function setFilter(array $filter)
|
216
|
|
|
{
|
217
|
|
|
$this->filter = $filter;
|
218
|
|
|
|
219
|
|
|
return $this;
|
220
|
|
|
}
|
221
|
|
|
|
222
|
|
|
//--------------------------------------------------------------------
|
223
|
|
|
|
224
|
|
|
/**
|
225
|
|
|
* QueryStatement::addOption
|
226
|
|
|
*
|
227
|
|
|
* Add Query Option
|
228
|
|
|
*
|
229
|
|
|
* @param string $option
|
230
|
|
|
* @param mixed $value
|
231
|
|
|
*
|
232
|
|
|
* @return static
|
233
|
|
|
*/
|
234
|
|
|
public function addOption($option, $value)
|
235
|
|
|
{
|
236
|
|
|
$this->options[ $option ] = $value;
|
237
|
|
|
|
238
|
|
|
return $this;
|
239
|
|
|
}
|
240
|
|
|
|
241
|
|
|
//--------------------------------------------------------------------
|
242
|
|
|
|
243
|
|
|
/**
|
244
|
|
|
* QueryStatement::getOptions
|
245
|
|
|
*
|
246
|
|
|
* Get Query Options
|
247
|
|
|
*
|
248
|
|
|
* @return array
|
249
|
|
|
*/
|
250
|
|
|
public function getOptions()
|
251
|
|
|
{
|
252
|
|
|
return $this->options;
|
253
|
|
|
}
|
254
|
|
|
|
255
|
|
|
//--------------------------------------------------------------------
|
256
|
|
|
|
257
|
|
|
/**
|
258
|
|
|
* QueryStatement::setOptions
|
259
|
|
|
*
|
260
|
|
|
* Set Query Options
|
261
|
|
|
*
|
262
|
|
|
* @param array $options
|
263
|
|
|
*
|
264
|
|
|
* @return static
|
265
|
|
|
*/
|
266
|
|
|
public function setOptions(array $options)
|
267
|
|
|
{
|
268
|
|
|
$this->options = $options;
|
269
|
|
|
|
270
|
|
|
return $this;
|
271
|
|
|
}
|
272
|
|
|
|
273
|
|
|
//--------------------------------------------------------------------
|
274
|
|
|
|
275
|
|
|
/**
|
276
|
|
|
* QueryStatement::getDocument
|
277
|
|
|
*
|
278
|
|
|
* Get Query Document
|
279
|
|
|
*
|
280
|
|
|
* @return array
|
281
|
|
|
*/
|
282
|
|
|
public function getDocument()
|
283
|
|
|
{
|
284
|
|
|
return $this->document;
|
285
|
|
|
}
|
286
|
|
|
|
287
|
|
|
//--------------------------------------------------------------------
|
288
|
|
|
|
289
|
|
|
/**
|
290
|
|
|
* QueryStatement::setDuration
|
291
|
|
|
*
|
292
|
|
|
* Records the execution time of the statement using microtime(true)
|
293
|
|
|
* for it's start and end values. If no end value is present, will
|
294
|
|
|
* use the current time to determine total duration.
|
295
|
|
|
*
|
296
|
|
|
* @param int $start
|
297
|
|
|
* @param int|null $end
|
298
|
|
|
*
|
299
|
|
|
* @return static
|
300
|
|
|
*/
|
301
|
|
|
public function setDuration($start, $end = null)
|
302
|
|
|
{
|
303
|
|
|
$this->startExecutionTime = $start;
|
304
|
|
|
|
305
|
|
|
if (is_null($end)) {
|
306
|
|
|
$end = microtime(true);
|
307
|
|
|
}
|
308
|
|
|
|
309
|
|
|
$this->endExecutionTime = $end;
|
310
|
|
|
|
311
|
|
|
return $this;
|
312
|
|
|
}
|
313
|
|
|
|
314
|
|
|
//--------------------------------------------------------------------
|
315
|
|
|
|
316
|
|
|
/**
|
317
|
|
|
* QueryStatement::getStartExecutionTime
|
318
|
|
|
*
|
319
|
|
|
* Returns the start time in seconds with microseconds.
|
320
|
|
|
*
|
321
|
|
|
* @param bool $numberFormat
|
322
|
|
|
* @param int $decimals
|
323
|
|
|
*
|
324
|
|
|
* @return mixed
|
325
|
|
|
*/
|
326
|
|
|
public function getStartExecutionTime($numberFormat = false, $decimals = 6)
|
327
|
|
|
{
|
328
|
|
|
if ( ! $numberFormat) {
|
329
|
|
|
return $this->startExecutionTime;
|
330
|
|
|
}
|
331
|
|
|
|
332
|
|
|
return number_format($this->startExecutionTime, $decimals);
|
333
|
|
|
}
|
334
|
|
|
|
335
|
|
|
//--------------------------------------------------------------------
|
336
|
|
|
|
337
|
|
|
/**
|
338
|
|
|
* QueryStatement::getExecutionDuration
|
339
|
|
|
*
|
340
|
|
|
* Returns the duration of this query during execution, or null if
|
341
|
|
|
* the query has not been executed yet.
|
342
|
|
|
*
|
343
|
|
|
* @param int $decimals The accuracy of the returned time.
|
344
|
|
|
*
|
345
|
|
|
* @return mixed
|
346
|
|
|
*/
|
347
|
|
|
public function getExecutionDuration($decimals = 6)
|
348
|
|
|
{
|
349
|
|
|
return number_format(($this->endExecutionTime - $this->startExecutionTime), $decimals);
|
350
|
|
|
}
|
351
|
|
|
|
352
|
|
|
//--------------------------------------------------------------------
|
353
|
|
|
|
354
|
|
|
/**
|
355
|
|
|
* QueryStatement::getAffectedRows
|
356
|
|
|
*
|
357
|
|
|
* Gets numbers of affected rows.
|
358
|
|
|
*
|
359
|
|
|
* @return int
|
360
|
|
|
*/
|
361
|
|
|
public function getAffectedDocuments()
|
362
|
|
|
{
|
363
|
|
|
return $this->affectedDocuments;
|
364
|
|
|
}
|
365
|
|
|
|
366
|
|
|
//--------------------------------------------------------------------
|
367
|
|
|
|
368
|
|
|
/**
|
369
|
|
|
* QueryStatement::setAffectedRows
|
370
|
|
|
*
|
371
|
|
|
* Sets numbers of affected rows.
|
372
|
|
|
*
|
373
|
|
|
* @param int $affectedDocuments Numbers of affected rows,
|
374
|
|
|
*
|
375
|
|
|
* @return static
|
376
|
|
|
*/
|
377
|
|
|
public function setAffectedDocuments($affectedDocuments)
|
378
|
|
|
{
|
379
|
|
|
$this->affectedDocuments = $affectedDocuments;
|
380
|
|
|
|
381
|
|
|
return $this;
|
382
|
|
|
}
|
383
|
|
|
|
384
|
|
|
//--------------------------------------------------------------------
|
385
|
|
|
|
386
|
|
|
/**
|
387
|
|
|
* QueryStatement::getAffectedRows
|
388
|
|
|
*
|
389
|
|
|
* Gets query last insert id.
|
390
|
|
|
*
|
391
|
|
|
* @return string|int
|
392
|
|
|
*/
|
393
|
|
|
public function getLastInsertId()
|
394
|
|
|
{
|
395
|
|
|
return $this->lastInsertId;
|
396
|
|
|
}
|
397
|
|
|
|
398
|
|
|
//--------------------------------------------------------------------
|
399
|
|
|
|
400
|
|
|
/**
|
401
|
|
|
* QueryStatement::setAffectedRows
|
402
|
|
|
*
|
403
|
|
|
* Sets query last insert id.
|
404
|
|
|
*
|
405
|
|
|
* @param string|int
|
406
|
|
|
*
|
407
|
|
|
* @return static
|
408
|
|
|
*/
|
409
|
|
|
public function setLastInsertId($lastInsertId)
|
410
|
|
|
{
|
411
|
|
|
$this->lastInsertId = $lastInsertId;
|
412
|
|
|
|
413
|
|
|
return $this;
|
414
|
|
|
}
|
415
|
|
|
}
|
416
|
|
|
|