@@ -101,17 +101,17 @@ discard block |
||
| 101 | 101 | * @inheritDoc |
| 102 | 102 | */ |
| 103 | 103 | public function fetchObjects(string $className = 'stdClass', $callback = null): array { |
| 104 | - return $this->createTempStatement(function (QueryStatement $statement) use ($className, $callback) { |
|
| 104 | + return $this->createTempStatement(function(QueryStatement $statement) use ($className, $callback) { |
|
| 105 | 105 | $data = $statement->fetchAll(PDO::FETCH_CLASS, $className); |
| 106 | - if($this->preserveTypes) { |
|
| 106 | + if ($this->preserveTypes) { |
|
| 107 | 107 | $columnDefinitions = FieldTypeProvider::getFieldTypes($statement); |
| 108 | 108 | $data = array_map(static fn($row) => FieldValueConverter::convertValues($row, $columnDefinitions), $data); |
| 109 | 109 | } |
| 110 | - if($callback !== null) { |
|
| 111 | - return call_user_func(static function ($resultData = []) use ($data, $callback) { |
|
| 112 | - foreach($data as $row) { |
|
| 110 | + if ($callback !== null) { |
|
| 111 | + return call_user_func(static function($resultData = []) use ($data, $callback) { |
|
| 112 | + foreach ($data as $row) { |
|
| 113 | 113 | $result = $callback($row); |
| 114 | - if($result !== null && !($result instanceof DBIgnoreRow)) { |
|
| 114 | + if ($result !== null && !($result instanceof DBIgnoreRow)) { |
|
| 115 | 115 | $resultData[] = $result; |
| 116 | 116 | } else { |
| 117 | 117 | $resultData[] = $row; |
@@ -147,11 +147,11 @@ discard block |
||
| 147 | 147 | * @inheritDoc |
| 148 | 148 | */ |
| 149 | 149 | public function fetchKeyValue($treatValueAsArray = false): array { |
| 150 | - return $this->createTempStatement(static function (QueryStatement $statement) use ($treatValueAsArray) { |
|
| 151 | - if($treatValueAsArray) { |
|
| 150 | + return $this->createTempStatement(static function(QueryStatement $statement) use ($treatValueAsArray) { |
|
| 151 | + if ($treatValueAsArray) { |
|
| 152 | 152 | $rows = $statement->fetchAll(PDO::FETCH_ASSOC); |
| 153 | 153 | $result = []; |
| 154 | - foreach($rows as $row) { |
|
| 154 | + foreach ($rows as $row) { |
|
| 155 | 155 | [$key] = array_values($row); |
| 156 | 156 | $result[$key] = $row; |
| 157 | 157 | } |
@@ -167,12 +167,12 @@ discard block |
||
| 167 | 167 | public function fetchGroups(array $fields): array { |
| 168 | 168 | $rows = $this->fetchRows(); |
| 169 | 169 | $result = []; |
| 170 | - foreach($rows as $row) { |
|
| 170 | + foreach ($rows as $row) { |
|
| 171 | 171 | /** @var array<string, mixed> $tmp */ |
| 172 | 172 | $tmp = &$result; |
| 173 | - foreach($fields as $field) { |
|
| 173 | + foreach ($fields as $field) { |
|
| 174 | 174 | $value = (string) $row[$field]; |
| 175 | - if(!array_key_exists($value, $tmp)) { |
|
| 175 | + if (!array_key_exists($value, $tmp)) { |
|
| 176 | 176 | $tmp[$value] = []; |
| 177 | 177 | } |
| 178 | 178 | $tmp = &$tmp[$value]; |
@@ -186,8 +186,8 @@ discard block |
||
| 186 | 186 | * @inheritDoc |
| 187 | 187 | */ |
| 188 | 188 | public function fetchArray(?callable $fn = null): array { |
| 189 | - return $this->createTempStatement(static function (QueryStatement $stmt) use ($fn) { |
|
| 190 | - if($fn !== null) { |
|
| 189 | + return $this->createTempStatement(static function(QueryStatement $stmt) use ($fn) { |
|
| 190 | + if ($fn !== null) { |
|
| 191 | 191 | return $stmt->fetchAll(PDO::FETCH_FUNC, $fn); |
| 192 | 192 | } |
| 193 | 193 | return $stmt->fetchAll(PDO::FETCH_COLUMN); |
@@ -198,9 +198,9 @@ discard block |
||
| 198 | 198 | * @inheritDoc |
| 199 | 199 | */ |
| 200 | 200 | public function fetchValue($default = null, ?callable $fn = null) { |
| 201 | - return $this->createTempStatement(static function (QueryStatement $stmt) use ($default, $fn) { |
|
| 201 | + return $this->createTempStatement(static function(QueryStatement $stmt) use ($default, $fn) { |
|
| 202 | 202 | $result = $stmt->fetchAll(PDO::FETCH_COLUMN); |
| 203 | - if($result !== false && array_key_exists(0, $result)) { |
|
| 203 | + if ($result !== false && array_key_exists(0, $result)) { |
|
| 204 | 204 | return $fn !== null ? $fn($result[0]) : $result[0]; |
| 205 | 205 | } |
| 206 | 206 | return $default; |
@@ -235,7 +235,7 @@ discard block |
||
| 235 | 235 | $query = $this->__toString(); |
| 236 | 236 | $statement = $db->prepare($query); |
| 237 | 237 | $statement->execute($this->values); |
| 238 | - if($this->getCalcFoundRows()) { |
|
| 238 | + if ($this->getCalcFoundRows()) { |
|
| 239 | 239 | $this->foundRows = (int) $db->query('SELECT FOUND_ROWS()')->fetchColumn(); |
| 240 | 240 | } |
| 241 | 241 | return $statement; |
@@ -256,21 +256,21 @@ discard block |
||
| 256 | 256 | * @return ($callback is null ? array<int, ($mode is PDO::FETCH_NUM ? array<int, null|scalar> : array<string, null|scalar>)> : array<int, TFnReturnType>) |
| 257 | 257 | */ |
| 258 | 258 | private function fetchAll($callback = null, int $mode = 0, $arg0 = null) { |
| 259 | - return $this->createTempStatement(function (QueryStatement $statement) use ($callback, $mode, $arg0) { |
|
| 259 | + return $this->createTempStatement(function(QueryStatement $statement) use ($callback, $mode, $arg0) { |
|
| 260 | 260 | $statement->setFetchMode($mode, $arg0); |
| 261 | 261 | $data = $statement->fetchAll(); |
| 262 | - if($this->preserveTypes) { |
|
| 262 | + if ($this->preserveTypes) { |
|
| 263 | 263 | $columnDefinitions = FieldTypeProvider::getFieldTypes($statement); |
| 264 | 264 | $data = array_map(static fn($row) => FieldValueConverter::convertValues($row, $columnDefinitions), $data); |
| 265 | 265 | } |
| 266 | - if($callback !== null) { |
|
| 267 | - return call_user_func(static function ($resultData = []) use ($data, $callback) { |
|
| 268 | - foreach($data as $row) { |
|
| 266 | + if ($callback !== null) { |
|
| 267 | + return call_user_func(static function($resultData = []) use ($data, $callback) { |
|
| 268 | + foreach ($data as $row) { |
|
| 269 | 269 | $result = $callback($row); |
| 270 | - if($result instanceof DBIgnoreRow) { |
|
| 270 | + if ($result instanceof DBIgnoreRow) { |
|
| 271 | 271 | continue; |
| 272 | 272 | } |
| 273 | - if($result !== null) { |
|
| 273 | + if ($result !== null) { |
|
| 274 | 274 | $resultData[] = $result; |
| 275 | 275 | } else { |
| 276 | 276 | $resultData[] = $row; |
@@ -294,18 +294,18 @@ discard block |
||
| 294 | 294 | $statement = $this->createStatement(); |
| 295 | 295 | $statement->setFetchMode($mode, $arg0); |
| 296 | 296 | try { |
| 297 | - while($row = $statement->fetch()) { |
|
| 297 | + while ($row = $statement->fetch()) { |
|
| 298 | 298 | /** @var T $row */ |
| 299 | 299 | // if($this->preserveTypes) { |
| 300 | 300 | // $columnDefinitions = FieldTypeProvider::getFieldTypes($statement); |
| 301 | 301 | // $row = FieldValueConverter::convertValues($row, $columnDefinitions); |
| 302 | 302 | // } |
| 303 | 303 | $result = $callback($row); |
| 304 | - if($result instanceof DBIgnoreRow) { |
|
| 304 | + if ($result instanceof DBIgnoreRow) { |
|
| 305 | 305 | // Skip row in this case |
| 306 | 306 | continue; |
| 307 | 307 | } |
| 308 | - if($result !== null) { |
|
| 308 | + if ($result !== null) { |
|
| 309 | 309 | yield $result; |
| 310 | 310 | } else { |
| 311 | 311 | yield $row; |
@@ -326,20 +326,20 @@ discard block |
||
| 326 | 326 | * @return T|U|array<string, mixed> |
| 327 | 327 | */ |
| 328 | 328 | private function fetch($callback, int $mode = PDO::FETCH_ASSOC, $arg0 = null, ?Closure $resultValidator = null) { |
| 329 | - return $this->createTempStatement(function (QueryStatement $statement) use ($callback, $mode, $arg0, $resultValidator) { |
|
| 329 | + return $this->createTempStatement(function(QueryStatement $statement) use ($callback, $mode, $arg0, $resultValidator) { |
|
| 330 | 330 | $statement->setFetchMode($mode, $arg0); |
| 331 | 331 | $row = $statement->fetch(); |
| 332 | 332 | $result = $resultValidator === null ? ['valid' => true] : $resultValidator($row); |
| 333 | - if(!$result['valid']) { |
|
| 333 | + if (!$result['valid']) { |
|
| 334 | 334 | return $result['default']; |
| 335 | 335 | } |
| 336 | - if($this->preserveTypes) { |
|
| 336 | + if ($this->preserveTypes) { |
|
| 337 | 337 | $columnDefinitions = FieldTypeProvider::getFieldTypes($statement); |
| 338 | 338 | $row = FieldValueConverter::convertValues($row, $columnDefinitions); |
| 339 | 339 | } |
| 340 | - if($callback !== null) { |
|
| 340 | + if ($callback !== null) { |
|
| 341 | 341 | $result = $callback($row); |
| 342 | - if($result !== null) { |
|
| 342 | + if ($result !== null) { |
|
| 343 | 343 | $row = $result; |
| 344 | 344 | } |
| 345 | 345 | } |
@@ -19,31 +19,31 @@ discard block |
||
| 19 | 19 | * @return string |
| 20 | 20 | */ |
| 21 | 21 | public function quote($value): string { |
| 22 | - if(is_null($value)) { |
|
| 22 | + if (is_null($value)) { |
|
| 23 | 23 | return 'NULL'; |
| 24 | 24 | } |
| 25 | 25 | |
| 26 | - if(is_bool($value)) { |
|
| 26 | + if (is_bool($value)) { |
|
| 27 | 27 | return $value ? '1' : '0'; |
| 28 | 28 | } |
| 29 | 29 | |
| 30 | - if(is_array($value)) { |
|
| 30 | + if (is_array($value)) { |
|
| 31 | 31 | return implode(', ', array_map([$this, __FUNCTION__], $value)); |
| 32 | 32 | } |
| 33 | 33 | |
| 34 | - if($value instanceof DBExpr) { |
|
| 34 | + if ($value instanceof DBExpr) { |
|
| 35 | 35 | return $value->getExpression(); |
| 36 | 36 | } |
| 37 | 37 | |
| 38 | - if($value instanceof Select) { |
|
| 38 | + if ($value instanceof Select) { |
|
| 39 | 39 | return sprintf('(%s)', (string) $value); |
| 40 | 40 | } |
| 41 | 41 | |
| 42 | - if(is_int($value) || is_float($value)) { |
|
| 42 | + if (is_int($value) || is_float($value)) { |
|
| 43 | 43 | return (string) $value; |
| 44 | 44 | } |
| 45 | 45 | |
| 46 | - if($value instanceof DateTimeInterface) { |
|
| 46 | + if ($value instanceof DateTimeInterface) { |
|
| 47 | 47 | $value = (new DateTimeImmutable($value->format('c')))->setTimezone($this->timeZone)->format('Y-m-d H:i:s'); |
| 48 | 48 | } |
| 49 | 49 | |
@@ -57,12 +57,12 @@ discard block |
||
| 57 | 57 | */ |
| 58 | 58 | public function quoteExpression(string $expression, array $arguments = []): string { |
| 59 | 59 | $index = -1; |
| 60 | - $func = function () use ($arguments, &$index) { |
|
| 60 | + $func = function() use ($arguments, &$index) { |
|
| 61 | 61 | $index++; |
| 62 | - if(array_key_exists($index, $arguments)) { |
|
| 62 | + if (array_key_exists($index, $arguments)) { |
|
| 63 | 63 | $argument = $arguments[$index]; |
| 64 | 64 | $value = $this->quote($argument); |
| 65 | - } elseif(count($arguments) > 0) { |
|
| 65 | + } elseif (count($arguments) > 0) { |
|
| 66 | 66 | $args = $arguments; |
| 67 | 67 | $value = array_pop($args); |
| 68 | 68 | $value = $this->quote($value); |
@@ -52,7 +52,7 @@ discard block |
||
| 52 | 52 | * @param array<string, mixed> $options |
| 53 | 53 | */ |
| 54 | 54 | public function __construct(PDO $pdo, array $options = []) { |
| 55 | - if($pdo->getAttribute(PDO::ATTR_ERRMODE) === PDO::ERRMODE_SILENT) { |
|
| 55 | + if ($pdo->getAttribute(PDO::ATTR_ERRMODE) === PDO::ERRMODE_SILENT) { |
|
| 56 | 56 | $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); |
| 57 | 57 | } |
| 58 | 58 | $this->pdo = $pdo; |
@@ -67,7 +67,7 @@ discard block |
||
| 67 | 67 | ]; |
| 68 | 68 | $this->options = array_merge($defaultOptions, $options); |
| 69 | 69 | $this->options['timezone'] ??= date_default_timezone_get(); |
| 70 | - if(!($this->options['timezone'] instanceof DateTimeZone)) { |
|
| 70 | + if (!($this->options['timezone'] instanceof DateTimeZone)) { |
|
| 71 | 71 | $this->options['timezone'] = new DateTimeZone((string) $this->options['timezone']); |
| 72 | 72 | } |
| 73 | 73 | $this->quoter = new MySQLQuoter($pdo, $this->options['timezone']); |
@@ -91,7 +91,7 @@ discard block |
||
| 91 | 91 | * @return VirtualTables |
| 92 | 92 | */ |
| 93 | 93 | public function getVirtualTables(): VirtualTables { |
| 94 | - if($this->virtualTables === null) { |
|
| 94 | + if ($this->virtualTables === null) { |
|
| 95 | 95 | $this->virtualTables = new VirtualTables(); |
| 96 | 96 | } |
| 97 | 97 | return $this->virtualTables; |
@@ -136,11 +136,11 @@ discard block |
||
| 136 | 136 | array $params = [] |
| 137 | 137 | ): int { |
| 138 | 138 | return $this->getQueryLoggers()->logRegion($query, fn() => |
| 139 | - $this->exceptionHandler(function () use ($query, $params) { |
|
| 139 | + $this->exceptionHandler(function() use ($query, $params) { |
|
| 140 | 140 | $stmt = $this->pdo->prepare($query); |
| 141 | 141 | $timer = microtime(true); |
| 142 | 142 | $stmt->execute($params); |
| 143 | - $this->queryLoggers->log($query, microtime(true) - $timer); |
|
| 143 | + $this->queryLoggers->log($query, microtime(true)-$timer); |
|
| 144 | 144 | $result = $stmt->rowCount(); |
| 145 | 145 | $stmt->closeCursor(); |
| 146 | 146 | return $result; |
@@ -154,7 +154,7 @@ discard block |
||
| 154 | 154 | */ |
| 155 | 155 | public function getLastInsertId(?string $name = null): ?string { |
| 156 | 156 | $result = $this->pdo->lastInsertId(); |
| 157 | - if($result === false) { |
|
| 157 | + if ($result === false) { |
|
| 158 | 158 | return null; |
| 159 | 159 | } |
| 160 | 160 | return $result; |
@@ -166,15 +166,15 @@ discard block |
||
| 166 | 166 | */ |
| 167 | 167 | public function getTableFields(string $table): array { |
| 168 | 168 | $fqTable = $this->select()->aliasReplacer()->replace($table); |
| 169 | - if(array_key_exists($fqTable, $this->tableFields)) { |
|
| 169 | + if (array_key_exists($fqTable, $this->tableFields)) { |
|
| 170 | 170 | return $this->tableFields[$fqTable]; |
| 171 | 171 | } |
| 172 | 172 | $query = "DESCRIBE {$fqTable}"; |
| 173 | 173 | return $this->getQueryLoggers()->logRegion($query, fn() => |
| 174 | - $this->exceptionHandler(function () use ($query, $fqTable) { |
|
| 174 | + $this->exceptionHandler(function() use ($query, $fqTable) { |
|
| 175 | 175 | $stmt = $this->pdo->query($query); |
| 176 | 176 | try { |
| 177 | - if($stmt === false) { |
|
| 177 | + if ($stmt === false) { |
|
| 178 | 178 | throw new RuntimeException('Invalid return type'); |
| 179 | 179 | } |
| 180 | 180 | $rows = $stmt->fetchAll(PDO::FETCH_ASSOC); |
@@ -182,7 +182,7 @@ discard block |
||
| 182 | 182 | return $this->tableFields[$fqTable]; |
| 183 | 183 | } finally { |
| 184 | 184 | try { |
| 185 | - if($stmt instanceof PDOStatement) { |
|
| 185 | + if ($stmt instanceof PDOStatement) { |
|
| 186 | 186 | $stmt->closeCursor(); |
| 187 | 187 | } |
| 188 | 188 | } catch (Throwable $e) {} |
@@ -224,7 +224,7 @@ discard block |
||
| 224 | 224 | $select = array_key_exists('select-factory', $this->options) |
| 225 | 225 | ? call_user_func($this->options['select-factory'], $this, $this->options['select-options']) |
| 226 | 226 | : new MySQL\MySQLRunnableSelect($this, $this->options['select-options']); |
| 227 | - if($fields !== null) { |
|
| 227 | + if ($fields !== null) { |
|
| 228 | 228 | $select->fields($fields); |
| 229 | 229 | } |
| 230 | 230 | return $select; |
@@ -238,7 +238,7 @@ discard block |
||
| 238 | 238 | $insert = array_key_exists('insert-factory', $this->options) |
| 239 | 239 | ? call_user_func($this->options['insert-factory'], $this, $this->options['insert-options']) |
| 240 | 240 | : new Builder\RunnableInsert($this, $this->options['insert-options']); |
| 241 | - if($fields !== null) { |
|
| 241 | + if ($fields !== null) { |
|
| 242 | 242 | $insert->addAll($fields); |
| 243 | 243 | } |
| 244 | 244 | return $insert; |
@@ -252,7 +252,7 @@ discard block |
||
| 252 | 252 | $update = array_key_exists('update-factory', $this->options) |
| 253 | 253 | ? call_user_func($this->options['update-factory'], $this, $this->options['update-options']) |
| 254 | 254 | : new Builder\RunnableUpdate($this, $this->options['update-options']); |
| 255 | - if($fields !== null) { |
|
| 255 | + if ($fields !== null) { |
|
| 256 | 256 | $update->setAll($fields); |
| 257 | 257 | } |
| 258 | 258 | return $update; |
@@ -271,8 +271,8 @@ discard block |
||
| 271 | 271 | * @return $this |
| 272 | 272 | */ |
| 273 | 273 | public function transactionStart() { |
| 274 | - if($this->transactionLevel === 0) { |
|
| 275 | - if($this->pdo->inTransaction()) { |
|
| 274 | + if ($this->transactionLevel === 0) { |
|
| 275 | + if ($this->pdo->inTransaction()) { |
|
| 276 | 276 | $this->outerTransaction = true; |
| 277 | 277 | } else { |
| 278 | 278 | $this->pdo->beginTransaction(); |
@@ -286,7 +286,7 @@ discard block |
||
| 286 | 286 | * @return $this |
| 287 | 287 | */ |
| 288 | 288 | public function transactionCommit() { |
| 289 | - return $this->transactionEnd(function () { |
|
| 289 | + return $this->transactionEnd(function() { |
|
| 290 | 290 | $this->pdo->commit(); |
| 291 | 291 | }); |
| 292 | 292 | } |
@@ -295,7 +295,7 @@ discard block |
||
| 295 | 295 | * @return $this |
| 296 | 296 | */ |
| 297 | 297 | public function transactionRollback() { |
| 298 | - return $this->transactionEnd(function () { |
|
| 298 | + return $this->transactionEnd(function() { |
|
| 299 | 299 | $this->pdo->rollBack(); |
| 300 | 300 | }); |
| 301 | 301 | } |
@@ -306,7 +306,7 @@ discard block |
||
| 306 | 306 | * @return T |
| 307 | 307 | */ |
| 308 | 308 | public function dryRun(callable $callback) { |
| 309 | - if(!$this->pdo->inTransaction()) { |
|
| 309 | + if (!$this->pdo->inTransaction()) { |
|
| 310 | 310 | $this->transactionStart(); |
| 311 | 311 | try { |
| 312 | 312 | return $callback($this); |
@@ -331,14 +331,14 @@ discard block |
||
| 331 | 331 | * @throws Throwable |
| 332 | 332 | */ |
| 333 | 333 | public function transaction(callable $callback) { |
| 334 | - if(!$this->pdo->inTransaction()) { |
|
| 334 | + if (!$this->pdo->inTransaction()) { |
|
| 335 | 335 | $this->transactionStart(); |
| 336 | 336 | try { |
| 337 | 337 | $result = $callback($this); |
| 338 | 338 | $this->transactionCommit(); |
| 339 | 339 | return $result; |
| 340 | 340 | } catch (Throwable $e) { |
| 341 | - if($this->pdo->inTransaction()) { |
|
| 341 | + if ($this->pdo->inTransaction()) { |
|
| 342 | 342 | $this->transactionRollback(); |
| 343 | 343 | } |
| 344 | 344 | throw $e; |
@@ -362,11 +362,11 @@ discard block |
||
| 362 | 362 | */ |
| 363 | 363 | private function transactionEnd($fn): self { |
| 364 | 364 | $this->transactionLevel--; |
| 365 | - if($this->transactionLevel < 0) { |
|
| 365 | + if ($this->transactionLevel < 0) { |
|
| 366 | 366 | throw new RuntimeException("Transaction-Nesting-Problem: Trying to invoke commit on a already closed transaction"); |
| 367 | 367 | } |
| 368 | - if($this->transactionLevel < 1) { |
|
| 369 | - if($this->outerTransaction) { |
|
| 368 | + if ($this->transactionLevel < 1) { |
|
| 369 | + if ($this->outerTransaction) { |
|
| 370 | 370 | $this->outerTransaction = false; |
| 371 | 371 | } else { |
| 372 | 372 | $fn(); |
@@ -383,7 +383,7 @@ discard block |
||
| 383 | 383 | */ |
| 384 | 384 | private function buildQueryStatement(string $query, callable $fn): QueryStatement { |
| 385 | 385 | $stmt = $fn($query); |
| 386 | - if(!$stmt) { |
|
| 386 | + if (!$stmt) { |
|
| 387 | 387 | throw new RuntimeException("Could not execute statement:\n{$query}"); |
| 388 | 388 | } |
| 389 | 389 | return new QueryStatement($stmt, $query, $this->exceptionInterpreter, $this->queryLoggers); |
@@ -29,7 +29,7 @@ discard block |
||
| 29 | 29 | * @return $this |
| 30 | 30 | */ |
| 31 | 31 | public function table(string $alias, $table = null): self { |
| 32 | - if($table === null) { |
|
| 32 | + if ($table === null) { |
|
| 33 | 33 | [$alias, $table] = [$table, $alias]; |
| 34 | 34 | } |
| 35 | 35 | $this->addTable($alias, $table); |
@@ -64,7 +64,7 @@ discard block |
||
| 64 | 64 | * @return $this |
| 65 | 65 | */ |
| 66 | 66 | public function setExpr(string $expr, ...$args): self { |
| 67 | - if(count($args) > 0) { |
|
| 67 | + if (count($args) > 0) { |
|
| 68 | 68 | $this->fields[] = func_get_args(); |
| 69 | 69 | } else { |
| 70 | 70 | $this->fields[] = $expr; |
@@ -78,15 +78,15 @@ discard block |
||
| 78 | 78 | * @return $this |
| 79 | 79 | */ |
| 80 | 80 | public function setAll(array $data, ?array $allowedFields = null): self { |
| 81 | - if($allowedFields !== null) { |
|
| 82 | - foreach($data as $fieldName => $value) { |
|
| 83 | - if(in_array($fieldName, $allowedFields)) { |
|
| 81 | + if ($allowedFields !== null) { |
|
| 82 | + foreach ($data as $fieldName => $value) { |
|
| 83 | + if (in_array($fieldName, $allowedFields)) { |
|
| 84 | 84 | $this->set($fieldName, $value); |
| 85 | 85 | } |
| 86 | 86 | } |
| 87 | 87 | } else { |
| 88 | 88 | $values = $this->clearValues($data); |
| 89 | - foreach($values as $fieldName => $value) { |
|
| 89 | + foreach ($values as $fieldName => $value) { |
|
| 90 | 90 | $this->set($fieldName, $value); |
| 91 | 91 | } |
| 92 | 92 | } |
@@ -121,7 +121,7 @@ discard block |
||
| 121 | 121 | */ |
| 122 | 122 | private function buildAssignments(string $query): string { |
| 123 | 123 | $sqlFields = $this->buildFieldList($this->fields); |
| 124 | - if(!count($sqlFields)) { |
|
| 124 | + if (!count($sqlFields)) { |
|
| 125 | 125 | throw new RuntimeException('No field-data found'); |
| 126 | 126 | } |
| 127 | 127 | return sprintf("%s%s\n", $query, implode(",\n", $sqlFields)); |
@@ -132,14 +132,14 @@ discard block |
||
| 132 | 132 | * @return array<string, mixed> |
| 133 | 133 | */ |
| 134 | 134 | private function clearValues(array $values): array { |
| 135 | - if(!count($values)) { |
|
| 135 | + if (!count($values)) { |
|
| 136 | 136 | return []; |
| 137 | 137 | } |
| 138 | 138 | $tables = $this->getTables(); |
| 139 | - if(!count($tables)) { |
|
| 139 | + if (!count($tables)) { |
|
| 140 | 140 | throw new RuntimeException('Table name is missing'); |
| 141 | 141 | } |
| 142 | - if(count($tables) > 1) { |
|
| 142 | + if (count($tables) > 1) { |
|
| 143 | 143 | throw new RuntimeException('Batch values only work with max. one table'); |
| 144 | 144 | } |
| 145 | 145 | |
@@ -147,11 +147,11 @@ discard block |
||
| 147 | 147 | $tableName = $table['name']; |
| 148 | 148 | |
| 149 | 149 | $result = []; |
| 150 | - if(is_string($tableName)) { |
|
| 150 | + if (is_string($tableName)) { |
|
| 151 | 151 | $fields = $this->db()->getTableFields($tableName); |
| 152 | 152 | |
| 153 | - foreach($values as $fieldName => $fieldValue) { |
|
| 154 | - if(in_array($fieldName, $fields, true)) { |
|
| 153 | + foreach ($values as $fieldName => $fieldValue) { |
|
| 154 | + if (in_array($fieldName, $fields, true)) { |
|
| 155 | 155 | $result[$fieldName] = $fieldValue; |
| 156 | 156 | } |
| 157 | 157 | } |
@@ -89,7 +89,7 @@ discard block |
||
| 89 | 89 | * @return $this |
| 90 | 90 | */ |
| 91 | 91 | public function addExpr(string $str, ...$args) { |
| 92 | - if(count($args) > 0) { |
|
| 92 | + if (count($args) > 0) { |
|
| 93 | 93 | $this->fields[] = func_get_args(); |
| 94 | 94 | } else { |
| 95 | 95 | $this->fields[] = $str; |
@@ -103,7 +103,7 @@ discard block |
||
| 103 | 103 | * @return $this |
| 104 | 104 | */ |
| 105 | 105 | public function updateExpr(string $str, ...$args) { |
| 106 | - if(count($args) > 0) { |
|
| 106 | + if (count($args) > 0) { |
|
| 107 | 107 | $this->update[] = func_get_args(); |
| 108 | 108 | } else { |
| 109 | 109 | $this->update[] = $str; |
@@ -117,7 +117,7 @@ discard block |
||
| 117 | 117 | * @return $this |
| 118 | 118 | */ |
| 119 | 119 | public function addOrUpdateExpr(string $expr, ...$args) { |
| 120 | - if(count($args) > 0) { |
|
| 120 | + if (count($args) > 0) { |
|
| 121 | 121 | $this->fields[] = func_get_args(); |
| 122 | 122 | $this->update[] = func_get_args(); |
| 123 | 123 | } else { |
@@ -134,7 +134,7 @@ discard block |
||
| 134 | 134 | * @return $this |
| 135 | 135 | */ |
| 136 | 136 | public function addAll(array $data, ?array $mask = null, ?array $excludeFields = null) { |
| 137 | - $this->addAllTo($data, $mask, $excludeFields, function ($field, $value) { |
|
| 137 | + $this->addAllTo($data, $mask, $excludeFields, function($field, $value) { |
|
| 138 | 138 | $this->add($field, $value); |
| 139 | 139 | }); |
| 140 | 140 | return $this; |
@@ -147,8 +147,8 @@ discard block |
||
| 147 | 147 | * @return $this |
| 148 | 148 | */ |
| 149 | 149 | public function updateAll(array $data, ?array $mask = null, ?array $excludeFields = null) { |
| 150 | - $this->addAllTo($data, $mask, $excludeFields, function ($field, $value) { |
|
| 151 | - if($field !== $this->keyField) { |
|
| 150 | + $this->addAllTo($data, $mask, $excludeFields, function($field, $value) { |
|
| 151 | + if ($field !== $this->keyField) { |
|
| 152 | 152 | $this->update($field, $value); |
| 153 | 153 | } |
| 154 | 154 | }); |
@@ -192,7 +192,7 @@ discard block |
||
| 192 | 192 | * @return string |
| 193 | 193 | */ |
| 194 | 194 | public function __toString(): string { |
| 195 | - if($this->table === null) { |
|
| 195 | + if ($this->table === null) { |
|
| 196 | 196 | throw new RuntimeException('Specify a table-name'); |
| 197 | 197 | } |
| 198 | 198 | |
@@ -202,21 +202,21 @@ discard block |
||
| 202 | 202 | $ignoreStr = $this->ignore ? ' IGNORE' : ''; |
| 203 | 203 | $queryArr[] = "INSERT{$ignoreStr} INTO\n\t{$tableName}\n"; |
| 204 | 204 | |
| 205 | - if($this->from !== null) { |
|
| 205 | + if ($this->from !== null) { |
|
| 206 | 206 | $fields = $this->from->getFields(); |
| 207 | 207 | $queryArr[] = sprintf("\t(%s)\n", implode(', ', array_keys($fields))); |
| 208 | 208 | $queryArr[] = $this->from; |
| 209 | 209 | } else { |
| 210 | 210 | $fields = $this->fields; |
| 211 | 211 | $insertData = $this->buildFieldList($fields); |
| 212 | - if(!count($insertData)) { |
|
| 212 | + if (!count($insertData)) { |
|
| 213 | 213 | throw new RuntimeException('No field-data found'); |
| 214 | 214 | } |
| 215 | 215 | $queryArr[] = sprintf("SET\n%s\n", implode(",\n", $insertData)); |
| 216 | 216 | } |
| 217 | 217 | |
| 218 | 218 | $updateData = $this->buildUpdate(); |
| 219 | - if($updateData) { |
|
| 219 | + if ($updateData) { |
|
| 220 | 220 | $queryArr[] = "{$updateData}\n"; |
| 221 | 221 | } |
| 222 | 222 | |
@@ -230,7 +230,7 @@ discard block |
||
| 230 | 230 | * @return array<string|int, mixed> |
| 231 | 231 | */ |
| 232 | 232 | private function addTo(array $fields, string $field, $value): array { |
| 233 | - if(!$this->isFieldNameValid($field)) { |
|
| 233 | + if (!$this->isFieldNameValid($field)) { |
|
| 234 | 234 | throw new UnexpectedValueException('Field name is invalid'); |
| 235 | 235 | } |
| 236 | 236 | $sqlField = $field; |
@@ -246,18 +246,18 @@ discard block |
||
| 246 | 246 | * @param callable(string, mixed): void $fn |
| 247 | 247 | */ |
| 248 | 248 | private function addAllTo(array $data, ?array $mask, ?array $excludeFields, $fn): void { |
| 249 | - if($mask !== null) { |
|
| 249 | + if ($mask !== null) { |
|
| 250 | 250 | $data = array_intersect_key($data, array_combine($mask, $mask)); |
| 251 | 251 | } |
| 252 | - if($excludeFields !== null) { |
|
| 253 | - foreach($excludeFields as $excludeField) { |
|
| 254 | - if(array_key_exists($excludeField, $data)) { |
|
| 252 | + if ($excludeFields !== null) { |
|
| 253 | + foreach ($excludeFields as $excludeField) { |
|
| 254 | + if (array_key_exists($excludeField, $data)) { |
|
| 255 | 255 | unset($data[$excludeField]); |
| 256 | 256 | } |
| 257 | 257 | } |
| 258 | 258 | } |
| 259 | 259 | $data = $this->clearValues($data); |
| 260 | - foreach($data as $field => $value) { |
|
| 260 | + foreach ($data as $field => $value) { |
|
| 261 | 261 | $fn($field, $value); |
| 262 | 262 | } |
| 263 | 263 | } |
@@ -267,10 +267,10 @@ discard block |
||
| 267 | 267 | */ |
| 268 | 268 | private function buildUpdate(): string { |
| 269 | 269 | $queryArr = []; |
| 270 | - if(!empty($this->update)) { |
|
| 270 | + if (!empty($this->update)) { |
|
| 271 | 271 | $queryArr[] = "ON DUPLICATE KEY UPDATE\n"; |
| 272 | 272 | $updateArr = []; |
| 273 | - if($this->keyField !== null) { |
|
| 273 | + if ($this->keyField !== null) { |
|
| 274 | 274 | $updateArr[] = "\t`{$this->keyField}` = LAST_INSERT_ID({$this->keyField})"; |
| 275 | 275 | } |
| 276 | 276 | $updateArr = $this->buildFieldList($this->update, $updateArr); |
@@ -293,7 +293,7 @@ discard block |
||
| 293 | 293 | * @return array<string, mixed> |
| 294 | 294 | */ |
| 295 | 295 | private function clearValues(array $values): array { |
| 296 | - if(!count($values)) { |
|
| 296 | + if (!count($values)) { |
|
| 297 | 297 | return []; |
| 298 | 298 | } |
| 299 | 299 | |
@@ -301,8 +301,8 @@ discard block |
||
| 301 | 301 | $fields = $this->db()->getTableFields($tableName); |
| 302 | 302 | $result = []; |
| 303 | 303 | |
| 304 | - foreach($values as $fieldName => $fieldValue) { |
|
| 305 | - if(in_array($fieldName, $fields)) { |
|
| 304 | + foreach ($values as $fieldName => $fieldValue) { |
|
| 305 | + if (in_array($fieldName, $fields)) { |
|
| 306 | 306 | $result[$fieldName] = $fieldValue; |
| 307 | 307 | } |
| 308 | 308 | } |