@@ -91,7 +91,7 @@ discard block |
||
| 91 | 91 | * @return mixed[] |
| 92 | 92 | */ |
| 93 | 93 | public function fetchRow(Closure $callback = null) { |
| 94 | - return $this->fetch($callback, PDO::FETCH_ASSOC, null, function ($row) { |
|
| 94 | + return $this->fetch($callback, PDO::FETCH_ASSOC, null, function($row) { |
|
| 95 | 95 | return ['valid' => is_array($row), 'default' => []]; |
| 96 | 96 | }); |
| 97 | 97 | } |
@@ -122,7 +122,7 @@ discard block |
||
| 122 | 122 | * @return object[] |
| 123 | 123 | */ |
| 124 | 124 | public function fetchObject($className = null, Closure $callback = null) { |
| 125 | - return $this->fetch($callback, PDO::FETCH_CLASS, $className ?: $this->defaultClassName, function ($row) { |
|
| 125 | + return $this->fetch($callback, PDO::FETCH_CLASS, $className ?: $this->defaultClassName, function($row) { |
|
| 126 | 126 | return ['valid' => is_object($row), 'default' => null]; |
| 127 | 127 | }); |
| 128 | 128 | } |
@@ -132,11 +132,11 @@ discard block |
||
| 132 | 132 | * @return mixed[] |
| 133 | 133 | */ |
| 134 | 134 | public function fetchKeyValue($treatValueAsArray = false) { |
| 135 | - return $this->createTempStatement(function (QueryStatement $statement) use ($treatValueAsArray) { |
|
| 136 | - if($treatValueAsArray) { |
|
| 135 | + return $this->createTempStatement(function(QueryStatement $statement) use ($treatValueAsArray) { |
|
| 136 | + if ($treatValueAsArray) { |
|
| 137 | 137 | $rows = $statement->fetchAll(\PDO::FETCH_ASSOC); |
| 138 | 138 | $result = []; |
| 139 | - foreach($rows as $row) { |
|
| 139 | + foreach ($rows as $row) { |
|
| 140 | 140 | list($key) = array_values($row); |
| 141 | 141 | $result[$key] = $row; |
| 142 | 142 | } |
@@ -153,11 +153,11 @@ discard block |
||
| 153 | 153 | public function fetchGroups(array $fields) { |
| 154 | 154 | $rows = $this->fetchRows(); |
| 155 | 155 | $result = []; |
| 156 | - foreach($rows as $row) { |
|
| 156 | + foreach ($rows as $row) { |
|
| 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() { |
| 174 | - return $this->createTempStatement(function (QueryStatement $stmt) { |
|
| 174 | + return $this->createTempStatement(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(function (QueryStatement $stmt) use ($default) { |
|
| 184 | + return $this->createTempStatement(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; |
@@ -219,7 +219,7 @@ discard block |
||
| 219 | 219 | $query = $this->__toString(); |
| 220 | 220 | $statement = $db->prepare($query); |
| 221 | 221 | $statement->execute($this->values); |
| 222 | - if($this->getCalcFoundRows()) { |
|
| 222 | + if ($this->getCalcFoundRows()) { |
|
| 223 | 223 | $this->foundRows = (int) $db->query('SELECT FOUND_ROWS()')->fetchColumn(); |
| 224 | 224 | } |
| 225 | 225 | return $statement; |
@@ -241,20 +241,20 @@ discard block |
||
| 241 | 241 | * @return mixed |
| 242 | 242 | */ |
| 243 | 243 | private function fetchAll(Closure $callback = null, $mode, $arg0 = null) { |
| 244 | - return $this->createTempStatement(function (QueryStatement $statement) use ($callback, $mode, $arg0) { |
|
| 244 | + return $this->createTempStatement(function(QueryStatement $statement) use ($callback, $mode, $arg0) { |
|
| 245 | 245 | $statement->setFetchMode($mode, $arg0); |
| 246 | 246 | $data = $statement->fetchAll(); |
| 247 | - if($this->preserveTypes) { |
|
| 247 | + if ($this->preserveTypes) { |
|
| 248 | 248 | $columnDefinitions = FieldTypeProvider::getFieldTypes($statement); |
| 249 | - foreach($data as &$row) { |
|
| 249 | + foreach ($data as &$row) { |
|
| 250 | 250 | $row = FieldValueConverter::convertValues($row, $columnDefinitions); |
| 251 | 251 | } |
| 252 | 252 | } |
| 253 | - if($callback !== null) { |
|
| 254 | - return call_user_func(function ($resultData = []) use ($data, $callback) { |
|
| 255 | - foreach($data as $row) { |
|
| 253 | + if ($callback !== null) { |
|
| 254 | + return call_user_func(function($resultData = []) use ($data, $callback) { |
|
| 255 | + foreach ($data as $row) { |
|
| 256 | 256 | $result = $callback($row); |
| 257 | - if($result !== null && !($result instanceof DBIgnoreRow)) { |
|
| 257 | + if ($result !== null && !($result instanceof DBIgnoreRow)) { |
|
| 258 | 258 | $resultData[] = $result; |
| 259 | 259 | } else { |
| 260 | 260 | $resultData[] = $row; |
@@ -288,20 +288,20 @@ discard block |
||
| 288 | 288 | * @return mixed |
| 289 | 289 | */ |
| 290 | 290 | private function fetch(Closure $callback = null, $mode, $arg0 = null, Closure $resultValidator = null) { |
| 291 | - return $this->createTempStatement(function (QueryStatement $statement) use ($callback, $mode, $arg0, $resultValidator) { |
|
| 291 | + return $this->createTempStatement(function(QueryStatement $statement) use ($callback, $mode, $arg0, $resultValidator) { |
|
| 292 | 292 | $statement->setFetchMode($mode, $arg0); |
| 293 | 293 | $row = $statement->fetch(); |
| 294 | 294 | $result = $resultValidator($row); |
| 295 | - if(!$result['valid']) { |
|
| 295 | + if (!$result['valid']) { |
|
| 296 | 296 | return $result['default']; |
| 297 | 297 | } |
| 298 | - if($this->preserveTypes) { |
|
| 298 | + if ($this->preserveTypes) { |
|
| 299 | 299 | $columnDefinitions = FieldTypeProvider::getFieldTypes($statement); |
| 300 | 300 | $row = FieldValueConverter::convertValues($row, $columnDefinitions); |
| 301 | 301 | } |
| 302 | - if($callback !== null) { |
|
| 302 | + if ($callback !== null) { |
|
| 303 | 303 | $result = $callback($row); |
| 304 | - if($result !== null) { |
|
| 304 | + if ($result !== null) { |
|
| 305 | 305 | $row = $result; |
| 306 | 306 | } |
| 307 | 307 | } |
@@ -13,36 +13,36 @@ |
||
| 13 | 13 | * @return string |
| 14 | 14 | */ |
| 15 | 15 | protected function buildTableName($alias, $name) { |
| 16 | - if(is_object($name) && !($name instanceof VirtualTable) && method_exists($name, '__toString')) { |
|
| 16 | + if (is_object($name) && !($name instanceof VirtualTable) && method_exists($name, '__toString')) { |
|
| 17 | 17 | $name = (string) $name; |
| 18 | 18 | $lines = explode("\n", $name); |
| 19 | - foreach($lines as &$line) { |
|
| 19 | + foreach ($lines as &$line) { |
|
| 20 | 20 | $line = "\t{$line}"; |
| 21 | 21 | } |
| 22 | 22 | $name = join("\n", $lines); |
| 23 | - $name = '(' . trim(rtrim(trim($name), ';')) . ')'; |
|
| 23 | + $name = '('.trim(rtrim(trim($name), ';')).')'; |
|
| 24 | 24 | } |
| 25 | - if(is_array($name)) { |
|
| 25 | + if (is_array($name)) { |
|
| 26 | 26 | $parts = []; |
| 27 | - foreach($name as $index => $bucket) { |
|
| 28 | - if(ctype_digit((string) $index) && is_scalar($bucket)) { |
|
| 27 | + foreach ($name as $index => $bucket) { |
|
| 28 | + if (ctype_digit((string) $index) && is_scalar($bucket)) { |
|
| 29 | 29 | $parts[] = "SELECT {$this->db()->quote($bucket)} AS {$this->db()->quoteField('value')}"; |
| 30 | 30 | } else { |
| 31 | 31 | $values = []; |
| 32 | - foreach($bucket as $field => $value) { |
|
| 32 | + foreach ($bucket as $field => $value) { |
|
| 33 | 33 | $values[] = sprintf('%s AS %s', $this->db()->quote($value), $this->db()->quoteField($field)); |
| 34 | 34 | } |
| 35 | 35 | $parts[] = sprintf("SELECT %s", join(', ', $values)); |
| 36 | 36 | } |
| 37 | 37 | } |
| 38 | - $name = '(' . join("\n\tUNION\n\t", $parts) . ')'; |
|
| 38 | + $name = '('.join("\n\tUNION\n\t", $parts).')'; |
|
| 39 | 39 | } |
| 40 | - if($this->db()->getVirtualTables()->has($name)) { |
|
| 40 | + if ($this->db()->getVirtualTables()->has($name)) { |
|
| 41 | 41 | $select = (string) $this->db()->getVirtualTables()->get($name); |
| 42 | 42 | $name = sprintf('(%s)', join("\n\t", explode("\n", trim($select)))); |
| 43 | 43 | } |
| 44 | 44 | $name = $this->aliasReplacer()->replace($name); |
| 45 | - if($alias !== null) { |
|
| 45 | + if ($alias !== null) { |
|
| 46 | 46 | return sprintf("%s %s", $name, $alias); |
| 47 | 47 | } |
| 48 | 48 | return $name; |
@@ -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; |
@@ -86,7 +86,7 @@ discard block |
||
| 86 | 86 | * @return QueryStatement |
| 87 | 87 | */ |
| 88 | 88 | public function query($query) { |
| 89 | - return $this->buildQueryStatement($query, function ($query) { |
|
| 89 | + return $this->buildQueryStatement($query, function($query) { |
|
| 90 | 90 | $stmt = $this->pdo->query($query); |
| 91 | 91 | return $stmt; |
| 92 | 92 | }); |
@@ -97,7 +97,7 @@ discard block |
||
| 97 | 97 | * @return QueryStatement |
| 98 | 98 | */ |
| 99 | 99 | public function prepare($query) { |
| 100 | - return $this->buildQueryStatement((string) $query, function ($query) { |
|
| 100 | + return $this->buildQueryStatement((string) $query, function($query) { |
|
| 101 | 101 | $stmt = $this->pdo->prepare($query); |
| 102 | 102 | return $stmt; |
| 103 | 103 | }); |
@@ -109,11 +109,11 @@ discard block |
||
| 109 | 109 | * @return int |
| 110 | 110 | */ |
| 111 | 111 | public function exec($query, array $params = []) { |
| 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; |
@@ -133,12 +133,12 @@ discard block |
||
| 133 | 133 | */ |
| 134 | 134 | public function getTableFields($table) { |
| 135 | 135 | $table = $this->select()->aliasReplacer()->replace($table); |
| 136 | - if(array_key_exists($table, self::$tableFields)) { |
|
| 136 | + if (array_key_exists($table, self::$tableFields)) { |
|
| 137 | 137 | return self::$tableFields[$table]; |
| 138 | 138 | } |
| 139 | 139 | $stmt = $this->pdo->query("DESCRIBE {$table}"); |
| 140 | 140 | $rows = $stmt->fetchAll(\PDO::FETCH_ASSOC); |
| 141 | - self::$tableFields[$table] = array_map(function ($row) { return $row['Field']; }, $rows); |
|
| 141 | + self::$tableFields[$table] = array_map(function($row) { return $row['Field']; }, $rows); |
|
| 142 | 142 | $stmt->closeCursor(); |
| 143 | 143 | return self::$tableFields[$table]; |
| 144 | 144 | } |
@@ -150,12 +150,12 @@ discard block |
||
| 150 | 150 | */ |
| 151 | 151 | public function quoteExpression($expression, array $arguments = array()) { |
| 152 | 152 | $index = -1; |
| 153 | - $func = function () use ($arguments, &$index) { |
|
| 153 | + $func = function() use ($arguments, &$index) { |
|
| 154 | 154 | $index++; |
| 155 | - if(array_key_exists($index, $arguments)) { |
|
| 155 | + if (array_key_exists($index, $arguments)) { |
|
| 156 | 156 | $argument = $arguments[$index]; |
| 157 | 157 | $value = $this->quote($argument); |
| 158 | - } elseif(count($arguments) > 0) { |
|
| 158 | + } elseif (count($arguments) > 0) { |
|
| 159 | 159 | $args = $arguments; |
| 160 | 160 | $value = array_pop($args); |
| 161 | 161 | $value = $this->quote($value); |
@@ -173,14 +173,14 @@ discard block |
||
| 173 | 173 | * @return string |
| 174 | 174 | */ |
| 175 | 175 | public function quote($value) { |
| 176 | - if(is_null($value)) { |
|
| 176 | + if (is_null($value)) { |
|
| 177 | 177 | $result = 'NULL'; |
| 178 | - } elseif($value instanceof Builder\DBExpr) { |
|
| 178 | + } elseif ($value instanceof Builder\DBExpr) { |
|
| 179 | 179 | $result = $value->getExpression(); |
| 180 | - } elseif($value instanceof Builder\Select) { |
|
| 180 | + } elseif ($value instanceof Builder\Select) { |
|
| 181 | 181 | $result = sprintf('(%s)', (string) $value); |
| 182 | - } elseif(is_array($value)) { |
|
| 183 | - $result = join(', ', array_map(function ($value) { return $this->quote($value); }, $value)); |
|
| 182 | + } elseif (is_array($value)) { |
|
| 183 | + $result = join(', ', array_map(function($value) { return $this->quote($value); }, $value)); |
|
| 184 | 184 | } else { |
| 185 | 185 | $result = $this->pdo->quote($value); |
| 186 | 186 | } |
@@ -195,7 +195,7 @@ discard block |
||
| 195 | 195 | if (is_numeric($field) || !is_string($field)) { |
| 196 | 196 | throw new UnexpectedValueException('Field name is invalid'); |
| 197 | 197 | } |
| 198 | - if(strpos($field, '`') !== false) { |
|
| 198 | + if (strpos($field, '`') !== false) { |
|
| 199 | 199 | return $field; |
| 200 | 200 | } |
| 201 | 201 | $parts = explode('.', $field); |
@@ -210,7 +210,7 @@ discard block |
||
| 210 | 210 | $select = array_key_exists('select-factory', $this->options) |
| 211 | 211 | ? call_user_func($this->options['select-factory'], $this, $this->options['select-options']) |
| 212 | 212 | : new Builder\RunnableSelect($this, $this->options['select-options']); |
| 213 | - if($fields !== null) { |
|
| 213 | + if ($fields !== null) { |
|
| 214 | 214 | $select->fields($fields); |
| 215 | 215 | } |
| 216 | 216 | return $select; |
@@ -224,7 +224,7 @@ discard block |
||
| 224 | 224 | $insert = array_key_exists('insert-factory', $this->options) |
| 225 | 225 | ? call_user_func($this->options['insert-factory'], $this, $this->options['insert-options']) |
| 226 | 226 | : new Builder\RunnableInsert($this, $this->options['insert-options']); |
| 227 | - if($fields !== null) { |
|
| 227 | + if ($fields !== null) { |
|
| 228 | 228 | $insert->addAll($fields); |
| 229 | 229 | } |
| 230 | 230 | return $insert; |
@@ -238,7 +238,7 @@ discard block |
||
| 238 | 238 | $update = array_key_exists('update-factory', $this->options) |
| 239 | 239 | ? call_user_func($this->options['update-factory'], $this, $this->options['update-options']) |
| 240 | 240 | : new Builder\RunnableUpdate($this, $this->options['update-options']); |
| 241 | - if($fields !== null) { |
|
| 241 | + if ($fields !== null) { |
|
| 242 | 242 | $update->setAll($fields); |
| 243 | 243 | } |
| 244 | 244 | return $update; |
@@ -257,8 +257,8 @@ discard block |
||
| 257 | 257 | * @return $this |
| 258 | 258 | */ |
| 259 | 259 | public function transactionStart() { |
| 260 | - if($this->transactionLevel === 0) { |
|
| 261 | - if($this->pdo->inTransaction()) { |
|
| 260 | + if ($this->transactionLevel === 0) { |
|
| 261 | + if ($this->pdo->inTransaction()) { |
|
| 262 | 262 | $this->outerTransaction = true; |
| 263 | 263 | } else { |
| 264 | 264 | $this->pdo->beginTransaction(); |
@@ -272,7 +272,7 @@ discard block |
||
| 272 | 272 | * @return $this |
| 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 | } |
@@ -281,7 +281,7 @@ discard block |
||
| 281 | 281 | * @return $this |
| 282 | 282 | */ |
| 283 | 283 | public function transactionRollback() { |
| 284 | - return $this->transactionEnd(function () { |
|
| 284 | + return $this->transactionEnd(function() { |
|
| 285 | 285 | $this->pdo->rollBack(); |
| 286 | 286 | }); |
| 287 | 287 | } |
@@ -291,7 +291,7 @@ discard block |
||
| 291 | 291 | * @return mixed |
| 292 | 292 | */ |
| 293 | 293 | public function dryRun($callback = null) { |
| 294 | - if(!$this->pdo->inTransaction()) { |
|
| 294 | + if (!$this->pdo->inTransaction()) { |
|
| 295 | 295 | $this->transactionStart(); |
| 296 | 296 | try { |
| 297 | 297 | return call_user_func($callback, $this); |
@@ -315,14 +315,14 @@ discard block |
||
| 315 | 315 | */ |
| 316 | 316 | public function transaction(callable $callback = null) { |
| 317 | 317 | $result = null; |
| 318 | - if(!$this->pdo->inTransaction()) { |
|
| 318 | + if (!$this->pdo->inTransaction()) { |
|
| 319 | 319 | $this->transactionStart(); |
| 320 | 320 | try { |
| 321 | 321 | $result = call_user_func($callback, $this); |
| 322 | 322 | $this->transactionCommit(); |
| 323 | 323 | return $result; |
| 324 | 324 | } finally { |
| 325 | - if($this->pdo->inTransaction()) { |
|
| 325 | + if ($this->pdo->inTransaction()) { |
|
| 326 | 326 | $this->transactionRollback(); |
| 327 | 327 | } |
| 328 | 328 | } |
@@ -335,7 +335,7 @@ discard block |
||
| 335 | 335 | $rollback = false; |
| 336 | 336 | return $result; |
| 337 | 337 | } finally { |
| 338 | - if($rollback) { |
|
| 338 | + if ($rollback) { |
|
| 339 | 339 | $this->exec("ROLLBACK TO {$uniqueId}"); |
| 340 | 340 | } else { |
| 341 | 341 | $this->exec("RELEASE SAVEPOINT {$uniqueId}"); |
@@ -350,10 +350,10 @@ discard block |
||
| 350 | 350 | */ |
| 351 | 351 | private function transactionEnd($fn) { |
| 352 | 352 | $this->transactionLevel--; |
| 353 | - if($this->transactionLevel < 0) { |
|
| 353 | + if ($this->transactionLevel < 0) { |
|
| 354 | 354 | throw new RuntimeException("Transaction-Nesting-Problem: Trying to invoke commit on a already closed transaction"); |
| 355 | - } elseif($this->transactionLevel < 1) { |
|
| 356 | - if($this->outerTransaction) { |
|
| 355 | + } elseif ($this->transactionLevel < 1) { |
|
| 356 | + if ($this->outerTransaction) { |
|
| 357 | 357 | $this->outerTransaction = false; |
| 358 | 358 | } else { |
| 359 | 359 | call_user_func($fn); |
@@ -369,7 +369,7 @@ discard block |
||
| 369 | 369 | */ |
| 370 | 370 | private function buildQueryStatement($query, $fn) { |
| 371 | 371 | $stmt = call_user_func($fn, $query); |
| 372 | - if(!$stmt) { |
|
| 372 | + if (!$stmt) { |
|
| 373 | 373 | throw new RuntimeException("Could not execute statement:\n{$query}"); |
| 374 | 374 | } |
| 375 | 375 | $stmtWrapper = new QueryStatement($stmt, $query, $this->exceptionInterpreter, $this->queryLoggers); |
@@ -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); |