@@ -12,32 +12,32 @@ |
||
| 12 | 12 | * @return string |
| 13 | 13 | */ |
| 14 | 14 | protected function buildTableName($alias, $name) { |
| 15 | - if(is_object($name)) { |
|
| 15 | + if (is_object($name)) { |
|
| 16 | 16 | $name = (string) $name; |
| 17 | 17 | $lines = explode("\n", $name); |
| 18 | - foreach($lines as &$line) { |
|
| 18 | + foreach ($lines as &$line) { |
|
| 19 | 19 | $line = "\t{$line}"; |
| 20 | 20 | } |
| 21 | 21 | $name = join("\n", $lines); |
| 22 | - $name = '(' . trim(rtrim(trim($name), ';')) . ')'; |
|
| 22 | + $name = '('.trim(rtrim(trim($name), ';')).')'; |
|
| 23 | 23 | } |
| 24 | - if(is_array($name)) { |
|
| 24 | + if (is_array($name)) { |
|
| 25 | 25 | $parts = []; |
| 26 | - foreach($name as $bucket) { |
|
| 26 | + foreach ($name as $bucket) { |
|
| 27 | 27 | $values = []; |
| 28 | - foreach($bucket as $field => $value) { |
|
| 28 | + foreach ($bucket as $field => $value) { |
|
| 29 | 29 | $values[] = sprintf('%s AS %s', $this->db()->quote($value), $this->db()->quoteField($field)); |
| 30 | 30 | } |
| 31 | 31 | $parts[] = sprintf("SELECT %s", join(', ', $values)); |
| 32 | 32 | } |
| 33 | - $name = '(' . join("\n\tUNION\n\t", $parts) . ')'; |
|
| 33 | + $name = '('.join("\n\tUNION\n\t", $parts).')'; |
|
| 34 | 34 | } |
| 35 | - if($this->db()->getVirtualTables()->has($name)) { |
|
| 36 | - $select = (string )$this->db()->getVirtualTables()->get($name); |
|
| 35 | + if ($this->db()->getVirtualTables()->has($name)) { |
|
| 36 | + $select = (string) $this->db()->getVirtualTables()->get($name); |
|
| 37 | 37 | $name = sprintf('(%s)', join("\n\t", explode("\n", trim($select)))); |
| 38 | 38 | } |
| 39 | 39 | $name = $this->aliasReplacer()->replace($name); |
| 40 | - if($alias !== null) { |
|
| 40 | + if ($alias !== null) { |
|
| 41 | 41 | return sprintf("%s %s", $name, $alias); |
| 42 | 42 | } |
| 43 | 43 | return $name; |
@@ -30,7 +30,7 @@ |
||
| 30 | 30 | * @return Select|null |
| 31 | 31 | */ |
| 32 | 32 | public function get($tableName) { |
| 33 | - if($this->has($tableName)) { |
|
| 33 | + if ($this->has($tableName)) { |
|
| 34 | 34 | return $this->virtualTables[$tableName]; |
| 35 | 35 | } |
| 36 | 36 | return null; |
@@ -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() { |
| 78 | - if($this->virtualTables === null) { |
|
| 78 | + if ($this->virtualTables === null) { |
|
| 79 | 79 | $this->virtualTables = new VirtualTables(); |
| 80 | 80 | } |
| 81 | 81 | return $this->virtualTables; |
@@ -87,7 +87,7 @@ discard block |
||
| 87 | 87 | * @return QueryStatement |
| 88 | 88 | */ |
| 89 | 89 | public function query($query) { |
| 90 | - return $this->buildQueryStatement($query, function ($query) { |
|
| 90 | + return $this->buildQueryStatement($query, function($query) { |
|
| 91 | 91 | $stmt = $this->pdo->query($query); |
| 92 | 92 | return $stmt; |
| 93 | 93 | }); |
@@ -99,7 +99,7 @@ discard block |
||
| 99 | 99 | * @return QueryStatement |
| 100 | 100 | */ |
| 101 | 101 | public function prepare($query) { |
| 102 | - return $this->buildQueryStatement((string) $query, function ($query) { |
|
| 102 | + return $this->buildQueryStatement((string) $query, function($query) { |
|
| 103 | 103 | $stmt = $this->pdo->prepare($query); |
| 104 | 104 | return $stmt; |
| 105 | 105 | }); |
@@ -111,11 +111,11 @@ discard block |
||
| 111 | 111 | * @return int |
| 112 | 112 | */ |
| 113 | 113 | public function exec($query, array $params = array()) { |
| 114 | - return $this->exceptionHandler(function () use ($query, $params) { |
|
| 114 | + return $this->exceptionHandler(function() use ($query, $params) { |
|
| 115 | 115 | $stmt = $this->pdo->prepare($query); |
| 116 | 116 | $timer = microtime(true); |
| 117 | 117 | $stmt->execute($params); |
| 118 | - $this->queryLoggers->log($query, microtime(true) - $timer); |
|
| 118 | + $this->queryLoggers->log($query, microtime(true)-$timer); |
|
| 119 | 119 | $result = $stmt->rowCount(); |
| 120 | 120 | $stmt->closeCursor(); |
| 121 | 121 | return $result; |
@@ -135,12 +135,12 @@ discard block |
||
| 135 | 135 | */ |
| 136 | 136 | public function getTableFields($table) { |
| 137 | 137 | $table = $this->select()->aliasReplacer()->replace($table); |
| 138 | - if(array_key_exists($table, self::$tableFields)) { |
|
| 138 | + if (array_key_exists($table, self::$tableFields)) { |
|
| 139 | 139 | return self::$tableFields[$table]; |
| 140 | 140 | } |
| 141 | 141 | $stmt = $this->pdo->query("DESCRIBE {$table}"); |
| 142 | 142 | $rows = $stmt->fetchAll(\PDO::FETCH_ASSOC); |
| 143 | - self::$tableFields[$table] = array_map(function ($row) { return $row['Field']; }, $rows); |
|
| 143 | + self::$tableFields[$table] = array_map(function($row) { return $row['Field']; }, $rows); |
|
| 144 | 144 | $stmt->closeCursor(); |
| 145 | 145 | return self::$tableFields[$table]; |
| 146 | 146 | } |
@@ -151,11 +151,11 @@ discard block |
||
| 151 | 151 | * @return string |
| 152 | 152 | */ |
| 153 | 153 | public function quoteExpression($expression, array $arguments = array()) { |
| 154 | - $func = function () use ($arguments) { |
|
| 154 | + $func = function() use ($arguments) { |
|
| 155 | 155 | static $idx = -1; |
| 156 | 156 | $idx++; |
| 157 | 157 | $index = $idx; |
| 158 | - if(array_key_exists($index, $arguments)) { |
|
| 158 | + if (array_key_exists($index, $arguments)) { |
|
| 159 | 159 | $argument = $arguments[$index]; |
| 160 | 160 | $value = $this->quote($argument); |
| 161 | 161 | } else { |
@@ -172,14 +172,14 @@ discard block |
||
| 172 | 172 | * @return string |
| 173 | 173 | */ |
| 174 | 174 | public function quote($value) { |
| 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 = join(', ', array_map(function ($value) { return $this->quote($value); }, $value)); |
|
| 181 | + } elseif (is_array($value)) { |
|
| 182 | + $result = join(', ', 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 (string) $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((int) $this->transactionLevel === 0) { |
|
| 260 | - if($this->pdo->inTransaction()) { |
|
| 259 | + if ((int) $this->transactionLevel === 0) { |
|
| 260 | + if ($this->pdo->inTransaction()) { |
|
| 261 | 261 | $this->outerTransaction = true; |
| 262 | 262 | } else { |
| 263 | 263 | $this->pdo->beginTransaction(); |
@@ -272,7 +272,7 @@ discard block |
||
| 272 | 272 | * @throws \Exception |
| 273 | 273 | */ |
| 274 | 274 | public function transactionCommit() { |
| 275 | - return $this->transactionEnd(function () { |
|
| 275 | + return $this->transactionEnd(function() { |
|
| 276 | 276 | $this->pdo->commit(); |
| 277 | 277 | }); |
| 278 | 278 | } |
@@ -282,7 +282,7 @@ discard block |
||
| 282 | 282 | * @throws \Exception |
| 283 | 283 | */ |
| 284 | 284 | public function transactionRollback() { |
| 285 | - return $this->transactionEnd(function () { |
|
| 285 | + return $this->transactionEnd(function() { |
|
| 286 | 286 | $this->pdo->rollBack(); |
| 287 | 287 | }); |
| 288 | 288 | } |
@@ -295,7 +295,7 @@ discard block |
||
| 295 | 295 | */ |
| 296 | 296 | public function dryRun($callback = null) { |
| 297 | 297 | $result = null; |
| 298 | - if(!$this->pdo->inTransaction()) { |
|
| 298 | + if (!$this->pdo->inTransaction()) { |
|
| 299 | 299 | $this->transactionStart(); |
| 300 | 300 | try { |
| 301 | 301 | $result = call_user_func($callback, $this); |
@@ -332,16 +332,16 @@ discard block |
||
| 332 | 332 | * @throws \Error |
| 333 | 333 | */ |
| 334 | 334 | public function transaction($tries = 1, $callback = null) { |
| 335 | - if(is_callable($tries)) { |
|
| 335 | + if (is_callable($tries)) { |
|
| 336 | 336 | $callback = $tries; |
| 337 | 337 | $tries = 1; |
| 338 | - } elseif(!is_callable($callback)) { |
|
| 338 | + } elseif (!is_callable($callback)) { |
|
| 339 | 339 | throw new RuntimeException('$callback must be a callable'); |
| 340 | 340 | } |
| 341 | 341 | $result = null; |
| 342 | 342 | $exception = null; |
| 343 | - for(; $tries--;) { |
|
| 344 | - if(!$this->pdo->inTransaction()) { |
|
| 343 | + for (; $tries--;) { |
|
| 344 | + if (!$this->pdo->inTransaction()) { |
|
| 345 | 345 | $this->transactionStart(); |
| 346 | 346 | try { |
| 347 | 347 | $result = call_user_func($callback, $this); |
@@ -370,7 +370,7 @@ discard block |
||
| 370 | 370 | } |
| 371 | 371 | } |
| 372 | 372 | } |
| 373 | - if($exception !== null) { |
|
| 373 | + if ($exception !== null) { |
|
| 374 | 374 | throw $exception; |
| 375 | 375 | } |
| 376 | 376 | return $result; |
@@ -383,11 +383,11 @@ discard block |
||
| 383 | 383 | */ |
| 384 | 384 | private function transactionEnd($fn) { |
| 385 | 385 | $this->transactionLevel--; |
| 386 | - if($this->transactionLevel < 0) { |
|
| 386 | + if ($this->transactionLevel < 0) { |
|
| 387 | 387 | throw new RuntimeException("Transaction-Nesting-Problem: Trying to invoke commit on a already closed transaction"); |
| 388 | 388 | } |
| 389 | - if((int) $this->transactionLevel === 0) { |
|
| 390 | - if($this->outerTransaction) { |
|
| 389 | + if ((int) $this->transactionLevel === 0) { |
|
| 390 | + if ($this->outerTransaction) { |
|
| 391 | 391 | $this->outerTransaction = false; |
| 392 | 392 | } else { |
| 393 | 393 | call_user_func($fn); |
@@ -404,7 +404,7 @@ discard block |
||
| 404 | 404 | */ |
| 405 | 405 | private function buildQueryStatement($query, $fn) { |
| 406 | 406 | $stmt = call_user_func($fn, $query); |
| 407 | - if(!$stmt) { |
|
| 407 | + if (!$stmt) { |
|
| 408 | 408 | throw new RuntimeException("Could not execute statement:\n{$query}"); |
| 409 | 409 | } |
| 410 | 410 | $stmtWrapper = new QueryStatement($stmt, $query, $this->exceptionInterpreter, $this->queryLoggers); |