@@ -25,7 +25,7 @@ |
||
25 | 25 | public function run(array $params = []) { |
26 | 26 | $this->query->execute($params); |
27 | 27 | $response = $this->query->getStatement()->rowCount(); |
28 | - if($this->callbackFn !== null) { |
|
28 | + if ($this->callbackFn !== null) { |
|
29 | 29 | $response = call_user_func($this->callbackFn, $response); |
30 | 30 | } |
31 | 31 | return $response; |
@@ -19,7 +19,7 @@ |
||
19 | 19 | /** @link http://php.net/manual/en/class.exception.php#Hcom115813 (cHao's comment) */ |
20 | 20 | $code = is_array($errorInfo) && isset($errorInfo[1]) ? ((int) $errorInfo[1]) : ((int) $exception->getCode()); |
21 | 21 | $message = $exception->getMessage(); |
22 | - switch($code) { |
|
22 | + switch ($code) { |
|
23 | 23 | case 2006: throw new DatabaseHasGoneAwayException($message, $code, $exception); |
24 | 24 | case 1213: throw new SqlDeadLockException($message, $code, $exception); |
25 | 25 | case 1205: throw new LockWaitTimeoutExceededException($message, $code, $exception); |
@@ -11,13 +11,13 @@ discard block |
||
11 | 11 | * @param mixed[] $args |
12 | 12 | */ |
13 | 13 | public static function addCondition(Closure $addFn, $expression, ...$args) { |
14 | - if($expression instanceof OptionalExpression) { |
|
15 | - if($expression->isValid()) { |
|
14 | + if ($expression instanceof OptionalExpression) { |
|
15 | + if ($expression->isValid()) { |
|
16 | 16 | $addFn($expression->getExpression(), $expression->getValue()); |
17 | 17 | } |
18 | - } elseif(is_object($expression)) { |
|
18 | + } elseif (is_object($expression)) { |
|
19 | 19 | self::addAsArray($addFn, (array) $expression, $args); |
20 | - } elseif(is_array($expression)) { |
|
20 | + } elseif (is_array($expression)) { |
|
21 | 21 | self::addAsArray($addFn, $expression, $args); |
22 | 22 | } else { |
23 | 23 | $addFn($expression, $args); |
@@ -30,7 +30,7 @@ discard block |
||
30 | 30 | * @param array $args |
31 | 31 | */ |
32 | 32 | private static function addAsArray(Closure $addFn, array $expression, array $args) { |
33 | - if(count($expression) > 0) { |
|
33 | + if (count($expression) > 0) { |
|
34 | 34 | $addFn($expression, $args); |
35 | 35 | } |
36 | 36 | } |
@@ -17,7 +17,7 @@ |
||
17 | 17 | * @return $this |
18 | 18 | */ |
19 | 19 | public function having($expression, ...$args) { |
20 | - $fn = function (...$args) { $this->having[] = $args; }; |
|
20 | + $fn = function(...$args) { $this->having[] = $args; }; |
|
21 | 21 | ConditionAddHelper::addCondition($fn, $expression, ...$args); |
22 | 22 | return $this; |
23 | 23 | } |
@@ -17,7 +17,7 @@ |
||
17 | 17 | * @return $this |
18 | 18 | */ |
19 | 19 | public function where($expression, ...$args) { |
20 | - $fn = function (...$args) { $this->where[] = $args; }; |
|
20 | + $fn = function(...$args) { $this->where[] = $args; }; |
|
21 | 21 | ConditionAddHelper::addCondition($fn, $expression, ...$args); |
22 | 22 | return $this; |
23 | 23 | } |
@@ -41,7 +41,7 @@ discard block |
||
41 | 41 | * @param array $options |
42 | 42 | */ |
43 | 43 | public function __construct(PDO $pdo, array $options = []) { |
44 | - if($pdo->getAttribute(PDO::ATTR_ERRMODE) === PDO::ERRMODE_SILENT) { |
|
44 | + if ($pdo->getAttribute(PDO::ATTR_ERRMODE) === PDO::ERRMODE_SILENT) { |
|
45 | 45 | $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); |
46 | 46 | } |
47 | 47 | $this->pdo = $pdo; |
@@ -75,7 +75,7 @@ discard block |
||
75 | 75 | * @return VirtualTables |
76 | 76 | */ |
77 | 77 | public function getVirtualTables(): VirtualTables { |
78 | - if($this->virtualTables === null) { |
|
78 | + if ($this->virtualTables === null) { |
|
79 | 79 | $this->virtualTables = new VirtualTables(); |
80 | 80 | } |
81 | 81 | return $this->virtualTables; |
@@ -86,7 +86,7 @@ discard block |
||
86 | 86 | * @return QueryStatement |
87 | 87 | */ |
88 | 88 | public function query(string $query) { |
89 | - return $this->buildQueryStatement($query, function ($query) { |
|
89 | + return $this->buildQueryStatement($query, function($query) { |
|
90 | 90 | return $this->pdo->query($query); |
91 | 91 | }); |
92 | 92 | } |
@@ -96,7 +96,7 @@ discard block |
||
96 | 96 | * @return QueryStatement |
97 | 97 | */ |
98 | 98 | public function prepare(string $query) { |
99 | - return $this->buildQueryStatement((string) $query, function ($query) { |
|
99 | + return $this->buildQueryStatement((string) $query, function($query) { |
|
100 | 100 | return $this->pdo->prepare($query); |
101 | 101 | }); |
102 | 102 | } |
@@ -107,11 +107,11 @@ discard block |
||
107 | 107 | * @return int |
108 | 108 | */ |
109 | 109 | public function exec(string $query, array $params = []): int { |
110 | - return $this->exceptionHandler(function () use ($query, $params) { |
|
110 | + return $this->exceptionHandler(function() use ($query, $params) { |
|
111 | 111 | $stmt = $this->pdo->prepare($query); |
112 | 112 | $timer = microtime(true); |
113 | 113 | $stmt->execute($params); |
114 | - $this->queryLoggers->log($query, microtime(true) - $timer); |
|
114 | + $this->queryLoggers->log($query, microtime(true)-$timer); |
|
115 | 115 | $result = $stmt->rowCount(); |
116 | 116 | $stmt->closeCursor(); |
117 | 117 | return $result; |
@@ -132,12 +132,12 @@ discard block |
||
132 | 132 | */ |
133 | 133 | public function getTableFields(string $table): array { |
134 | 134 | $table = $this->select()->aliasReplacer()->replace($table); |
135 | - if(array_key_exists($table, self::$tableFields)) { |
|
135 | + if (array_key_exists($table, self::$tableFields)) { |
|
136 | 136 | return self::$tableFields[$table]; |
137 | 137 | } |
138 | 138 | $stmt = $this->pdo->query("DESCRIBE {$table}"); |
139 | 139 | $rows = $stmt->fetchAll(PDO::FETCH_ASSOC); |
140 | - self::$tableFields[$table] = array_map(static function ($row) { return $row['Field']; }, $rows); |
|
140 | + self::$tableFields[$table] = array_map(static function($row) { return $row['Field']; }, $rows); |
|
141 | 141 | $stmt->closeCursor(); |
142 | 142 | return self::$tableFields[$table]; |
143 | 143 | } |
@@ -149,12 +149,12 @@ discard block |
||
149 | 149 | */ |
150 | 150 | public function quoteExpression($expression, array $arguments = []): string { |
151 | 151 | $index = -1; |
152 | - $func = function () use ($arguments, &$index) { |
|
152 | + $func = function() use ($arguments, &$index) { |
|
153 | 153 | $index++; |
154 | - if(array_key_exists($index, $arguments)) { |
|
154 | + if (array_key_exists($index, $arguments)) { |
|
155 | 155 | $argument = $arguments[$index]; |
156 | 156 | $value = $this->quote($argument); |
157 | - } elseif(count($arguments) > 0) { |
|
157 | + } elseif (count($arguments) > 0) { |
|
158 | 158 | $args = $arguments; |
159 | 159 | $value = array_pop($args); |
160 | 160 | $value = $this->quote($value); |
@@ -172,14 +172,14 @@ discard block |
||
172 | 172 | * @return string |
173 | 173 | */ |
174 | 174 | public function quote($value): string { |
175 | - if(is_null($value)) { |
|
175 | + if (is_null($value)) { |
|
176 | 176 | $result = 'NULL'; |
177 | - } elseif($value instanceof Builder\DBExpr) { |
|
177 | + } elseif ($value instanceof Builder\DBExpr) { |
|
178 | 178 | $result = $value->getExpression(); |
179 | - } elseif($value instanceof Builder\Select) { |
|
179 | + } elseif ($value instanceof Builder\Select) { |
|
180 | 180 | $result = sprintf('(%s)', (string) $value); |
181 | - } elseif(is_array($value)) { |
|
182 | - $result = implode(', ', array_map(function ($value) { return $this->quote($value); }, $value)); |
|
181 | + } elseif (is_array($value)) { |
|
182 | + $result = implode(', ', array_map(function($value) { return $this->quote($value); }, $value)); |
|
183 | 183 | } else { |
184 | 184 | $result = $this->pdo->quote($value); |
185 | 185 | } |
@@ -194,7 +194,7 @@ discard block |
||
194 | 194 | if (is_numeric($field) || !is_string($field)) { |
195 | 195 | throw new UnexpectedValueException('Field name is invalid'); |
196 | 196 | } |
197 | - if(strpos($field, '`') !== false) { |
|
197 | + if (strpos($field, '`') !== false) { |
|
198 | 198 | return $field; |
199 | 199 | } |
200 | 200 | $parts = explode('.', $field); |
@@ -209,7 +209,7 @@ discard block |
||
209 | 209 | $select = array_key_exists('select-factory', $this->options) |
210 | 210 | ? call_user_func($this->options['select-factory'], $this, $this->options['select-options']) |
211 | 211 | : new Builder\RunnableSelect($this, $this->options['select-options']); |
212 | - if($fields !== null) { |
|
212 | + if ($fields !== null) { |
|
213 | 213 | $select->fields($fields); |
214 | 214 | } |
215 | 215 | return $select; |
@@ -223,7 +223,7 @@ discard block |
||
223 | 223 | $insert = array_key_exists('insert-factory', $this->options) |
224 | 224 | ? call_user_func($this->options['insert-factory'], $this, $this->options['insert-options']) |
225 | 225 | : new Builder\RunnableInsert($this, $this->options['insert-options']); |
226 | - if($fields !== null) { |
|
226 | + if ($fields !== null) { |
|
227 | 227 | $insert->addAll($fields); |
228 | 228 | } |
229 | 229 | return $insert; |
@@ -237,7 +237,7 @@ discard block |
||
237 | 237 | $update = array_key_exists('update-factory', $this->options) |
238 | 238 | ? call_user_func($this->options['update-factory'], $this, $this->options['update-options']) |
239 | 239 | : new Builder\RunnableUpdate($this, $this->options['update-options']); |
240 | - if($fields !== null) { |
|
240 | + if ($fields !== null) { |
|
241 | 241 | $update->setAll($fields); |
242 | 242 | } |
243 | 243 | return $update; |
@@ -256,8 +256,8 @@ discard block |
||
256 | 256 | * @return $this |
257 | 257 | */ |
258 | 258 | public function transactionStart() { |
259 | - if($this->transactionLevel === 0) { |
|
260 | - if($this->pdo->inTransaction()) { |
|
259 | + if ($this->transactionLevel === 0) { |
|
260 | + if ($this->pdo->inTransaction()) { |
|
261 | 261 | $this->outerTransaction = true; |
262 | 262 | } else { |
263 | 263 | $this->pdo->beginTransaction(); |
@@ -271,7 +271,7 @@ discard block |
||
271 | 271 | * @return $this |
272 | 272 | */ |
273 | 273 | public function transactionCommit() { |
274 | - return $this->transactionEnd(function () { |
|
274 | + return $this->transactionEnd(function() { |
|
275 | 275 | $this->pdo->commit(); |
276 | 276 | }); |
277 | 277 | } |
@@ -280,7 +280,7 @@ discard block |
||
280 | 280 | * @return $this |
281 | 281 | */ |
282 | 282 | public function transactionRollback() { |
283 | - return $this->transactionEnd(function () { |
|
283 | + return $this->transactionEnd(function() { |
|
284 | 284 | $this->pdo->rollBack(); |
285 | 285 | }); |
286 | 286 | } |
@@ -290,7 +290,7 @@ discard block |
||
290 | 290 | * @return mixed |
291 | 291 | */ |
292 | 292 | public function dryRun($callback = null) { |
293 | - if(!$this->pdo->inTransaction()) { |
|
293 | + if (!$this->pdo->inTransaction()) { |
|
294 | 294 | $this->transactionStart(); |
295 | 295 | try { |
296 | 296 | return $callback($this); |
@@ -314,14 +314,14 @@ discard block |
||
314 | 314 | */ |
315 | 315 | public function transaction(callable $callback = null) { |
316 | 316 | $result = null; |
317 | - if(!$this->pdo->inTransaction()) { |
|
317 | + if (!$this->pdo->inTransaction()) { |
|
318 | 318 | $this->transactionStart(); |
319 | 319 | try { |
320 | 320 | $result = $callback($this); |
321 | 321 | $this->transactionCommit(); |
322 | 322 | return $result; |
323 | 323 | } catch (Throwable $e) { |
324 | - if($this->pdo->inTransaction()) { |
|
324 | + if ($this->pdo->inTransaction()) { |
|
325 | 325 | $this->transactionRollback(); |
326 | 326 | } |
327 | 327 | throw $e; |
@@ -345,11 +345,11 @@ discard block |
||
345 | 345 | */ |
346 | 346 | private function transactionEnd(callable $fn) { |
347 | 347 | $this->transactionLevel--; |
348 | - if($this->transactionLevel < 0) { |
|
348 | + if ($this->transactionLevel < 0) { |
|
349 | 349 | throw new RuntimeException("Transaction-Nesting-Problem: Trying to invoke commit on a already closed transaction"); |
350 | 350 | } |
351 | - if($this->transactionLevel < 1) { |
|
352 | - if($this->outerTransaction) { |
|
351 | + if ($this->transactionLevel < 1) { |
|
352 | + if ($this->outerTransaction) { |
|
353 | 353 | $this->outerTransaction = false; |
354 | 354 | } else { |
355 | 355 | $fn(); |
@@ -365,7 +365,7 @@ discard block |
||
365 | 365 | */ |
366 | 366 | private function buildQueryStatement(string $query, callable $fn): QueryStatement { |
367 | 367 | $stmt = $fn($query); |
368 | - if(!$stmt) { |
|
368 | + if (!$stmt) { |
|
369 | 369 | throw new RuntimeException("Could not execute statement:\n{$query}"); |
370 | 370 | } |
371 | 371 | return new QueryStatement($stmt, $query, $this->exceptionInterpreter, $this->queryLoggers); |
@@ -31,10 +31,10 @@ |
||
31 | 31 | * @return Select|null |
32 | 32 | */ |
33 | 33 | public function get($tableName): ?Select { |
34 | - if($this->has($tableName)) { |
|
34 | + if ($this->has($tableName)) { |
|
35 | 35 | $table = $this->virtualTables[(string) $tableName]; |
36 | - if($table instanceof Closure) { |
|
37 | - if($tableName instanceof VirtualTable) { |
|
36 | + if ($table instanceof Closure) { |
|
37 | + if ($tableName instanceof VirtualTable) { |
|
38 | 38 | $params = $tableName->getParams(); |
39 | 39 | return $table($params); |
40 | 40 | } |
@@ -12,15 +12,15 @@ |
||
12 | 12 | * @return string |
13 | 13 | */ |
14 | 14 | public static function build(Database $db, string $query, array $conditions, string $token): string { |
15 | - if(!count($conditions)) { |
|
15 | + if (!count($conditions)) { |
|
16 | 16 | return $query; |
17 | 17 | } |
18 | 18 | $query .= "{$token}\n"; |
19 | 19 | $arr = []; |
20 | - foreach($conditions as list($expression, $arguments)) { |
|
21 | - if(is_array($expression)) { |
|
22 | - foreach($expression as $key => $value) { |
|
23 | - if($value === null) { |
|
20 | + foreach ($conditions as list($expression, $arguments)) { |
|
21 | + if (is_array($expression)) { |
|
22 | + foreach ($expression as $key => $value) { |
|
23 | + if ($value === null) { |
|
24 | 24 | $arr = self::buildCondition($arr, "ISNULL(`{$key}`)", [$value], $db); |
25 | 25 | } else { |
26 | 26 | $arr = self::buildCondition($arr, "`{$key}`=?", [$value], $db); |
@@ -92,7 +92,7 @@ discard block |
||
92 | 92 | * @return mixed[] |
93 | 93 | */ |
94 | 94 | public function fetchRow(Closure $callback = null): array { |
95 | - return $this->fetch($callback, PDO::FETCH_ASSOC, null, static function ($row) { |
|
95 | + return $this->fetch($callback, PDO::FETCH_ASSOC, null, static function($row) { |
|
96 | 96 | return ['valid' => is_array($row), 'default' => []]; |
97 | 97 | }); |
98 | 98 | } |
@@ -121,7 +121,7 @@ discard block |
||
121 | 121 | * @return object[] |
122 | 122 | */ |
123 | 123 | public function fetchObject($className = null, Closure $callback = null): array { |
124 | - return $this->fetch($callback, PDO::FETCH_CLASS, $className ?: $this->defaultClassName, static function ($row) { |
|
124 | + return $this->fetch($callback, PDO::FETCH_CLASS, $className ?: $this->defaultClassName, static function($row) { |
|
125 | 125 | return ['valid' => is_object($row), 'default' => null]; |
126 | 126 | }); |
127 | 127 | } |
@@ -131,11 +131,11 @@ discard block |
||
131 | 131 | * @return mixed[] |
132 | 132 | */ |
133 | 133 | public function fetchKeyValue($treatValueAsArray = false): array { |
134 | - return $this->createTempStatement(static function (QueryStatement $statement) use ($treatValueAsArray) { |
|
135 | - if($treatValueAsArray) { |
|
134 | + return $this->createTempStatement(static function(QueryStatement $statement) use ($treatValueAsArray) { |
|
135 | + if ($treatValueAsArray) { |
|
136 | 136 | $rows = $statement->fetchAll(PDO::FETCH_ASSOC); |
137 | 137 | $result = []; |
138 | - foreach($rows as $row) { |
|
138 | + foreach ($rows as $row) { |
|
139 | 139 | list($key) = array_values($row); |
140 | 140 | $result[$key] = $row; |
141 | 141 | } |
@@ -152,12 +152,12 @@ discard block |
||
152 | 152 | public function fetchGroups(array $fields): array { |
153 | 153 | $rows = $this->fetchRows(); |
154 | 154 | $result = []; |
155 | - foreach($rows as $row) { |
|
155 | + foreach ($rows as $row) { |
|
156 | 156 | /** @var array $tmp */ |
157 | 157 | $tmp = &$result; |
158 | - foreach($fields as $field) { |
|
158 | + foreach ($fields as $field) { |
|
159 | 159 | $value = (string) $row[$field]; |
160 | - if(!array_key_exists($value, $tmp)) { |
|
160 | + if (!array_key_exists($value, $tmp)) { |
|
161 | 161 | $tmp[$value] = []; |
162 | 162 | } |
163 | 163 | $tmp = &$tmp[$value]; |
@@ -171,7 +171,7 @@ discard block |
||
171 | 171 | * @return string[] |
172 | 172 | */ |
173 | 173 | public function fetchArray(): array { |
174 | - return $this->createTempStatement(static function (QueryStatement $stmt) { |
|
174 | + return $this->createTempStatement(static function(QueryStatement $stmt) { |
|
175 | 175 | return $stmt->fetchAll(PDO::FETCH_COLUMN); |
176 | 176 | }); |
177 | 177 | } |
@@ -181,9 +181,9 @@ discard block |
||
181 | 181 | * @return null|bool|string|int|float |
182 | 182 | */ |
183 | 183 | public function fetchValue($default = null) { |
184 | - return $this->createTempStatement(static function (QueryStatement $stmt) use ($default) { |
|
184 | + return $this->createTempStatement(static function(QueryStatement $stmt) use ($default) { |
|
185 | 185 | $result = $stmt->fetch(PDO::FETCH_NUM); |
186 | - if($result !== false) { |
|
186 | + if ($result !== false) { |
|
187 | 187 | return $result[0]; |
188 | 188 | } |
189 | 189 | return $default; |
@@ -218,7 +218,7 @@ discard block |
||
218 | 218 | $query = $this->__toString(); |
219 | 219 | $statement = $db->prepare($query); |
220 | 220 | $statement->execute($this->values); |
221 | - if($this->getCalcFoundRows()) { |
|
221 | + if ($this->getCalcFoundRows()) { |
|
222 | 222 | $this->foundRows = (int) $db->query('SELECT FOUND_ROWS()')->fetchColumn(); |
223 | 223 | } |
224 | 224 | return $statement; |
@@ -240,18 +240,18 @@ discard block |
||
240 | 240 | * @return mixed |
241 | 241 | */ |
242 | 242 | private function fetchAll(Closure $callback = null, int $mode = 0, $arg0 = null) { |
243 | - return $this->createTempStatement(function (QueryStatement $statement) use ($callback, $mode, $arg0) { |
|
243 | + return $this->createTempStatement(function(QueryStatement $statement) use ($callback, $mode, $arg0) { |
|
244 | 244 | $statement->setFetchMode($mode, $arg0); |
245 | 245 | $data = $statement->fetchAll(); |
246 | - if($this->preserveTypes) { |
|
246 | + if ($this->preserveTypes) { |
|
247 | 247 | $columnDefinitions = FieldTypeProvider::getFieldTypes($statement); |
248 | - $data = array_map(static function ($row) use ($columnDefinitions) { return FieldValueConverter::convertValues($row, $columnDefinitions); }, $data); |
|
248 | + $data = array_map(static function($row) use ($columnDefinitions) { return FieldValueConverter::convertValues($row, $columnDefinitions); }, $data); |
|
249 | 249 | } |
250 | - if($callback !== null) { |
|
251 | - return call_user_func(static function ($resultData = []) use ($data, $callback) { |
|
252 | - foreach($data as $row) { |
|
250 | + if ($callback !== null) { |
|
251 | + return call_user_func(static function($resultData = []) use ($data, $callback) { |
|
252 | + foreach ($data as $row) { |
|
253 | 253 | $result = $callback($row); |
254 | - if($result !== null && !($result instanceof DBIgnoreRow)) { |
|
254 | + if ($result !== null && !($result instanceof DBIgnoreRow)) { |
|
255 | 255 | $resultData[] = $result; |
256 | 256 | } else { |
257 | 257 | $resultData[] = $row; |
@@ -285,20 +285,20 @@ discard block |
||
285 | 285 | * @return mixed |
286 | 286 | */ |
287 | 287 | private function fetch(Closure $callback = null, int $mode = PDO::FETCH_ASSOC, $arg0 = null, Closure $resultValidator = null) { |
288 | - return $this->createTempStatement(function (QueryStatement $statement) use ($callback, $mode, $arg0, $resultValidator) { |
|
288 | + return $this->createTempStatement(function(QueryStatement $statement) use ($callback, $mode, $arg0, $resultValidator) { |
|
289 | 289 | $statement->setFetchMode($mode, $arg0); |
290 | 290 | $row = $statement->fetch(); |
291 | 291 | $result = $resultValidator($row); |
292 | - if(!$result['valid']) { |
|
292 | + if (!$result['valid']) { |
|
293 | 293 | return $result['default']; |
294 | 294 | } |
295 | - if($this->preserveTypes) { |
|
295 | + if ($this->preserveTypes) { |
|
296 | 296 | $columnDefinitions = FieldTypeProvider::getFieldTypes($statement); |
297 | 297 | $row = FieldValueConverter::convertValues($row, $columnDefinitions); |
298 | 298 | } |
299 | - if($callback !== null) { |
|
299 | + if ($callback !== null) { |
|
300 | 300 | $result = $callback($row); |
301 | - if($result !== null) { |
|
301 | + if ($result !== null) { |
|
302 | 302 | $row = $result; |
303 | 303 | } |
304 | 304 | } |