@@ -21,7 +21,7 @@ |
||
| 21 | 21 | * @return string |
| 22 | 22 | */ |
| 23 | 23 | protected function buildOffset($query) { |
| 24 | - if($this->offset !== null) { |
|
| 24 | + if ($this->offset !== null) { |
|
| 25 | 25 | $query .= "OFFSET\n\t{$this->offset}\n"; |
| 26 | 26 | } |
| 27 | 27 | return $query; |
@@ -13,9 +13,9 @@ discard block |
||
| 13 | 13 | * @return $this |
| 14 | 14 | */ |
| 15 | 15 | public function groupBy() { |
| 16 | - foreach(func_get_args() as $expression) { |
|
| 17 | - if(is_array($expression)) { |
|
| 18 | - if(!count($expression)) { |
|
| 16 | + foreach (func_get_args() as $expression) { |
|
| 17 | + if (is_array($expression)) { |
|
| 18 | + if (!count($expression)) { |
|
| 19 | 19 | continue; |
| 20 | 20 | } |
| 21 | 21 | $arguments = array( |
@@ -34,12 +34,12 @@ discard block |
||
| 34 | 34 | * @return string |
| 35 | 35 | */ |
| 36 | 36 | protected function buildGroups($query) { |
| 37 | - if(!count($this->groupBy)) { |
|
| 37 | + if (!count($this->groupBy)) { |
|
| 38 | 38 | return $query; |
| 39 | 39 | } |
| 40 | 40 | $query .= "GROUP BY\n"; |
| 41 | 41 | $arr = array(); |
| 42 | - foreach($this->groupBy as $expression) { |
|
| 42 | + foreach ($this->groupBy as $expression) { |
|
| 43 | 43 | $arr[] = "\t{$expression}"; |
| 44 | 44 | } |
| 45 | 45 | return $query.join(",\n", $arr)."\n"; |
@@ -30,21 +30,21 @@ |
||
| 30 | 30 | * @return string |
| 31 | 31 | */ |
| 32 | 32 | protected function buildUnions($query) { |
| 33 | - $wrap = function ($query) { |
|
| 33 | + $wrap = function($query) { |
|
| 34 | 34 | $query = trim($query); |
| 35 | 35 | $query = join("\n\t", explode("\n", $query)); |
| 36 | 36 | return sprintf("(\n\t%s\n)", $query); |
| 37 | 37 | }; |
| 38 | 38 | $queries = [$wrap($query)]; |
| 39 | - foreach($this->unions as $unionQuery) { |
|
| 40 | - if($unionQuery[0] === 'ALL') { |
|
| 39 | + foreach ($this->unions as $unionQuery) { |
|
| 40 | + if ($unionQuery[0] === 'ALL') { |
|
| 41 | 41 | $queries[] = 'UNION ALL'; |
| 42 | 42 | } else { |
| 43 | 43 | $queries[] = 'UNION'; |
| 44 | 44 | } |
| 45 | 45 | $queries[] = $wrap($unionQuery[1]); |
| 46 | 46 | } |
| 47 | - if(count($queries) > 1) { |
|
| 47 | + if (count($queries) > 1) { |
|
| 48 | 48 | return join(" ", $queries); |
| 49 | 49 | } |
| 50 | 50 | return $query; |
@@ -15,7 +15,7 @@ discard block |
||
| 15 | 15 | * @return $this |
| 16 | 16 | */ |
| 17 | 17 | protected function addTable($alias, $table = null) { |
| 18 | - if($table === null) { |
|
| 18 | + if ($table === null) { |
|
| 19 | 19 | list($alias, $table) = [$table, $alias]; |
| 20 | 20 | } |
| 21 | 21 | $this->tables[] = array('alias' => $alias, 'name' => $table); |
@@ -28,10 +28,10 @@ discard block |
||
| 28 | 28 | */ |
| 29 | 29 | protected function buildTables($query) { |
| 30 | 30 | $arr = array(); |
| 31 | - foreach($this->tables as $table) { |
|
| 31 | + foreach ($this->tables as $table) { |
|
| 32 | 32 | $arr[] = "\t".$this->buildTableName($table['alias'], $table['name']); |
| 33 | 33 | } |
| 34 | - if(count($arr)) { |
|
| 34 | + if (count($arr)) { |
|
| 35 | 35 | $query .= join(",\n", $arr)."\n"; |
| 36 | 36 | } |
| 37 | 37 | return $query; |
@@ -49,15 +49,15 @@ |
||
| 49 | 49 | */ |
| 50 | 50 | protected function buildJoins($query) { |
| 51 | 51 | $arr = array(); |
| 52 | - foreach($this->joinTables as $table) { |
|
| 52 | + foreach ($this->joinTables as $table) { |
|
| 53 | 53 | $join = $table['type']." JOIN\n"; |
| 54 | - $join .= "\t" . $this->buildTableName($table['alias'], $table['name']); |
|
| 55 | - if($table['expression']) { |
|
| 56 | - $join .= " ON " . $this->db()->quoteExpression($table['expression'], $table['arguments']); |
|
| 54 | + $join .= "\t".$this->buildTableName($table['alias'], $table['name']); |
|
| 55 | + if ($table['expression']) { |
|
| 56 | + $join .= " ON ".$this->db()->quoteExpression($table['expression'], $table['arguments']); |
|
| 57 | 57 | } |
| 58 | 58 | $arr[] = $join; |
| 59 | 59 | } |
| 60 | - if(count($arr)) { |
|
| 60 | + if (count($arr)) { |
|
| 61 | 61 | $query .= join("\n", $arr)."\n"; |
| 62 | 62 | } |
| 63 | 63 | return $query; |
@@ -29,7 +29,7 @@ discard block |
||
| 29 | 29 | * @return $this |
| 30 | 30 | */ |
| 31 | 31 | public function from($alias, $table = null) { |
| 32 | - if($table !== null) { |
|
| 32 | + if ($table !== null) { |
|
| 33 | 33 | $this->aliases[] = $alias; |
| 34 | 34 | } |
| 35 | 35 | $this->addTable($alias, $table); |
@@ -43,7 +43,7 @@ discard block |
||
| 43 | 43 | public function __toString() { |
| 44 | 44 | $query = "DELETE "; |
| 45 | 45 | $query .= join(', ', $this->aliases); |
| 46 | - $query = trim($query) . " FROM\n"; |
|
| 46 | + $query = trim($query)." FROM\n"; |
|
| 47 | 47 | $query = $this->buildTables($query); |
| 48 | 48 | $query = $this->buildJoins($query); |
| 49 | 49 | $query = $this->buildWhereConditions($query); |
@@ -8,8 +8,8 @@ |
||
| 8 | 8 | * @return array |
| 9 | 9 | */ |
| 10 | 10 | public static function convertValues(array $row, array $columnDefinitions) { |
| 11 | - foreach($row as $key => &$value) { |
|
| 12 | - if($value !== null) { |
|
| 11 | + foreach ($row as $key => &$value) { |
|
| 12 | + if ($value !== null) { |
|
| 13 | 13 | $value = self::convertValue($value, $columnDefinitions[$key]); |
| 14 | 14 | } |
| 15 | 15 | } |
@@ -25,7 +25,7 @@ |
||
| 25 | 25 | public function run(array $params = array()) { |
| 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; |
@@ -261,7 +261,7 @@ |
||
| 261 | 261 | } |
| 262 | 262 | |
| 263 | 263 | /** |
| 264 | - * @param int|callable $tries |
|
| 264 | + * @param integer $tries |
|
| 265 | 265 | * @param callable|null $callback |
| 266 | 266 | * @return mixed |
| 267 | 267 | * @throws \Exception |
@@ -38,7 +38,7 @@ discard block |
||
| 38 | 38 | * @param array $options |
| 39 | 39 | */ |
| 40 | 40 | public function __construct(PDO $pdo, array $options = []) { |
| 41 | - if($pdo->getAttribute(PDO::ATTR_ERRMODE) === PDO::ERRMODE_SILENT) { |
|
| 41 | + if ($pdo->getAttribute(PDO::ATTR_ERRMODE) === PDO::ERRMODE_SILENT) { |
|
| 42 | 42 | $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); |
| 43 | 43 | } |
| 44 | 44 | $this->pdo = $pdo; |
@@ -74,7 +74,7 @@ discard block |
||
| 74 | 74 | * @return QueryStatement |
| 75 | 75 | */ |
| 76 | 76 | public function query($query) { |
| 77 | - return $this->buildQueryStatement($query, function ($query) { |
|
| 77 | + return $this->buildQueryStatement($query, function($query) { |
|
| 78 | 78 | $stmt = $this->pdo->query($query); |
| 79 | 79 | return $stmt; |
| 80 | 80 | }); |
@@ -86,7 +86,7 @@ discard block |
||
| 86 | 86 | * @return QueryStatement |
| 87 | 87 | */ |
| 88 | 88 | public function prepare($query) { |
| 89 | - return $this->buildQueryStatement((string) $query, function ($query) { |
|
| 89 | + return $this->buildQueryStatement((string) $query, function($query) { |
|
| 90 | 90 | $stmt = $this->pdo->prepare($query); |
| 91 | 91 | return $stmt; |
| 92 | 92 | }); |
@@ -98,11 +98,11 @@ discard block |
||
| 98 | 98 | * @return int |
| 99 | 99 | */ |
| 100 | 100 | public function exec($query, array $params = array()) { |
| 101 | - return $this->exceptionHandler(function () use ($query, $params) { |
|
| 101 | + return $this->exceptionHandler(function() use ($query, $params) { |
|
| 102 | 102 | $stmt = $this->pdo->prepare($query); |
| 103 | 103 | $timer = microtime(true); |
| 104 | 104 | $stmt->execute($params); |
| 105 | - $this->queryLoggers->log($query, microtime(true) - $timer); |
|
| 105 | + $this->queryLoggers->log($query, microtime(true)-$timer); |
|
| 106 | 106 | $result = $stmt->rowCount(); |
| 107 | 107 | $stmt->closeCursor(); |
| 108 | 108 | return $result; |
@@ -122,12 +122,12 @@ discard block |
||
| 122 | 122 | */ |
| 123 | 123 | public function getTableFields($table) { |
| 124 | 124 | $table = $this->select()->aliasReplacer()->replace($table); |
| 125 | - if(array_key_exists($table, self::$tableFields)) { |
|
| 125 | + if (array_key_exists($table, self::$tableFields)) { |
|
| 126 | 126 | return self::$tableFields[$table]; |
| 127 | 127 | } |
| 128 | 128 | $stmt = $this->pdo->query("DESCRIBE {$table}"); |
| 129 | 129 | $rows = $stmt->fetchAll(\PDO::FETCH_ASSOC); |
| 130 | - self::$tableFields[$table] = array_map(function ($row) { return $row['Field']; }, $rows); |
|
| 130 | + self::$tableFields[$table] = array_map(function($row) { return $row['Field']; }, $rows); |
|
| 131 | 131 | $stmt->closeCursor(); |
| 132 | 132 | return self::$tableFields[$table]; |
| 133 | 133 | } |
@@ -138,11 +138,11 @@ discard block |
||
| 138 | 138 | * @return string |
| 139 | 139 | */ |
| 140 | 140 | public function quoteExpression($expression, array $arguments = array()) { |
| 141 | - $func = function () use ($arguments) { |
|
| 141 | + $func = function() use ($arguments) { |
|
| 142 | 142 | static $idx = -1; |
| 143 | 143 | $idx++; |
| 144 | 144 | $index = $idx; |
| 145 | - if(array_key_exists($index, $arguments)) { |
|
| 145 | + if (array_key_exists($index, $arguments)) { |
|
| 146 | 146 | $argument = $arguments[$index]; |
| 147 | 147 | $value = $this->quote($argument); |
| 148 | 148 | } else { |
@@ -159,14 +159,14 @@ discard block |
||
| 159 | 159 | * @return string |
| 160 | 160 | */ |
| 161 | 161 | public function quote($value) { |
| 162 | - if(is_null($value)) { |
|
| 162 | + if (is_null($value)) { |
|
| 163 | 163 | $result = 'NULL'; |
| 164 | - } elseif($value instanceof Builder\DBExpr) { |
|
| 164 | + } elseif ($value instanceof Builder\DBExpr) { |
|
| 165 | 165 | $result = $value->getExpression(); |
| 166 | - } elseif($value instanceof Builder\Select) { |
|
| 166 | + } elseif ($value instanceof Builder\Select) { |
|
| 167 | 167 | $result = sprintf('(%s)', (string) $value); |
| 168 | - } elseif(is_array($value)) { |
|
| 169 | - $result = join(', ', array_map(function ($value) { return $this->quote($value); }, $value)); |
|
| 168 | + } elseif (is_array($value)) { |
|
| 169 | + $result = join(', ', array_map(function($value) { return $this->quote($value); }, $value)); |
|
| 170 | 170 | } else { |
| 171 | 171 | $result = $this->pdo->quote($value); |
| 172 | 172 | } |
@@ -181,7 +181,7 @@ discard block |
||
| 181 | 181 | if (is_numeric($field) || !is_string($field)) { |
| 182 | 182 | throw new UnexpectedValueException('Field name is invalid'); |
| 183 | 183 | } |
| 184 | - if(strpos($field, '`') !== false) { |
|
| 184 | + if (strpos($field, '`') !== false) { |
|
| 185 | 185 | return (string) $field; |
| 186 | 186 | } |
| 187 | 187 | $parts = explode('.', $field); |
@@ -196,7 +196,7 @@ discard block |
||
| 196 | 196 | $select = array_key_exists('select-factory', $this->options) |
| 197 | 197 | ? call_user_func($this->options['select-factory'], $this, $this->options['select-options']) |
| 198 | 198 | : new Builder\RunnableSelect($this, $this->options['select-options']); |
| 199 | - if($fields !== null) { |
|
| 199 | + if ($fields !== null) { |
|
| 200 | 200 | $select->fields($fields); |
| 201 | 201 | } |
| 202 | 202 | return $select; |
@@ -210,7 +210,7 @@ discard block |
||
| 210 | 210 | $insert = array_key_exists('insert-factory', $this->options) |
| 211 | 211 | ? call_user_func($this->options['insert-factory'], $this, $this->options['insert-options']) |
| 212 | 212 | : new Builder\RunnableInsert($this, $this->options['insert-options']); |
| 213 | - if($fields !== null) { |
|
| 213 | + if ($fields !== null) { |
|
| 214 | 214 | $insert->addAll($fields); |
| 215 | 215 | } |
| 216 | 216 | return $insert; |
@@ -224,7 +224,7 @@ discard block |
||
| 224 | 224 | $update = array_key_exists('update-factory', $this->options) |
| 225 | 225 | ? call_user_func($this->options['update-factory'], $this, $this->options['update-options']) |
| 226 | 226 | : new Builder\RunnableUpdate($this, $this->options['update-options']); |
| 227 | - if($fields !== null) { |
|
| 227 | + if ($fields !== null) { |
|
| 228 | 228 | $update->setAll($fields); |
| 229 | 229 | } |
| 230 | 230 | return $update; |
@@ -243,8 +243,8 @@ discard block |
||
| 243 | 243 | * @return $this |
| 244 | 244 | */ |
| 245 | 245 | public function transactionStart() { |
| 246 | - if((int) $this->transactionLevel === 0) { |
|
| 247 | - if($this->pdo->inTransaction()) { |
|
| 246 | + if ((int) $this->transactionLevel === 0) { |
|
| 247 | + if ($this->pdo->inTransaction()) { |
|
| 248 | 248 | $this->outerTransaction = true; |
| 249 | 249 | } else { |
| 250 | 250 | $this->pdo->beginTransaction(); |
@@ -259,7 +259,7 @@ discard block |
||
| 259 | 259 | * @throws \Exception |
| 260 | 260 | */ |
| 261 | 261 | public function transactionCommit() { |
| 262 | - return $this->transactionEnd(function () { |
|
| 262 | + return $this->transactionEnd(function() { |
|
| 263 | 263 | $this->pdo->commit(); |
| 264 | 264 | }); |
| 265 | 265 | } |
@@ -269,7 +269,7 @@ discard block |
||
| 269 | 269 | * @throws \Exception |
| 270 | 270 | */ |
| 271 | 271 | public function transactionRollback() { |
| 272 | - return $this->transactionEnd(function () { |
|
| 272 | + return $this->transactionEnd(function() { |
|
| 273 | 273 | $this->pdo->rollBack(); |
| 274 | 274 | }); |
| 275 | 275 | } |
@@ -284,7 +284,7 @@ discard block |
||
| 284 | 284 | public function dryRun($callback = null) { |
| 285 | 285 | $result = null; |
| 286 | 286 | $exception = null; |
| 287 | - if(!$this->pdo->inTransaction()) { |
|
| 287 | + if (!$this->pdo->inTransaction()) { |
|
| 288 | 288 | $this->transactionStart(); |
| 289 | 289 | try { |
| 290 | 290 | $result = call_user_func($callback, $this); |
@@ -321,16 +321,16 @@ discard block |
||
| 321 | 321 | * @throws \Error |
| 322 | 322 | */ |
| 323 | 323 | public function transaction($tries = 1, $callback = null) { |
| 324 | - if(is_callable($tries)) { |
|
| 324 | + if (is_callable($tries)) { |
|
| 325 | 325 | $callback = $tries; |
| 326 | 326 | $tries = 1; |
| 327 | - } elseif(!is_callable($callback)) { |
|
| 327 | + } elseif (!is_callable($callback)) { |
|
| 328 | 328 | throw new RuntimeException('$callback must be a callable'); |
| 329 | 329 | } |
| 330 | 330 | $result = null; |
| 331 | 331 | $exception = null; |
| 332 | - for(; $tries--;) { |
|
| 333 | - if(!$this->pdo->inTransaction()) { |
|
| 332 | + for (; $tries--;) { |
|
| 333 | + if (!$this->pdo->inTransaction()) { |
|
| 334 | 334 | $this->transactionStart(); |
| 335 | 335 | try { |
| 336 | 336 | $result = call_user_func($callback, $this); |
@@ -359,7 +359,7 @@ discard block |
||
| 359 | 359 | } |
| 360 | 360 | } |
| 361 | 361 | } |
| 362 | - if($exception !== null) { |
|
| 362 | + if ($exception !== null) { |
|
| 363 | 363 | throw $exception; |
| 364 | 364 | } |
| 365 | 365 | return $result; |
@@ -372,11 +372,11 @@ discard block |
||
| 372 | 372 | */ |
| 373 | 373 | private function transactionEnd($fn) { |
| 374 | 374 | $this->transactionLevel--; |
| 375 | - if($this->transactionLevel < 0) { |
|
| 375 | + if ($this->transactionLevel < 0) { |
|
| 376 | 376 | throw new RuntimeException("Transaction-Nesting-Problem: Trying to invoke commit on a already closed transaction"); |
| 377 | 377 | } |
| 378 | - if((int) $this->transactionLevel === 0) { |
|
| 379 | - if($this->outerTransaction) { |
|
| 378 | + if ((int) $this->transactionLevel === 0) { |
|
| 379 | + if ($this->outerTransaction) { |
|
| 380 | 380 | $this->outerTransaction = false; |
| 381 | 381 | } else { |
| 382 | 382 | call_user_func($fn); |
@@ -393,7 +393,7 @@ discard block |
||
| 393 | 393 | */ |
| 394 | 394 | private function buildQueryStatement($query, $fn) { |
| 395 | 395 | $stmt = call_user_func($fn, $query); |
| 396 | - if(!$stmt) { |
|
| 396 | + if (!$stmt) { |
|
| 397 | 397 | throw new RuntimeException("Could not execute statement:\n{$query}"); |
| 398 | 398 | } |
| 399 | 399 | $stmtWrapper = new QueryStatement($stmt, $query, $this->exceptionInterpreter, $this->queryLoggers); |