1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* DronePHP (http://www.dronephp.com) |
4
|
|
|
* |
5
|
|
|
* @link http://github.com/Pleets/DronePHP |
6
|
|
|
* @copyright Copyright (c) 2016-2018 Pleets. (http://www.pleets.org) |
7
|
|
|
* @license http://www.dronephp.com/license |
8
|
|
|
* @author Darío Rivera <[email protected]> |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Drone\Db\Driver; |
12
|
|
|
|
13
|
|
|
use Drone\Error\Errno; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* AbstractDriver Class |
17
|
|
|
* |
18
|
|
|
* This class defines standard behavior for database drivers |
19
|
|
|
*/ |
20
|
|
|
abstract class AbstractDriver |
21
|
|
|
{ |
22
|
|
|
use \Drone\Error\ErrorTrait; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Database host |
26
|
|
|
* |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
protected $dbhost; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Database user |
33
|
|
|
* |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
protected $dbuser; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Database password |
40
|
|
|
* |
41
|
|
|
* @var string |
42
|
|
|
*/ |
43
|
|
|
protected $dbpass; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Database name |
47
|
|
|
* |
48
|
|
|
* @var string |
49
|
|
|
*/ |
50
|
|
|
protected $dbname; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Database charset |
54
|
|
|
* |
55
|
|
|
* @var string |
56
|
|
|
*/ |
57
|
|
|
protected $dbchar; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Database port |
61
|
|
|
* |
62
|
|
|
* @var integer |
63
|
|
|
*/ |
64
|
|
|
protected $dbport; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Driver name |
68
|
|
|
* |
69
|
|
|
* @var string |
70
|
|
|
*/ |
71
|
|
|
protected $driverName; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Connection identifier |
75
|
|
|
* |
76
|
|
|
* @var object|resource |
77
|
|
|
*/ |
78
|
|
|
protected $dbconn; |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Rows returned on execute() method |
82
|
|
|
* |
83
|
|
|
* @var integer |
84
|
|
|
*/ |
85
|
|
|
protected $numRows; |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Fields returned on execute() method |
89
|
|
|
* |
90
|
|
|
* @var integer |
91
|
|
|
*/ |
92
|
|
|
protected $numFields; |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Rows affected returned on execute() method |
96
|
|
|
* |
97
|
|
|
* @var integer |
98
|
|
|
*/ |
99
|
|
|
protected $rowsAffected; |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Statement handle |
103
|
|
|
* |
104
|
|
|
* @var resource|mixed |
105
|
|
|
*/ |
106
|
|
|
protected $result; |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Data stored in select statements |
110
|
|
|
* |
111
|
|
|
* @var array |
112
|
|
|
*/ |
113
|
|
|
protected $arrayResult; |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Defines if consecutive querys are part of a transaction |
117
|
|
|
* |
118
|
|
|
* @var boolean |
119
|
|
|
*/ |
120
|
|
|
protected $transac_mode = false; |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Latest result of a transaction process |
124
|
|
|
* |
125
|
|
|
* The first time would be a result of a query(), execute() or parse() methods |
126
|
|
|
* On subsequent times it's a boolean resulting of the boolean operation AND with the latest result. |
127
|
|
|
* |
128
|
|
|
* @var boolean |
129
|
|
|
*/ |
130
|
|
|
protected $transac_result = null; |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Autocommit behavior |
134
|
|
|
* |
135
|
|
|
* @var boolean |
136
|
|
|
*/ |
137
|
|
|
protected $autocommit = true; |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Returns the dbhost attribute |
141
|
|
|
* |
142
|
|
|
* @return string |
143
|
|
|
*/ |
144
|
1 |
|
public function getDbhost() |
145
|
|
|
{ |
146
|
1 |
|
return $this->dbhost; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Returns the dbuser attribute |
151
|
|
|
* |
152
|
|
|
* @return string |
153
|
|
|
*/ |
154
|
1 |
|
public function getDbuser() |
155
|
|
|
{ |
156
|
1 |
|
return $this->dbuser; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Returns the dbname attribute |
161
|
|
|
* |
162
|
|
|
* @return string |
163
|
|
|
*/ |
164
|
1 |
|
public function getDbname() |
165
|
|
|
{ |
166
|
1 |
|
return $this->dbname; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Returns the dbchar attribute |
171
|
|
|
* |
172
|
|
|
* @return string |
173
|
|
|
*/ |
174
|
1 |
|
public function getDbchar() |
175
|
|
|
{ |
176
|
1 |
|
return $this->dbchar; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Returns the dbport attribute |
181
|
|
|
* |
182
|
|
|
* @return string |
183
|
|
|
*/ |
184
|
1 |
|
public function getDbport() |
185
|
|
|
{ |
186
|
1 |
|
return $this->dbport; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* Returns the driverName attribute |
191
|
|
|
* |
192
|
|
|
* @return string |
193
|
|
|
*/ |
194
|
8 |
|
public function getDriverName() |
195
|
|
|
{ |
196
|
8 |
|
return $this->driverName; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* Returns the numRows attribute |
201
|
|
|
* |
202
|
|
|
* @return integer |
203
|
|
|
*/ |
204
|
20 |
|
public function getNumRows() |
205
|
|
|
{ |
206
|
20 |
|
return $this->numRows; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* Returns the numFields attribute |
211
|
|
|
* |
212
|
|
|
* @return integer |
213
|
|
|
*/ |
214
|
20 |
|
public function getNumFields() |
215
|
|
|
{ |
216
|
20 |
|
return $this->numFields; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* Returns the rowsAffected attribute |
221
|
|
|
* |
222
|
|
|
* @return integer |
223
|
|
|
*/ |
224
|
20 |
|
public function getRowsAffected() |
225
|
|
|
{ |
226
|
20 |
|
return $this->rowsAffected; |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* Returns an array with all results of the last execute statement |
231
|
|
|
* |
232
|
|
|
* @return array |
233
|
|
|
*/ |
234
|
10 |
|
public function getArrayResult() |
235
|
|
|
{ |
236
|
10 |
|
if (!empty($this->arrayResult)) { |
237
|
|
|
return $this->arrayResult; |
238
|
|
|
} |
239
|
|
|
|
240
|
10 |
|
return $this->toArray(); |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* Returns the autocommit attribute |
245
|
|
|
* |
246
|
|
|
* @return boolean |
247
|
|
|
*/ |
248
|
|
|
public function getAutoCommit() |
249
|
|
|
{ |
250
|
|
|
return $this->autocommit; |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* Sets dbhost attribute |
255
|
|
|
* |
256
|
|
|
* @param string $value |
257
|
|
|
* |
258
|
|
|
* @return null |
259
|
|
|
*/ |
260
|
21 |
|
public function setDbhost($value) |
261
|
|
|
{ |
262
|
21 |
|
$this->dbhost = $value; |
263
|
21 |
|
} |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* Sets dbuser attribute |
267
|
|
|
* |
268
|
|
|
* @param string $value |
269
|
|
|
* |
270
|
|
|
* @return null |
271
|
|
|
*/ |
272
|
21 |
|
public function setDbuser($value) |
273
|
|
|
{ |
274
|
21 |
|
$this->dbuser = $value; |
275
|
21 |
|
} |
276
|
|
|
|
277
|
|
|
/** |
278
|
|
|
* Sets dbpass attribute |
279
|
|
|
* |
280
|
|
|
* @param string $value |
281
|
|
|
* |
282
|
|
|
* @return null |
283
|
|
|
*/ |
284
|
21 |
|
public function setDbpass($value) |
285
|
|
|
{ |
286
|
21 |
|
$this->dbpass = $value; |
287
|
21 |
|
} |
288
|
|
|
|
289
|
|
|
/** |
290
|
|
|
* Sets dbname attribute |
291
|
|
|
* |
292
|
|
|
* @param string $value |
293
|
|
|
* |
294
|
|
|
* @return null |
295
|
|
|
*/ |
296
|
21 |
|
public function setDbname($value) |
297
|
|
|
{ |
298
|
21 |
|
$this->dbname = $value; |
299
|
21 |
|
} |
300
|
|
|
|
301
|
|
|
/** |
302
|
|
|
* Sets dbchar attribute |
303
|
|
|
* |
304
|
|
|
* @param string $value |
305
|
|
|
* |
306
|
|
|
* @return null |
307
|
|
|
*/ |
308
|
21 |
|
public function setDbchar($value) |
309
|
|
|
{ |
310
|
21 |
|
$this->dbchar = $value; |
311
|
21 |
|
} |
312
|
|
|
|
313
|
|
|
/** |
314
|
|
|
* Sets dbport attribute |
315
|
|
|
* |
316
|
|
|
* @param integer $value |
317
|
|
|
* |
318
|
|
|
* @return null |
319
|
|
|
*/ |
320
|
21 |
|
public function setDbport($value) |
321
|
|
|
{ |
322
|
21 |
|
$this->dbport = $value; |
323
|
21 |
|
} |
324
|
|
|
|
325
|
|
|
/** |
326
|
|
|
* Sets autocommit behavior |
327
|
|
|
* |
328
|
|
|
* @param boolean $value |
329
|
|
|
* |
330
|
|
|
* @return null |
331
|
|
|
*/ |
332
|
4 |
|
public function autocommit($value) |
333
|
|
|
{ |
334
|
4 |
|
if ($this->transac_mode) { |
335
|
|
|
throw new \LogicException("You cannot change autocommit behavior during a transaction"); |
336
|
|
|
} |
337
|
|
|
|
338
|
4 |
|
$this->autocommit = $value; |
339
|
4 |
|
} |
340
|
|
|
|
341
|
|
|
/** |
342
|
|
|
* Driver Constructor |
343
|
|
|
* |
344
|
|
|
* All modifiable attributes (i.e. with setter method) can be passed as key |
345
|
|
|
* |
346
|
|
|
* @param array $options |
347
|
|
|
*/ |
348
|
21 |
|
public function __construct($options) |
349
|
|
|
{ |
350
|
21 |
|
foreach ($options as $option => $value) { |
351
|
21 |
|
if (property_exists(__CLASS__, strtolower($option)) && method_exists($this, 'set'.$option)) { |
352
|
21 |
|
$this->{'set'.$option}($value); |
353
|
|
|
} |
354
|
|
|
} |
355
|
21 |
|
} |
356
|
|
|
|
357
|
|
|
/** |
358
|
|
|
* Returns true if there is a stablished connection |
359
|
|
|
* |
360
|
|
|
* @return boolean |
361
|
|
|
*/ |
362
|
13 |
|
public function isConnected() |
363
|
|
|
{ |
364
|
13 |
|
return (is_resource($this->dbconn) || is_object($this->dbconn)); |
365
|
|
|
} |
366
|
|
|
|
367
|
|
|
/** |
368
|
|
|
* Abstract connect |
369
|
|
|
* |
370
|
|
|
* @throws RuntimeException |
371
|
|
|
* |
372
|
|
|
* @return resource|object |
373
|
|
|
*/ |
374
|
|
|
abstract public function connect(); |
375
|
|
|
|
376
|
|
|
/** |
377
|
|
|
* Abstract execute |
378
|
|
|
* |
379
|
|
|
* @param string $sql |
380
|
|
|
* @param array $params to bind |
381
|
|
|
* |
382
|
|
|
* @throws RuntimeException |
383
|
|
|
* |
384
|
|
|
* @return resource|object |
385
|
|
|
*/ |
386
|
|
|
abstract public function execute($sql, array $params = []); |
387
|
|
|
|
388
|
|
|
/** |
389
|
|
|
* Reconnects to the database |
390
|
|
|
* |
391
|
|
|
* @throws LogicException |
392
|
|
|
* |
393
|
|
|
* @return resource|object |
394
|
|
|
*/ |
395
|
2 |
|
public function reconnect() |
396
|
|
|
{ |
397
|
2 |
|
if (!$this->isConnected()) { |
398
|
1 |
|
throw new \LogicException("Connection was not established"); |
399
|
|
|
} |
400
|
|
|
|
401
|
1 |
|
$this->disconnect(); |
402
|
|
|
|
403
|
1 |
|
return $this->connect(); |
404
|
|
|
} |
405
|
|
|
|
406
|
|
|
/** |
407
|
|
|
* Commit definition |
408
|
|
|
* |
409
|
|
|
* @return boolean |
410
|
|
|
*/ |
411
|
|
|
abstract public function commit(); |
412
|
|
|
|
413
|
|
|
/** |
414
|
|
|
* Rollback definition |
415
|
|
|
* |
416
|
|
|
* @return boolean |
417
|
|
|
*/ |
418
|
|
|
abstract public function rollback(); |
419
|
|
|
|
420
|
|
|
/** |
421
|
|
|
* Closes the connection |
422
|
|
|
* |
423
|
|
|
* @throws LogicException |
424
|
|
|
* |
425
|
|
|
* @return boolean |
426
|
|
|
*/ |
427
|
3 |
|
public function disconnect() |
428
|
|
|
{ |
429
|
3 |
|
if (!$this->isConnected()) { |
430
|
1 |
|
throw new \LogicException("Connection was not established"); |
431
|
|
|
} |
432
|
2 |
|
} |
433
|
|
|
|
434
|
|
|
/** |
435
|
|
|
* Defines the start point of a transaction |
436
|
|
|
* |
437
|
|
|
* @throws LogicException if transaction was already started |
438
|
|
|
* |
439
|
|
|
* @return null |
440
|
|
|
*/ |
441
|
2 |
|
public function beginTransaction() |
442
|
|
|
{ |
443
|
2 |
|
if (!$this->isConnected()) { |
444
|
|
|
$this->connect(); |
445
|
|
|
} |
446
|
|
|
|
447
|
2 |
|
if ($this->transac_mode) { |
448
|
|
|
throw new \LogicException($this->standardErrors[Errno::DB_TRANSACTION_STARTED]); |
449
|
|
|
} |
450
|
|
|
|
451
|
2 |
|
$this->transac_mode = true; |
452
|
2 |
|
} |
453
|
|
|
|
454
|
|
|
/** |
455
|
|
|
* Defines the end point of a transaction |
456
|
|
|
* |
457
|
|
|
* @throws LogicException if transaction has not been started or it's empty |
458
|
|
|
* |
459
|
|
|
* @return null |
460
|
|
|
*/ |
461
|
2 |
|
public function endTransaction() |
462
|
|
|
{ |
463
|
2 |
|
if (!$this->transac_mode) { |
464
|
|
|
throw new \LogicException($this->standardErrors[Errno::DB_TRANSACTION_NOT_STARTED]); |
465
|
|
|
} |
466
|
|
|
|
467
|
2 |
|
if (is_null($this->transac_result)) { |
|
|
|
|
468
|
|
|
throw new \LogicException($this->standardErrors[Errno::DB_TRANSACTION_EMPTY]); |
469
|
|
|
} |
470
|
|
|
|
471
|
2 |
|
if ($this->transac_result) { |
472
|
2 |
|
$this->commit(); |
473
|
|
|
} else { |
474
|
|
|
$this->rollback(); |
475
|
|
|
} |
476
|
|
|
|
477
|
2 |
|
$this->result = $this->transac_result; |
478
|
|
|
|
479
|
2 |
|
$this->transac_result = null; |
480
|
2 |
|
$this->transac_mode = false; |
481
|
2 |
|
} |
482
|
|
|
|
483
|
|
|
/** |
484
|
|
|
* Abstract result set |
485
|
|
|
* |
486
|
|
|
* By default all Drivers must be implement toArray() function. |
487
|
|
|
* The toArray() method must take the latest result from an execute statement |
488
|
|
|
* and convert it to an array. To get this array getArrayResult() has been implemented. |
489
|
|
|
* |
490
|
|
|
* @throws LogicException if execute() was not executed before this |
491
|
|
|
* |
492
|
|
|
* @return array |
493
|
|
|
*/ |
494
|
|
|
abstract protected function toArray(); |
495
|
|
|
|
496
|
|
|
/** |
497
|
|
|
* Excecutes multiple statements as transaction |
498
|
|
|
* |
499
|
|
|
* @param array $querys |
500
|
|
|
* |
501
|
|
|
* @return null |
502
|
|
|
*/ |
503
|
|
|
public function transaction(array $querys) |
504
|
|
|
{ |
505
|
|
|
$this->beginTransaction(); |
506
|
|
|
|
507
|
|
|
foreach ($querys as $sql) { |
508
|
|
|
$this->execute($sql); |
509
|
|
|
} |
510
|
|
|
|
511
|
|
|
$this->endTransaction(); |
512
|
|
|
} |
513
|
|
|
} |
514
|
|
|
|