|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Ubiquity\db\traits; |
|
4
|
|
|
|
|
5
|
|
|
use Ubiquity\exceptions\DBException; |
|
6
|
|
|
use Ubiquity\log\Logger; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Manages database transactions. |
|
10
|
|
|
* Ubiquity\db\traits$DatabaseTransactionsTrait |
|
11
|
|
|
* This class is part of Ubiquity |
|
12
|
|
|
* |
|
13
|
|
|
* @author jcheron <[email protected]> |
|
14
|
|
|
* @version 1.1.1 |
|
15
|
|
|
* @property \Ubiquity\db\providers\AbstractDbWrapper $wrapperObject |
|
16
|
|
|
* @property string $dbType |
|
17
|
|
|
*/ |
|
18
|
|
|
trait DatabaseTransactionsTrait { |
|
19
|
|
|
protected $transactionLevel = 0; |
|
20
|
|
|
|
|
21
|
8 |
|
protected function nestable() { |
|
22
|
8 |
|
return $this->wrapperObject->nestable (); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Initiates a transaction |
|
27
|
|
|
* |
|
28
|
|
|
* @return boolean true on success or false on failure |
|
29
|
|
|
*/ |
|
30
|
19 |
|
public function beginTransaction() { |
|
31
|
19 |
|
if ($this->transactionLevel == 0 || ! $this->nestable ()) { |
|
32
|
19 |
|
$ret = $this->wrapperObject->beginTransaction (); |
|
33
|
19 |
|
Logger::info ( 'Transactions', 'Start transaction', 'beginTransaction' ); |
|
34
|
19 |
|
$this->transactionLevel ++; |
|
35
|
19 |
|
return $ret; |
|
36
|
|
|
} |
|
37
|
8 |
|
$this->wrapperObject->savePoint ( $this->transactionLevel ); |
|
38
|
8 |
|
Logger::info ( 'Transactions', 'Savepoint level', 'beginTransaction', $this->transactionLevel ); |
|
39
|
8 |
|
$this->transactionLevel ++; |
|
40
|
8 |
|
return true; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Commits a transaction |
|
45
|
|
|
* |
|
46
|
|
|
* @return boolean true on success or false on failure |
|
47
|
|
|
*/ |
|
48
|
11 |
|
public function commit() { |
|
49
|
11 |
|
$this->transactionLevel --; |
|
50
|
11 |
|
if ($this->transactionLevel == 0 || ! $this->nestable ()) { |
|
51
|
11 |
|
Logger::info ( 'Transactions', 'Commit transaction', 'commit' ); |
|
52
|
11 |
|
return $this->wrapperObject->commit (); |
|
53
|
|
|
} |
|
54
|
4 |
|
$this->wrapperObject->releasePoint ( $this->transactionLevel ); |
|
55
|
4 |
|
Logger::info ( 'Transactions', 'Release savepoint level', 'commit', $this->transactionLevel ); |
|
56
|
4 |
|
return true; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Commits nested transactions up to level $transactionLevel |
|
61
|
|
|
* |
|
62
|
|
|
* @param int $transactionLevel |
|
63
|
|
|
* @return boolean true on success or false on failure |
|
64
|
|
|
*/ |
|
65
|
2 |
|
public function commitToLevel($transactionLevel) { |
|
66
|
2 |
|
$res = true; |
|
67
|
2 |
|
while ( $res && $this->transactionLevel > $transactionLevel ) { |
|
68
|
2 |
|
$res = $this->commit (); |
|
69
|
|
|
} |
|
70
|
2 |
|
return $res; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Commits all nested transactions (up to level 0) |
|
75
|
|
|
* |
|
76
|
|
|
* @return boolean true on success or false on failure |
|
77
|
|
|
*/ |
|
78
|
2 |
|
public function commitAll() { |
|
79
|
2 |
|
return $this->commitToLevel ( 0 ); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Rolls back a transaction |
|
84
|
|
|
* |
|
85
|
|
|
* @return boolean true on success or false on failure |
|
86
|
|
|
*/ |
|
87
|
6 |
|
public function rollBack() { |
|
88
|
6 |
|
$this->transactionLevel --; |
|
89
|
6 |
|
if ($this->transactionLevel == 0 || ! $this->nestable ()) { |
|
90
|
6 |
|
Logger::info ( 'Transactions', 'Rollback transaction', 'rollBack' ); |
|
91
|
6 |
|
return $this->wrapperObject->rollBack (); |
|
92
|
|
|
} |
|
93
|
4 |
|
$this->wrapperObject->rollbackPoint ( $this->transactionLevel ); |
|
94
|
4 |
|
Logger::info ( 'Transactions', 'Rollback to savepoint level', 'rollBack', $this->transactionLevel ); |
|
95
|
4 |
|
return true; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Rolls back nested transactions up to level $transactionLevel |
|
100
|
|
|
* |
|
101
|
|
|
* @param int $transactionLevel |
|
102
|
|
|
* @return boolean true on success or false on failure |
|
103
|
|
|
*/ |
|
104
|
2 |
|
public function rollBackToLevel($transactionLevel) { |
|
105
|
2 |
|
$res = true; |
|
106
|
2 |
|
while ( $res && $this->transactionLevel > $transactionLevel ) { |
|
107
|
2 |
|
$res = $this->rollBack (); |
|
108
|
|
|
} |
|
109
|
2 |
|
return $res; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Rolls back all nested transactions (up to level 0) |
|
114
|
|
|
* |
|
115
|
|
|
* @return boolean true on success or false on failure |
|
116
|
|
|
*/ |
|
117
|
2 |
|
public function rollBackAll() { |
|
118
|
2 |
|
return $this->rollBackToLevel ( 0 ); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Checks if inside a transaction |
|
123
|
|
|
* |
|
124
|
|
|
* @return boolean |
|
125
|
|
|
*/ |
|
126
|
|
|
public function inTransaction() { |
|
127
|
|
|
return $this->wrapperObject->inTransaction (); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* Call a callback with an array of parameters in a transaction |
|
132
|
|
|
* |
|
133
|
|
|
* @param callable $callback |
|
134
|
|
|
* @param mixed ...$parameters |
|
135
|
|
|
* @throws \Exception |
|
136
|
|
|
* @return mixed |
|
137
|
|
|
*/ |
|
138
|
|
|
public function callInTransaction($callback, ...$parameters) { |
|
139
|
|
|
if ($this->beginTransaction ()) { |
|
140
|
|
|
try { |
|
141
|
|
|
$ret = call_user_func_array ( $callback, $parameters ); |
|
142
|
|
|
} catch ( \Exception $e ) { |
|
143
|
|
|
$this->wrapperObject->rollBack (); |
|
144
|
|
|
throw $e; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
if ($ret) { |
|
148
|
|
|
if (! $this->commit ()) { |
|
149
|
|
|
throw new DBException ( 'Transaction was not committed.' ); |
|
150
|
|
|
} |
|
151
|
|
|
} else { |
|
152
|
|
|
$this->rollBack (); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
return $ret; |
|
156
|
|
|
} |
|
157
|
|
|
throw new DBException ( 'Transaction was not started.' ); |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* Sets the isolation level for transactions. |
|
162
|
|
|
* @param $isolationLevel |
|
163
|
|
|
* @return mixed|void |
|
164
|
|
|
* @throws DBException |
|
165
|
|
|
*/ |
|
166
|
|
|
public function setIsolationLevel($isolationLevel) { |
|
167
|
|
|
return $this->wrapperObject->setIsolationLevel($isolationLevel); |
|
168
|
|
|
} |
|
169
|
|
|
} |
|
170
|
|
|
|