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\Abstracts;
|
14
|
|
|
|
15
|
|
|
// ------------------------------------------------------------------------
|
16
|
|
|
|
17
|
|
|
use O2System\Database\DataObjects\Result;
|
18
|
|
|
use O2System\Database\DataStructures\Config;
|
19
|
|
|
use O2System\Database\NoSql\DataStructures\Query;
|
20
|
|
|
use O2System\Spl\Exceptions\RuntimeException;
|
21
|
|
|
use O2System\Spl\Traits\Collectors\ConfigCollectorTrait;
|
22
|
|
|
|
23
|
|
|
/**
|
24
|
|
|
* Class AbstractConnection
|
25
|
|
|
*
|
26
|
|
|
* @package O2System\Database\Abstracts
|
27
|
|
|
*/
|
28
|
|
|
abstract class AbstractConnection
|
29
|
|
|
{
|
30
|
|
|
use ConfigCollectorTrait;
|
31
|
|
|
|
32
|
|
|
/**
|
33
|
|
|
* AbstractConnection::$debugEnabled
|
34
|
|
|
*
|
35
|
|
|
* Connection debug mode flag.
|
36
|
|
|
*
|
37
|
|
|
* @var bool
|
38
|
|
|
*/
|
39
|
|
|
public $debugEnabled = true;
|
40
|
|
|
|
41
|
|
|
/**
|
42
|
|
|
* AbstractConnection::$database
|
43
|
|
|
*
|
44
|
|
|
* Connection database name.
|
45
|
|
|
*
|
46
|
|
|
* @var string
|
47
|
|
|
*/
|
48
|
|
|
public $database;
|
49
|
|
|
|
50
|
|
|
/**
|
51
|
|
|
* AbstractConnection::$isProtectIdentifiers
|
52
|
|
|
*
|
53
|
|
|
* Protect identifiers mode flag.
|
54
|
|
|
*
|
55
|
|
|
* @var bool
|
56
|
|
|
*/
|
57
|
|
|
public $protectIdentifiers = true;
|
58
|
|
|
|
59
|
|
|
/**
|
60
|
|
|
* AbstractConnection::$disableQueryExecution
|
61
|
|
|
*
|
62
|
|
|
* Query execution mode flag.
|
63
|
|
|
*
|
64
|
|
|
* @var bool
|
65
|
|
|
*/
|
66
|
|
|
public $disableQueryExecution = false;
|
67
|
|
|
|
68
|
|
|
/**
|
69
|
|
|
* AbstractConnection::$queriesResultCache
|
70
|
|
|
*
|
71
|
|
|
* Array of query objects that have executed
|
72
|
|
|
* on this connection.
|
73
|
|
|
*
|
74
|
|
|
* @var array
|
75
|
|
|
*/
|
76
|
|
|
public $queriesResultCache = [];
|
77
|
|
|
/**
|
78
|
|
|
* AbstractConnection::$handle
|
79
|
|
|
*
|
80
|
|
|
* Connection handle
|
81
|
|
|
*
|
82
|
|
|
* @var mixed
|
83
|
|
|
*/
|
84
|
|
|
public $handle;
|
85
|
|
|
/**
|
86
|
|
|
* AbstractConnection::$platform
|
87
|
|
|
*
|
88
|
|
|
* Database driver platform name.
|
89
|
|
|
*
|
90
|
|
|
* @var string
|
91
|
|
|
*/
|
92
|
|
|
protected $platform;
|
93
|
|
|
/**
|
94
|
|
|
* AbstractConnection::$connectTimeStart
|
95
|
|
|
*
|
96
|
|
|
* Microtime when connection was made.
|
97
|
|
|
*
|
98
|
|
|
* @var float
|
99
|
|
|
*/
|
100
|
|
|
protected $connectTimeStart;
|
101
|
|
|
|
102
|
|
|
/**
|
103
|
|
|
* AbstractConnection::$connectTimeDuration
|
104
|
|
|
*
|
105
|
|
|
* How long it took to establish connection.
|
106
|
|
|
*
|
107
|
|
|
* @var float
|
108
|
|
|
*/
|
109
|
|
|
protected $connectTimeDuration;
|
110
|
|
|
|
111
|
|
|
/**
|
112
|
|
|
* AbstractConnection::$transactionInProgress
|
113
|
|
|
*
|
114
|
|
|
* Transaction is in progress.
|
115
|
|
|
*
|
116
|
|
|
* @var bool
|
117
|
|
|
*/
|
118
|
|
|
protected $transactionInProgress = false;
|
119
|
|
|
|
120
|
|
|
/**
|
121
|
|
|
* AbstractConnection::$transactionStatus
|
122
|
|
|
*
|
123
|
|
|
* Transaction status flag.
|
124
|
|
|
*
|
125
|
|
|
* @var bool
|
126
|
|
|
*/
|
127
|
|
|
protected $transactionStatus = false;
|
128
|
|
|
|
129
|
|
|
/**
|
130
|
|
|
* AbstractConnection::$transactionDepth
|
131
|
|
|
*
|
132
|
|
|
* Transaction depth numbers.
|
133
|
|
|
*
|
134
|
|
|
* @var int
|
135
|
|
|
*/
|
136
|
|
|
protected $transactionDepth = 0;
|
137
|
|
|
|
138
|
|
|
/**
|
139
|
|
|
* AbstractConnection::$queriesCache
|
140
|
|
|
*
|
141
|
|
|
* Array of query objects that have executed
|
142
|
|
|
* on this connection.
|
143
|
|
|
*
|
144
|
|
|
* @var array
|
145
|
|
|
*/
|
146
|
|
|
protected $queriesCache = [];
|
147
|
|
|
|
148
|
|
|
/**
|
149
|
|
|
* AbstractConnection::$queryBuilder
|
150
|
|
|
*
|
151
|
|
|
* Query Builder instance.
|
152
|
|
|
*
|
153
|
|
|
* @var AbstractQueryBuilder
|
154
|
|
|
*/
|
155
|
|
|
protected $queryBuilder;
|
156
|
|
|
|
157
|
|
|
protected $affectedDocuments;
|
158
|
|
|
protected $lastInsertId;
|
159
|
|
|
protected $lastUpsertedIds = [];
|
160
|
|
|
|
161
|
|
|
// ------------------------------------------------------------------------
|
162
|
|
|
|
163
|
|
|
/**
|
164
|
|
|
* AbstractConnection::__construct
|
165
|
|
|
*
|
166
|
|
|
* @param \O2System\Database\DataStructures\Config $config
|
167
|
|
|
*
|
168
|
|
|
* @throws \O2System\Spl\Exceptions\RuntimeException
|
169
|
|
|
*/
|
170
|
|
|
public function __construct(Config $config)
|
171
|
|
|
{
|
172
|
|
|
language()
|
173
|
|
|
->addFilePath(str_replace('NoSql' . DIRECTORY_SEPARATOR . 'Abstracts', '', __DIR__) . DIRECTORY_SEPARATOR)
|
174
|
|
|
->loadFile('database');
|
175
|
|
|
|
176
|
|
|
$config->merge(
|
177
|
|
|
array_merge(
|
178
|
|
|
[
|
179
|
|
|
'escapeCharacter' => '"',
|
180
|
|
|
'reservedIdentifiers' => ['*'],
|
181
|
|
|
'likeEscapeStatement' => ' ESCAPE \'%s\' ',
|
182
|
|
|
'likeEscapeCharacter' => '!',
|
183
|
|
|
],
|
184
|
|
|
$this->getConfig()
|
|
|
|
|
185
|
|
|
)
|
186
|
|
|
);
|
187
|
|
|
|
188
|
|
|
$this->config = $config;
|
|
|
|
|
189
|
|
|
|
190
|
|
|
$this->debugEnabled = $config->offsetGet('debugEnabled');
|
191
|
|
|
$this->transactionEnabled = $config->offsetGet('transEnabled');
|
|
|
|
|
192
|
|
|
$this->database = $config->offsetGet('database');
|
193
|
|
|
|
194
|
|
|
$this->connect();
|
195
|
|
|
}
|
196
|
|
|
|
197
|
|
|
// ------------------------------------------------------------------------
|
198
|
|
|
|
199
|
|
|
/**
|
200
|
|
|
* AbstractConnection::connect
|
201
|
|
|
*
|
202
|
|
|
* Establish the connection.
|
203
|
|
|
*
|
204
|
|
|
* @return void
|
205
|
|
|
* @throws \O2System\Spl\Exceptions\RuntimeException
|
206
|
|
|
*/
|
207
|
|
|
final public function connect()
|
208
|
|
|
{
|
209
|
|
|
/* If an established connection is available, then there's
|
210
|
|
|
* no need to connect and select the database.
|
211
|
|
|
*
|
212
|
|
|
* Depending on the database driver, conn_id can be either
|
213
|
|
|
* boolean TRUE, a resource or an object.
|
214
|
|
|
*/
|
215
|
|
|
if ($this->handle) {
|
216
|
|
|
return;
|
217
|
|
|
}
|
218
|
|
|
|
219
|
|
|
//--------------------------------------------------------------------
|
220
|
|
|
|
221
|
|
|
$this->connectTimeStart = microtime(true);
|
222
|
|
|
|
223
|
|
|
// Connect to the database and set the connection ID
|
224
|
|
|
$this->platformConnectHandler($this->config);
|
|
|
|
|
225
|
|
|
|
226
|
|
|
// No connection resource? Check if there is a failover else throw an error
|
227
|
|
|
if ( ! $this->handle) {
|
228
|
|
|
// Check if there is a failover set
|
229
|
|
|
if ( ! empty($this->config[ 'failover' ]) && is_array($this->config[ 'failover' ])) {
|
230
|
|
|
// Go over all the failovers
|
231
|
|
|
foreach ($this->config[ 'failover' ] as $failover) {
|
232
|
|
|
|
233
|
|
|
// Try to connect
|
234
|
|
|
$this->platformConnectHandler($failover = new Config($failover));
|
235
|
|
|
|
236
|
|
|
// If a connection is made break the foreach loop
|
237
|
|
|
if ($this->handle) {
|
238
|
|
|
$this->config = $failover;
|
|
|
|
|
239
|
|
|
break;
|
240
|
|
|
}
|
241
|
|
|
}
|
242
|
|
|
}
|
243
|
|
|
|
244
|
|
|
// We still don't have a connection?
|
245
|
|
|
if ( ! $this->handle) {
|
246
|
|
|
throw new RuntimeException('DB_E_UNABLE_TO_CONNECT', 0, [$this->platform]);
|
247
|
|
|
}
|
248
|
|
|
}
|
249
|
|
|
|
250
|
|
|
$this->connectTimeDuration = microtime(true) - $this->connectTimeStart;
|
251
|
|
|
}
|
252
|
|
|
|
253
|
|
|
// ------------------------------------------------------------------------
|
254
|
|
|
|
255
|
|
|
/**
|
256
|
|
|
* AbstractConnection::platformConnectHandler
|
257
|
|
|
*
|
258
|
|
|
* Driver dependent way method for open the connection.
|
259
|
|
|
*
|
260
|
|
|
* @param Config $config The connection configuration.
|
261
|
|
|
*
|
262
|
|
|
* @return mixed
|
263
|
|
|
*/
|
264
|
|
|
abstract protected function platformConnectHandler(Config $config);
|
265
|
|
|
|
266
|
|
|
//--------------------------------------------------------------------
|
267
|
|
|
|
268
|
|
|
/**
|
269
|
|
|
* AbstractConnection::isSupported
|
270
|
|
|
*
|
271
|
|
|
* Check if the platform is supported.
|
272
|
|
|
*
|
273
|
|
|
* @return bool
|
274
|
|
|
*/
|
275
|
|
|
abstract public function isSupported();
|
276
|
|
|
|
277
|
|
|
//--------------------------------------------------------------------
|
278
|
|
|
|
279
|
|
|
/**
|
280
|
|
|
* AbstractConnection::getPlatform
|
281
|
|
|
*
|
282
|
|
|
* Get the name of the database platform of this connection.
|
283
|
|
|
*
|
284
|
|
|
* @return string The name of the database platform.
|
285
|
|
|
*/
|
286
|
|
|
public function getPlatform()
|
287
|
|
|
{
|
288
|
|
|
return $this->platform;
|
289
|
|
|
}
|
290
|
|
|
|
291
|
|
|
//--------------------------------------------------------------------
|
292
|
|
|
|
293
|
|
|
/**
|
294
|
|
|
* AbstractConnection::getVersion
|
295
|
|
|
*
|
296
|
|
|
* Get the version of the database platform of this connection.
|
297
|
|
|
*
|
298
|
|
|
* @return \O2System\Spl\DataStructures\SplArrayObject
|
299
|
|
|
*/
|
300
|
|
|
public function getPlatformInfo()
|
301
|
|
|
{
|
302
|
|
|
if (isset($this->queriesResultCache[ 'platformInfo' ])) {
|
303
|
|
|
return $this->queriesResultCache[ 'platformInfo' ];
|
304
|
|
|
}
|
305
|
|
|
|
306
|
|
|
return $this->queriesResultCache[ 'platformInfo' ] = $this->platformGetPlatformInfoHandler();
|
307
|
|
|
}
|
308
|
|
|
|
309
|
|
|
// ------------------------------------------------------------------------
|
310
|
|
|
|
311
|
|
|
/**
|
312
|
|
|
* AbstractConnection::platformGetPlatformVersionHandler
|
313
|
|
|
*
|
314
|
|
|
* Platform getting version handler.
|
315
|
|
|
*
|
316
|
|
|
* @return \O2System\Spl\DataStructures\SplArrayObject
|
317
|
|
|
*/
|
318
|
|
|
abstract protected function platformGetPlatformInfoHandler();
|
319
|
|
|
|
320
|
|
|
// ------------------------------------------------------------------------
|
321
|
|
|
|
322
|
|
|
/**
|
323
|
|
|
* AbstractConnection::connected
|
324
|
|
|
*
|
325
|
|
|
* Determine if the connection is connected
|
326
|
|
|
*
|
327
|
|
|
* @return bool
|
328
|
|
|
*/
|
329
|
|
|
final public function connected()
|
330
|
|
|
{
|
331
|
|
|
return (bool)($this->handle === false
|
332
|
|
|
? false
|
333
|
|
|
: true);
|
334
|
|
|
}
|
335
|
|
|
|
336
|
|
|
// ------------------------------------------------------------------------
|
337
|
|
|
|
338
|
|
|
/**
|
339
|
|
|
* AbstractConnection::disconnect
|
340
|
|
|
*
|
341
|
|
|
* Disconnect database connection.
|
342
|
|
|
*
|
343
|
|
|
* @return void
|
344
|
|
|
*/
|
345
|
|
|
final public function disconnect()
|
346
|
|
|
{
|
347
|
|
|
if ($this->handle) {
|
348
|
|
|
$this->platformDisconnectHandler();
|
349
|
|
|
$this->handle = false;
|
350
|
|
|
}
|
351
|
|
|
}
|
352
|
|
|
|
353
|
|
|
//--------------------------------------------------------------------
|
354
|
|
|
|
355
|
|
|
/**
|
356
|
|
|
* AbstractConnection::disconnectHandler
|
357
|
|
|
*
|
358
|
|
|
* Driver dependent way method for closing the connection.
|
359
|
|
|
*
|
360
|
|
|
* @return mixed
|
361
|
|
|
*/
|
362
|
|
|
abstract protected function platformDisconnectHandler();
|
363
|
|
|
|
364
|
|
|
// ------------------------------------------------------------------------
|
365
|
|
|
|
366
|
|
|
/**
|
367
|
|
|
* AbstractConnection::getConnectionTimeStart
|
368
|
|
|
*
|
369
|
|
|
* Returns the time we started to connect to this database in
|
370
|
|
|
* seconds with microseconds.
|
371
|
|
|
*
|
372
|
|
|
* Used by the Debug Toolbar's timeline.
|
373
|
|
|
*
|
374
|
|
|
* @return float
|
375
|
|
|
*/
|
376
|
|
|
final public function getConnectTimeStart()
|
377
|
|
|
{
|
378
|
|
|
return (int)$this->connectTimeStart;
|
379
|
|
|
}
|
380
|
|
|
|
381
|
|
|
//--------------------------------------------------------------------
|
382
|
|
|
|
383
|
|
|
/**
|
384
|
|
|
* AbstractConnection::getConnectTimeDuration
|
385
|
|
|
*
|
386
|
|
|
* Returns the number of seconds with microseconds that it took
|
387
|
|
|
* to connect to the database.
|
388
|
|
|
*
|
389
|
|
|
* Used by the Debug Toolbar's timeline.
|
390
|
|
|
*
|
391
|
|
|
* @param int $decimals
|
392
|
|
|
*
|
393
|
|
|
* @return mixed
|
394
|
|
|
*/
|
395
|
|
|
final public function getConnectTimeDuration($decimals = 6)
|
396
|
|
|
{
|
397
|
|
|
return number_format($this->connectTimeDuration, $decimals);
|
398
|
|
|
}
|
399
|
|
|
|
400
|
|
|
// ------------------------------------------------------------------------
|
401
|
|
|
|
402
|
|
|
/**
|
403
|
|
|
* AbstractConnection::getQueries
|
404
|
|
|
*
|
405
|
|
|
* Returns Queries Collections
|
406
|
|
|
*
|
407
|
|
|
* @return array
|
408
|
|
|
*/
|
409
|
|
|
final public function getQueries()
|
410
|
|
|
{
|
411
|
|
|
return $this->queriesCache;
|
412
|
|
|
}
|
413
|
|
|
|
414
|
|
|
// ------------------------------------------------------------------------
|
415
|
|
|
|
416
|
|
|
/**
|
417
|
|
|
* AbstractConnection::getQueriesCount
|
418
|
|
|
*
|
419
|
|
|
* Returns the total number of queries that have been performed
|
420
|
|
|
* on this connection.
|
421
|
|
|
*
|
422
|
|
|
* @return int
|
423
|
|
|
*/
|
424
|
|
|
final public function getQueriesCount()
|
425
|
|
|
{
|
426
|
|
|
return (int)count($this->queriesCache);
|
427
|
|
|
}
|
428
|
|
|
|
429
|
|
|
//--------------------------------------------------------------------
|
430
|
|
|
|
431
|
|
|
/**
|
432
|
|
|
* AbstractConnection::getLastQuery
|
433
|
|
|
*
|
434
|
|
|
* Returns the last query's statement object.
|
435
|
|
|
*
|
436
|
|
|
* @return \O2System\Database\Sql\DataStructures\Query\Statement
|
437
|
|
|
*/
|
438
|
|
|
final public function getLatestQuery()
|
439
|
|
|
{
|
440
|
|
|
return end($this->queriesCache);
|
441
|
|
|
}
|
442
|
|
|
|
443
|
|
|
//--------------------------------------------------------------------
|
444
|
|
|
|
445
|
|
|
/**
|
446
|
|
|
* AbstractConnection::setDatabase
|
447
|
|
|
*
|
448
|
|
|
* Set a specific database table to use.
|
449
|
|
|
*
|
450
|
|
|
* @param string $database Database name.
|
451
|
|
|
*
|
452
|
|
|
* @return static
|
453
|
|
|
*/
|
454
|
|
|
public function setDatabase($database)
|
455
|
|
|
{
|
456
|
|
|
$this->database = $database;
|
457
|
|
|
|
458
|
|
|
return $this;
|
459
|
|
|
}
|
460
|
|
|
|
461
|
|
|
//--------------------------------------------------------------------
|
462
|
|
|
|
463
|
|
|
/**
|
464
|
|
|
* AbstractConnection::hasDatabase
|
465
|
|
|
*
|
466
|
|
|
* Check if the database exists or not.
|
467
|
|
|
*
|
468
|
|
|
* @param string $databaseName The database name.
|
469
|
|
|
*
|
470
|
|
|
* @return bool Returns false if database doesn't exists.
|
471
|
|
|
* @throws \O2System\Spl\Exceptions\RuntimeException
|
472
|
|
|
*/
|
473
|
|
|
final public function hasDatabase($databaseName)
|
474
|
|
|
{
|
475
|
|
|
if (empty($this->queriesResultCache[ 'databaseNames' ])) {
|
476
|
|
|
$this->getDatabases();
|
477
|
|
|
}
|
478
|
|
|
|
479
|
|
|
return (bool)in_array($databaseName, $this->queriesResultCache[ 'databaseNames' ]);
|
480
|
|
|
}
|
481
|
|
|
|
482
|
|
|
//--------------------------------------------------------------------
|
483
|
|
|
|
484
|
|
|
/**
|
485
|
|
|
* AbstractConnection::getDatabaseList
|
486
|
|
|
*
|
487
|
|
|
* Get list of current connection databases.
|
488
|
|
|
*
|
489
|
|
|
* @return array Returns an array.
|
490
|
|
|
* @throws \O2System\Spl\Exceptions\RuntimeException
|
491
|
|
|
*/
|
492
|
|
|
abstract public function getDatabases();
|
493
|
|
|
|
494
|
|
|
// ------------------------------------------------------------------------
|
495
|
|
|
|
496
|
|
|
/**
|
497
|
|
|
* AbstractConnection::hasCollection
|
498
|
|
|
*
|
499
|
|
|
* Check if collection exists at current connection database.
|
500
|
|
|
*
|
501
|
|
|
* @param $collection
|
502
|
|
|
*
|
503
|
|
|
* @return bool
|
504
|
|
|
* @throws \O2System\Spl\Exceptions\RuntimeException
|
505
|
|
|
*/
|
506
|
|
|
public function hasCollection($collection)
|
507
|
|
|
{
|
508
|
|
|
if (empty($this->queriesResultCache[ 'collectionNames' ])) {
|
509
|
|
|
$this->getCollections();
|
510
|
|
|
}
|
511
|
|
|
|
512
|
|
|
return (bool)in_array($collection, $this->queriesResultCache[ 'collectionNames' ]);
|
513
|
|
|
}
|
514
|
|
|
|
515
|
|
|
// ------------------------------------------------------------------------
|
516
|
|
|
|
517
|
|
|
/**
|
518
|
|
|
* AbstractConnection::getCollections
|
519
|
|
|
*
|
520
|
|
|
* Get list of current database collections.
|
521
|
|
|
*
|
522
|
|
|
* @return array Returns an array
|
523
|
|
|
* @throws \O2System\Spl\Exceptions\RuntimeException
|
524
|
|
|
*/
|
525
|
|
|
abstract public function getCollections();
|
526
|
|
|
|
527
|
|
|
// ------------------------------------------------------------------------
|
528
|
|
|
|
529
|
|
|
/**
|
530
|
|
|
* AbstractConnection::hasCollection
|
531
|
|
|
*
|
532
|
|
|
* Check if collection exists at current connection database.
|
533
|
|
|
*
|
534
|
|
|
* @param $collection
|
535
|
|
|
*
|
536
|
|
|
* @return bool
|
537
|
|
|
* @throws \O2System\Spl\Exceptions\RuntimeException
|
538
|
|
|
*/
|
539
|
|
|
public function hasKey($key, $collection)
|
540
|
|
|
{
|
541
|
|
|
if (empty($this->queriesResultCache[ 'collectionKeys' ][ $collection ])) {
|
542
|
|
|
$this->getKeys($collection);
|
543
|
|
|
}
|
544
|
|
|
|
545
|
|
|
return (bool)in_array($key, $this->queriesResultCache[ 'collectionKeys' ][ $collection ]);
|
546
|
|
|
}
|
547
|
|
|
|
548
|
|
|
// ------------------------------------------------------------------------
|
549
|
|
|
|
550
|
|
|
/**
|
551
|
|
|
* AbstractConnection::getKeys
|
552
|
|
|
*
|
553
|
|
|
* Get list of current database collections keys.
|
554
|
|
|
*
|
555
|
|
|
* @param string $collection The database table name.
|
556
|
|
|
*
|
557
|
|
|
* @return array Returns an array
|
558
|
|
|
* @throws \O2System\Spl\Exceptions\RuntimeException
|
559
|
|
|
*/
|
560
|
|
|
abstract public function getKeys($collection);
|
561
|
|
|
|
562
|
|
|
// ------------------------------------------------------------------------
|
563
|
|
|
|
564
|
|
|
/**
|
565
|
|
|
* AbstractConnection::hasIndex
|
566
|
|
|
*
|
567
|
|
|
* Check if table exists at current connection database.
|
568
|
|
|
*
|
569
|
|
|
* @param string $index Database table field name.
|
570
|
|
|
* @param string $collection Database table name.
|
571
|
|
|
*
|
572
|
|
|
* @return bool
|
573
|
|
|
* @throws \O2System\Spl\Exceptions\RuntimeException
|
574
|
|
|
*/
|
575
|
|
|
public function hasIndex($index, $collection)
|
576
|
|
|
{
|
577
|
|
|
if (empty($this->queriesResultCache[ 'collectionIndexes' ][ $collection ])) {
|
578
|
|
|
$this->getIndexes($collection);
|
579
|
|
|
}
|
580
|
|
|
|
581
|
|
|
return (bool)in_array($index, $this->queriesResultCache[ 'collectionIndexes' ][ $collection ]);
|
582
|
|
|
}
|
583
|
|
|
|
584
|
|
|
// ------------------------------------------------------------------------
|
585
|
|
|
|
586
|
|
|
/**
|
587
|
|
|
* AbstractConnection::getIndexes
|
588
|
|
|
*
|
589
|
|
|
* @param string $collection The database table name.
|
590
|
|
|
*
|
591
|
|
|
* @return array
|
592
|
|
|
* @throws \O2System\Spl\Exceptions\RuntimeException
|
593
|
|
|
*/
|
594
|
|
|
abstract public function getIndexes($collection);
|
595
|
|
|
|
596
|
|
|
// ------------------------------------------------------------------------
|
597
|
|
|
|
598
|
|
|
/**
|
599
|
|
|
* AbstractConnection::execute
|
600
|
|
|
*
|
601
|
|
|
* Execute Sql statement against database.
|
602
|
|
|
*
|
603
|
|
|
* @param Query\BuilderCache $builderCache
|
604
|
|
|
*
|
605
|
|
|
* @return mixed
|
606
|
|
|
* @throws \O2System\Spl\Exceptions\RuntimeException
|
607
|
|
|
*/
|
608
|
|
|
public function execute(Query\BuilderCache $builderCache, array $options = [])
|
609
|
|
|
{
|
610
|
|
|
// Reconnect the connection in case isn't connected yet.
|
611
|
|
|
$this->reconnect();
|
612
|
|
|
|
613
|
|
|
$statement = new Query\Statement($builderCache);
|
614
|
|
|
|
615
|
|
|
if ($statement->getCollection()) {
|
616
|
|
|
$startTime = microtime(true);
|
617
|
|
|
$result = $this->platformExecuteHandler($statement, $options);
|
618
|
|
|
|
619
|
|
|
$statement->setDuration($startTime);
|
620
|
|
|
$statement->setAffectedDocuments($this->getAffectedDocuments());
|
621
|
|
|
$statement->setLastInsertId($this->getLastInsertId());
|
622
|
|
|
|
623
|
|
|
$this->queriesCache[] = $statement;
|
624
|
|
|
|
625
|
|
|
if ($statement->hasErrors()) {
|
626
|
|
|
if ($this->debugEnabled) {
|
627
|
|
|
throw new RuntimeException($statement->getLatestErrorMessage(),
|
628
|
|
|
$statement->getLatestErrorCode());
|
629
|
|
|
}
|
630
|
|
|
}
|
631
|
|
|
|
632
|
|
|
return (bool)$result;
|
633
|
|
|
}
|
634
|
|
|
|
635
|
|
|
return false;
|
636
|
|
|
}
|
637
|
|
|
|
638
|
|
|
// ------------------------------------------------------------------------
|
639
|
|
|
|
640
|
|
|
/**
|
641
|
|
|
* AbstractConnection::reconnect
|
642
|
|
|
*
|
643
|
|
|
* Keep or establish the connection if no queries have been sent for
|
644
|
|
|
* a length of time exceeding the server's idle timeout.
|
645
|
|
|
*
|
646
|
|
|
* @return void
|
647
|
|
|
*/
|
648
|
|
|
public function reconnect()
|
649
|
|
|
{
|
650
|
|
|
if (empty($this->handle)) {
|
651
|
|
|
$this->platformConnectHandler($this->config);
|
|
|
|
|
652
|
|
|
}
|
653
|
|
|
}
|
654
|
|
|
|
655
|
|
|
// ------------------------------------------------------------------------
|
656
|
|
|
|
657
|
|
|
/**
|
658
|
|
|
* AbstractConnection::executeHandler
|
659
|
|
|
*
|
660
|
|
|
* Driver dependent way method for execute the Sql statement.
|
661
|
|
|
*
|
662
|
|
|
* @param Query\Statement $statement Query statement object.
|
663
|
|
|
*
|
664
|
|
|
* @return bool
|
665
|
|
|
*/
|
666
|
|
|
abstract protected function platformExecuteHandler(Query\Statement &$statement, array $options = []);
|
667
|
|
|
|
668
|
|
|
// ------------------------------------------------------------------------
|
669
|
|
|
|
670
|
|
|
/**
|
671
|
|
|
* AbstractConnection::getAffectedDocuments
|
672
|
|
|
*
|
673
|
|
|
* Get the total number of affected rows from the last query execution.
|
674
|
|
|
*
|
675
|
|
|
* @return int Returns total number of affected rows
|
676
|
|
|
*/
|
677
|
|
|
abstract public function getAffectedDocuments();
|
678
|
|
|
|
679
|
|
|
// ------------------------------------------------------------------------
|
680
|
|
|
|
681
|
|
|
/**
|
682
|
|
|
* AbstractConnection::getLastInsertId
|
683
|
|
|
*
|
684
|
|
|
* Get last insert id from the last insert query execution.
|
685
|
|
|
*
|
686
|
|
|
* @return int Returns total number of affected rows
|
687
|
|
|
*/
|
688
|
|
|
abstract public function getLastInsertId();
|
689
|
|
|
|
690
|
|
|
// ------------------------------------------------------------------------
|
691
|
|
|
|
692
|
|
|
/**
|
693
|
|
|
* AbstractConnection::query
|
694
|
|
|
*
|
695
|
|
|
* @param Query\BuilderCache $builderCache
|
696
|
|
|
*
|
697
|
|
|
* @return bool|\O2System\Database\DataObjects\Result Returns boolean if the query is contains writing syntax
|
698
|
|
|
* @throws \O2System\Spl\Exceptions\RuntimeException
|
699
|
|
|
*/
|
700
|
|
|
public function query(Query\BuilderCache $builderCache)
|
701
|
|
|
{
|
702
|
|
|
// Reconnect the connection in case isn't connected yet.
|
703
|
|
|
$this->reconnect();
|
704
|
|
|
|
705
|
|
|
$result = false;
|
706
|
|
|
$statement = new Query\Statement($builderCache);
|
707
|
|
|
|
708
|
|
|
$startTime = microtime(true);
|
709
|
|
|
|
710
|
|
|
// Run the query for real
|
711
|
|
|
if ($this->disableQueryExecution === false) {
|
712
|
|
|
$rows = $this->platformQueryHandler($statement);
|
713
|
|
|
$result = new Result($rows);
|
714
|
|
|
|
715
|
|
|
if ($this->transactionInProgress) {
|
716
|
|
|
$this->transactionStatus = ($statement->hasErrors() ? false : true);
|
717
|
|
|
}
|
718
|
|
|
}
|
719
|
|
|
|
720
|
|
|
$statement->setDuration($startTime);
|
721
|
|
|
|
722
|
|
|
$this->queriesCache[] = $statement;
|
723
|
|
|
|
724
|
|
|
if ($statement->hasErrors()) {
|
725
|
|
|
if ($this->debugEnabled) {
|
726
|
|
|
throw new RuntimeException($statement->getLatestErrorMessage(),
|
727
|
|
|
$statement->getLatestErrorCode());
|
728
|
|
|
}
|
729
|
|
|
|
730
|
|
|
if ($this->transactionInProgress) {
|
731
|
|
|
$this->transactionStatus = false;
|
732
|
|
|
$this->transactionInProgress = false;
|
733
|
|
|
}
|
734
|
|
|
|
735
|
|
|
return false;
|
736
|
|
|
}
|
737
|
|
|
|
738
|
|
|
return $result;
|
739
|
|
|
}
|
740
|
|
|
|
741
|
|
|
// ------------------------------------------------------------------------
|
742
|
|
|
|
743
|
|
|
/**
|
744
|
|
|
* AbstractConnection::platformQueryHandler
|
745
|
|
|
*
|
746
|
|
|
* Driver dependent way method for execute the Sql statement.
|
747
|
|
|
*
|
748
|
|
|
* @param Query\Statement $statement Query statement object.
|
749
|
|
|
*
|
750
|
|
|
* @return array
|
751
|
|
|
*/
|
752
|
|
|
abstract protected function platformQueryHandler(Query\Statement &$statement);
|
753
|
|
|
|
754
|
|
|
// ------------------------------------------------------------------------
|
755
|
|
|
|
756
|
|
|
/**
|
757
|
|
|
* AbstractConnection::collection
|
758
|
|
|
*
|
759
|
|
|
* @param string $collectionName
|
760
|
|
|
*
|
761
|
|
|
* @return AbstractQueryBuilder
|
762
|
|
|
*/
|
763
|
|
|
public function collection($collectionName)
|
764
|
|
|
{
|
765
|
|
|
return $this->getQueryBuilder()->collection($collectionName);
|
766
|
|
|
}
|
767
|
|
|
|
768
|
|
|
// ------------------------------------------------------------------------
|
769
|
|
|
|
770
|
|
|
/**
|
771
|
|
|
* AbstractConnection::getQueryBuilder
|
772
|
|
|
*
|
773
|
|
|
* Get connection query builder.
|
774
|
|
|
*
|
775
|
|
|
* @return AbstractQueryBuilder
|
776
|
|
|
*/
|
777
|
|
|
public function getQueryBuilder()
|
778
|
|
|
{
|
779
|
|
|
if ( ! $this->queryBuilder instanceof AbstractQueryBuilder) {
|
|
|
|
|
780
|
|
|
$className = str_replace('Connection', 'QueryBuilder', get_called_class());
|
781
|
|
|
|
782
|
|
|
if (class_exists($className)) {
|
783
|
|
|
$this->queryBuilder = new $className($this);
|
784
|
|
|
}
|
785
|
|
|
}
|
786
|
|
|
|
787
|
|
|
return $this->queryBuilder;
|
788
|
|
|
}
|
789
|
|
|
}
|
790
|
|
|
|