1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ByJG\MicroOrm; |
4
|
|
|
|
5
|
|
|
use ByJG\AnyDataset\DbDriverInterface; |
6
|
|
|
use ByJG\AnyDataset\Factory; |
7
|
|
|
use ByJG\MicroOrm\Exception\OrmBeforeInvalidException; |
8
|
|
|
use ByJG\MicroOrm\Exception\OrmInvalidFieldsException; |
9
|
|
|
use ByJG\MicroOrm\Exception\TransactionException; |
10
|
|
|
use ByJG\Serializer\BinderObject; |
11
|
|
|
use ByJG\Util\Uri; |
12
|
|
|
|
13
|
|
|
class ConnectionManager |
14
|
|
|
{ |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var DbDriverInterface[] |
18
|
|
|
*/ |
19
|
|
|
protected static $connectionList = []; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var bool |
23
|
|
|
*/ |
24
|
|
|
protected static $transaction = false; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param $uriString |
28
|
|
|
* @return \ByJG\AnyDataset\DbDriverInterface |
29
|
|
|
*/ |
30
|
|
|
public function addConnection($uriString) |
31
|
|
|
{ |
32
|
|
|
if (!isset(self::$connectionList[$uriString])) { |
33
|
|
|
self::$connectionList[$uriString] = Factory::getDbRelationalInstance($uriString); |
34
|
|
|
|
35
|
|
|
if (self::$transaction) { |
36
|
|
|
self::$connectionList[$uriString]->beginTransaction(); |
37
|
|
|
} |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
return self::$connectionList[$uriString]; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function beginTransaction() |
44
|
|
|
{ |
45
|
|
|
if (self::$transaction) { |
46
|
|
|
throw new TransactionException("Transaction Already Started"); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
self::$transaction = true; |
50
|
|
|
foreach (self::$connectionList as $dbDriver) { |
51
|
|
|
$dbDriver->beginTransaction(); |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
|
56
|
|
|
public function commitTransaction() |
57
|
|
|
{ |
58
|
|
|
if (!self::$transaction) { |
59
|
|
|
throw new TransactionException("There is no Active Transaction"); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
self::$transaction = false; |
63
|
|
|
foreach (self::$connectionList as $dbDriver) { |
64
|
|
|
$dbDriver->commitTransaction(); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
|
69
|
|
|
public function rollbackTransaction() |
70
|
|
|
{ |
71
|
|
|
if (!self::$transaction) { |
72
|
|
|
throw new TransactionException("There is no Active Transaction"); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
self::$transaction = false; |
76
|
|
|
foreach (self::$connectionList as $dbDriver) { |
77
|
|
|
$dbDriver->rollbackTransaction(); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
|
82
|
|
|
} |
83
|
|
|
|