@@ -43,7 +43,7 @@ discard block |
||
| 43 | 43 | * @param array<string, mixed> $options |
| 44 | 44 | */ |
| 45 | 45 | public function __construct(PDO $pdo, array $options = []) { |
| 46 | - if($pdo->getAttribute(PDO::ATTR_ERRMODE) === PDO::ERRMODE_SILENT) { |
|
| 46 | + if ($pdo->getAttribute(PDO::ATTR_ERRMODE) === PDO::ERRMODE_SILENT) { |
|
| 47 | 47 | $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); |
| 48 | 48 | } |
| 49 | 49 | $this->pdo = $pdo; |
@@ -77,7 +77,7 @@ discard block |
||
| 77 | 77 | * @return VirtualTables |
| 78 | 78 | */ |
| 79 | 79 | public function getVirtualTables(): VirtualTables { |
| 80 | - if($this->virtualTables === null) { |
|
| 80 | + if ($this->virtualTables === null) { |
|
| 81 | 81 | $this->virtualTables = new VirtualTables(); |
| 82 | 82 | } |
| 83 | 83 | return $this->virtualTables; |
@@ -88,7 +88,7 @@ discard block |
||
| 88 | 88 | * @return QueryStatement |
| 89 | 89 | */ |
| 90 | 90 | public function query(string $query) { |
| 91 | - return $this->buildQueryStatement($query, function ($query) { |
|
| 91 | + return $this->buildQueryStatement($query, function($query) { |
|
| 92 | 92 | return $this->pdo->query($query); |
| 93 | 93 | }); |
| 94 | 94 | } |
@@ -98,7 +98,7 @@ discard block |
||
| 98 | 98 | * @return QueryStatement |
| 99 | 99 | */ |
| 100 | 100 | public function prepare(string $query) { |
| 101 | - return $this->buildQueryStatement($query, function ($query) { |
|
| 101 | + return $this->buildQueryStatement($query, function($query) { |
|
| 102 | 102 | return $this->pdo->prepare($query); |
| 103 | 103 | }); |
| 104 | 104 | } |
@@ -109,11 +109,11 @@ discard block |
||
| 109 | 109 | * @return int |
| 110 | 110 | */ |
| 111 | 111 | public function exec(string $query, array $params = []): int { |
| 112 | - return $this->exceptionHandler(function () use ($query, $params) { |
|
| 112 | + return $this->exceptionHandler(function() use ($query, $params) { |
|
| 113 | 113 | $stmt = $this->pdo->prepare($query); |
| 114 | 114 | $timer = microtime(true); |
| 115 | 115 | $stmt->execute($params); |
| 116 | - $this->queryLoggers->log($query, microtime(true) - $timer); |
|
| 116 | + $this->queryLoggers->log($query, microtime(true)-$timer); |
|
| 117 | 117 | $result = $stmt->rowCount(); |
| 118 | 118 | $stmt->closeCursor(); |
| 119 | 119 | return $result; |
@@ -134,15 +134,15 @@ discard block |
||
| 134 | 134 | */ |
| 135 | 135 | public function getTableFields(string $table): array { |
| 136 | 136 | $table = $this->select()->aliasReplacer()->replace($table); |
| 137 | - if(array_key_exists($table, $this->tableFields)) { |
|
| 137 | + if (array_key_exists($table, $this->tableFields)) { |
|
| 138 | 138 | return $this->tableFields[$table]; |
| 139 | 139 | } |
| 140 | 140 | $stmt = $this->pdo->query("DESCRIBE {$table}"); |
| 141 | - if($stmt === false) { |
|
| 141 | + if ($stmt === false) { |
|
| 142 | 142 | throw new RuntimeException('Invalid return type'); |
| 143 | 143 | } |
| 144 | 144 | $rows = $stmt->fetchAll(PDO::FETCH_ASSOC); |
| 145 | - $this->tableFields[$table] = array_map(static function ($row) { return $row['Field']; }, $rows ?: []); |
|
| 145 | + $this->tableFields[$table] = array_map(static function($row) { return $row['Field']; }, $rows ?: []); |
|
| 146 | 146 | $stmt->closeCursor(); |
| 147 | 147 | return $this->tableFields[$table]; |
| 148 | 148 | } |
@@ -154,12 +154,12 @@ discard block |
||
| 154 | 154 | */ |
| 155 | 155 | public function quoteExpression(string $expression, array $arguments = []): string { |
| 156 | 156 | $index = -1; |
| 157 | - $func = function () use ($arguments, &$index) { |
|
| 157 | + $func = function() use ($arguments, &$index) { |
|
| 158 | 158 | $index++; |
| 159 | - if(array_key_exists($index, $arguments)) { |
|
| 159 | + if (array_key_exists($index, $arguments)) { |
|
| 160 | 160 | $argument = $arguments[$index]; |
| 161 | 161 | $value = $this->quote($argument); |
| 162 | - } elseif(count($arguments) > 0) { |
|
| 162 | + } elseif (count($arguments) > 0) { |
|
| 163 | 163 | $args = $arguments; |
| 164 | 164 | $value = array_pop($args); |
| 165 | 165 | $value = $this->quote($value); |
@@ -177,15 +177,15 @@ discard block |
||
| 177 | 177 | * @return string |
| 178 | 178 | */ |
| 179 | 179 | public function quote($value): string { |
| 180 | - if(is_null($value)) { |
|
| 180 | + if (is_null($value)) { |
|
| 181 | 181 | $result = 'NULL'; |
| 182 | - } elseif($value instanceof Builder\DBExpr) { |
|
| 182 | + } elseif ($value instanceof Builder\DBExpr) { |
|
| 183 | 183 | $result = $value->getExpression(); |
| 184 | - } elseif($value instanceof Builder\Select) { |
|
| 184 | + } elseif ($value instanceof Builder\Select) { |
|
| 185 | 185 | $result = sprintf('(%s)', (string) $value); |
| 186 | - } elseif(is_array($value)) { |
|
| 187 | - $result = implode(', ', array_map(function ($value) { return $this->quote($value); }, $value)); |
|
| 188 | - } elseif(is_int($value) || is_float($value)) { |
|
| 186 | + } elseif (is_array($value)) { |
|
| 187 | + $result = implode(', ', array_map(function($value) { return $this->quote($value); }, $value)); |
|
| 188 | + } elseif (is_int($value) || is_float($value)) { |
|
| 189 | 189 | $result = (string) $value; |
| 190 | 190 | } else { |
| 191 | 191 | $result = $this->pdo->quote($value); |
@@ -201,7 +201,7 @@ discard block |
||
| 201 | 201 | if (is_numeric($field) || !is_string($field)) { |
| 202 | 202 | throw new UnexpectedValueException('Field name is invalid'); |
| 203 | 203 | } |
| 204 | - if(strpos($field, '`') !== false) { |
|
| 204 | + if (strpos($field, '`') !== false) { |
|
| 205 | 205 | return $field; |
| 206 | 206 | } |
| 207 | 207 | $parts = explode('.', $field); |
@@ -216,7 +216,7 @@ discard block |
||
| 216 | 216 | $select = array_key_exists('select-factory', $this->options) |
| 217 | 217 | ? call_user_func($this->options['select-factory'], $this, $this->options['select-options']) |
| 218 | 218 | : new MySQL\MySQLRunnableSelect($this, $this->options['select-options']); |
| 219 | - if($fields !== null) { |
|
| 219 | + if ($fields !== null) { |
|
| 220 | 220 | $select->fields($fields); |
| 221 | 221 | } |
| 222 | 222 | return $select; |
@@ -230,7 +230,7 @@ discard block |
||
| 230 | 230 | $insert = array_key_exists('insert-factory', $this->options) |
| 231 | 231 | ? call_user_func($this->options['insert-factory'], $this, $this->options['insert-options']) |
| 232 | 232 | : new Builder\RunnableInsert($this, $this->options['insert-options']); |
| 233 | - if($fields !== null) { |
|
| 233 | + if ($fields !== null) { |
|
| 234 | 234 | $insert->addAll($fields); |
| 235 | 235 | } |
| 236 | 236 | return $insert; |
@@ -244,7 +244,7 @@ discard block |
||
| 244 | 244 | $update = array_key_exists('update-factory', $this->options) |
| 245 | 245 | ? call_user_func($this->options['update-factory'], $this, $this->options['update-options']) |
| 246 | 246 | : new Builder\RunnableUpdate($this, $this->options['update-options']); |
| 247 | - if($fields !== null) { |
|
| 247 | + if ($fields !== null) { |
|
| 248 | 248 | $update->setAll($fields); |
| 249 | 249 | } |
| 250 | 250 | return $update; |
@@ -263,8 +263,8 @@ discard block |
||
| 263 | 263 | * @return $this |
| 264 | 264 | */ |
| 265 | 265 | public function transactionStart() { |
| 266 | - if($this->transactionLevel === 0) { |
|
| 267 | - if($this->pdo->inTransaction()) { |
|
| 266 | + if ($this->transactionLevel === 0) { |
|
| 267 | + if ($this->pdo->inTransaction()) { |
|
| 268 | 268 | $this->outerTransaction = true; |
| 269 | 269 | } else { |
| 270 | 270 | $this->pdo->beginTransaction(); |
@@ -278,7 +278,7 @@ discard block |
||
| 278 | 278 | * @return $this |
| 279 | 279 | */ |
| 280 | 280 | public function transactionCommit() { |
| 281 | - return $this->transactionEnd(function () { |
|
| 281 | + return $this->transactionEnd(function() { |
|
| 282 | 282 | $this->pdo->commit(); |
| 283 | 283 | }); |
| 284 | 284 | } |
@@ -287,7 +287,7 @@ discard block |
||
| 287 | 287 | * @return $this |
| 288 | 288 | */ |
| 289 | 289 | public function transactionRollback() { |
| 290 | - return $this->transactionEnd(function () { |
|
| 290 | + return $this->transactionEnd(function() { |
|
| 291 | 291 | $this->pdo->rollBack(); |
| 292 | 292 | }); |
| 293 | 293 | } |
@@ -298,7 +298,7 @@ discard block |
||
| 298 | 298 | * @return T |
| 299 | 299 | */ |
| 300 | 300 | public function dryRun(callable $callback) { |
| 301 | - if(!$this->pdo->inTransaction()) { |
|
| 301 | + if (!$this->pdo->inTransaction()) { |
|
| 302 | 302 | $this->transactionStart(); |
| 303 | 303 | try { |
| 304 | 304 | return $callback($this); |
@@ -323,14 +323,14 @@ discard block |
||
| 323 | 323 | * @throws Throwable |
| 324 | 324 | */ |
| 325 | 325 | public function transaction(callable $callback) { |
| 326 | - if(!$this->pdo->inTransaction()) { |
|
| 326 | + if (!$this->pdo->inTransaction()) { |
|
| 327 | 327 | $this->transactionStart(); |
| 328 | 328 | try { |
| 329 | 329 | $result = $callback($this); |
| 330 | 330 | $this->transactionCommit(); |
| 331 | 331 | return $result; |
| 332 | 332 | } catch (Throwable $e) { |
| 333 | - if($this->pdo->inTransaction()) { |
|
| 333 | + if ($this->pdo->inTransaction()) { |
|
| 334 | 334 | $this->transactionRollback(); |
| 335 | 335 | } |
| 336 | 336 | throw $e; |
@@ -354,11 +354,11 @@ discard block |
||
| 354 | 354 | */ |
| 355 | 355 | private function transactionEnd(callable $fn): self { |
| 356 | 356 | $this->transactionLevel--; |
| 357 | - if($this->transactionLevel < 0) { |
|
| 357 | + if ($this->transactionLevel < 0) { |
|
| 358 | 358 | throw new RuntimeException("Transaction-Nesting-Problem: Trying to invoke commit on a already closed transaction"); |
| 359 | 359 | } |
| 360 | - if($this->transactionLevel < 1) { |
|
| 361 | - if($this->outerTransaction) { |
|
| 360 | + if ($this->transactionLevel < 1) { |
|
| 361 | + if ($this->outerTransaction) { |
|
| 362 | 362 | $this->outerTransaction = false; |
| 363 | 363 | } else { |
| 364 | 364 | $fn(); |
@@ -375,7 +375,7 @@ discard block |
||
| 375 | 375 | */ |
| 376 | 376 | private function buildQueryStatement(string $query, callable $fn): QueryStatement { |
| 377 | 377 | $stmt = $fn($query); |
| 378 | - if(!$stmt) { |
|
| 378 | + if (!$stmt) { |
|
| 379 | 379 | throw new RuntimeException("Could not execute statement:\n{$query}"); |
| 380 | 380 | } |
| 381 | 381 | return new QueryStatement($stmt, $query, $this->exceptionInterpreter, $this->queryLoggers); |