@@ -24,31 +24,31 @@ discard block |
||
24 | 24 | * @return string |
25 | 25 | */ |
26 | 26 | public function quote($value): string { |
27 | - if(is_null($value)) { |
|
27 | + if (is_null($value)) { |
|
28 | 28 | return 'NULL'; |
29 | 29 | } |
30 | 30 | |
31 | - if(is_bool($value)) { |
|
31 | + if (is_bool($value)) { |
|
32 | 32 | return $value ? '1' : '0'; |
33 | 33 | } |
34 | 34 | |
35 | - if(is_array($value)) { |
|
35 | + if (is_array($value)) { |
|
36 | 36 | return implode(', ', array_map([$this, __FUNCTION__], $value)); |
37 | 37 | } |
38 | 38 | |
39 | - if($value instanceof DBExpr) { |
|
39 | + if ($value instanceof DBExpr) { |
|
40 | 40 | return $value->getExpression(); |
41 | 41 | } |
42 | 42 | |
43 | - if($value instanceof Select) { |
|
43 | + if ($value instanceof Select) { |
|
44 | 44 | return sprintf('(%s)', (string) $value); |
45 | 45 | } |
46 | 46 | |
47 | - if(is_int($value) || is_float($value)) { |
|
47 | + if (is_int($value) || is_float($value)) { |
|
48 | 48 | return (string) $value; |
49 | 49 | } |
50 | 50 | |
51 | - if($value instanceof DateTimeInterface) { |
|
51 | + if ($value instanceof DateTimeInterface) { |
|
52 | 52 | $value = date_create_immutable($value->format('c'))->setTimezone($this->timeZone)->format('Y-m-d H:i:s'); |
53 | 53 | } |
54 | 54 | |
@@ -62,12 +62,12 @@ discard block |
||
62 | 62 | */ |
63 | 63 | public function quoteExpression(string $expression, array $arguments = []): string { |
64 | 64 | $index = -1; |
65 | - $func = function () use ($arguments, &$index) { |
|
65 | + $func = function() use ($arguments, &$index) { |
|
66 | 66 | $index++; |
67 | - if(array_key_exists($index, $arguments)) { |
|
67 | + if (array_key_exists($index, $arguments)) { |
|
68 | 68 | $argument = $arguments[$index]; |
69 | 69 | $value = $this->quote($argument); |
70 | - } elseif(count($arguments) > 0) { |
|
70 | + } elseif (count($arguments) > 0) { |
|
71 | 71 | $args = $arguments; |
72 | 72 | $value = array_pop($args); |
73 | 73 | $value = $this->quote($value); |
@@ -50,7 +50,7 @@ discard block |
||
50 | 50 | * @param array<string, mixed> $options |
51 | 51 | */ |
52 | 52 | public function __construct(PDO $pdo, array $options = []) { |
53 | - if($pdo->getAttribute(PDO::ATTR_ERRMODE) === PDO::ERRMODE_SILENT) { |
|
53 | + if ($pdo->getAttribute(PDO::ATTR_ERRMODE) === PDO::ERRMODE_SILENT) { |
|
54 | 54 | $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); |
55 | 55 | } |
56 | 56 | $this->pdo = $pdo; |
@@ -65,7 +65,7 @@ discard block |
||
65 | 65 | ]; |
66 | 66 | $this->options = array_merge($defaultOptions, $options); |
67 | 67 | $this->options['timezone'] = $this->options['timezone'] ?? date_default_timezone_get(); |
68 | - if(!($this->options['timezone'] instanceof DateTimeZone)) { |
|
68 | + if (!($this->options['timezone'] instanceof DateTimeZone)) { |
|
69 | 69 | $this->options['timezone'] = new DateTimeZone((string) $this->options['timezone']); |
70 | 70 | } |
71 | 71 | $this->quoter = new MySQLQuoter($pdo, $this->options['timezone']); |
@@ -89,7 +89,7 @@ discard block |
||
89 | 89 | * @return VirtualTables |
90 | 90 | */ |
91 | 91 | public function getVirtualTables(): VirtualTables { |
92 | - if($this->virtualTables === null) { |
|
92 | + if ($this->virtualTables === null) { |
|
93 | 93 | $this->virtualTables = new VirtualTables(); |
94 | 94 | } |
95 | 95 | return $this->virtualTables; |
@@ -101,7 +101,7 @@ discard block |
||
101 | 101 | */ |
102 | 102 | public function query(string $query) { |
103 | 103 | return $this->getQueryLoggers()->logRegion($query, function() use ($query) { |
104 | - return $this->buildQueryStatement($query, function ($query) { |
|
104 | + return $this->buildQueryStatement($query, function($query) { |
|
105 | 105 | return $this->pdo->query($query); |
106 | 106 | }); |
107 | 107 | }); |
@@ -112,7 +112,7 @@ discard block |
||
112 | 112 | * @return QueryStatement |
113 | 113 | */ |
114 | 114 | public function prepare(string $query) { |
115 | - return $this->buildQueryStatement((string) $query, function ($query) { |
|
115 | + return $this->buildQueryStatement((string) $query, function($query) { |
|
116 | 116 | return $this->pdo->prepare($query); |
117 | 117 | }); |
118 | 118 | } |
@@ -124,11 +124,11 @@ discard block |
||
124 | 124 | */ |
125 | 125 | public function exec(string $query, array $params = []): int { |
126 | 126 | return $this->getQueryLoggers()->logRegion($query, function() use ($query, $params) { |
127 | - return $this->exceptionHandler(function () use ($query, $params) { |
|
127 | + return $this->exceptionHandler(function() use ($query, $params) { |
|
128 | 128 | $stmt = $this->pdo->prepare($query); |
129 | 129 | $timer = microtime(true); |
130 | 130 | $stmt->execute($params); |
131 | - $this->queryLoggers->log($query, microtime(true) - $timer); |
|
131 | + $this->queryLoggers->log($query, microtime(true)-$timer); |
|
132 | 132 | $result = $stmt->rowCount(); |
133 | 133 | $stmt->closeCursor(); |
134 | 134 | return $result; |
@@ -150,19 +150,19 @@ discard block |
||
150 | 150 | */ |
151 | 151 | public function getTableFields(string $table): array { |
152 | 152 | $fqTable = $this->select()->aliasReplacer()->replace($table); |
153 | - if(array_key_exists($fqTable, $this->tableFields)) { |
|
153 | + if (array_key_exists($fqTable, $this->tableFields)) { |
|
154 | 154 | return $this->tableFields[$fqTable]; |
155 | 155 | } |
156 | 156 | $query = "DESCRIBE {$fqTable}"; |
157 | 157 | return $this->getQueryLoggers()->logRegion($query, function() use ($query, $fqTable) { |
158 | - return $this->exceptionHandler(function () use ($query, $fqTable) { |
|
158 | + return $this->exceptionHandler(function() use ($query, $fqTable) { |
|
159 | 159 | $stmt = $this->pdo->query($query); |
160 | 160 | try { |
161 | - if($stmt === false) { |
|
161 | + if ($stmt === false) { |
|
162 | 162 | throw new RuntimeException('Invalid return type'); |
163 | 163 | } |
164 | 164 | $rows = $stmt->fetchAll(PDO::FETCH_ASSOC); |
165 | - $this->tableFields[$fqTable] = array_map(static function ($row) { return $row['Field']; }, $rows ?: []); |
|
165 | + $this->tableFields[$fqTable] = array_map(static function($row) { return $row['Field']; }, $rows ?: []); |
|
166 | 166 | return $this->tableFields[$fqTable]; |
167 | 167 | } finally { |
168 | 168 | try { |
@@ -206,7 +206,7 @@ discard block |
||
206 | 206 | $select = array_key_exists('select-factory', $this->options) |
207 | 207 | ? call_user_func($this->options['select-factory'], $this, $this->options['select-options']) |
208 | 208 | : new MySQL\MySQLRunnableSelect($this, $this->options['select-options']); |
209 | - if($fields !== null) { |
|
209 | + if ($fields !== null) { |
|
210 | 210 | $select->fields($fields); |
211 | 211 | } |
212 | 212 | return $select; |
@@ -220,7 +220,7 @@ discard block |
||
220 | 220 | $insert = array_key_exists('insert-factory', $this->options) |
221 | 221 | ? call_user_func($this->options['insert-factory'], $this, $this->options['insert-options']) |
222 | 222 | : new Builder\RunnableInsert($this, $this->options['insert-options']); |
223 | - if($fields !== null) { |
|
223 | + if ($fields !== null) { |
|
224 | 224 | $insert->addAll($fields); |
225 | 225 | } |
226 | 226 | return $insert; |
@@ -234,7 +234,7 @@ discard block |
||
234 | 234 | $update = array_key_exists('update-factory', $this->options) |
235 | 235 | ? call_user_func($this->options['update-factory'], $this, $this->options['update-options']) |
236 | 236 | : new Builder\RunnableUpdate($this, $this->options['update-options']); |
237 | - if($fields !== null) { |
|
237 | + if ($fields !== null) { |
|
238 | 238 | $update->setAll($fields); |
239 | 239 | } |
240 | 240 | return $update; |
@@ -253,8 +253,8 @@ discard block |
||
253 | 253 | * @return $this |
254 | 254 | */ |
255 | 255 | public function transactionStart() { |
256 | - if($this->transactionLevel === 0) { |
|
257 | - if($this->pdo->inTransaction()) { |
|
256 | + if ($this->transactionLevel === 0) { |
|
257 | + if ($this->pdo->inTransaction()) { |
|
258 | 258 | $this->outerTransaction = true; |
259 | 259 | } else { |
260 | 260 | $this->pdo->beginTransaction(); |
@@ -268,7 +268,7 @@ discard block |
||
268 | 268 | * @return $this |
269 | 269 | */ |
270 | 270 | public function transactionCommit() { |
271 | - return $this->transactionEnd(function () { |
|
271 | + return $this->transactionEnd(function() { |
|
272 | 272 | $this->pdo->commit(); |
273 | 273 | }); |
274 | 274 | } |
@@ -277,7 +277,7 @@ discard block |
||
277 | 277 | * @return $this |
278 | 278 | */ |
279 | 279 | public function transactionRollback() { |
280 | - return $this->transactionEnd(function () { |
|
280 | + return $this->transactionEnd(function() { |
|
281 | 281 | $this->pdo->rollBack(); |
282 | 282 | }); |
283 | 283 | } |
@@ -288,7 +288,7 @@ discard block |
||
288 | 288 | * @return T |
289 | 289 | */ |
290 | 290 | public function dryRun(callable $callback) { |
291 | - if(!$this->pdo->inTransaction()) { |
|
291 | + if (!$this->pdo->inTransaction()) { |
|
292 | 292 | $this->transactionStart(); |
293 | 293 | try { |
294 | 294 | return $callback($this); |
@@ -313,7 +313,7 @@ discard block |
||
313 | 313 | * @throws Throwable |
314 | 314 | */ |
315 | 315 | public function transaction(callable $callback) { |
316 | - if(!$this->pdo->inTransaction()) { |
|
316 | + if (!$this->pdo->inTransaction()) { |
|
317 | 317 | $this->transactionStart(); |
318 | 318 | try { |
319 | 319 | $result = $callback($this); |
@@ -321,7 +321,7 @@ discard block |
||
321 | 321 | return $result; |
322 | 322 | } catch (Throwable $e) { |
323 | 323 | // @phpstan-ignore-next-line; If condition is always false as it is not. |
324 | - if($this->pdo->inTransaction()) { |
|
324 | + if ($this->pdo->inTransaction()) { |
|
325 | 325 | $this->transactionRollback(); |
326 | 326 | } |
327 | 327 | throw $e; |
@@ -345,11 +345,11 @@ discard block |
||
345 | 345 | */ |
346 | 346 | private function transactionEnd($fn): self { |
347 | 347 | $this->transactionLevel--; |
348 | - if($this->transactionLevel < 0) { |
|
348 | + if ($this->transactionLevel < 0) { |
|
349 | 349 | throw new RuntimeException("Transaction-Nesting-Problem: Trying to invoke commit on a already closed transaction"); |
350 | 350 | } |
351 | - if($this->transactionLevel < 1) { |
|
352 | - if($this->outerTransaction) { |
|
351 | + if ($this->transactionLevel < 1) { |
|
352 | + if ($this->outerTransaction) { |
|
353 | 353 | $this->outerTransaction = false; |
354 | 354 | } else { |
355 | 355 | $fn(); |
@@ -366,7 +366,7 @@ discard block |
||
366 | 366 | */ |
367 | 367 | private function buildQueryStatement(string $query, callable $fn): QueryStatement { |
368 | 368 | $stmt = $fn($query); |
369 | - if(!$stmt) { |
|
369 | + if (!$stmt) { |
|
370 | 370 | throw new RuntimeException("Could not execute statement:\n{$query}"); |
371 | 371 | } |
372 | 372 | return new QueryStatement($stmt, $query, $this->exceptionInterpreter, $this->queryLoggers); |