@@ -20,7 +20,7 @@ |
||
20 | 20 | * @return $this |
21 | 21 | */ |
22 | 22 | public function log($query, $duration) { |
23 | - foreach($this->queryLoggers as $queryLogger) { |
|
23 | + foreach ($this->queryLoggers as $queryLogger) { |
|
24 | 24 | $queryLogger->log($query, $duration); |
25 | 25 | } |
26 | 26 | return $this; |
@@ -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,16 +131,16 @@ discard block |
||
131 | 131 | * @return string |
132 | 132 | */ |
133 | 133 | public function quoteExpression($expression, array $arguments = array()) { |
134 | - $func = function ($oldValue) use ($arguments) { |
|
134 | + $func = function($oldValue) use ($arguments) { |
|
135 | 135 | static $idx = -1; |
136 | 136 | $idx++; |
137 | 137 | $index = $idx; |
138 | 138 | |
139 | - if(substr($oldValue[0], 0, 1) == ':') { |
|
139 | + if (substr($oldValue[0], 0, 1) == ':') { |
|
140 | 140 | $index = (int) substr($oldValue[0], 1); |
141 | 141 | } |
142 | 142 | |
143 | - if(array_key_exists($index, $arguments)) { |
|
143 | + if (array_key_exists($index, $arguments)) { |
|
144 | 144 | $argument = $arguments[$index]; |
145 | 145 | $value = $this->quote($argument); |
146 | 146 | } else { |
@@ -157,12 +157,12 @@ discard block |
||
157 | 157 | * @return string |
158 | 158 | */ |
159 | 159 | public function quote($value) { |
160 | - if(is_null($value)) { |
|
160 | + if (is_null($value)) { |
|
161 | 161 | $result = 'NULL'; |
162 | - } elseif($value instanceof Builder\Select) { |
|
162 | + } elseif ($value instanceof Builder\Select) { |
|
163 | 163 | $result = sprintf('(%s)', (string) $value); |
164 | - } elseif(is_array($value)) { |
|
165 | - $result = join(', ', array_map(function ($value) { return $this->quote($value); }, $value)); |
|
164 | + } elseif (is_array($value)) { |
|
165 | + $result = join(', ', array_map(function($value) { return $this->quote($value); }, $value)); |
|
166 | 166 | /*} elseif(is_int(trim($value)) && strpos('123456789', substr(0, 1, trim($value))) !== null) { |
167 | 167 | $result = $value;*/ |
168 | 168 | } else { |
@@ -179,7 +179,7 @@ discard block |
||
179 | 179 | if (is_numeric($field) || !is_string($field)) { |
180 | 180 | throw new UnexpectedValueException('Field name is invalid'); |
181 | 181 | } |
182 | - if(strpos($field, '`') !== false) { |
|
182 | + if (strpos($field, '`') !== false) { |
|
183 | 183 | return (string) $field; |
184 | 184 | } |
185 | 185 | $parts = explode('.', $field); |
@@ -192,7 +192,7 @@ discard block |
||
192 | 192 | */ |
193 | 193 | public function select(array $fields = null) { |
194 | 194 | $select = new RunnableSelect($this); |
195 | - if($fields !== null) { |
|
195 | + if ($fields !== null) { |
|
196 | 196 | $select->fields($fields); |
197 | 197 | } |
198 | 198 | return $select; |
@@ -204,7 +204,7 @@ discard block |
||
204 | 204 | */ |
205 | 205 | public function insert(array $fields = null) { |
206 | 206 | $insert = new Builder\RunnableInsert($this); |
207 | - if($fields !== null) { |
|
207 | + if ($fields !== null) { |
|
208 | 208 | $insert->addAll($fields); |
209 | 209 | } |
210 | 210 | return $insert; |
@@ -216,7 +216,7 @@ discard block |
||
216 | 216 | */ |
217 | 217 | public function update(array $fields = null) { |
218 | 218 | $update = new Builder\RunnableUpdate($this); |
219 | - if($fields !== null) { |
|
219 | + if ($fields !== null) { |
|
220 | 220 | $update->setAll($fields); |
221 | 221 | } |
222 | 222 | return $update; |
@@ -233,8 +233,8 @@ discard block |
||
233 | 233 | * @return $this |
234 | 234 | */ |
235 | 235 | public function transactionStart() { |
236 | - if((int) $this->transactionLevel === 0) { |
|
237 | - if($this->pdo->inTransaction()) { |
|
236 | + if ((int) $this->transactionLevel === 0) { |
|
237 | + if ($this->pdo->inTransaction()) { |
|
238 | 238 | $this->outerTransaction = true; |
239 | 239 | } else { |
240 | 240 | $this->pdo->beginTransaction(); |
@@ -249,7 +249,7 @@ discard block |
||
249 | 249 | * @throws \Exception |
250 | 250 | */ |
251 | 251 | public function transactionCommit() { |
252 | - return $this->transactionEnd(function () { |
|
252 | + return $this->transactionEnd(function() { |
|
253 | 253 | $this->pdo->commit(); |
254 | 254 | }); |
255 | 255 | } |
@@ -259,7 +259,7 @@ discard block |
||
259 | 259 | * @throws \Exception |
260 | 260 | */ |
261 | 261 | public function transactionRollback() { |
262 | - return $this->transactionEnd(function () { |
|
262 | + return $this->transactionEnd(function() { |
|
263 | 263 | $this->pdo->rollBack(); |
264 | 264 | }); |
265 | 265 | } |
@@ -272,14 +272,14 @@ discard block |
||
272 | 272 | * @throws null |
273 | 273 | */ |
274 | 274 | public function transaction($tries = 1, $callback = null) { |
275 | - if(is_callable($tries)) { |
|
275 | + if (is_callable($tries)) { |
|
276 | 276 | $callback = $tries; |
277 | 277 | $tries = 1; |
278 | - } elseif(!is_callable($callback)) { |
|
278 | + } elseif (!is_callable($callback)) { |
|
279 | 279 | throw new \Exception('$callback must be a callable'); |
280 | 280 | } |
281 | 281 | $e = null; |
282 | - for(; $tries--;) { |
|
282 | + for (; $tries--;) { |
|
283 | 283 | try { |
284 | 284 | $this->transactionStart(); |
285 | 285 | $result = call_user_func($callback, $this); |
@@ -299,11 +299,11 @@ discard block |
||
299 | 299 | */ |
300 | 300 | private function transactionEnd($fn) { |
301 | 301 | $this->transactionLevel--; |
302 | - if($this->transactionLevel < 0) { |
|
302 | + if ($this->transactionLevel < 0) { |
|
303 | 303 | throw new \Exception("Transaction-Nesting-Problem: Trying to invoke commit on a already closed transaction"); |
304 | 304 | } |
305 | - if((int) $this->transactionLevel === 0) { |
|
306 | - if($this->outerTransaction) { |
|
305 | + if ((int) $this->transactionLevel === 0) { |
|
306 | + if ($this->outerTransaction) { |
|
307 | 307 | $this->outerTransaction = false; |
308 | 308 | } else { |
309 | 309 | call_user_func($fn); |
@@ -320,7 +320,7 @@ discard block |
||
320 | 320 | */ |
321 | 321 | private function buildQueryStatement($query, $fn) { |
322 | 322 | $stmt = call_user_func($fn, $query); |
323 | - if(!$stmt) { |
|
323 | + if (!$stmt) { |
|
324 | 324 | throw new Exception("Could not execute statement:\n{$query}"); |
325 | 325 | } |
326 | 326 | $stmtWrapper = new QueryStatement($stmt, $query, $this->exceptionInterpreter, $this->queryLoggers); |
@@ -14,13 +14,13 @@ |
||
14 | 14 | public function throwMoreConcreteException(PDOException $exception) { |
15 | 15 | $code = $exception->errorInfo[1]; |
16 | 16 | $message = $exception->errorInfo[2]; |
17 | - if($code === 1213) { |
|
17 | + if ($code === 1213) { |
|
18 | 18 | throw new SqlDeadLockException($message, $code, $exception); |
19 | 19 | } |
20 | - if($code === 1205) { |
|
20 | + if ($code === 1205) { |
|
21 | 21 | throw new LockWaitTimeoutExceededException($message, $code, $exception); |
22 | 22 | } |
23 | - if($code === 1062) { |
|
23 | + if ($code === 1062) { |
|
24 | 24 | throw new DuplicateUniqueKeyException($message, $code, $exception); |
25 | 25 | } |
26 | 26 | throw $exception; |
@@ -19,11 +19,11 @@ |
||
19 | 19 | * @return string |
20 | 20 | */ |
21 | 21 | public function replace($name) { |
22 | - $fn = function ($values) { |
|
22 | + $fn = function($values) { |
|
23 | 23 | $alias = $values[1]; |
24 | 24 | $part = $values[2]; |
25 | 25 | $string = $this->aliasRegistry->get($alias); |
26 | - return $string . $part; |
|
26 | + return $string.$part; |
|
27 | 27 | }; |
28 | 28 | return preg_replace_callback('/^(\\w+)#(\\w+)$/', $fn, $name); |
29 | 29 | } |
@@ -23,7 +23,7 @@ |
||
23 | 23 | * @return string |
24 | 24 | */ |
25 | 25 | public function get($alias) { |
26 | - if(!array_key_exists($alias, $this->aliases)) { |
|
26 | + if (!array_key_exists($alias, $this->aliases)) { |
|
27 | 27 | throw new \Exception("Alias not found: {$alias}"); |
28 | 28 | } |
29 | 29 | return $this->aliases[$alias]; |
@@ -37,17 +37,17 @@ discard block |
||
37 | 37 | * @return $this |
38 | 38 | */ |
39 | 39 | public function field($expression, $alias = null) { |
40 | - if(is_object($expression)) { |
|
40 | + if (is_object($expression)) { |
|
41 | 41 | $expression = (string) $expression; |
42 | 42 | $expression = trim($expression); |
43 | 43 | $expression = rtrim($expression, ';'); |
44 | 44 | $expression = trim($expression); |
45 | 45 | $lines = explode("\n", $expression); |
46 | - $lines = array_map(function ($line) { return "\t\t{$line}"; }, $lines); |
|
46 | + $lines = array_map(function($line) { return "\t\t{$line}"; }, $lines); |
|
47 | 47 | $expression = join("\n", $lines); |
48 | 48 | $expression = sprintf("(\n%s\n\t)", $expression); |
49 | 49 | } |
50 | - if($alias === null) { |
|
50 | + if ($alias === null) { |
|
51 | 51 | $this->fields[] = $expression; |
52 | 52 | } else { |
53 | 53 | $this->fields[$alias] = $expression; |
@@ -60,7 +60,7 @@ discard block |
||
60 | 60 | * @return $this |
61 | 61 | */ |
62 | 62 | public function fields(array $fields) { |
63 | - foreach($fields as $alias => $expression) { |
|
63 | + foreach ($fields as $alias => $expression) { |
|
64 | 64 | $this->field($expression, $alias); |
65 | 65 | } |
66 | 66 | return $this; |
@@ -95,7 +95,7 @@ discard block |
||
95 | 95 | * @return $this |
96 | 96 | */ |
97 | 97 | public function setCalcFoundRows($calcFoundRows = true) { |
98 | - if(ini_get("mysql.trace_mode")) { |
|
98 | + if (ini_get("mysql.trace_mode")) { |
|
99 | 99 | throw new \Exception('This function cant operate with mysql.trace_mode is set.'); |
100 | 100 | } |
101 | 101 | $this->calcFoundRows = $calcFoundRows; |
@@ -117,12 +117,12 @@ discard block |
||
117 | 117 | */ |
118 | 118 | public function __toString() { |
119 | 119 | $query = "SELECT"; |
120 | - if($this->calcFoundRows) { |
|
120 | + if ($this->calcFoundRows) { |
|
121 | 121 | $query .= " SQL_CALC_FOUND_ROWS"; |
122 | 122 | } |
123 | 123 | $query .= "\n"; |
124 | 124 | $query = $this->buildFields($query); |
125 | - if(count($this->getTables())) { |
|
125 | + if (count($this->getTables())) { |
|
126 | 126 | $query .= "FROM\n"; |
127 | 127 | } |
128 | 128 | $query = $this->buildTables($query); |
@@ -144,9 +144,9 @@ discard block |
||
144 | 144 | */ |
145 | 145 | private function buildFields($query) { |
146 | 146 | $fields = array(); |
147 | - if(count($this->fields)) { |
|
148 | - foreach($this->fields as $alias => $expression) { |
|
149 | - if(is_numeric($alias)) { |
|
147 | + if (count($this->fields)) { |
|
148 | + foreach ($this->fields as $alias => $expression) { |
|
149 | + if (is_numeric($alias)) { |
|
150 | 150 | $fields[] = "\t{$expression}"; |
151 | 151 | } else { |
152 | 152 | $fields[] = "\t{$expression} AS `{$alias}`"; |
@@ -163,7 +163,7 @@ discard block |
||
163 | 163 | * @return string |
164 | 164 | */ |
165 | 165 | private function buildForUpdate($query) { |
166 | - if($this->forUpdate) { |
|
166 | + if ($this->forUpdate) { |
|
167 | 167 | $query .= "FOR UPDATE\n"; |
168 | 168 | } |
169 | 169 | return $query; |
@@ -43,10 +43,10 @@ discard block |
||
43 | 43 | * @return bool |
44 | 44 | */ |
45 | 45 | public function execute(array $params = []) { |
46 | - return $this->exceptionHandler(function () use ($params) { |
|
46 | + return $this->exceptionHandler(function() use ($params) { |
|
47 | 47 | $timer = microtime(true); |
48 | 48 | $response = $this->statement->execute($params); |
49 | - $this->queryLoggers->log($this->query, microtime(true) - $timer); |
|
49 | + $this->queryLoggers->log($this->query, microtime(true)-$timer); |
|
50 | 50 | return $response; |
51 | 51 | }); |
52 | 52 | } |
@@ -56,7 +56,7 @@ discard block |
||
56 | 56 | */ |
57 | 57 | public function fetchAll() { |
58 | 58 | $args = func_get_args(); |
59 | - return $this->exceptionHandler(function () use ($args) { |
|
59 | + return $this->exceptionHandler(function() use ($args) { |
|
60 | 60 | return call_user_func_array([$this->statement, 'fetchAll'], $args); |
61 | 61 | }); |
62 | 62 | } |
@@ -68,7 +68,7 @@ discard block |
||
68 | 68 | * @return mixed |
69 | 69 | */ |
70 | 70 | public function fetch($fetchStyle = PDO::FETCH_ASSOC, $cursorOrientation = PDO::FETCH_ORI_NEXT, $cursorOffset = 0) { |
71 | - return $this->exceptionHandler(function () use ($fetchStyle, $cursorOrientation, $cursorOffset) { |
|
71 | + return $this->exceptionHandler(function() use ($fetchStyle, $cursorOrientation, $cursorOffset) { |
|
72 | 72 | return $this->statement->fetch($fetchStyle, $cursorOrientation, $cursorOffset); |
73 | 73 | }); |
74 | 74 | } |
@@ -78,7 +78,7 @@ discard block |
||
78 | 78 | * @return mixed |
79 | 79 | */ |
80 | 80 | public function fetchColumn($columnNo = 0) { |
81 | - return $this->exceptionHandler(function () use ($columnNo) { |
|
81 | + return $this->exceptionHandler(function() use ($columnNo) { |
|
82 | 82 | return $this->statement->fetchColumn($columnNo); |
83 | 83 | }); |
84 | 84 | } |
@@ -87,7 +87,7 @@ discard block |
||
87 | 87 | * @return bool |
88 | 88 | */ |
89 | 89 | public function closeCursor() { |
90 | - return $this->exceptionHandler(function () { |
|
90 | + return $this->exceptionHandler(function() { |
|
91 | 91 | return $this->statement->closeCursor(); |
92 | 92 | }); |
93 | 93 | } |
@@ -96,7 +96,7 @@ discard block |
||
96 | 96 | * @return int |
97 | 97 | */ |
98 | 98 | public function columnCount() { |
99 | - return $this->exceptionHandler(function () { |
|
99 | + return $this->exceptionHandler(function() { |
|
100 | 100 | return $this->statement->columnCount(); |
101 | 101 | }); |
102 | 102 | } |
@@ -106,7 +106,7 @@ discard block |
||
106 | 106 | * @return array |
107 | 107 | */ |
108 | 108 | public function getColumnMeta($columnNo) { |
109 | - return $this->exceptionHandler(function () use ($columnNo) { |
|
109 | + return $this->exceptionHandler(function() use ($columnNo) { |
|
110 | 110 | return $this->statement->getColumnMeta($columnNo); |
111 | 111 | }); |
112 | 112 | } |
@@ -24,12 +24,12 @@ |
||
24 | 24 | * @return $this |
25 | 25 | */ |
26 | 26 | public function debug($stop = true) { |
27 | - if(php_sapi_name() == 'cli') { |
|
27 | + if (php_sapi_name() == 'cli') { |
|
28 | 28 | echo "\n{$this->__toString()}\n"; |
29 | 29 | } else { |
30 | 30 | echo "<pre>{$this->__toString()}</pre>"; |
31 | 31 | } |
32 | - if($stop) { |
|
32 | + if ($stop) { |
|
33 | 33 | exit; |
34 | 34 | } |
35 | 35 | return $this; |
@@ -74,15 +74,15 @@ discard block |
||
74 | 74 | * @throws Exception |
75 | 75 | */ |
76 | 76 | public function setAll(array $data, array $allowedFields = null) { |
77 | - if($allowedFields !== null) { |
|
78 | - foreach($data as $fieldName => $value) { |
|
79 | - if(in_array($fieldName, $allowedFields)) { |
|
77 | + if ($allowedFields !== null) { |
|
78 | + foreach ($data as $fieldName => $value) { |
|
79 | + if (in_array($fieldName, $allowedFields)) { |
|
80 | 80 | $this->set($fieldName, $value); |
81 | 81 | } |
82 | 82 | } |
83 | 83 | } else { |
84 | 84 | $values = $this->clearValues($data); |
85 | - foreach($values as $fieldName => $value) { |
|
85 | + foreach ($values as $fieldName => $value) { |
|
86 | 86 | $this->set($fieldName, $value); |
87 | 87 | } |
88 | 88 | } |
@@ -125,14 +125,14 @@ discard block |
||
125 | 125 | * @throws Exception |
126 | 126 | */ |
127 | 127 | private function clearValues(array $values) { |
128 | - if(!count($values)) { |
|
128 | + if (!count($values)) { |
|
129 | 129 | return []; |
130 | 130 | } |
131 | 131 | $tables = $this->getTables(); |
132 | - if(!count($tables)) { |
|
132 | + if (!count($tables)) { |
|
133 | 133 | throw new Exception('Table name is missing'); |
134 | 134 | } |
135 | - if(count($tables) > 1) { |
|
135 | + if (count($tables) > 1) { |
|
136 | 136 | throw new Exception('Batch values only work with max. one table'); |
137 | 137 | } |
138 | 138 | list($table) = $tables; |
@@ -142,7 +142,7 @@ discard block |
||
142 | 142 | $result = array(); |
143 | 143 | |
144 | 144 | foreach ($values as $fieldName => $fieldValue) { |
145 | - if(in_array($fieldName, $fields)) { |
|
145 | + if (in_array($fieldName, $fields)) { |
|
146 | 146 | $result[$fieldName] = $fieldValue; |
147 | 147 | } |
148 | 148 | } |