@@ -40,8 +40,8 @@ |
||
40 | 40 | */ |
41 | 41 | private function translateColumns(Database $db, $columns) { |
42 | 42 | $result = []; |
43 | - foreach($columns as $column) { |
|
44 | - if(!is_string($column)) { |
|
43 | + foreach ($columns as $column) { |
|
44 | + if (!is_string($column)) { |
|
45 | 45 | $column = sprintf('%s %s PATH \'%s\'', $column['name'], $column['type'], $column['jsonPath']); |
46 | 46 | } |
47 | 47 | $result[] = $column; |
@@ -26,19 +26,19 @@ discard block |
||
26 | 26 | public function __construct( |
27 | 27 | private string $expression, |
28 | 28 | array $data, |
29 | - string|array $keyPath, |
|
29 | + string | array $keyPath, |
|
30 | 30 | $validator = null, |
31 | 31 | $validationResultHandler = null |
32 | 32 | ) { |
33 | 33 | $this->keyPath = $this->buildKey($keyPath); |
34 | 34 | $this->value = RecursiveStructureAccess::recursiveGet($data, $this->keyPath, null); |
35 | 35 | $this->hasValue = is_scalar($this->value) ? trim((string) $this->value) !== '' : !empty($this->value); |
36 | - if($validator === null) { |
|
36 | + if ($validator === null) { |
|
37 | 37 | $validator = static fn() => true; |
38 | 38 | } |
39 | 39 | $this->validator = $validator; |
40 | - if($validationResultHandler === null) { |
|
41 | - $validationResultHandler = static function () {}; |
|
40 | + if ($validationResultHandler === null) { |
|
41 | + $validationResultHandler = static function() {}; |
|
42 | 42 | } |
43 | 43 | $this->validationResultHandler = $validationResultHandler; |
44 | 44 | } |
@@ -54,10 +54,10 @@ discard block |
||
54 | 54 | * @return bool |
55 | 55 | */ |
56 | 56 | public function isValid(): bool { |
57 | - if(!$this->hasValue) { |
|
57 | + if (!$this->hasValue) { |
|
58 | 58 | return false; |
59 | 59 | } |
60 | - if($this->validator !== null) { |
|
60 | + if ($this->validator !== null) { |
|
61 | 61 | $result = call_user_func($this->validator, $this->value); |
62 | 62 | call_user_func($this->validationResultHandler, $result, [ |
63 | 63 | 'value' => $this->value, |
@@ -80,10 +80,10 @@ discard block |
||
80 | 80 | * @return string[] |
81 | 81 | */ |
82 | 82 | private function buildKey($keyPath): array { |
83 | - if(is_string($keyPath)) { |
|
83 | + if (is_string($keyPath)) { |
|
84 | 84 | $keyPath = explode('.', $keyPath); |
85 | 85 | } |
86 | - if(!is_array($keyPath)) { |
|
86 | + if (!is_array($keyPath)) { |
|
87 | 87 | throw new RuntimeException('Invalid key'); |
88 | 88 | } |
89 | 89 | return $keyPath; |
@@ -5,13 +5,13 @@ discard block |
||
5 | 5 | use Kir\MySQL\Builder\Value\OptionalValue; |
6 | 6 | |
7 | 7 | trait LimitBuilder { |
8 | - private null|int|OptionalValue $limit = null; |
|
8 | + private null | int | OptionalValue $limit = null; |
|
9 | 9 | |
10 | 10 | /** |
11 | 11 | * @return null|int |
12 | 12 | */ |
13 | 13 | protected function getLimit(): ?int { |
14 | - if($this->limit instanceof OptionalValue) { |
|
14 | + if ($this->limit instanceof OptionalValue) { |
|
15 | 15 | return $this->limit->getValue(); |
16 | 16 | } |
17 | 17 | return $this->limit; |
@@ -33,18 +33,18 @@ discard block |
||
33 | 33 | */ |
34 | 34 | protected function buildLimit(string $query, ?int $offset = null) { |
35 | 35 | $limit = $this->getLimit(); |
36 | - if($limit === null && $offset !== null) { |
|
36 | + if ($limit === null && $offset !== null) { |
|
37 | 37 | $limit = '18446744073709551615'; |
38 | 38 | } |
39 | - if($this->limit instanceof OptionalValue) { |
|
40 | - if($this->limit->isValid()) { |
|
39 | + if ($this->limit instanceof OptionalValue) { |
|
40 | + if ($this->limit->isValid()) { |
|
41 | 41 | $value = $this->limit->getValue(); |
42 | - if(!preg_match('{\\d+}', $value)) { |
|
42 | + if (!preg_match('{\\d+}', $value)) { |
|
43 | 43 | throw new InvalidValueException('Value for LIMIT has to be a number'); |
44 | 44 | } |
45 | 45 | $query .= "LIMIT\n\t{$this->limit->getValue()}\n"; |
46 | 46 | } |
47 | - } elseif($limit !== null) { |
|
47 | + } elseif ($limit !== null) { |
|
48 | 48 | $query .= "LIMIT\n\t{$limit}\n"; |
49 | 49 | } |
50 | 50 | return $query; |
@@ -5,13 +5,13 @@ discard block |
||
5 | 5 | use Kir\MySQL\Builder\Value\OptionalValue; |
6 | 6 | |
7 | 7 | trait OffsetBuilder { |
8 | - private null|int|OptionalValue $offset = null; |
|
8 | + private null | int | OptionalValue $offset = null; |
|
9 | 9 | |
10 | 10 | /** |
11 | 11 | * @return null|int |
12 | 12 | */ |
13 | 13 | protected function getOffset(): ?int { |
14 | - if($this->offset instanceof OptionalValue) { |
|
14 | + if ($this->offset instanceof OptionalValue) { |
|
15 | 15 | return $this->offset->getValue(); |
16 | 16 | } |
17 | 17 | return $this->offset; |
@@ -32,15 +32,15 @@ discard block |
||
32 | 32 | */ |
33 | 33 | protected function buildOffset(string $query): string { |
34 | 34 | $offset = $this->getOffset(); |
35 | - if($this->offset instanceof OptionalValue) { |
|
36 | - if($this->offset->isValid()) { |
|
35 | + if ($this->offset instanceof OptionalValue) { |
|
36 | + if ($this->offset->isValid()) { |
|
37 | 37 | $value = $this->offset->getValue(); |
38 | - if(!preg_match('{\\d+}', $value)) { |
|
38 | + if (!preg_match('{\\d+}', $value)) { |
|
39 | 39 | throw new InvalidValueException('Value for OFFSET has to be a number'); |
40 | 40 | } |
41 | 41 | $query .= "OFFSET\n\t{$this->offset->getValue()}\n"; |
42 | 42 | } |
43 | - } elseif($offset !== null) { |
|
43 | + } elseif ($offset !== null) { |
|
44 | 44 | $query .= "OFFSET\n\t{$this->offset}\n"; |
45 | 45 | } |
46 | 46 | return $query; |
@@ -18,35 +18,35 @@ |
||
18 | 18 | * @return string |
19 | 19 | */ |
20 | 20 | protected function buildTableName(?string $alias, $name): string { |
21 | - if($name instanceof SpecialTable) { |
|
21 | + if ($name instanceof SpecialTable) { |
|
22 | 22 | $name = $name->asString($this->db()); |
23 | - } elseif(is_object($name) && !($name instanceof VirtualTable) && method_exists($name, '__toString')) { |
|
23 | + } elseif (is_object($name) && !($name instanceof VirtualTable) && method_exists($name, '__toString')) { |
|
24 | 24 | $name = (string) $name; |
25 | 25 | $lines = explode("\n", $name); |
26 | 26 | $lines = array_map(static fn(string $line) => "\t{$line}", $lines); |
27 | 27 | $name = implode("\n", $lines); |
28 | - $name = '(' . trim(rtrim(trim($name), ';')) . ')'; |
|
29 | - } elseif(is_array($name)) { |
|
28 | + $name = '('.trim(rtrim(trim($name), ';')).')'; |
|
29 | + } elseif (is_array($name)) { |
|
30 | 30 | $parts = []; |
31 | - foreach($name as /*$index => */$bucket) { |
|
32 | - if(is_scalar($bucket)/* && ctype_digit((string) $index)*/) { |
|
31 | + foreach ($name as /*$index => */$bucket) { |
|
32 | + if (is_scalar($bucket)/* && ctype_digit((string) $index)*/) { |
|
33 | 33 | $parts[] = "SELECT {$this->db()->quote($bucket)} AS {$this->db()->quoteField('value')}"; |
34 | 34 | } else { |
35 | 35 | $values = []; |
36 | - foreach($bucket as $field => $value) { |
|
36 | + foreach ($bucket as $field => $value) { |
|
37 | 37 | $values[] = sprintf('%s AS %s', $this->db()->quote($value), $this->db()->quoteField($field)); |
38 | 38 | } |
39 | 39 | $parts[] = sprintf("SELECT %s", implode(', ', $values)); |
40 | 40 | } |
41 | 41 | } |
42 | - $name = '(' . implode("\n\tUNION ALL\n\t", $parts) . ')'; |
|
42 | + $name = '('.implode("\n\tUNION ALL\n\t", $parts).')'; |
|
43 | 43 | } |
44 | - if((is_string($name) || $name instanceof VirtualTable) && $this->db()->getVirtualTables()->has($name)) { |
|
44 | + if ((is_string($name) || $name instanceof VirtualTable) && $this->db()->getVirtualTables()->has($name)) { |
|
45 | 45 | $select = (string) $this->db()->getVirtualTables()->get($name); |
46 | 46 | $name = sprintf('(%s)', implode("\n\t", explode("\n", trim($select)))); |
47 | 47 | } |
48 | 48 | $name = $this->aliasReplacer()->replace((string) $name); |
49 | - if($alias !== null) { |
|
49 | + if ($alias !== null) { |
|
50 | 50 | return sprintf("%s %s", $name, $alias); |
51 | 51 | } |
52 | 52 | return $name; |
@@ -11,8 +11,8 @@ discard block |
||
11 | 11 | * @param null|callable(): bool $validator |
12 | 12 | */ |
13 | 13 | public function __construct( |
14 | - private object|array $data, |
|
15 | - private string|array $path, |
|
14 | + private object | array $data, |
|
15 | + private string | array $path, |
|
16 | 16 | private $validator = null |
17 | 17 | ) {} |
18 | 18 | |
@@ -20,10 +20,10 @@ discard block |
||
20 | 20 | * @return bool |
21 | 21 | */ |
22 | 22 | public function isValid(): bool { |
23 | - if(!RecursiveStructureAccess::recursiveHas($this->data, $this->path)) { |
|
23 | + if (!RecursiveStructureAccess::recursiveHas($this->data, $this->path)) { |
|
24 | 24 | return false; |
25 | 25 | } |
26 | - if($this->validator !== null) { |
|
26 | + if ($this->validator !== null) { |
|
27 | 27 | $value = $this->getValue(); |
28 | 28 | return call_user_func($this->validator, $value); |
29 | 29 | } |
@@ -38,10 +38,10 @@ discard block |
||
38 | 38 | */ |
39 | 39 | public function setFetchMode(int $mode = PDO::FETCH_ASSOC, $arg0 = null, ?array $arg1 = null) { |
40 | 40 | $args = [$mode]; |
41 | - if($arg0 !== null) { |
|
41 | + if ($arg0 !== null) { |
|
42 | 42 | $args[] = $arg0; |
43 | 43 | } |
44 | - if($arg1 !== null) { |
|
44 | + if ($arg1 !== null) { |
|
45 | 45 | $args[] = $arg1; |
46 | 46 | } |
47 | 47 | $this->stmt->setFetchMode(...$args); |
@@ -57,7 +57,7 @@ discard block |
||
57 | 57 | $this->exceptionHandler(function() use ($params) { |
58 | 58 | $this->queryLoggers->logRegion($this->query, function() use ($params) { |
59 | 59 | $response = $this->stmt->execute($params); |
60 | - if(!$response) { |
|
60 | + if (!$response) { |
|
61 | 61 | throw new SqlException('Execution returned with "false".'); |
62 | 62 | } |
63 | 63 | }); |
@@ -73,13 +73,13 @@ discard block |
||
73 | 73 | */ |
74 | 74 | public function fetchAll($fetchStyle = PDO::FETCH_ASSOC, $fetchArgument = null, array $ctorArgs = []): array { |
75 | 75 | $result = $x = $this->exceptionHandler(function() use ($fetchStyle, $fetchArgument, $ctorArgs) { |
76 | - if($fetchArgument !== null) { |
|
76 | + if ($fetchArgument !== null) { |
|
77 | 77 | return $this->stmt->fetchAll($fetchStyle, $fetchArgument, ...$ctorArgs); |
78 | 78 | } |
79 | 79 | return $this->stmt->fetchAll($fetchStyle); |
80 | 80 | }); |
81 | 81 | /** @var array<mixed, mixed>|false $x */ |
82 | - if($x === false) { |
|
82 | + if ($x === false) { |
|
83 | 83 | return []; |
84 | 84 | } |
85 | 85 | return $result; |
@@ -124,7 +124,7 @@ discard block |
||
124 | 124 | public function getColumnMeta(int $columnNo): ?array { |
125 | 125 | return $this->exceptionHandler(function() use ($columnNo) { |
126 | 126 | $columnMeta = $this->stmt->getColumnMeta($columnNo); |
127 | - if($columnMeta === false) { |
|
127 | + if ($columnMeta === false) { |
|
128 | 128 | return null; |
129 | 129 | } |
130 | 130 | return $columnMeta; |
@@ -46,7 +46,7 @@ discard block |
||
46 | 46 | private PDO $pdo, |
47 | 47 | array $options = [] |
48 | 48 | ) { |
49 | - if($pdo->getAttribute(PDO::ATTR_ERRMODE) === PDO::ERRMODE_SILENT) { |
|
49 | + if ($pdo->getAttribute(PDO::ATTR_ERRMODE) === PDO::ERRMODE_SILENT) { |
|
50 | 50 | $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); |
51 | 51 | } |
52 | 52 | $this->aliasRegistry = new AliasRegistry(); |
@@ -60,7 +60,7 @@ discard block |
||
60 | 60 | ]; |
61 | 61 | $this->options = array_merge($defaultOptions, $options); |
62 | 62 | $this->options['timezone'] ??= date_default_timezone_get(); |
63 | - if(!($this->options['timezone'] instanceof DateTimeZone)) { |
|
63 | + if (!($this->options['timezone'] instanceof DateTimeZone)) { |
|
64 | 64 | $this->options['timezone'] = new DateTimeZone((string) $this->options['timezone']); |
65 | 65 | } |
66 | 66 | $this->quoter = new MySQLQuoter($pdo, $this->options['timezone']); |
@@ -84,7 +84,7 @@ discard block |
||
84 | 84 | * @return VirtualTables |
85 | 85 | */ |
86 | 86 | public function getVirtualTables(): VirtualTables { |
87 | - if($this->virtualTables === null) { |
|
87 | + if ($this->virtualTables === null) { |
|
88 | 88 | $this->virtualTables = new VirtualTables(); |
89 | 89 | } |
90 | 90 | return $this->virtualTables; |
@@ -129,11 +129,11 @@ discard block |
||
129 | 129 | array $params = [] |
130 | 130 | ): int { |
131 | 131 | return $this->getQueryLoggers()->logRegion($query, fn() => |
132 | - $this->exceptionHandler(function () use ($query, $params) { |
|
132 | + $this->exceptionHandler(function() use ($query, $params) { |
|
133 | 133 | $stmt = $this->pdo->prepare($query); |
134 | 134 | $timer = microtime(true); |
135 | 135 | $stmt->execute($params); |
136 | - $this->queryLoggers->log($query, microtime(true) - $timer); |
|
136 | + $this->queryLoggers->log($query, microtime(true)-$timer); |
|
137 | 137 | $result = $stmt->rowCount(); |
138 | 138 | $stmt->closeCursor(); |
139 | 139 | return $result; |
@@ -147,7 +147,7 @@ discard block |
||
147 | 147 | */ |
148 | 148 | public function getLastInsertId(?string $name = null): ?string { |
149 | 149 | $result = $this->pdo->lastInsertId(); |
150 | - if($result === false) { |
|
150 | + if ($result === false) { |
|
151 | 151 | return null; |
152 | 152 | } |
153 | 153 | return $result; |
@@ -159,15 +159,15 @@ discard block |
||
159 | 159 | */ |
160 | 160 | public function getTableFields(string $table): array { |
161 | 161 | $fqTable = $this->select()->aliasReplacer()->replace($table); |
162 | - if(array_key_exists($fqTable, $this->tableFields)) { |
|
162 | + if (array_key_exists($fqTable, $this->tableFields)) { |
|
163 | 163 | return $this->tableFields[$fqTable]; |
164 | 164 | } |
165 | 165 | $query = "DESCRIBE {$fqTable}"; |
166 | 166 | return $this->getQueryLoggers()->logRegion($query, fn() => |
167 | - $this->exceptionHandler(function () use ($query, $fqTable) { |
|
167 | + $this->exceptionHandler(function() use ($query, $fqTable) { |
|
168 | 168 | $stmt = $this->pdo->query($query); |
169 | 169 | try { |
170 | - if($stmt === false) { |
|
170 | + if ($stmt === false) { |
|
171 | 171 | throw new RuntimeException('Invalid return type'); |
172 | 172 | } |
173 | 173 | $rows = $stmt->fetchAll(PDO::FETCH_ASSOC); |
@@ -175,7 +175,7 @@ discard block |
||
175 | 175 | return $this->tableFields[$fqTable]; |
176 | 176 | } finally { |
177 | 177 | try { |
178 | - if($stmt instanceof PDOStatement) { |
|
178 | + if ($stmt instanceof PDOStatement) { |
|
179 | 179 | $stmt->closeCursor(); |
180 | 180 | } |
181 | 181 | } catch (Throwable $e) {} |
@@ -217,7 +217,7 @@ discard block |
||
217 | 217 | $select = array_key_exists('select-factory', $this->options) |
218 | 218 | ? call_user_func($this->options['select-factory'], $this, $this->options['select-options']) |
219 | 219 | : new MySQL\MySQLRunnableSelect($this, $this->options['select-options']); |
220 | - if($fields !== null) { |
|
220 | + if ($fields !== null) { |
|
221 | 221 | $select->fields($fields); |
222 | 222 | } |
223 | 223 | return $select; |
@@ -231,7 +231,7 @@ discard block |
||
231 | 231 | $insert = array_key_exists('insert-factory', $this->options) |
232 | 232 | ? call_user_func($this->options['insert-factory'], $this, $this->options['insert-options']) |
233 | 233 | : new Builder\RunnableInsert($this, $this->options['insert-options']); |
234 | - if($fields !== null) { |
|
234 | + if ($fields !== null) { |
|
235 | 235 | $insert->addAll($fields); |
236 | 236 | } |
237 | 237 | return $insert; |
@@ -245,7 +245,7 @@ discard block |
||
245 | 245 | $update = array_key_exists('update-factory', $this->options) |
246 | 246 | ? call_user_func($this->options['update-factory'], $this, $this->options['update-options']) |
247 | 247 | : new Builder\RunnableUpdate($this, $this->options['update-options']); |
248 | - if($fields !== null) { |
|
248 | + if ($fields !== null) { |
|
249 | 249 | $update->setAll($fields); |
250 | 250 | } |
251 | 251 | return $update; |
@@ -264,8 +264,8 @@ discard block |
||
264 | 264 | * @return $this |
265 | 265 | */ |
266 | 266 | public function transactionStart() { |
267 | - if($this->transactionLevel === 0) { |
|
268 | - if($this->pdo->inTransaction()) { |
|
267 | + if ($this->transactionLevel === 0) { |
|
268 | + if ($this->pdo->inTransaction()) { |
|
269 | 269 | $this->outerTransaction = true; |
270 | 270 | } else { |
271 | 271 | $this->pdo->beginTransaction(); |
@@ -279,7 +279,7 @@ discard block |
||
279 | 279 | * @return $this |
280 | 280 | */ |
281 | 281 | public function transactionCommit() { |
282 | - return $this->transactionEnd(function () { |
|
282 | + return $this->transactionEnd(function() { |
|
283 | 283 | $this->pdo->commit(); |
284 | 284 | }); |
285 | 285 | } |
@@ -288,7 +288,7 @@ discard block |
||
288 | 288 | * @return $this |
289 | 289 | */ |
290 | 290 | public function transactionRollback() { |
291 | - return $this->transactionEnd(function () { |
|
291 | + return $this->transactionEnd(function() { |
|
292 | 292 | $this->pdo->rollBack(); |
293 | 293 | }); |
294 | 294 | } |
@@ -299,7 +299,7 @@ discard block |
||
299 | 299 | * @return T |
300 | 300 | */ |
301 | 301 | public function dryRun(callable $callback) { |
302 | - if(!$this->pdo->inTransaction()) { |
|
302 | + if (!$this->pdo->inTransaction()) { |
|
303 | 303 | $this->transactionStart(); |
304 | 304 | try { |
305 | 305 | return $callback($this); |
@@ -324,14 +324,14 @@ discard block |
||
324 | 324 | * @throws Throwable |
325 | 325 | */ |
326 | 326 | public function transaction(callable $callback) { |
327 | - if(!$this->pdo->inTransaction()) { |
|
327 | + if (!$this->pdo->inTransaction()) { |
|
328 | 328 | $this->transactionStart(); |
329 | 329 | try { |
330 | 330 | $result = $callback($this); |
331 | 331 | $this->transactionCommit(); |
332 | 332 | return $result; |
333 | 333 | } catch (Throwable $e) { |
334 | - if($this->pdo->inTransaction()) { |
|
334 | + if ($this->pdo->inTransaction()) { |
|
335 | 335 | $this->transactionRollback(); |
336 | 336 | } |
337 | 337 | throw $e; |
@@ -355,11 +355,11 @@ discard block |
||
355 | 355 | */ |
356 | 356 | private function transactionEnd($fn): self { |
357 | 357 | $this->transactionLevel--; |
358 | - if($this->transactionLevel < 0) { |
|
358 | + if ($this->transactionLevel < 0) { |
|
359 | 359 | throw new RuntimeException("Transaction-Nesting-Problem: Trying to invoke commit on a already closed transaction"); |
360 | 360 | } |
361 | - if($this->transactionLevel < 1) { |
|
362 | - if($this->outerTransaction) { |
|
361 | + if ($this->transactionLevel < 1) { |
|
362 | + if ($this->outerTransaction) { |
|
363 | 363 | $this->outerTransaction = false; |
364 | 364 | } else { |
365 | 365 | $fn(); |
@@ -376,7 +376,7 @@ discard block |
||
376 | 376 | */ |
377 | 377 | private function buildQueryStatement(string $query, callable $fn): QueryStatement { |
378 | 378 | $stmt = $fn($query); |
379 | - if(!$stmt) { |
|
379 | + if (!$stmt) { |
|
380 | 380 | throw new RuntimeException("Could not execute statement:\n{$query}"); |
381 | 381 | } |
382 | 382 | return new QueryStatement($stmt, $query, $this->exceptionInterpreter, $this->queryLoggers); |