@@ -10,7 +10,7 @@ |
||
| 10 | 10 | */ |
| 11 | 11 | public static function getFieldTypes(QueryStatement $statement): array { |
| 12 | 12 | $fieldTypes = []; |
| 13 | - for($i = 0; $column = $statement->getColumnMeta($i); $i++) { |
|
| 13 | + for ($i = 0; $column = $statement->getColumnMeta($i); $i++) { |
|
| 14 | 14 | $fieldTypes[(string) $column['name']] = self::getTypeFromNativeType($column['native_type']); |
| 15 | 15 | } |
| 16 | 16 | return $fieldTypes; |
@@ -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); |
@@ -28,7 +28,7 @@ |
||
| 28 | 28 | public function run(array $params = []) { |
| 29 | 29 | $this->query->execute($params); |
| 30 | 30 | $response = $this->query->getStatement()->rowCount(); |
| 31 | - if($this->callbackFn !== null) { |
|
| 31 | + if ($this->callbackFn !== null) { |
|
| 32 | 32 | $response = call_user_func($this->callbackFn); |
| 33 | 33 | } |
| 34 | 34 | return $response; |
@@ -88,7 +88,7 @@ discard block |
||
| 88 | 88 | * @return $this |
| 89 | 89 | */ |
| 90 | 90 | public function addExpr(string $str, ...$args) { |
| 91 | - if(count($args) > 0) { |
|
| 91 | + if (count($args) > 0) { |
|
| 92 | 92 | $this->fields[] = func_get_args(); |
| 93 | 93 | } else { |
| 94 | 94 | $this->fields[] = $str; |
@@ -102,7 +102,7 @@ discard block |
||
| 102 | 102 | * @return $this |
| 103 | 103 | */ |
| 104 | 104 | public function updateExpr(string $str, ...$args) { |
| 105 | - if(count($args) > 0) { |
|
| 105 | + if (count($args) > 0) { |
|
| 106 | 106 | $this->update[] = func_get_args(); |
| 107 | 107 | } else { |
| 108 | 108 | $this->update[] = $str; |
@@ -116,7 +116,7 @@ discard block |
||
| 116 | 116 | * @return $this |
| 117 | 117 | */ |
| 118 | 118 | public function addOrUpdateExpr(string $expr, ...$args) { |
| 119 | - if(count($args) > 0) { |
|
| 119 | + if (count($args) > 0) { |
|
| 120 | 120 | $this->fields[] = func_get_args(); |
| 121 | 121 | $this->update[] = func_get_args(); |
| 122 | 122 | } else { |
@@ -133,7 +133,7 @@ discard block |
||
| 133 | 133 | * @return $this |
| 134 | 134 | */ |
| 135 | 135 | public function addAll(array $data, array $mask = null, array $excludeFields = null) { |
| 136 | - $this->addAllTo($data, $mask, $excludeFields, function ($field, $value) { |
|
| 136 | + $this->addAllTo($data, $mask, $excludeFields, function($field, $value) { |
|
| 137 | 137 | $this->add($field, $value); |
| 138 | 138 | }); |
| 139 | 139 | return $this; |
@@ -146,8 +146,8 @@ discard block |
||
| 146 | 146 | * @return $this |
| 147 | 147 | */ |
| 148 | 148 | public function updateAll(array $data, array $mask = null, array $excludeFields = null) { |
| 149 | - $this->addAllTo($data, $mask, $excludeFields, function ($field, $value) { |
|
| 150 | - if($field !== $this->keyField) { |
|
| 149 | + $this->addAllTo($data, $mask, $excludeFields, function($field, $value) { |
|
| 150 | + if ($field !== $this->keyField) { |
|
| 151 | 151 | $this->update($field, $value); |
| 152 | 152 | } |
| 153 | 153 | }); |
@@ -191,7 +191,7 @@ discard block |
||
| 191 | 191 | * @return string |
| 192 | 192 | */ |
| 193 | 193 | public function __toString(): string { |
| 194 | - if($this->table === null) { |
|
| 194 | + if ($this->table === null) { |
|
| 195 | 195 | throw new RuntimeException('Specify a table-name'); |
| 196 | 196 | } |
| 197 | 197 | |
@@ -201,21 +201,21 @@ discard block |
||
| 201 | 201 | $ignoreStr = $this->ignore ? ' IGNORE' : ''; |
| 202 | 202 | $queryArr[] = "INSERT{$ignoreStr} INTO\n\t{$tableName}\n"; |
| 203 | 203 | |
| 204 | - if($this->from !== null) { |
|
| 204 | + if ($this->from !== null) { |
|
| 205 | 205 | $fields = $this->from->getFields(); |
| 206 | 206 | $queryArr[] = sprintf("\t(%s)\n", implode(', ', array_keys($fields))); |
| 207 | 207 | $queryArr[] = $this->from; |
| 208 | 208 | } else { |
| 209 | 209 | $fields = $this->fields; |
| 210 | 210 | $insertData = $this->buildFieldList($fields); |
| 211 | - if(!count($insertData)) { |
|
| 211 | + if (!count($insertData)) { |
|
| 212 | 212 | throw new RuntimeException('No field-data found'); |
| 213 | 213 | } |
| 214 | 214 | $queryArr[] = sprintf("SET\n%s\n", implode(",\n", $insertData)); |
| 215 | 215 | } |
| 216 | 216 | |
| 217 | 217 | $updateData = $this->buildUpdate(); |
| 218 | - if($updateData) { |
|
| 218 | + if ($updateData) { |
|
| 219 | 219 | $queryArr[] = "{$updateData}\n"; |
| 220 | 220 | } |
| 221 | 221 | |
@@ -229,7 +229,7 @@ discard block |
||
| 229 | 229 | * @return array<string|int, mixed> |
| 230 | 230 | */ |
| 231 | 231 | private function addTo(array $fields, string $field, $value): array { |
| 232 | - if(!$this->isFieldNameValid($field)) { |
|
| 232 | + if (!$this->isFieldNameValid($field)) { |
|
| 233 | 233 | throw new UnexpectedValueException('Field name is invalid'); |
| 234 | 234 | } |
| 235 | 235 | $sqlField = $field; |
@@ -245,18 +245,18 @@ discard block |
||
| 245 | 245 | * @param callable(string, mixed): void $fn |
| 246 | 246 | */ |
| 247 | 247 | private function addAllTo(array $data, ?array $mask, ?array $excludeFields, callable $fn): void { |
| 248 | - if($mask !== null) { |
|
| 248 | + if ($mask !== null) { |
|
| 249 | 249 | $data = array_intersect_key($data, array_combine($mask, $mask)); |
| 250 | 250 | } |
| 251 | - if($excludeFields !== null) { |
|
| 252 | - foreach($excludeFields as $excludeField) { |
|
| 253 | - if(array_key_exists($excludeField, $data)) { |
|
| 251 | + if ($excludeFields !== null) { |
|
| 252 | + foreach ($excludeFields as $excludeField) { |
|
| 253 | + if (array_key_exists($excludeField, $data)) { |
|
| 254 | 254 | unset($data[$excludeField]); |
| 255 | 255 | } |
| 256 | 256 | } |
| 257 | 257 | } |
| 258 | 258 | $data = $this->clearValues($data); |
| 259 | - foreach($data as $field => $value) { |
|
| 259 | + foreach ($data as $field => $value) { |
|
| 260 | 260 | $fn($field, $value); |
| 261 | 261 | } |
| 262 | 262 | } |
@@ -266,10 +266,10 @@ discard block |
||
| 266 | 266 | */ |
| 267 | 267 | private function buildUpdate(): string { |
| 268 | 268 | $queryArr = []; |
| 269 | - if(!empty($this->update)) { |
|
| 269 | + if (!empty($this->update)) { |
|
| 270 | 270 | $queryArr[] = "ON DUPLICATE KEY UPDATE\n"; |
| 271 | 271 | $updateArr = []; |
| 272 | - if($this->keyField !== null) { |
|
| 272 | + if ($this->keyField !== null) { |
|
| 273 | 273 | $updateArr[] = "\t`{$this->keyField}` = LAST_INSERT_ID({$this->keyField})"; |
| 274 | 274 | } |
| 275 | 275 | $updateArr = $this->buildFieldList($this->update, $updateArr); |
@@ -292,7 +292,7 @@ discard block |
||
| 292 | 292 | * @return array<string, mixed> |
| 293 | 293 | */ |
| 294 | 294 | private function clearValues(array $values): array { |
| 295 | - if(!count($values)) { |
|
| 295 | + if (!count($values)) { |
|
| 296 | 296 | return []; |
| 297 | 297 | } |
| 298 | 298 | |
@@ -300,8 +300,8 @@ discard block |
||
| 300 | 300 | $fields = $this->db()->getTableFields($tableName); |
| 301 | 301 | $result = []; |
| 302 | 302 | |
| 303 | - foreach($values as $fieldName => $fieldValue) { |
|
| 304 | - if(in_array($fieldName, $fields)) { |
|
| 303 | + foreach ($values as $fieldName => $fieldValue) { |
|
| 304 | + if (in_array($fieldName, $fields)) { |
|
| 305 | 305 | $result[$fieldName] = $fieldValue; |
| 306 | 306 | } |
| 307 | 307 | } |
@@ -122,7 +122,7 @@ |
||
| 122 | 122 | * @return $this |
| 123 | 123 | */ |
| 124 | 124 | public function from(?string $alias, $table = null): self { |
| 125 | - if($table === null) { |
|
| 125 | + if ($table === null) { |
|
| 126 | 126 | [$alias, $table] = [$table, $alias]; |
| 127 | 127 | $this->addTable($alias, (string) $table); |
| 128 | 128 | } else { |
@@ -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 | } |
@@ -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); |