@@ -11,7 +11,7 @@ |
||
11 | 11 | public static function getFieldTypes(QueryStatement $statement) { |
12 | 12 | $c = $statement->columnCount(); |
13 | 13 | $fieldTypes = array(); |
14 | - for($i=0; $i<$c+20; $i++) { |
|
14 | + for ($i = 0; $i < $c+20; $i++) { |
|
15 | 15 | $column = $statement->getColumnMeta($i); |
16 | 16 | $fieldTypes[$column['name']] = self::getTypeFromNativeType($column['native_type']); |
17 | 17 | } |
@@ -21,14 +21,14 @@ |
||
21 | 21 | * @return array[]|\Generator |
22 | 22 | */ |
23 | 23 | public function generate(QueryStatement $statement, Closure $callback = null) { |
24 | - while($row = $statement->fetch(\PDO::FETCH_ASSOC)) { |
|
25 | - if($this->preserveTypes) { |
|
24 | + while ($row = $statement->fetch(\PDO::FETCH_ASSOC)) { |
|
25 | + if ($this->preserveTypes) { |
|
26 | 26 | $columnDefinitions = FieldTypeProvider::getFieldTypes($statement); |
27 | 27 | $row = FieldValueConverter::convertValues($row, $columnDefinitions); |
28 | 28 | } |
29 | - if($callback !== null) { |
|
29 | + if ($callback !== null) { |
|
30 | 30 | $row = call_user_func($callback); |
31 | - if($row !== null) { |
|
31 | + if ($row !== null) { |
|
32 | 32 | yield $row; |
33 | 33 | } |
34 | 34 | } else { |
@@ -12,12 +12,12 @@ |
||
12 | 12 | * @return string |
13 | 13 | */ |
14 | 14 | public static function build(Database $db, $query, array $conditions, $token) { |
15 | - if(!count($conditions)) { |
|
15 | + if (!count($conditions)) { |
|
16 | 16 | return $query; |
17 | 17 | } |
18 | 18 | $query .= "{$token}\n"; |
19 | 19 | $arr = array(); |
20 | - foreach($conditions as $condition) { |
|
20 | + foreach ($conditions as $condition) { |
|
21 | 21 | list($expression, $arguments) = $condition; |
22 | 22 | $expr = $db->quoteExpression($expression, $arguments); |
23 | 23 | $arr[] = "\t({$expr})"; |
@@ -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; |
@@ -88,7 +88,7 @@ discard block |
||
88 | 88 | * @return $this |
89 | 89 | */ |
90 | 90 | public function addExpr($str) { |
91 | - if(count(func_get_args()) > 1) { |
|
91 | + if (count(func_get_args()) > 1) { |
|
92 | 92 | $this->fields[] = func_get_args(); |
93 | 93 | } else { |
94 | 94 | $this->fields[] = $str; |
@@ -102,7 +102,7 @@ discard block |
||
102 | 102 | * @return $this |
103 | 103 | */ |
104 | 104 | public function updateExpr($str) { |
105 | - if(count(func_get_args()) > 1) { |
|
105 | + if (count(func_get_args()) > 1) { |
|
106 | 106 | $this->update[] = func_get_args(); |
107 | 107 | } else { |
108 | 108 | $this->update[] = $str; |
@@ -115,7 +115,7 @@ discard block |
||
115 | 115 | * @return $this |
116 | 116 | */ |
117 | 117 | public function addOrUpdateExpr($str) { |
118 | - if(count(func_get_args()) > 1) { |
|
118 | + if (count(func_get_args()) > 1) { |
|
119 | 119 | $this->fields[] = func_get_args(); |
120 | 120 | $this->update[] = func_get_args(); |
121 | 121 | } else { |
@@ -132,7 +132,7 @@ discard block |
||
132 | 132 | * @return $this |
133 | 133 | */ |
134 | 134 | public function addAll(array $data, array $mask = null, array $excludeFields = null) { |
135 | - $this->addAllTo($data, $mask, $excludeFields, function ($field, $value) { |
|
135 | + $this->addAllTo($data, $mask, $excludeFields, function($field, $value) { |
|
136 | 136 | $this->add($field, $value); |
137 | 137 | }); |
138 | 138 | return $this; |
@@ -145,7 +145,7 @@ discard block |
||
145 | 145 | * @return $this |
146 | 146 | */ |
147 | 147 | public function updateAll(array $data, array $mask = null, array $excludeFields = null) { |
148 | - $this->addAllTo($data, $mask, $excludeFields, function ($field, $value) { |
|
148 | + $this->addAllTo($data, $mask, $excludeFields, function($field, $value) { |
|
149 | 149 | if ($field !== $this->keyField) { |
150 | 150 | $this->update($field, $value); |
151 | 151 | } |
@@ -189,7 +189,7 @@ discard block |
||
189 | 189 | $ignoreStr = $this->ignore ? ' IGNORE' : ''; |
190 | 190 | $queryArr[] = "INSERT{$ignoreStr} INTO\n\t{$tableName}\n"; |
191 | 191 | |
192 | - if($this->from !== null) { |
|
192 | + if ($this->from !== null) { |
|
193 | 193 | $fields = $this->from->getFields(); |
194 | 194 | $queryArr[] = sprintf("\t(%s)\n", join(', ', array_keys($fields))); |
195 | 195 | $queryArr[] = $this->from; |
@@ -203,7 +203,7 @@ discard block |
||
203 | 203 | } |
204 | 204 | |
205 | 205 | $updateData = $this->buildUpdate(); |
206 | - if($updateData) { |
|
206 | + if ($updateData) { |
|
207 | 207 | $queryArr[] = "{$updateData}\n"; |
208 | 208 | } |
209 | 209 | |
@@ -236,12 +236,12 @@ discard block |
||
236 | 236 | * @return $this |
237 | 237 | */ |
238 | 238 | private function addAllTo(array $data, array $mask = null, array $excludeFields = null, $fn) { |
239 | - if($mask !== null) { |
|
239 | + if ($mask !== null) { |
|
240 | 240 | $data = array_intersect_key($data, array_combine($mask, $mask)); |
241 | 241 | } |
242 | - if($excludeFields !== null) { |
|
243 | - foreach($excludeFields as $excludeField) { |
|
244 | - if(array_key_exists($excludeField, $data)) { |
|
242 | + if ($excludeFields !== null) { |
|
243 | + foreach ($excludeFields as $excludeField) { |
|
244 | + if (array_key_exists($excludeField, $data)) { |
|
245 | 245 | unset($data[$excludeField]); |
246 | 246 | } |
247 | 247 | } |
@@ -257,10 +257,10 @@ discard block |
||
257 | 257 | */ |
258 | 258 | private function buildUpdate() { |
259 | 259 | $queryArr = array(); |
260 | - if(!empty($this->update)) { |
|
260 | + if (!empty($this->update)) { |
|
261 | 261 | $queryArr[] = "ON DUPLICATE KEY UPDATE\n"; |
262 | 262 | $updateArr = array(); |
263 | - if($this->keyField !== null) { |
|
263 | + if ($this->keyField !== null) { |
|
264 | 264 | $updateArr[] = "\t`{$this->keyField}` = LAST_INSERT_ID({$this->keyField})"; |
265 | 265 | } |
266 | 266 | $updateArr = $this->buildFieldList($this->update, $updateArr); |
@@ -284,7 +284,7 @@ discard block |
||
284 | 284 | * @throws Exception |
285 | 285 | */ |
286 | 286 | private function clearValues(array $values) { |
287 | - if(!count($values)) { |
|
287 | + if (!count($values)) { |
|
288 | 288 | return []; |
289 | 289 | } |
290 | 290 | |
@@ -293,7 +293,7 @@ discard block |
||
293 | 293 | $result = array(); |
294 | 294 | |
295 | 295 | foreach ($values as $fieldName => $fieldValue) { |
296 | - if(in_array($fieldName, $fields)) { |
|
296 | + if (in_array($fieldName, $fields)) { |
|
297 | 297 | $result[$fieldName] = $fieldValue; |
298 | 298 | } |
299 | 299 | } |
@@ -35,7 +35,7 @@ discard block |
||
35 | 35 | * @param PDO $pdo |
36 | 36 | */ |
37 | 37 | public function __construct(PDO $pdo) { |
38 | - if($pdo->getAttribute(PDO::ATTR_ERRMODE) === PDO::ERRMODE_SILENT) { |
|
38 | + if ($pdo->getAttribute(PDO::ATTR_ERRMODE) === PDO::ERRMODE_SILENT) { |
|
39 | 39 | $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); |
40 | 40 | } |
41 | 41 | $this->pdo = $pdo; |
@@ -64,7 +64,7 @@ discard block |
||
64 | 64 | * @return QueryStatement |
65 | 65 | */ |
66 | 66 | public function query($query) { |
67 | - return $this->buildQueryStatement($query, function ($query) { |
|
67 | + return $this->buildQueryStatement($query, function($query) { |
|
68 | 68 | $stmt = $this->pdo->query($query); |
69 | 69 | return $stmt; |
70 | 70 | }); |
@@ -76,7 +76,7 @@ discard block |
||
76 | 76 | * @return QueryStatement |
77 | 77 | */ |
78 | 78 | public function prepare($query) { |
79 | - return $this->buildQueryStatement((string) $query, function ($query) { |
|
79 | + return $this->buildQueryStatement((string) $query, function($query) { |
|
80 | 80 | $stmt = $this->pdo->prepare($query); |
81 | 81 | return $stmt; |
82 | 82 | }); |
@@ -92,7 +92,7 @@ discard block |
||
92 | 92 | $stmt = $this->pdo->prepare($query); |
93 | 93 | $timer = microtime(true); |
94 | 94 | $stmt->execute($params); |
95 | - $this->queryLoggers->log($query, microtime(true) - $timer); |
|
95 | + $this->queryLoggers->log($query, microtime(true)-$timer); |
|
96 | 96 | $result = $stmt->rowCount(); |
97 | 97 | $stmt->closeCursor(); |
98 | 98 | return $result; |
@@ -115,12 +115,12 @@ discard block |
||
115 | 115 | */ |
116 | 116 | public function getTableFields($table) { |
117 | 117 | $table = $this->select()->aliasReplacer()->replace($table); |
118 | - if(array_key_exists($table, self::$tableFields)) { |
|
118 | + if (array_key_exists($table, self::$tableFields)) { |
|
119 | 119 | return self::$tableFields[$table]; |
120 | 120 | } |
121 | 121 | $stmt = $this->pdo->query("DESCRIBE {$table}"); |
122 | 122 | $rows = $stmt->fetchAll(\PDO::FETCH_ASSOC); |
123 | - self::$tableFields[$table] = array_map(function ($row) { return $row['Field']; }, $rows); |
|
123 | + self::$tableFields[$table] = array_map(function($row) { return $row['Field']; }, $rows); |
|
124 | 124 | $stmt->closeCursor(); |
125 | 125 | return self::$tableFields[$table]; |
126 | 126 | } |
@@ -131,11 +131,11 @@ discard block |
||
131 | 131 | * @return string |
132 | 132 | */ |
133 | 133 | public function quoteExpression($expression, array $arguments = array()) { |
134 | - $func = function () use ($arguments) { |
|
134 | + $func = function() use ($arguments) { |
|
135 | 135 | static $idx = -1; |
136 | 136 | $idx++; |
137 | 137 | $index = $idx; |
138 | - if(array_key_exists($index, $arguments)) { |
|
138 | + if (array_key_exists($index, $arguments)) { |
|
139 | 139 | $argument = $arguments[$index]; |
140 | 140 | $value = $this->quote($argument); |
141 | 141 | } else { |
@@ -152,12 +152,12 @@ discard block |
||
152 | 152 | * @return string |
153 | 153 | */ |
154 | 154 | public function quote($value) { |
155 | - if(is_null($value)) { |
|
155 | + if (is_null($value)) { |
|
156 | 156 | $result = 'NULL'; |
157 | - } elseif($value instanceof Builder\Select) { |
|
157 | + } elseif ($value instanceof Builder\Select) { |
|
158 | 158 | $result = sprintf('(%s)', (string) $value); |
159 | - } elseif(is_array($value)) { |
|
160 | - $result = join(', ', array_map(function ($value) { return $this->quote($value); }, $value)); |
|
159 | + } elseif (is_array($value)) { |
|
160 | + $result = join(', ', array_map(function($value) { return $this->quote($value); }, $value)); |
|
161 | 161 | /*} elseif(is_int(trim($value)) && strpos('123456789', substr(0, 1, trim($value))) !== null) { |
162 | 162 | $result = $value;*/ |
163 | 163 | } else { |
@@ -174,7 +174,7 @@ discard block |
||
174 | 174 | if (is_numeric($field) || !is_string($field)) { |
175 | 175 | throw new UnexpectedValueException('Field name is invalid'); |
176 | 176 | } |
177 | - if(strpos($field, '`') !== false) { |
|
177 | + if (strpos($field, '`') !== false) { |
|
178 | 178 | return (string) $field; |
179 | 179 | } |
180 | 180 | $parts = explode('.', $field); |
@@ -187,7 +187,7 @@ discard block |
||
187 | 187 | */ |
188 | 188 | public function select(array $fields = null) { |
189 | 189 | $select = new RunnableSelect($this); |
190 | - if($fields !== null) { |
|
190 | + if ($fields !== null) { |
|
191 | 191 | $select->fields($fields); |
192 | 192 | } |
193 | 193 | return $select; |
@@ -199,7 +199,7 @@ discard block |
||
199 | 199 | */ |
200 | 200 | public function insert(array $fields = null) { |
201 | 201 | $insert = new Builder\RunnableInsert($this); |
202 | - if($fields !== null) { |
|
202 | + if ($fields !== null) { |
|
203 | 203 | $insert->addAll($fields); |
204 | 204 | } |
205 | 205 | return $insert; |
@@ -211,7 +211,7 @@ discard block |
||
211 | 211 | */ |
212 | 212 | public function update(array $fields = null) { |
213 | 213 | $update = new Builder\RunnableUpdate($this); |
214 | - if($fields !== null) { |
|
214 | + if ($fields !== null) { |
|
215 | 215 | $update->setAll($fields); |
216 | 216 | } |
217 | 217 | return $update; |
@@ -228,8 +228,8 @@ discard block |
||
228 | 228 | * @return $this |
229 | 229 | */ |
230 | 230 | public function transactionStart() { |
231 | - if((int) $this->transactionLevel === 0) { |
|
232 | - if($this->pdo->inTransaction()) { |
|
231 | + if ((int) $this->transactionLevel === 0) { |
|
232 | + if ($this->pdo->inTransaction()) { |
|
233 | 233 | $this->outerTransaction = true; |
234 | 234 | } else { |
235 | 235 | $this->pdo->beginTransaction(); |
@@ -244,7 +244,7 @@ discard block |
||
244 | 244 | * @throws \Exception |
245 | 245 | */ |
246 | 246 | public function transactionCommit() { |
247 | - return $this->transactionEnd(function () { |
|
247 | + return $this->transactionEnd(function() { |
|
248 | 248 | $this->pdo->commit(); |
249 | 249 | }); |
250 | 250 | } |
@@ -254,7 +254,7 @@ discard block |
||
254 | 254 | * @throws \Exception |
255 | 255 | */ |
256 | 256 | public function transactionRollback() { |
257 | - return $this->transactionEnd(function () { |
|
257 | + return $this->transactionEnd(function() { |
|
258 | 258 | $this->pdo->rollBack(); |
259 | 259 | }); |
260 | 260 | } |
@@ -267,14 +267,14 @@ discard block |
||
267 | 267 | * @throws null |
268 | 268 | */ |
269 | 269 | public function transaction($tries = 1, $callback = null) { |
270 | - if(is_callable($tries)) { |
|
270 | + if (is_callable($tries)) { |
|
271 | 271 | $callback = $tries; |
272 | 272 | $tries = 1; |
273 | - } elseif(!is_callable($callback)) { |
|
273 | + } elseif (!is_callable($callback)) { |
|
274 | 274 | throw new \Exception('$callback must be a callable'); |
275 | 275 | } |
276 | 276 | $e = null; |
277 | - for(; $tries--;) { |
|
277 | + for (; $tries--;) { |
|
278 | 278 | try { |
279 | 279 | $this->transactionStart(); |
280 | 280 | $result = call_user_func($callback, $this); |
@@ -294,11 +294,11 @@ discard block |
||
294 | 294 | */ |
295 | 295 | private function transactionEnd($fn) { |
296 | 296 | $this->transactionLevel--; |
297 | - if($this->transactionLevel < 0) { |
|
297 | + if ($this->transactionLevel < 0) { |
|
298 | 298 | throw new \Exception("Transaction-Nesting-Problem: Trying to invoke commit on a already closed transaction"); |
299 | 299 | } |
300 | - if((int) $this->transactionLevel === 0) { |
|
301 | - if($this->outerTransaction) { |
|
300 | + if ((int) $this->transactionLevel === 0) { |
|
301 | + if ($this->outerTransaction) { |
|
302 | 302 | $this->outerTransaction = false; |
303 | 303 | } else { |
304 | 304 | call_user_func($fn); |
@@ -315,7 +315,7 @@ discard block |
||
315 | 315 | */ |
316 | 316 | private function buildQueryStatement($query, $fn) { |
317 | 317 | $stmt = call_user_func($fn, $query); |
318 | - if(!$stmt) { |
|
318 | + if (!$stmt) { |
|
319 | 319 | throw new Exception("Could not execute statement:\n{$query}"); |
320 | 320 | } |
321 | 321 | $stmtWrapper = new QueryStatement($stmt, $query, $this->exceptionInterpreter, $this->queryLoggers); |
@@ -13,11 +13,11 @@ discard block |
||
13 | 13 | * @return $this |
14 | 14 | */ |
15 | 15 | public function orderBy($expression, $direction = 'asc') { |
16 | - if(strtolower($direction) != 'desc') { |
|
16 | + if (strtolower($direction) != 'desc') { |
|
17 | 17 | $direction = 'ASC'; |
18 | 18 | } |
19 | - if(is_array($expression)) { |
|
20 | - if(!count($expression)) { |
|
19 | + if (is_array($expression)) { |
|
20 | + if (!count($expression)) { |
|
21 | 21 | return $this; |
22 | 22 | } |
23 | 23 | $arguments = array( |
@@ -34,7 +34,7 @@ discard block |
||
34 | 34 | */ |
35 | 35 | public function orderByValues($fieldName, array $values) { |
36 | 36 | $expr = []; |
37 | - foreach(array_values($values) as $idx => $value) { |
|
37 | + foreach (array_values($values) as $idx => $value) { |
|
38 | 38 | $expr[] = $this->db()->quoteExpression("WHEN ? THEN ?", array($value, $idx)); |
39 | 39 | } |
40 | 40 | $this->orderBy[] = array(sprintf("CASE %s\n\t\t%s\n\tEND", $this->db()->quoteField($fieldName), join("\n\t\t", $expr)), 'ASC'); |
@@ -46,12 +46,12 @@ discard block |
||
46 | 46 | * @return string |
47 | 47 | */ |
48 | 48 | protected function buildOrder($query) { |
49 | - if(!count($this->orderBy)) { |
|
49 | + if (!count($this->orderBy)) { |
|
50 | 50 | return $query; |
51 | 51 | } |
52 | 52 | $query .= "ORDER BY\n"; |
53 | 53 | $arr = array(); |
54 | - foreach($this->orderBy as $order) { |
|
54 | + foreach ($this->orderBy as $order) { |
|
55 | 55 | list($expression, $direction) = $order; |
56 | 56 | $arr[] = sprintf("\t%s %s", $expression, strtoupper($direction)); |
57 | 57 | } |