1 | <?php |
||
21 | abstract class PDOConnection implements Connection, PDOExceptionMapper, Logger |
||
22 | { |
||
23 | /** |
||
24 | * @var PDO |
||
25 | */ |
||
26 | private $pdo; |
||
27 | |||
28 | /** |
||
29 | * @var TypeProvider |
||
30 | */ |
||
31 | private $types; |
||
32 | |||
33 | /** |
||
34 | * @var int number of nested calls to transact() |
||
35 | * |
||
36 | * @see transact() |
||
37 | */ |
||
38 | private $transaction_level = 0; |
||
39 | |||
40 | /** |
||
41 | * @var bool net result of nested calls to transact() |
||
42 | * |
||
43 | * @see transact() |
||
44 | */ |
||
45 | private $transaction_result; |
||
46 | |||
47 | /** |
||
48 | * @var Logger[] |
||
49 | */ |
||
50 | private $loggers = []; |
||
51 | |||
52 | /** |
||
53 | * To avoid duplicating dependencies, you should use DatabaseContainer::createPDOConnection() |
||
54 | * rather than calling this constructor directly. |
||
55 | * |
||
56 | * @param PDO $pdo |
||
57 | * @param TypeProvider $types |
||
58 | */ |
||
59 | 1 | public function __construct(PDO $pdo, TypeProvider $types) |
|
64 | |||
65 | /** |
||
66 | * @return PDO the internal PDO connection object |
||
67 | */ |
||
68 | public function getPDO() |
||
72 | |||
73 | /** |
||
74 | * @inheritdoc |
||
75 | */ |
||
76 | 1 | public function prepare(Statement $statement) |
|
102 | |||
103 | /** |
||
104 | * @inheritdoc |
||
105 | */ |
||
106 | 1 | public function fetch(Statement $statement, $batch_size = 1000) |
|
118 | |||
119 | /** |
||
120 | * @inheritdoc |
||
121 | */ |
||
122 | 1 | public function execute(Statement $statement) |
|
130 | |||
131 | /** |
||
132 | * @inheritdoc |
||
133 | */ |
||
134 | 1 | public function count(Countable $statement) |
|
138 | |||
139 | /** |
||
140 | * @inheritdoc |
||
141 | */ |
||
142 | 1 | public function transact(callable $func) |
|
143 | { |
||
144 | 1 | if ($this->transaction_level === 0) { |
|
145 | // starting a new stack of transactions - assume success: |
||
146 | 1 | $this->pdo->beginTransaction(); |
|
147 | 1 | $this->transaction_result = true; |
|
148 | } |
||
149 | |||
150 | 1 | $this->transaction_level += 1; |
|
151 | |||
152 | /** @var mixed $commit return type of $func isn't guaranteed, therefore mixed rather than bool */ |
||
153 | |||
154 | try { |
||
155 | 1 | $commit = call_user_func($func); |
|
156 | 1 | } catch (Throwable $exception) { |
|
|
|||
157 | 1 | $commit = false; // PHP 7+ |
|
158 | } catch (Exception $exception) { |
||
159 | $commit = false; // PHP < 7 (compatibility) |
||
160 | } |
||
161 | |||
162 | 1 | $this->transaction_result = ($commit === true) && $this->transaction_result; |
|
163 | |||
164 | 1 | $this->transaction_level -= 1; |
|
165 | |||
166 | 1 | if ($this->transaction_level === 0) { |
|
167 | 1 | if ($this->transaction_result === true) { |
|
168 | 1 | $this->pdo->commit(); |
|
169 | |||
170 | 1 | return true; // the net transaction is a success! |
|
171 | } else { |
||
172 | 1 | $this->pdo->rollBack(); |
|
173 | } |
||
174 | } |
||
175 | |||
176 | 1 | if (isset($exception)) { |
|
177 | // re-throw unhandled Exception: |
||
178 | 1 | throw $exception; |
|
179 | } |
||
180 | |||
181 | 1 | if ($this->transaction_level > 0 && $commit === false) { |
|
182 | 1 | throw new TransactionAbortedException("a nested call to transact() returned FALSE"); |
|
183 | } |
||
184 | |||
185 | 1 | if (! is_bool($commit)) { |
|
186 | 1 | throw new UnexpectedValueException("\$func must return TRUE (to commit) or FALSE (to roll back)"); |
|
187 | } |
||
188 | |||
189 | 1 | return $this->transaction_result; |
|
190 | } |
||
191 | |||
192 | /** |
||
193 | * Internally expand SQL placeholders (for array-types) |
||
194 | * |
||
195 | * @param string $sql SQL with placeholders |
||
196 | * @param array $params placeholder name/value pairs |
||
197 | * |
||
198 | * @return string SQL with expanded placeholders |
||
199 | */ |
||
200 | 1 | private function expandPlaceholders($sql, array $params) |
|
220 | |||
221 | /** |
||
222 | * @param string|null $sequence_name auto-sequence name (or NULL for e.g. MySQL which supports only one auto-key) |
||
223 | * |
||
224 | * @return int|string |
||
225 | */ |
||
226 | public function lastInsertId($sequence_name = null) |
||
234 | |||
235 | /** |
||
236 | * @inheritdoc |
||
237 | */ |
||
238 | 1 | public function addLogger(Logger $logger) |
|
242 | |||
243 | /** |
||
244 | * @inheritdoc |
||
245 | */ |
||
246 | 1 | public function logQuery($sql, $params, $time_msec) |
|
252 | } |
||
253 |