@@ -16,8 +16,8 @@ |
||
16 | 16 | * @return $this |
17 | 17 | */ |
18 | 18 | public function having($expression) { |
19 | - if($expression instanceof OptionalExpression) { |
|
20 | - if($expression->isValid()) { |
|
19 | + if ($expression instanceof OptionalExpression) { |
|
20 | + if ($expression->isValid()) { |
|
21 | 21 | $this->having[] = [$expression->getExpression(), $expression->getValue()]; |
22 | 22 | } |
23 | 23 | } else { |
@@ -15,8 +15,8 @@ discard block |
||
15 | 15 | * @return $this |
16 | 16 | */ |
17 | 17 | public function orderBy($expression, $direction = 'asc') { |
18 | - if($expression instanceof OrderBySpecification) { |
|
19 | - foreach($expression->getFields() as $field) { |
|
18 | + if ($expression instanceof OrderBySpecification) { |
|
19 | + foreach ($expression->getFields() as $field) { |
|
20 | 20 | $this->addOrder($field[0], $field[1]); |
21 | 21 | } |
22 | 22 | return $this; |
@@ -32,7 +32,7 @@ discard block |
||
32 | 32 | */ |
33 | 33 | public function orderByValues($fieldName, array $values) { |
34 | 34 | $expr = []; |
35 | - foreach(array_values($values) as $idx => $value) { |
|
35 | + foreach (array_values($values) as $idx => $value) { |
|
36 | 36 | $expr[] = $this->db()->quoteExpression("WHEN ? THEN ?", array($value, $idx)); |
37 | 37 | } |
38 | 38 | $this->orderBy[] = array(sprintf("CASE %s\n\t\t%s\n\tEND", $this->db()->quoteField($fieldName), join("\n\t\t", $expr)), 'ASC'); |
@@ -44,12 +44,12 @@ discard block |
||
44 | 44 | * @return string |
45 | 45 | */ |
46 | 46 | protected function buildOrder($query) { |
47 | - if(!count($this->orderBy)) { |
|
47 | + if (!count($this->orderBy)) { |
|
48 | 48 | return $query; |
49 | 49 | } |
50 | 50 | $query .= "ORDER BY\n"; |
51 | 51 | $arr = array(); |
52 | - foreach($this->orderBy as $order) { |
|
52 | + foreach ($this->orderBy as $order) { |
|
53 | 53 | list($expression, $direction) = $order; |
54 | 54 | $arr[] = sprintf("\t%s %s", $expression, strtoupper($direction)); |
55 | 55 | } |
@@ -62,8 +62,8 @@ discard block |
||
62 | 62 | */ |
63 | 63 | private function addOrder($expression, $direction) { |
64 | 64 | $direction = $this->fixDirection($direction); |
65 | - if(is_array($expression)) { |
|
66 | - if(!count($expression)) { |
|
65 | + if (is_array($expression)) { |
|
66 | + if (!count($expression)) { |
|
67 | 67 | return; |
68 | 68 | } |
69 | 69 | $arguments = array( |
@@ -16,8 +16,8 @@ |
||
16 | 16 | * @return $this |
17 | 17 | */ |
18 | 18 | public function where($expression) { |
19 | - if($expression instanceof OptionalExpression) { |
|
20 | - if($expression->isValid()) { |
|
19 | + if ($expression instanceof OptionalExpression) { |
|
20 | + if ($expression->isValid()) { |
|
21 | 21 | $this->where[] = [$expression->getExpression(), $expression->getValue()]; |
22 | 22 | } |
23 | 23 | } else { |
@@ -11,16 +11,16 @@ |
||
11 | 11 | */ |
12 | 12 | public function __construct($spec, $sortFieldsSpec) { |
13 | 13 | $expressions = []; |
14 | - foreach($spec as $specReference => $dbExpr) { |
|
15 | - if(is_int($specReference)) { |
|
14 | + foreach ($spec as $specReference => $dbExpr) { |
|
15 | + if (is_int($specReference)) { |
|
16 | 16 | $specReference = $dbExpr; |
17 | 17 | } |
18 | 18 | $expressions[$specReference] = $dbExpr; |
19 | 19 | } |
20 | - foreach($sortFieldsSpec as $sortFieldSpec) { |
|
21 | - if(array_key_exists(0, $sortFieldSpec) && array_key_exists($sortFieldSpec[0], $expressions)) { |
|
20 | + foreach ($sortFieldsSpec as $sortFieldSpec) { |
|
21 | + if (array_key_exists(0, $sortFieldSpec) && array_key_exists($sortFieldSpec[0], $expressions)) { |
|
22 | 22 | $direction = 'ASC'; |
23 | - if(array_key_exists(1, $sortFieldSpec) && strtoupper($sortFieldSpec[1]) !== 'ASC') { |
|
23 | + if (array_key_exists(1, $sortFieldSpec) && strtoupper($sortFieldSpec[1]) !== 'ASC') { |
|
24 | 24 | $direction = 'DESC'; |
25 | 25 | } |
26 | 26 | $this->fields[] = [ |
@@ -16,14 +16,14 @@ |
||
16 | 16 | public function throwMoreConcreteException(PDOException $exception) { |
17 | 17 | $code = $exception->getCode(); |
18 | 18 | $message = (string) $exception->getMessage(); |
19 | - switch($code) { |
|
19 | + switch ($code) { |
|
20 | 20 | case 2006: throw new DatabaseHasGoneAwayException($message, $code, $exception); |
21 | 21 | case 1213: throw new SqlDeadLockException($message, $code, $exception); |
22 | 22 | case 1205: throw new LockWaitTimeoutExceededException($message, $code, $exception); |
23 | 23 | case 1062: throw new DuplicateUniqueKeyException($message, $code, $exception); |
24 | 24 | } |
25 | 25 | /** @link http://php.net/manual/en/class.exception.php#Hcom115813 (cHao's comment) */ |
26 | - if(!is_string($message) || !is_int($code)) { |
|
26 | + if (!is_string($message) || !is_int($code)) { |
|
27 | 27 | throw new SqlException((string) $message, (int) $code, $exception); |
28 | 28 | } |
29 | 29 | throw $exception; |
@@ -34,7 +34,7 @@ discard block |
||
34 | 34 | * @param PDO $pdo |
35 | 35 | */ |
36 | 36 | public function __construct(PDO $pdo) { |
37 | - if($pdo->getAttribute(PDO::ATTR_ERRMODE) === PDO::ERRMODE_SILENT) { |
|
37 | + if ($pdo->getAttribute(PDO::ATTR_ERRMODE) === PDO::ERRMODE_SILENT) { |
|
38 | 38 | $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); |
39 | 39 | } |
40 | 40 | $this->pdo = $pdo; |
@@ -63,7 +63,7 @@ discard block |
||
63 | 63 | * @return QueryStatement |
64 | 64 | */ |
65 | 65 | public function query($query) { |
66 | - return $this->buildQueryStatement($query, function ($query) { |
|
66 | + return $this->buildQueryStatement($query, function($query) { |
|
67 | 67 | $stmt = $this->pdo->query($query); |
68 | 68 | return $stmt; |
69 | 69 | }); |
@@ -75,7 +75,7 @@ discard block |
||
75 | 75 | * @return QueryStatement |
76 | 76 | */ |
77 | 77 | public function prepare($query) { |
78 | - return $this->buildQueryStatement((string) $query, function ($query) { |
|
78 | + return $this->buildQueryStatement((string) $query, function($query) { |
|
79 | 79 | $stmt = $this->pdo->prepare($query); |
80 | 80 | return $stmt; |
81 | 81 | }); |
@@ -87,11 +87,11 @@ discard block |
||
87 | 87 | * @return int |
88 | 88 | */ |
89 | 89 | public function exec($query, array $params = array()) { |
90 | - return $this->exceptionHandler(function () use ($query, $params) { |
|
90 | + return $this->exceptionHandler(function() use ($query, $params) { |
|
91 | 91 | $stmt = $this->pdo->prepare($query); |
92 | 92 | $timer = microtime(true); |
93 | 93 | $stmt->execute($params); |
94 | - $this->queryLoggers->log($query, microtime(true) - $timer); |
|
94 | + $this->queryLoggers->log($query, microtime(true)-$timer); |
|
95 | 95 | $result = $stmt->rowCount(); |
96 | 96 | $stmt->closeCursor(); |
97 | 97 | return $result; |
@@ -111,12 +111,12 @@ discard block |
||
111 | 111 | */ |
112 | 112 | public function getTableFields($table) { |
113 | 113 | $table = $this->select()->aliasReplacer()->replace($table); |
114 | - if(array_key_exists($table, self::$tableFields)) { |
|
114 | + if (array_key_exists($table, self::$tableFields)) { |
|
115 | 115 | return self::$tableFields[$table]; |
116 | 116 | } |
117 | 117 | $stmt = $this->pdo->query("DESCRIBE {$table}"); |
118 | 118 | $rows = $stmt->fetchAll(\PDO::FETCH_ASSOC); |
119 | - self::$tableFields[$table] = array_map(function ($row) { return $row['Field']; }, $rows); |
|
119 | + self::$tableFields[$table] = array_map(function($row) { return $row['Field']; }, $rows); |
|
120 | 120 | $stmt->closeCursor(); |
121 | 121 | return self::$tableFields[$table]; |
122 | 122 | } |
@@ -127,11 +127,11 @@ discard block |
||
127 | 127 | * @return string |
128 | 128 | */ |
129 | 129 | public function quoteExpression($expression, array $arguments = array()) { |
130 | - $func = function () use ($arguments) { |
|
130 | + $func = function() use ($arguments) { |
|
131 | 131 | static $idx = -1; |
132 | 132 | $idx++; |
133 | 133 | $index = $idx; |
134 | - if(array_key_exists($index, $arguments)) { |
|
134 | + if (array_key_exists($index, $arguments)) { |
|
135 | 135 | $argument = $arguments[$index]; |
136 | 136 | $value = $this->quote($argument); |
137 | 137 | } else { |
@@ -148,14 +148,14 @@ discard block |
||
148 | 148 | * @return string |
149 | 149 | */ |
150 | 150 | public function quote($value) { |
151 | - if(is_null($value)) { |
|
151 | + if (is_null($value)) { |
|
152 | 152 | $result = 'NULL'; |
153 | - } elseif($value instanceof Builder\DBExpr) { |
|
153 | + } elseif ($value instanceof Builder\DBExpr) { |
|
154 | 154 | $result = $value->getExpression(); |
155 | - } elseif($value instanceof Builder\Select) { |
|
155 | + } elseif ($value instanceof Builder\Select) { |
|
156 | 156 | $result = sprintf('(%s)', (string) $value); |
157 | - } elseif(is_array($value)) { |
|
158 | - $result = join(', ', array_map(function ($value) { return $this->quote($value); }, $value)); |
|
157 | + } elseif (is_array($value)) { |
|
158 | + $result = join(', ', array_map(function($value) { return $this->quote($value); }, $value)); |
|
159 | 159 | } else { |
160 | 160 | $result = $this->pdo->quote($value); |
161 | 161 | } |
@@ -170,7 +170,7 @@ discard block |
||
170 | 170 | if (is_numeric($field) || !is_string($field)) { |
171 | 171 | throw new UnexpectedValueException('Field name is invalid'); |
172 | 172 | } |
173 | - if(strpos($field, '`') !== false) { |
|
173 | + if (strpos($field, '`') !== false) { |
|
174 | 174 | return (string) $field; |
175 | 175 | } |
176 | 176 | $parts = explode('.', $field); |
@@ -183,7 +183,7 @@ discard block |
||
183 | 183 | */ |
184 | 184 | public function select(array $fields = null) { |
185 | 185 | $select = new Builder\RunnableSelect($this); |
186 | - if($fields !== null) { |
|
186 | + if ($fields !== null) { |
|
187 | 187 | $select->fields($fields); |
188 | 188 | } |
189 | 189 | return $select; |
@@ -195,7 +195,7 @@ discard block |
||
195 | 195 | */ |
196 | 196 | public function insert(array $fields = null) { |
197 | 197 | $insert = new Builder\RunnableInsert($this); |
198 | - if($fields !== null) { |
|
198 | + if ($fields !== null) { |
|
199 | 199 | $insert->addAll($fields); |
200 | 200 | } |
201 | 201 | return $insert; |
@@ -207,7 +207,7 @@ discard block |
||
207 | 207 | */ |
208 | 208 | public function update(array $fields = null) { |
209 | 209 | $update = new Builder\RunnableUpdate($this); |
210 | - if($fields !== null) { |
|
210 | + if ($fields !== null) { |
|
211 | 211 | $update->setAll($fields); |
212 | 212 | } |
213 | 213 | return $update; |
@@ -224,8 +224,8 @@ discard block |
||
224 | 224 | * @return $this |
225 | 225 | */ |
226 | 226 | public function transactionStart() { |
227 | - if((int) $this->transactionLevel === 0) { |
|
228 | - if($this->pdo->inTransaction()) { |
|
227 | + if ((int) $this->transactionLevel === 0) { |
|
228 | + if ($this->pdo->inTransaction()) { |
|
229 | 229 | $this->outerTransaction = true; |
230 | 230 | } else { |
231 | 231 | $this->pdo->beginTransaction(); |
@@ -240,7 +240,7 @@ discard block |
||
240 | 240 | * @throws \Exception |
241 | 241 | */ |
242 | 242 | public function transactionCommit() { |
243 | - return $this->transactionEnd(function () { |
|
243 | + return $this->transactionEnd(function() { |
|
244 | 244 | $this->pdo->commit(); |
245 | 245 | }); |
246 | 246 | } |
@@ -250,7 +250,7 @@ discard block |
||
250 | 250 | * @throws \Exception |
251 | 251 | */ |
252 | 252 | public function transactionRollback() { |
253 | - return $this->transactionEnd(function () { |
|
253 | + return $this->transactionEnd(function() { |
|
254 | 254 | $this->pdo->rollBack(); |
255 | 255 | }); |
256 | 256 | } |
@@ -264,7 +264,7 @@ discard block |
||
264 | 264 | public function dryRun($callback = null) { |
265 | 265 | $result = null; |
266 | 266 | $exception = null; |
267 | - if(!$this->pdo->inTransaction()) { |
|
267 | + if (!$this->pdo->inTransaction()) { |
|
268 | 268 | $this->transactionStart(); |
269 | 269 | try { |
270 | 270 | $result = call_user_func($callback, $this); |
@@ -272,7 +272,7 @@ discard block |
||
272 | 272 | $exception = $e; |
273 | 273 | } |
274 | 274 | $this->transactionRollback(); |
275 | - if($exception !== null) { |
|
275 | + if ($exception !== null) { |
|
276 | 276 | throw $exception; |
277 | 277 | } |
278 | 278 | } else { |
@@ -284,7 +284,7 @@ discard block |
||
284 | 284 | $exception = $e; |
285 | 285 | } |
286 | 286 | $this->exec("ROLLBACK TO {$uniqueId}"); |
287 | - if($exception !== null) { |
|
287 | + if ($exception !== null) { |
|
288 | 288 | throw $exception; |
289 | 289 | } |
290 | 290 | } |
@@ -299,15 +299,15 @@ discard block |
||
299 | 299 | * @throws null |
300 | 300 | */ |
301 | 301 | public function transaction($tries = 1, $callback = null) { |
302 | - if(is_callable($tries)) { |
|
302 | + if (is_callable($tries)) { |
|
303 | 303 | $callback = $tries; |
304 | 304 | $tries = 1; |
305 | - } elseif(!is_callable($callback)) { |
|
305 | + } elseif (!is_callable($callback)) { |
|
306 | 306 | throw new \Exception('$callback must be a callable'); |
307 | 307 | } |
308 | 308 | $e = null; |
309 | - if(!$this->pdo->inTransaction()) { |
|
310 | - for(; $tries--;) { |
|
309 | + if (!$this->pdo->inTransaction()) { |
|
310 | + for (; $tries--;) { |
|
311 | 311 | try { |
312 | 312 | $this->transactionStart(); |
313 | 313 | $result = call_user_func($callback, $this); |
@@ -338,11 +338,11 @@ discard block |
||
338 | 338 | */ |
339 | 339 | private function transactionEnd($fn) { |
340 | 340 | $this->transactionLevel--; |
341 | - if($this->transactionLevel < 0) { |
|
341 | + if ($this->transactionLevel < 0) { |
|
342 | 342 | throw new \Exception("Transaction-Nesting-Problem: Trying to invoke commit on a already closed transaction"); |
343 | 343 | } |
344 | - if((int) $this->transactionLevel === 0) { |
|
345 | - if($this->outerTransaction) { |
|
344 | + if ((int) $this->transactionLevel === 0) { |
|
345 | + if ($this->outerTransaction) { |
|
346 | 346 | $this->outerTransaction = false; |
347 | 347 | } else { |
348 | 348 | call_user_func($fn); |
@@ -359,7 +359,7 @@ discard block |
||
359 | 359 | */ |
360 | 360 | private function buildQueryStatement($query, $fn) { |
361 | 361 | $stmt = call_user_func($fn, $query); |
362 | - if(!$stmt) { |
|
362 | + if (!$stmt) { |
|
363 | 363 | throw new Exception("Could not execute statement:\n{$query}"); |
364 | 364 | } |
365 | 365 | $stmtWrapper = new QueryStatement($stmt, $query, $this->exceptionInterpreter, $this->queryLoggers); |