@@ -9,8 +9,8 @@ |
||
| 9 | 9 | */ |
| 10 | 10 | public static function convertValues(array $row, array $columnDefinitions): array { |
| 11 | 11 | $result = []; |
| 12 | - foreach($row as $key => $value) { |
|
| 13 | - if($value !== null) { |
|
| 12 | + foreach ($row as $key => $value) { |
|
| 13 | + if ($value !== null) { |
|
| 14 | 14 | $result[$key] = self::convertValue($value, $columnDefinitions[$key]); |
| 15 | 15 | } else { |
| 16 | 16 | $result[$key] = $value; |
@@ -13,15 +13,15 @@ |
||
| 13 | 13 | * @return string |
| 14 | 14 | */ |
| 15 | 15 | public static function build(Database $db, string $query, array $conditions, string $token): string { |
| 16 | - if(!count($conditions)) { |
|
| 16 | + if (!count($conditions)) { |
|
| 17 | 17 | return $query; |
| 18 | 18 | } |
| 19 | 19 | $query .= "{$token}\n"; |
| 20 | 20 | $arr = []; |
| 21 | - foreach($conditions as [$expression, $arguments]) { |
|
| 22 | - if(is_array($expression)) { |
|
| 23 | - foreach($expression as $key => $value) { |
|
| 24 | - if($value === null) { |
|
| 21 | + foreach ($conditions as [$expression, $arguments]) { |
|
| 22 | + if (is_array($expression)) { |
|
| 23 | + foreach ($expression as $key => $value) { |
|
| 24 | + if ($value === null) { |
|
| 25 | 25 | $arr = self::buildCondition($arr, "ISNULL(`{$key}`)", [$value], $db); |
| 26 | 26 | } else { |
| 27 | 27 | $arr = self::buildCondition($arr, "`{$key}`=?", [$value], $db); |
@@ -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 | } |
@@ -29,14 +29,14 @@ |
||
| 29 | 29 | * @return $this |
| 30 | 30 | */ |
| 31 | 31 | public function debug($stop = true) { |
| 32 | - if(array_key_exists('debug_formatter', $this->options)) { |
|
| 32 | + if (array_key_exists('debug_formatter', $this->options)) { |
|
| 33 | 33 | $this->options['debug_formatter'](); |
| 34 | - } elseif(PHP_SAPI === 'cli') { |
|
| 34 | + } elseif (PHP_SAPI === 'cli') { |
|
| 35 | 35 | echo "\n{$this->__toString()}\n"; |
| 36 | 36 | } else { |
| 37 | 37 | echo "<pre>{$this->__toString()}</pre>"; |
| 38 | 38 | } |
| 39 | - if($stop) { |
|
| 39 | + if ($stop) { |
|
| 40 | 40 | exit; |
| 41 | 41 | } |
| 42 | 42 | return $this; |
@@ -78,7 +78,7 @@ discard block |
||
| 78 | 78 | * @inheritDoc |
| 79 | 79 | */ |
| 80 | 80 | public function fetchRowsLazy($callback = null) { |
| 81 | - $callback = $callback ?? (static function ($row) { return $row; }); |
|
| 81 | + $callback = $callback ?? (static function($row) { return $row; }); |
|
| 82 | 82 | yield from $this->fetchLazy($callback, PDO::FETCH_ASSOC); |
| 83 | 83 | } |
| 84 | 84 | |
@@ -86,8 +86,8 @@ discard block |
||
| 86 | 86 | * @inheritDoc |
| 87 | 87 | */ |
| 88 | 88 | public function fetchRow($callback = null): array { |
| 89 | - $callback = $callback ?? (static function ($row) { return $row; }); |
|
| 90 | - return $this->fetch($callback, PDO::FETCH_ASSOC, null, static function ($row) { |
|
| 89 | + $callback = $callback ?? (static function($row) { return $row; }); |
|
| 90 | + return $this->fetch($callback, PDO::FETCH_ASSOC, null, static function($row) { |
|
| 91 | 91 | return ['valid' => is_array($row), 'default' => []]; |
| 92 | 92 | }); |
| 93 | 93 | } |
@@ -96,17 +96,17 @@ discard block |
||
| 96 | 96 | * @inheritDoc |
| 97 | 97 | */ |
| 98 | 98 | public function fetchObjects(string $className = 'stdClass', $callback = null): array { |
| 99 | - return $this->createTempStatement(function (QueryStatement $statement) use ($className, $callback) { |
|
| 99 | + return $this->createTempStatement(function(QueryStatement $statement) use ($className, $callback) { |
|
| 100 | 100 | $data = $statement->fetchAll(PDO::FETCH_CLASS, $className); |
| 101 | - if($this->preserveTypes) { |
|
| 101 | + if ($this->preserveTypes) { |
|
| 102 | 102 | $columnDefinitions = FieldTypeProvider::getFieldTypes($statement); |
| 103 | - $data = array_map(static function ($row) use ($columnDefinitions) { return FieldValueConverter::convertValues($row, $columnDefinitions); }, $data); |
|
| 103 | + $data = array_map(static function($row) use ($columnDefinitions) { return FieldValueConverter::convertValues($row, $columnDefinitions); }, $data); |
|
| 104 | 104 | } |
| 105 | - if($callback !== null) { |
|
| 106 | - return call_user_func(static function ($resultData = []) use ($data, $callback) { |
|
| 107 | - foreach($data as $row) { |
|
| 105 | + if ($callback !== null) { |
|
| 106 | + return call_user_func(static function($resultData = []) use ($data, $callback) { |
|
| 107 | + foreach ($data as $row) { |
|
| 108 | 108 | $result = $callback($row); |
| 109 | - if($result !== null && !($result instanceof DBIgnoreRow)) { |
|
| 109 | + if ($result !== null && !($result instanceof DBIgnoreRow)) { |
|
| 110 | 110 | $resultData[] = $result; |
| 111 | 111 | } else { |
| 112 | 112 | $resultData[] = $row; |
@@ -123,7 +123,7 @@ discard block |
||
| 123 | 123 | * @inheritDoc |
| 124 | 124 | */ |
| 125 | 125 | public function fetchObjectsLazy($className = null, $callback = null) { |
| 126 | - $callback = $callback ?? (static function ($row) { return $row; }); |
|
| 126 | + $callback = $callback ?? (static function($row) { return $row; }); |
|
| 127 | 127 | yield from $this->fetchLazy($callback, PDO::FETCH_CLASS, $className ?: $this->defaultClassName); |
| 128 | 128 | } |
| 129 | 129 | |
@@ -131,8 +131,8 @@ discard block |
||
| 131 | 131 | * @inheritDoc |
| 132 | 132 | */ |
| 133 | 133 | public function fetchObject($className = null, $callback = null) { |
| 134 | - $callback = $callback ?? (static function ($row) { return $row; }); |
|
| 135 | - return $this->fetch($callback, PDO::FETCH_CLASS, $className ?: $this->defaultClassName, static function ($row) { |
|
| 134 | + $callback = $callback ?? (static function($row) { return $row; }); |
|
| 135 | + return $this->fetch($callback, PDO::FETCH_CLASS, $className ?: $this->defaultClassName, static function($row) { |
|
| 136 | 136 | return ['valid' => is_object($row), 'default' => null]; |
| 137 | 137 | }); |
| 138 | 138 | } |
@@ -141,11 +141,11 @@ discard block |
||
| 141 | 141 | * @inheritDoc |
| 142 | 142 | */ |
| 143 | 143 | public function fetchKeyValue($treatValueAsArray = false): array { |
| 144 | - return $this->createTempStatement(static function (QueryStatement $statement) use ($treatValueAsArray) { |
|
| 145 | - if($treatValueAsArray) { |
|
| 144 | + return $this->createTempStatement(static function(QueryStatement $statement) use ($treatValueAsArray) { |
|
| 145 | + if ($treatValueAsArray) { |
|
| 146 | 146 | $rows = $statement->fetchAll(PDO::FETCH_ASSOC); |
| 147 | 147 | $result = []; |
| 148 | - foreach($rows as $row) { |
|
| 148 | + foreach ($rows as $row) { |
|
| 149 | 149 | [$key] = array_values($row); |
| 150 | 150 | $result[$key] = $row; |
| 151 | 151 | } |
@@ -161,12 +161,12 @@ discard block |
||
| 161 | 161 | public function fetchGroups(array $fields): array { |
| 162 | 162 | $rows = $this->fetchRows(); |
| 163 | 163 | $result = []; |
| 164 | - foreach($rows as $row) { |
|
| 164 | + foreach ($rows as $row) { |
|
| 165 | 165 | /** @var array<string, mixed> $tmp */ |
| 166 | 166 | $tmp = &$result; |
| 167 | - foreach($fields as $field) { |
|
| 167 | + foreach ($fields as $field) { |
|
| 168 | 168 | $value = (string) $row[$field]; |
| 169 | - if(!array_key_exists($value, $tmp)) { |
|
| 169 | + if (!array_key_exists($value, $tmp)) { |
|
| 170 | 170 | $tmp[$value] = []; |
| 171 | 171 | } |
| 172 | 172 | $tmp = &$tmp[$value]; |
@@ -180,8 +180,8 @@ discard block |
||
| 180 | 180 | * @inheritDoc |
| 181 | 181 | */ |
| 182 | 182 | public function fetchArray(?callable $fn = null): array { |
| 183 | - return $this->createTempStatement(static function (QueryStatement $stmt) use ($fn) { |
|
| 184 | - if($fn !== null) { |
|
| 183 | + return $this->createTempStatement(static function(QueryStatement $stmt) use ($fn) { |
|
| 184 | + if ($fn !== null) { |
|
| 185 | 185 | return $stmt->fetchAll(PDO::FETCH_FUNC, $fn); |
| 186 | 186 | } |
| 187 | 187 | return $stmt->fetchAll(PDO::FETCH_COLUMN); |
@@ -192,9 +192,9 @@ discard block |
||
| 192 | 192 | * @inheritDoc |
| 193 | 193 | */ |
| 194 | 194 | public function fetchValue($default = null, ?callable $fn = null) { |
| 195 | - return $this->createTempStatement(static function (QueryStatement $stmt) use ($default, $fn) { |
|
| 195 | + return $this->createTempStatement(static function(QueryStatement $stmt) use ($default, $fn) { |
|
| 196 | 196 | $result = $stmt->fetchAll(PDO::FETCH_COLUMN); |
| 197 | - if($result !== false && array_key_exists(0, $result)) { |
|
| 197 | + if ($result !== false && array_key_exists(0, $result)) { |
|
| 198 | 198 | return $fn !== null ? $fn($result[0]) : $result[0]; |
| 199 | 199 | } |
| 200 | 200 | return $default; |
@@ -229,7 +229,7 @@ discard block |
||
| 229 | 229 | $query = $this->__toString(); |
| 230 | 230 | $statement = $db->prepare($query); |
| 231 | 231 | $statement->execute($this->values); |
| 232 | - if($this->getCalcFoundRows()) { |
|
| 232 | + if ($this->getCalcFoundRows()) { |
|
| 233 | 233 | $this->foundRows = (int) $db->query('SELECT FOUND_ROWS()')->fetchColumn(); |
| 234 | 234 | } |
| 235 | 235 | return $statement; |
@@ -249,21 +249,21 @@ discard block |
||
| 249 | 249 | * @return array<string, mixed>[] |
| 250 | 250 | */ |
| 251 | 251 | private function fetchAll($callback = null, int $mode = 0, $arg0 = null) { |
| 252 | - return $this->createTempStatement(function (QueryStatement $statement) use ($callback, $mode, $arg0) { |
|
| 252 | + return $this->createTempStatement(function(QueryStatement $statement) use ($callback, $mode, $arg0) { |
|
| 253 | 253 | $statement->setFetchMode($mode, $arg0); |
| 254 | 254 | $data = $statement->fetchAll(); |
| 255 | - if($this->preserveTypes) { |
|
| 255 | + if ($this->preserveTypes) { |
|
| 256 | 256 | $columnDefinitions = FieldTypeProvider::getFieldTypes($statement); |
| 257 | - $data = array_map(static function ($row) use ($columnDefinitions) { return FieldValueConverter::convertValues($row, $columnDefinitions); }, $data); |
|
| 257 | + $data = array_map(static function($row) use ($columnDefinitions) { return FieldValueConverter::convertValues($row, $columnDefinitions); }, $data); |
|
| 258 | 258 | } |
| 259 | - if($callback !== null) { |
|
| 260 | - return call_user_func(static function ($resultData = []) use ($data, $callback) { |
|
| 261 | - foreach($data as $row) { |
|
| 259 | + if ($callback !== null) { |
|
| 260 | + return call_user_func(static function($resultData = []) use ($data, $callback) { |
|
| 261 | + foreach ($data as $row) { |
|
| 262 | 262 | $result = $callback($row); |
| 263 | - if($result instanceof DBIgnoreRow) { |
|
| 263 | + if ($result instanceof DBIgnoreRow) { |
|
| 264 | 264 | continue; |
| 265 | 265 | } |
| 266 | - if($result !== null) { |
|
| 266 | + if ($result !== null) { |
|
| 267 | 267 | $resultData[] = $result; |
| 268 | 268 | } else { |
| 269 | 269 | $resultData[] = $row; |
@@ -288,18 +288,18 @@ discard block |
||
| 288 | 288 | $statement = $this->createStatement(); |
| 289 | 289 | $statement->setFetchMode($mode, $arg0); |
| 290 | 290 | try { |
| 291 | - while($row = $statement->fetch()) { |
|
| 291 | + while ($row = $statement->fetch()) { |
|
| 292 | 292 | /** @var T $row */ |
| 293 | 293 | // if($this->preserveTypes) { |
| 294 | 294 | // $columnDefinitions = FieldTypeProvider::getFieldTypes($statement); |
| 295 | 295 | // $row = FieldValueConverter::convertValues($row, $columnDefinitions); |
| 296 | 296 | // } |
| 297 | 297 | $result = $callback($row); |
| 298 | - if($result instanceof DBIgnoreRow) { |
|
| 298 | + if ($result instanceof DBIgnoreRow) { |
|
| 299 | 299 | // Skip row in this case |
| 300 | 300 | continue; |
| 301 | 301 | } |
| 302 | - if($result !== null) { |
|
| 302 | + if ($result !== null) { |
|
| 303 | 303 | yield $result; |
| 304 | 304 | } else { |
| 305 | 305 | yield $row; |
@@ -320,20 +320,20 @@ discard block |
||
| 320 | 320 | * @return T|U|array<string, mixed> |
| 321 | 321 | */ |
| 322 | 322 | private function fetch($callback, int $mode = PDO::FETCH_ASSOC, $arg0 = null, Closure $resultValidator = null) { |
| 323 | - return $this->createTempStatement(function (QueryStatement $statement) use ($callback, $mode, $arg0, $resultValidator) { |
|
| 323 | + return $this->createTempStatement(function(QueryStatement $statement) use ($callback, $mode, $arg0, $resultValidator) { |
|
| 324 | 324 | $statement->setFetchMode($mode, $arg0); |
| 325 | 325 | $row = $statement->fetch(); |
| 326 | 326 | $result = $resultValidator === null ? ['valid' => true] : $resultValidator($row); |
| 327 | - if(!$result['valid']) { |
|
| 327 | + if (!$result['valid']) { |
|
| 328 | 328 | return $result['default']; |
| 329 | 329 | } |
| 330 | - if($this->preserveTypes) { |
|
| 330 | + if ($this->preserveTypes) { |
|
| 331 | 331 | $columnDefinitions = FieldTypeProvider::getFieldTypes($statement); |
| 332 | 332 | $row = FieldValueConverter::convertValues($row, $columnDefinitions); |
| 333 | 333 | } |
| 334 | - if($callback !== null) { |
|
| 334 | + if ($callback !== null) { |
|
| 335 | 335 | $result = $callback($row); |
| 336 | - if($result !== null) { |
|
| 336 | + if ($result !== null) { |
|
| 337 | 337 | $row = $result; |
| 338 | 338 | } |
| 339 | 339 | } |
@@ -125,7 +125,7 @@ |
||
| 125 | 125 | * @return $this |
| 126 | 126 | */ |
| 127 | 127 | public function from(?string $alias, $table = null) { |
| 128 | - if($table === null) { |
|
| 128 | + if ($table === null) { |
|
| 129 | 129 | [$alias, $table] = [$table, $alias]; |
| 130 | 130 | $this->addTable($alias, (string) $table); |
| 131 | 131 | } else { |
@@ -19,7 +19,7 @@ |
||
| 19 | 19 | * @return $this |
| 20 | 20 | */ |
| 21 | 21 | public function having($expression, ...$args) { |
| 22 | - $fn = function ($expression, $args) { $this->having[] = [$expression, $args]; }; |
|
| 22 | + $fn = function($expression, $args) { $this->having[] = [$expression, $args]; }; |
|
| 23 | 23 | ConditionAddHelper::addCondition($fn, $expression, $args); |
| 24 | 24 | return $this; |
| 25 | 25 | } |
@@ -19,7 +19,7 @@ |
||
| 19 | 19 | * @return $this |
| 20 | 20 | */ |
| 21 | 21 | public function where($expression, ...$args) { |
| 22 | - $fn = function ($expression, $args) { $this->where[] = [$expression, $args]; }; |
|
| 22 | + $fn = function($expression, $args) { $this->where[] = [$expression, $args]; }; |
|
| 23 | 23 | ConditionAddHelper::addCondition($fn, $expression, $args); |
| 24 | 24 | return $this; |
| 25 | 25 | } |
@@ -15,8 +15,8 @@ discard block |
||
| 15 | 15 | * @return $this |
| 16 | 16 | */ |
| 17 | 17 | public function orderBy($expression, string $direction = 'ASC') { |
| 18 | - if($expression instanceof OrderBySpecification) { |
|
| 19 | - foreach($expression->getFields() as $field) { |
|
| 18 | + if ($expression instanceof OrderBySpecification) { |
|
| 19 | + foreach ($expression->getFields() as $field) { |
|
| 20 | 20 | $this->addOrder($field[0], $field[1]); |
| 21 | 21 | } |
| 22 | 22 | return $this; |
@@ -32,7 +32,7 @@ discard block |
||
| 32 | 32 | */ |
| 33 | 33 | public function orderByValues(string $fieldName, array $values) { |
| 34 | 34 | $expr = []; |
| 35 | - foreach(array_values($values) as $idx => $value) { |
|
| 35 | + foreach (array_values($values) as $idx => $value) { |
|
| 36 | 36 | $expr[] = $this->db()->quoteExpression("WHEN ? THEN ?", [$value, $idx]); |
| 37 | 37 | } |
| 38 | 38 | $this->orderBy[] = [sprintf("CASE %s\n\t\t%s\n\tEND", $this->db()->quoteField($fieldName), implode("\n\t\t", $expr)), 'ASC']; |
@@ -44,12 +44,12 @@ discard block |
||
| 44 | 44 | * @return string |
| 45 | 45 | */ |
| 46 | 46 | protected function buildOrder(string $query): string { |
| 47 | - if(!count($this->orderBy)) { |
|
| 47 | + if (!count($this->orderBy)) { |
|
| 48 | 48 | return $query; |
| 49 | 49 | } |
| 50 | 50 | $query .= "ORDER BY\n"; |
| 51 | 51 | $arr = []; |
| 52 | - foreach($this->orderBy as [$expression, $direction]) { |
|
| 52 | + foreach ($this->orderBy as [$expression, $direction]) { |
|
| 53 | 53 | $arr[] = sprintf("\t%s %s", $expression, strtoupper($direction)); |
| 54 | 54 | } |
| 55 | 55 | return $query.implode(",\n", $arr)."\n"; |
@@ -61,8 +61,8 @@ discard block |
||
| 61 | 61 | */ |
| 62 | 62 | private function addOrder($expression, string $direction): void { |
| 63 | 63 | $direction = $this->fixDirection($direction); |
| 64 | - if(is_array($expression)) { |
|
| 65 | - if(count($expression) < 1) { |
|
| 64 | + if (is_array($expression)) { |
|
| 65 | + if (count($expression) < 1) { |
|
| 66 | 66 | return; |
| 67 | 67 | } |
| 68 | 68 | $expr = (string) $expression[0]; |