This repository has been archived by the owner on Sep 20, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Dal.php
569 lines (503 loc) · 14.5 KB
/
Dal.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
<?php
/**
* Hoa
*
*
* @license
*
* New BSD License
*
* Copyright © 2007-2017, Hoa community. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the Hoa nor the names of its contributors may be
* used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
namespace Hoa\Database;
use Hoa\Consistency;
use Hoa\Event;
use Hoa\Zformat;
/**
* Class \Hoa\Database\Dal.
*
* The higher class of the Database Abstract Layer. It wrappes all DAL.
*
* @copyright Copyright © 2007-2017 Hoa community
* @license New BSD License
*/
class Dal implements Zformat\Parameterizable, Event\Source
{
/**
* Abstract layer: DBA.
*
* @const string
*/
const DBA = 'Dba';
/**
* Abstract layer: DBX.
*
* @const string
*/
const DBX = 'Dbx';
/**
* Abstract layer: Odbc.
*
* @const string
*/
const ODBC = 'Odbc';
/**
* Abstract layer: PDO.
*
* @const string
*/
const PDO = 'Pdo';
/**
* Multiton.
*
* @var array
*/
private static $_instance = [];
/**
* Current singleton ID.
*
* @var string
*/
private static $_id = null;
/**
* Current ID.
*
* @var string
*/
protected $__id = null;
/**
* The layer instance.
*
* @var \Hoa\Database\IDal\Wrapper
*/
protected $_layer = null;
/**
* Parameter of \Hoa\Database\Dal.
*
* @var \Hoa\Zformat\Parameter
*/
protected static $_parameters = null;
/**
* The layer connection parameter.
*
* @var array
*/
protected $_connectionParameters = [];
/**
* Create a DAL instance, representing a connection to a database.
* The constructor is private to make a multiton.
*
* @param array $connectionParameters The layer connection parameter.
*/
private function __construct(array $connectionParameters)
{
$this->_connectionParameters = $connectionParameters;
$id = $this->__id = self::$_id;
$event = 'hoa://Event/Database/' . $id;
Event::register($event . ':opened', $this);
Event::register($event . ':closed', $this);
return;
}
/**
* Initialize parameters.
*
* @param array $parameters Parameters.
* @return void
*/
public static function initializeParameters(array $parameters = [])
{
self::$_parameters = new Zformat\Parameter(
__CLASS__,
[],
[
/**
* Example:
* 'connection.list.default.dal' => Dal::PDO,
* 'connection.list.default.dsn' => 'sqlite:hoa://Data/Variable/Database/Foo.sqlite',
* 'connection.list.default.username' => '',
* 'connection.list.default.password' => '',
* 'connection.list.default.options' => null,
*/
'connection.autoload' => null // or connection ID, e.g. 'default'.
]
);
self::$_parameters->setParameters($parameters);
return;
}
/**
* Make a multiton on the $id.
*
* @param string $id The instance ID.
* @param string $dalName The database abstract layer name.
* @param string $dsn The DSN of database.
* @param string $username The username to connect to database.
* @param string $password The password to connect to database.
* @param array $driverOptions The driver options.
* @return \Hoa\Database\Dal
* @throws \Hoa\Database\Exception
*/
public static function getInstance(
$id,
$dalName = null,
$dsn = null,
$username = null,
$password = null,
array $driverOptions = []
) {
if (null === self::$_parameters) {
self::initializeParameters();
}
self::$_id = $id;
if (isset(self::$_instance[$id])) {
return self::$_instance[$id];
}
if (null === $dalName &&
null === $dsn &&
null === $username &&
null === $password &&
empty($driverOptions)) {
$list = self::$_parameters->unlinearizeBranch('connection.list');
if (!isset($list[$id])) {
throw new Exception(
'Connection ID %s does not exist in the connection list.',
0,
$id
);
}
$handle = $list[$id];
$dalName = @$handle['dal'] ?: 'Undefined';
$dsn = @$handle['dsn'] ?: '';
$username = @$handle['username'] ?: '';
$password = @$handle['password'] ?: '';
$driverOptions = @$handle['options'] ?: [];
}
return self::$_instance[$id] = new self([
$dalName,
$dsn,
$username,
$password,
$driverOptions
]);
}
/**
* Get the last instance of a DAL, i.e. the last used singleton.
* If no instance was set but if the connection.autoload parameter is set,
* then we auto-connect (autoload) a connection.
*
* @return \Hoa\Database\IDal\Wrapper
* @throws \Hoa\Database\Exception
*/
public static function getLastInstance()
{
if (null === self::$_parameters) {
self::initializeParameters();
}
if (null === self::$_id) {
$autoload = self::$_parameters->getFormattedParameter(
'connection.autoload'
);
if (null !== $autoload) {
self::getInstance($autoload);
}
}
if (null === self::$_id) {
throw new Exception(
'No instance was set, cannot return the last instance.',
1
);
}
return self::$_instance[self::$_id];
}
/**
* Get parameters.
*
* @return \Hoa\Zformat\Parameter
*/
public function getParameters()
{
return self::$_parameters;
}
/**
* Open a connection to the database.
*
* @return void
*/
private function open()
{
list(
$dalName,
$dsn,
$username,
$password,
$driverOptions
) = $this->_connectionParameters;
// Please see https://bugs.php.net/55154.
if (0 !== preg_match('#^sqlite:(.+)$#i', $dsn, $matches)) {
$dsn = 'sqlite:' . resolve($matches[1]);
}
$this->setDal(
Consistency\Autoloader::dnew(
'Hoa\Database\Layer\\' . $dalName,
[$dsn, $username, $password, $driverOptions]
)
);
$id = $this->getId();
Event::notify(
'hoa://Event/Database/' . $id . ':opened',
$this,
new Event\Bucket([
'id' => $id,
'dsn' => $dsn,
'username' => $username,
'driverOptions' => $driverOptions
])
);
return;
}
/**
* Close connection to the database.
*
* @return bool
*/
public function close()
{
$id = $this->getId();
$event = 'hoa://Event/Database/' . $id;
$this->_layer = null;
self::$_id = null;
unset(self::$_instance[$id]);
Event::notify(
$event . ':closed',
$this,
new Event\Bucket(['id' => $id])
);
Event::unregister($event . ':opened');
Event::unregister($event . ':closed');
return true;
}
/**
* Set database abstract layer instance.
*
* @param \Hoa\Database\IDal\Wrapper $dal The DAL instance.
* @return \Hoa\Database\IDal\Wrapper
*/
protected function setDal(IDal\Wrapper $dal)
{
$old = $this->_layer;
$this->_layer = $dal;
return $old;
}
/**
* Get the database abstract layer instance.
*
* @return \Hoa\Database\IDal\Wrapper
*/
protected function getDal()
{
if (null === $this->_layer) {
$this->open();
}
return $this->_layer;
}
/**
* Initiate a transaction.
*
* @return bool
* @throws \Hoa\Database\Exception
*/
public function beginTransaction()
{
return $this->getDal()->beginTransaction();
}
/**
* Commit a transaction.
*
* @return bool
* @throws \Hoa\Database\Exception
*/
public function commit()
{
return $this->getDal()->commit();
}
/**
* Roll back a transaction.
*
* @return bool
* @throws \Hoa\Database\Exception
*/
public function rollBack()
{
return $this->getDal()->rollBack();
}
/**
* Return the ID of the last inserted row or sequence value.
*
* @param string $name Name of sequence object (needed for some
* driver).
* @return string
* @throws \Hoa\Database\Exception
*/
public function lastInsertId($name = null)
{
if (null === $name) {
return $this->getDal()->lastInsertId();
}
return $this->getDal()->lastInsertId($name);
}
/**
* Prepare a statement for execution and returns a statement object.
*
* @param string $statement This must be a valid SQL statement for the
* target database server.
* @param array $options Options to set attributes values for the
* layer statement.
* @return \Hoa\Database\DalStatement
* @throws \Hoa\Database\Exception
*/
public function prepare($statement, array $options = [])
{
return new DalStatement(
$this->getDal()->prepare(
$statement, $options
)
);
}
/**
* Quote a string for use in a query.
*
* @param string $string The string to be quoted.
* @param int $type Provide a data type hint for drivers that
* have alternate quoting styles.
* @return string
* @throws \Hoa\Database\Exception
*/
public function quote($string = null, $type = -1)
{
if ($type < 0) {
return $this->getDal()->quote($string);
}
return $this->getDal()->quote($string, $type);
}
/**
* Execute an SQL statement, returning a result set as a
* \Hoa\Database\DalStatement object.
*
* @param string $statement The SQL statement to prepare and execute.
* @return \Hoa\Database\DalStatement
* @throws \Hoa\Database\Exception
*/
public function query($statement)
{
return new DalStatement(
$this->getDal()->query($statement)
);
}
/**
* Fetch the SQLSTATE associated with the last operation on the database
* handle.
*
* @return string
* @throws \Hoa\Database\Exception
*/
public function errorCode()
{
return $this->getDal()->errorCode();
}
/**
* Fetch extends error information associated with the last operation on the
* database handle.
*
* @return array
* @throws \Hoa\Database\Exception
*/
public function errorInfo()
{
return $this->getDal()->errorInfo();
}
/**
* Return an array of available drivers.
*
* @return array
* @throws \Hoa\Database\Exception
*/
public function getAvailableDrivers()
{
return $this->getDal()->getAvailableDrivers();
}
/**
* Set attributes.
*
* @param array $attributes Attributes values.
* @return array
* @throws \Hoa\Database\Exception
*/
public function setAttributes(array $attributes)
{
return $this->getDal()->setAttributes($attributes);
}
/**
* Set a specific attribute.
*
* @param mixed $attribute Attribute name.
* @param mixed $value Attribute value.
* @return mixed
* @throws \Hoa\Database\Exception
*/
public function setAttribute($attribute, $value)
{
return $this->getDal()->setAttribute($attribute, $value);
}
/**
* Retrieve all database connection attributes.
*
* @return array
* @throws \Hoa\Database\Exception
*/
public function getAttributes()
{
return $this->getDal()->getAttributes();
}
/**
* Retrieve a database connection attribute.
*
* @param string $attribute Attribute name.
* @return mixed
* @throws \Hoa\Database\Exception
*/
public function getAttribute($attribute)
{
return $this->getDal()->getAttribute($attribute);
}
/**
* Get current ID.
*
* @return string
*/
public function getId()
{
return $this->__id;
}
}