@@ -55,7 +55,7 @@ |
||
55 | 55 | } |
56 | 56 | public function next() |
57 | 57 | { |
58 | - $this->fetched ++; |
|
58 | + $this->fetched++; |
|
59 | 59 | $this->last = $this->statement->fetch(\PDO::FETCH_ASSOC); |
60 | 60 | } |
61 | 61 | public function valid() |
@@ -44,7 +44,7 @@ |
||
44 | 44 | isset($this->connection['opts']) ? $this->connection['opts'] : [] |
45 | 45 | ); |
46 | 46 | } catch (\PDOException $e) { |
47 | - throw new DBException('Connect error: ' . $e->getMessage()); |
|
47 | + throw new DBException('Connect error: '.$e->getMessage()); |
|
48 | 48 | } |
49 | 49 | } |
50 | 50 | } |
@@ -78,7 +78,7 @@ discard block |
||
78 | 78 | public function setPrimaryKey($column) : Table |
79 | 79 | { |
80 | 80 | if (!is_array($column)) { |
81 | - $column = [ $column ]; |
|
81 | + $column = [$column]; |
|
82 | 82 | } |
83 | 83 | $this->data['primary'] = $column; |
84 | 84 | return $this; |
@@ -165,7 +165,7 @@ discard block |
||
165 | 165 | } |
166 | 166 | |
167 | 167 | if (!isset($name)) { |
168 | - $name = $toTable->getName() . '_' . implode('_', array_keys($keymap)); |
|
168 | + $name = $toTable->getName().'_'.implode('_', array_keys($keymap)); |
|
169 | 169 | } |
170 | 170 | $this->addRelation(new TableRelation( |
171 | 171 | $name, |
@@ -83,7 +83,7 @@ |
||
83 | 83 | } |
84 | 84 | } |
85 | 85 | if (!$this->statement->execute()) { |
86 | - throw new DBException('Prepared execute error: ' . $this->statement->error); |
|
86 | + throw new DBException('Prepared execute error: '.$this->statement->error); |
|
87 | 87 | } |
88 | 88 | return new Result($this->statement); |
89 | 89 | } |
@@ -74,7 +74,7 @@ |
||
74 | 74 | } |
75 | 75 | public function next() |
76 | 76 | { |
77 | - $this->fetched ++; |
|
77 | + $this->fetched++; |
|
78 | 78 | $temp = \odbc_fetch_row($this->statement); |
79 | 79 | if (!$temp) { |
80 | 80 | $this->last = false; |
@@ -18,6 +18,9 @@ |
||
18 | 18 | protected $fetched = -1; |
19 | 19 | protected $iid = null; |
20 | 20 | |
21 | + /** |
|
22 | + * @param resource $statement |
|
23 | + */ |
|
21 | 24 | public function __construct($statement, $data, $iid, $charIn = null, $charOut = null) |
22 | 25 | { |
23 | 26 | $this->statement = $statement; |
@@ -70,7 +70,7 @@ discard block |
||
70 | 70 | $fields = []; |
71 | 71 | $exists = false; |
72 | 72 | foreach ($relation->table->getColumns() as $column) { |
73 | - $nm = $name . static::SEP . $column; |
|
73 | + $nm = $name.static::SEP.$column; |
|
74 | 74 | if (isset($this->aliases[$nm])) { |
75 | 75 | $nm = $this->aliases[$nm]; |
76 | 76 | } |
@@ -84,16 +84,16 @@ discard block |
||
84 | 84 | $parts = explode(static::SEP, $name); |
85 | 85 | $name = array_pop($parts); |
86 | 86 | if (!$exists && !count($parts) && !isset($temp[$name])) { |
87 | - $temp[$name] = $relation->many ? [ '___clean' => true ] : null; |
|
87 | + $temp[$name] = $relation->many ? ['___clean' => true] : null; |
|
88 | 88 | } |
89 | 89 | if ($exists) { |
90 | - $full = ''; |
|
90 | + $full = ''; |
|
91 | 91 | foreach ($parts as $item) { |
92 | - $full = $full ? $full . static::SEP . $item : $item; |
|
92 | + $full = $full ? $full.static::SEP.$item : $item; |
|
93 | 93 | $temp = &$temp[$item]; |
94 | 94 | $rpk = []; |
95 | 95 | foreach ($this->relations[$full][0]->table->getPrimaryKey() as $pkey) { |
96 | - $nm = $full . static::SEP . $pkey; |
|
96 | + $nm = $full.static::SEP.$pkey; |
|
97 | 97 | if (isset($this->aliases[$nm])) { |
98 | 98 | $nm = $this->aliases[$nm]; |
99 | 99 | } |
@@ -102,7 +102,7 @@ discard block |
||
102 | 102 | $temp = &$temp[json_encode($rpk)]; |
103 | 103 | } |
104 | 104 | if (!isset($temp[$name])) { |
105 | - $temp[$name] = $relation->many ? [ '___clean' => true ] : null; |
|
105 | + $temp[$name] = $relation->many ? ['___clean' => true] : null; |
|
106 | 106 | } |
107 | 107 | $temp = &$temp[$name]; |
108 | 108 | if ($relation->many) { |
@@ -160,7 +160,7 @@ discard block |
||
160 | 160 | return; |
161 | 161 | } |
162 | 162 | } |
163 | - $this->fetched ++; |
|
163 | + $this->fetched++; |
|
164 | 164 | while ($this->result->valid()) { |
165 | 165 | $row = $this->result->current(); |
166 | 166 | $pk = []; |
@@ -4,7 +4,6 @@ |
||
4 | 4 | use vakata\collection\Collection; |
5 | 5 | use vakata\database\DBInterface; |
6 | 6 | use vakata\database\DBException; |
7 | -use vakata\database\ResultInterface; |
|
8 | 7 | |
9 | 8 | /** |
10 | 9 | * A database query class |
@@ -378,7 +378,7 @@ |
||
378 | 378 | } |
379 | 379 | /** |
380 | 380 | * Group by a column (or columns) |
381 | - * @param string|array $column the column name (or names) to group by |
|
381 | + * @param string $column the column name (or names) to group by |
|
382 | 382 | * @return $this |
383 | 383 | */ |
384 | 384 | public function group($column) : self |
@@ -44,7 +44,7 @@ discard block |
||
44 | 44 | /** |
45 | 45 | * @var int[] |
46 | 46 | */ |
47 | - protected $li_of = [0,0,0]; |
|
47 | + protected $li_of = [0, 0, 0]; |
|
48 | 48 | /** |
49 | 49 | * @var array |
50 | 50 | */ |
@@ -74,7 +74,7 @@ discard block |
||
74 | 74 | public function __construct(DBInterface $db, $table) |
75 | 75 | { |
76 | 76 | $this->db = $db; |
77 | - $this->definition = $table instanceof Table ? $table : $this->db->definition((string)$table); |
|
77 | + $this->definition = $table instanceof Table ? $table : $this->db->definition((string) $table); |
|
78 | 78 | $primary = $this->definition->getPrimaryKey(); |
79 | 79 | $columns = $this->definition->getColumns(); |
80 | 80 | $this->pkey = count($primary) ? $primary : $columns; |
@@ -97,7 +97,7 @@ discard block |
||
97 | 97 | { |
98 | 98 | $column = explode('.', $column); |
99 | 99 | if (count($column) === 1) { |
100 | - $column = [ $this->definition->getName(), $column[0] ]; |
|
100 | + $column = [$this->definition->getName(), $column[0]]; |
|
101 | 101 | $col = $this->definition->getColumn($column[1]); |
102 | 102 | if (!$col) { |
103 | 103 | throw new DBException('Invalid column name in own table'); |
@@ -120,7 +120,7 @@ discard block |
||
120 | 120 | throw new DBException('Invalid column name in related table'); |
121 | 121 | } |
122 | 122 | } else { |
123 | - throw new DBException('Invalid foreign table name: ' . implode(',', $column)); |
|
123 | + throw new DBException('Invalid foreign table name: '.implode(',', $column)); |
|
124 | 124 | } |
125 | 125 | } |
126 | 126 | } else { |
@@ -129,19 +129,19 @@ discard block |
||
129 | 129 | $table = $this->definition; |
130 | 130 | $table = array_reduce( |
131 | 131 | $column, |
132 | - function ($carry, $item) use (&$table) { |
|
132 | + function($carry, $item) use (&$table) { |
|
133 | 133 | $table = $table->getRelation($item)->table; |
134 | 134 | return $table; |
135 | 135 | } |
136 | 136 | ); |
137 | 137 | $col = $table->getColumn($name); |
138 | - $column = [ implode(static::SEP, $column), $name ]; |
|
138 | + $column = [implode(static::SEP, $column), $name]; |
|
139 | 139 | } |
140 | - return [ 'name' => implode('.', $column), 'data' => $col ]; |
|
140 | + return ['name' => implode('.', $column), 'data' => $col]; |
|
141 | 141 | } |
142 | 142 | protected function normalizeValue(TableColumn $col, $value) |
143 | 143 | { |
144 | - $strict = (int)$this->db->driverOption('strict', 0) > 0; |
|
144 | + $strict = (int) $this->db->driverOption('strict', 0) > 0; |
|
145 | 145 | if ($value === null && $col->isNullable()) { |
146 | 146 | return null; |
147 | 147 | } |
@@ -151,7 +151,7 @@ discard block |
||
151 | 151 | $temp = strtotime($value); |
152 | 152 | if (!$temp) { |
153 | 153 | if ($strict) { |
154 | - throw new DBException('Invalid value for date column ' . $col->getName()); |
|
154 | + throw new DBException('Invalid value for date column '.$col->getName()); |
|
155 | 155 | } |
156 | 156 | return null; |
157 | 157 | } |
@@ -164,7 +164,7 @@ discard block |
||
164 | 164 | return $value->format('Y-m-d'); |
165 | 165 | } |
166 | 166 | if ($strict) { |
167 | - throw new DBException('Invalid value (unknown data type) for date column ' . $col->getName()); |
|
167 | + throw new DBException('Invalid value (unknown data type) for date column '.$col->getName()); |
|
168 | 168 | } |
169 | 169 | return $value; |
170 | 170 | case 'datetime': |
@@ -172,7 +172,7 @@ discard block |
||
172 | 172 | $temp = strtotime($value); |
173 | 173 | if (!$temp) { |
174 | 174 | if ($strict) { |
175 | - throw new DBException('Invalid value for datetime column ' . $col->getName()); |
|
175 | + throw new DBException('Invalid value for datetime column '.$col->getName()); |
|
176 | 176 | } |
177 | 177 | return null; |
178 | 178 | } |
@@ -185,7 +185,7 @@ discard block |
||
185 | 185 | return $value->format('Y-m-d H:i:s'); |
186 | 186 | } |
187 | 187 | if ($strict) { |
188 | - throw new DBException('Invalid value (unknown data type) for datetime column ' . $col->getName()); |
|
188 | + throw new DBException('Invalid value (unknown data type) for datetime column '.$col->getName()); |
|
189 | 189 | } |
190 | 190 | return $value; |
191 | 191 | case 'enum': |
@@ -193,7 +193,7 @@ discard block |
||
193 | 193 | if (is_int($value)) { |
194 | 194 | if (!isset($values[$value])) { |
195 | 195 | if ($strict) { |
196 | - throw new DBException('Invalid value (using integer) for enum ' . $col->getName()); |
|
196 | + throw new DBException('Invalid value (using integer) for enum '.$col->getName()); |
|
197 | 197 | } |
198 | 198 | return $value; |
199 | 199 | } |
@@ -201,21 +201,21 @@ discard block |
||
201 | 201 | } |
202 | 202 | if (!in_array($value, $col->getValues())) { |
203 | 203 | if ($strict) { |
204 | - throw new DBException('Invalid value for enum ' . $col->getName()); |
|
204 | + throw new DBException('Invalid value for enum '.$col->getName()); |
|
205 | 205 | } |
206 | 206 | return 0; |
207 | 207 | } |
208 | 208 | return $value; |
209 | 209 | case 'int': |
210 | - return (int)preg_replace('([^+\-0-9]+)', '', $value); |
|
210 | + return (int) preg_replace('([^+\-0-9]+)', '', $value); |
|
211 | 211 | case 'float': |
212 | - return (float)preg_replace('([^+\-0-9.]+)', '', str_replace(',', '.', $value)); |
|
212 | + return (float) preg_replace('([^+\-0-9.]+)', '', str_replace(',', '.', $value)); |
|
213 | 213 | case 'text': |
214 | 214 | // check using strlen first, in order to avoid hitting mb_ functions which might be polyfilled |
215 | 215 | // because the polyfill is quite slow |
216 | 216 | if ($col->hasLength() && strlen($value) > $col->getLength() && mb_strlen($value) > $col->getLength()) { |
217 | 217 | if ($strict) { |
218 | - throw new DBException('Invalid value for text column ' . $col->getName()); |
|
218 | + throw new DBException('Invalid value for text column '.$col->getName()); |
|
219 | 219 | } |
220 | 220 | return mb_substr($value, 0, $col->getLength()); |
221 | 221 | } |
@@ -237,35 +237,32 @@ discard block |
||
237 | 237 | // str_replace(['%', '_'], ['\\%','\\_'], $q) |
238 | 238 | return $negate ? |
239 | 239 | [ |
240 | - $name . ' NOT LIKE ?', |
|
241 | - [ $this->normalizeValue($column, $value) ] |
|
242 | - ] : |
|
243 | - [ |
|
244 | - $name . ' LIKE ?', |
|
245 | - [ $this->normalizeValue($column, $value) ] |
|
240 | + $name.' NOT LIKE ?', |
|
241 | + [$this->normalizeValue($column, $value)] |
|
242 | + ] : [ |
|
243 | + $name.' LIKE ?', |
|
244 | + [$this->normalizeValue($column, $value)] |
|
246 | 245 | ]; |
247 | 246 | } |
248 | 247 | if (is_null($value)) { |
249 | 248 | return $negate ? |
250 | - [ $name . ' IS NOT NULL', [] ]: |
|
251 | - [ $name . ' IS NULL', [] ]; |
|
249 | + [$name.' IS NOT NULL', []] : [$name.' IS NULL', []]; |
|
252 | 250 | } |
253 | 251 | if (!is_array($value)) { |
254 | 252 | return $negate ? |
255 | 253 | [ |
256 | - $name . ' <> ?', |
|
257 | - [ $this->normalizeValue($column, $value) ] |
|
258 | - ] : |
|
259 | - [ |
|
260 | - $name . ' = ?', |
|
261 | - [ $this->normalizeValue($column, $value) ] |
|
254 | + $name.' <> ?', |
|
255 | + [$this->normalizeValue($column, $value)] |
|
256 | + ] : [ |
|
257 | + $name.' = ?', |
|
258 | + [$this->normalizeValue($column, $value)] |
|
262 | 259 | ]; |
263 | 260 | } |
264 | 261 | if (isset($value['beg']) && strlen($value['beg']) && (!isset($value['end']) || !strlen($value['end']))) { |
265 | - $value = [ 'gte' => $value['beg'] ]; |
|
262 | + $value = ['gte' => $value['beg']]; |
|
266 | 263 | } |
267 | 264 | if (isset($value['end']) && strlen($value['end']) && (!isset($value['beg']) || !strlen($value['beg']))) { |
268 | - $value = [ 'lte' => $value['end'] ]; |
|
265 | + $value = ['lte' => $value['end']]; |
|
269 | 266 | } |
270 | 267 | if (isset($value['beg']) && isset($value['end'])) { |
271 | 268 | return $negate ? |
@@ -275,8 +272,7 @@ discard block |
||
275 | 272 | $this->normalizeValue($column, $value['beg']), |
276 | 273 | $this->normalizeValue($column, $value['end']) |
277 | 274 | ] |
278 | - ] : |
|
279 | - [ |
|
275 | + ] : [ |
|
280 | 276 | $name.' BETWEEN ? AND ?', |
281 | 277 | [ |
282 | 278 | $this->normalizeValue($column, $value['beg']), |
@@ -288,34 +284,34 @@ discard block |
||
288 | 284 | $sql = []; |
289 | 285 | $par = []; |
290 | 286 | if (isset($value['gt'])) { |
291 | - $sql[] = $name. ' ' . ($negate ? '<=' : '>') . ' ?'; |
|
287 | + $sql[] = $name.' '.($negate ? '<=' : '>').' ?'; |
|
292 | 288 | $par[] = $this->normalizeValue($column, $value['gt']); |
293 | 289 | } |
294 | 290 | if (isset($value['gte'])) { |
295 | - $sql[] = $name. ' ' . ($negate ? '<' : '>=') . ' ?'; |
|
291 | + $sql[] = $name.' '.($negate ? '<' : '>=').' ?'; |
|
296 | 292 | $par[] = $this->normalizeValue($column, $value['gte']); |
297 | 293 | } |
298 | 294 | if (isset($value['lt'])) { |
299 | - $sql[] = $name. ' ' . ($negate ? '>=' : '<') . ' ?'; |
|
295 | + $sql[] = $name.' '.($negate ? '>=' : '<').' ?'; |
|
300 | 296 | $par[] = $this->normalizeValue($column, $value['lt']); |
301 | 297 | } |
302 | 298 | if (isset($value['lte'])) { |
303 | - $sql[] = $name. ' ' . ($negate ? '>' : '<=') . ' ?'; |
|
299 | + $sql[] = $name.' '.($negate ? '>' : '<=').' ?'; |
|
304 | 300 | $par[] = $this->normalizeValue($column, $value['lte']); |
305 | 301 | } |
306 | 302 | return [ |
307 | - '(' . implode(' AND ', $sql) . ')', |
|
303 | + '('.implode(' AND ', $sql).')', |
|
308 | 304 | $par |
309 | 305 | ]; |
310 | 306 | } |
311 | 307 | return $negate ? |
312 | 308 | [ |
313 | - $name . ' NOT IN (??)', |
|
314 | - [ array_map(function ($v) use ($column) { return $this->normalizeValue($column, $v); }, $value) ] |
|
309 | + $name.' NOT IN (??)', |
|
310 | + [array_map(function($v) use ($column) { return $this->normalizeValue($column, $v); }, $value)] |
|
315 | 311 | ] : |
316 | 312 | [ |
317 | - $name . ' IN (??)', |
|
318 | - [ array_map(function ($v) use ($column) { return $this->normalizeValue($column, $v); }, $value) ] |
|
313 | + $name.' IN (??)', |
|
314 | + [array_map(function($v) use ($column) { return $this->normalizeValue($column, $v); }, $value)] |
|
319 | 315 | ]; |
320 | 316 | } |
321 | 317 | /** |
@@ -346,7 +342,7 @@ discard block |
||
346 | 342 | $par = array_merge($par, $temp[1]); |
347 | 343 | } |
348 | 344 | } |
349 | - return $this->where('(' . implode(' OR ', $sql) . ')', $par); |
|
345 | + return $this->where('('.implode(' OR ', $sql).')', $par); |
|
350 | 346 | } |
351 | 347 | /** |
352 | 348 | * Filter the results matching all of the criteria |
@@ -364,7 +360,7 @@ discard block |
||
364 | 360 | $par = array_merge($par, $temp[1]); |
365 | 361 | } |
366 | 362 | } |
367 | - return $this->where('(' . implode(' AND ', $sql) . ')', $par); |
|
363 | + return $this->where('('.implode(' AND ', $sql).')', $par); |
|
368 | 364 | } |
369 | 365 | /** |
370 | 366 | * Sort by a column |
@@ -374,7 +370,7 @@ discard block |
||
374 | 370 | */ |
375 | 371 | public function sort(string $column, bool $desc = false) : self |
376 | 372 | { |
377 | - return $this->order($this->getColumn($column)['name'] . ' ' . ($desc ? 'DESC' : 'ASC')); |
|
373 | + return $this->order($this->getColumn($column)['name'].' '.($desc ? 'DESC' : 'ASC')); |
|
378 | 374 | } |
379 | 375 | /** |
380 | 376 | * Group by a column (or columns) |
@@ -384,7 +380,7 @@ discard block |
||
384 | 380 | public function group($column) : self |
385 | 381 | { |
386 | 382 | if (!is_array($column)) { |
387 | - $column = [ $column ]; |
|
383 | + $column = [$column]; |
|
388 | 384 | } |
389 | 385 | foreach ($column as $k => $v) { |
390 | 386 | $column[$k] = $this->getColumn($v)['name']; |
@@ -426,7 +422,7 @@ discard block |
||
426 | 422 | $this->order = []; |
427 | 423 | $this->having = []; |
428 | 424 | $this->aliases = []; |
429 | - $this->li_of = [0,0,0]; |
|
425 | + $this->li_of = [0, 0, 0]; |
|
430 | 426 | $this->qiterator = null; |
431 | 427 | return $this; |
432 | 428 | } |
@@ -439,7 +435,7 @@ discard block |
||
439 | 435 | public function groupBy(string $sql, array $params = []) : self |
440 | 436 | { |
441 | 437 | $this->qiterator = null; |
442 | - $this->group = [ $sql, $params ]; |
|
438 | + $this->group = [$sql, $params]; |
|
443 | 439 | return $this; |
444 | 440 | } |
445 | 441 | /** |
@@ -452,7 +448,7 @@ discard block |
||
452 | 448 | */ |
453 | 449 | public function join($table, array $fields, string $name = null, bool $multiple = true) |
454 | 450 | { |
455 | - $table = $table instanceof Table ? $table : $this->db->definition((string)$table); |
|
451 | + $table = $table instanceof Table ? $table : $this->db->definition((string) $table); |
|
456 | 452 | $name = $name ?? $table->getName(); |
457 | 453 | if (isset($this->joins[$name]) || $this->definition->hasRelation($name)) { |
458 | 454 | throw new DBException('Alias / table name already in use'); |
@@ -461,7 +457,7 @@ discard block |
||
461 | 457 | foreach ($fields as $k => $v) { |
462 | 458 | $k = explode('.', $k, 2); |
463 | 459 | $k = count($k) == 2 ? $k[1] : $k[0]; |
464 | - $this->joins[$name]->keymap[$this->getColumn($name . '.' . $k)['name']] = $this->getColumn($v)['name']; |
|
460 | + $this->joins[$name]->keymap[$this->getColumn($name.'.'.$k)['name']] = $this->getColumn($v)['name']; |
|
465 | 461 | } |
466 | 462 | return $this; |
467 | 463 | } |
@@ -474,7 +470,7 @@ discard block |
||
474 | 470 | public function where(string $sql, array $params = []) : self |
475 | 471 | { |
476 | 472 | $this->qiterator = null; |
477 | - $this->where[] = [ $sql, $params ]; |
|
473 | + $this->where[] = [$sql, $params]; |
|
478 | 474 | return $this; |
479 | 475 | } |
480 | 476 | /** |
@@ -486,7 +482,7 @@ discard block |
||
486 | 482 | public function having(string $sql, array $params = []) : self |
487 | 483 | { |
488 | 484 | $this->qiterator = null; |
489 | - $this->having[] = [ $sql, $params ]; |
|
485 | + $this->having[] = [$sql, $params]; |
|
490 | 486 | return $this; |
491 | 487 | } |
492 | 488 | /** |
@@ -507,7 +503,7 @@ discard block |
||
507 | 503 | $name = null; |
508 | 504 | } |
509 | 505 | } |
510 | - $this->order = [ $sql, $params, $name ]; |
|
506 | + $this->order = [$sql, $params, $name]; |
|
511 | 507 | return $this; |
512 | 508 | } |
513 | 509 | /** |
@@ -519,7 +515,7 @@ discard block |
||
519 | 515 | public function limit(int $limit, int $offset = 0, bool $limitOnMainTable = false) : self |
520 | 516 | { |
521 | 517 | $this->qiterator = null; |
522 | - $this->li_of = [ $limit, $offset, $limitOnMainTable ? 1 : 0 ]; |
|
518 | + $this->li_of = [$limit, $offset, $limitOnMainTable ? 1 : 0]; |
|
523 | 519 | return $this; |
524 | 520 | } |
525 | 521 | /** |
@@ -529,9 +525,9 @@ discard block |
||
529 | 525 | public function count() : int |
530 | 526 | { |
531 | 527 | $aliases = []; |
532 | - $getAlias = function ($name) use (&$aliases) { |
|
528 | + $getAlias = function($name) use (&$aliases) { |
|
533 | 529 | // to bypass use: return $name; |
534 | - return $aliases[$name] = $aliases[$name] ?? 'alias' . static::SEP . count($aliases); |
|
530 | + return $aliases[$name] = $aliases[$name] ?? 'alias'.static::SEP.count($aliases); |
|
535 | 531 | }; |
536 | 532 | $table = $this->definition->getName(); |
537 | 533 | $sql = 'SELECT COUNT(DISTINCT '.$table.'.'.implode(', '.$table.'.', $this->pkey).') FROM '.$table.' '; |
@@ -546,44 +542,44 @@ discard block |
||
546 | 542 | $h = $this->having; |
547 | 543 | $o = $this->order; |
548 | 544 | $g = $this->group; |
549 | - $j = array_map(function ($v) { return clone $v; }, $this->joins); |
|
545 | + $j = array_map(function($v) { return clone $v; }, $this->joins); |
|
550 | 546 | foreach ($this->definition->getRelations() as $k => $v) { |
551 | 547 | foreach ($w as $kk => $vv) { |
552 | - if (preg_match('(\b'.preg_quote($k . '.'). ')i', $vv[0])) { |
|
553 | - $relations[$k] = [ $v, $table ]; |
|
554 | - $w[$kk][0] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $vv[0]); |
|
548 | + if (preg_match('(\b'.preg_quote($k.'.').')i', $vv[0])) { |
|
549 | + $relations[$k] = [$v, $table]; |
|
550 | + $w[$kk][0] = preg_replace('(\b'.preg_quote($k.'.').')i', $getAlias($k).'.', $vv[0]); |
|
555 | 551 | } |
556 | 552 | } |
557 | - if (isset($o[0]) && preg_match('(\b'.preg_quote($k . '.'). ')i', $o[0])) { |
|
558 | - $relations[$k] = [ $v, $table ]; |
|
553 | + if (isset($o[0]) && preg_match('(\b'.preg_quote($k.'.').')i', $o[0])) { |
|
554 | + $relations[$k] = [$v, $table]; |
|
559 | 555 | } |
560 | 556 | foreach ($h as $kk => $vv) { |
561 | - if (preg_match('(\b'.preg_quote($k . '.'). ')i', $vv[0])) { |
|
562 | - $relations[$k] = [ $relation, $table ]; |
|
563 | - $h[$kk][0] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $vv[0]); |
|
557 | + if (preg_match('(\b'.preg_quote($k.'.').')i', $vv[0])) { |
|
558 | + $relations[$k] = [$relation, $table]; |
|
559 | + $h[$kk][0] = preg_replace('(\b'.preg_quote($k.'.').')i', $getAlias($k).'.', $vv[0]); |
|
564 | 560 | } |
565 | 561 | } |
566 | - if (isset($g[0]) && preg_match('(\b'.preg_quote($k . '.'). ')i', $g[0])) { |
|
567 | - $relations[$k] = [ $relation, $table ]; |
|
568 | - $g[0] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $g[0]); |
|
562 | + if (isset($g[0]) && preg_match('(\b'.preg_quote($k.'.').')i', $g[0])) { |
|
563 | + $relations[$k] = [$relation, $table]; |
|
564 | + $g[0] = preg_replace('(\b'.preg_quote($k.'.').')i', $getAlias($k).'.', $g[0]); |
|
569 | 565 | } |
570 | 566 | foreach ($j as $kk => $v) { |
571 | 567 | foreach ($v->keymap as $kkk => $vv) { |
572 | - if (preg_match('(\b'.preg_quote($k . '.'). ')i', $vv)) { |
|
573 | - $relations[$k] = [ $relation, $table ]; |
|
574 | - $j[$k]->keymap[$kkk] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $vv); |
|
568 | + if (preg_match('(\b'.preg_quote($k.'.').')i', $vv)) { |
|
569 | + $relations[$k] = [$relation, $table]; |
|
570 | + $j[$k]->keymap[$kkk] = preg_replace('(\b'.preg_quote($k.'.').')i', $getAlias($k).'.', $vv); |
|
575 | 571 | } |
576 | 572 | } |
577 | 573 | } |
578 | 574 | } |
579 | 575 | |
580 | 576 | foreach ($j as $k => $v) { |
581 | - $sql .= ($v->many ? 'LEFT ' : '' ) . 'JOIN '.$v->table->getName().' '.$k.' ON '; |
|
577 | + $sql .= ($v->many ? 'LEFT ' : '').'JOIN '.$v->table->getName().' '.$k.' ON '; |
|
582 | 578 | $tmp = []; |
583 | 579 | foreach ($v->keymap as $kk => $vv) { |
584 | 580 | $tmp[] = $kk.' = '.$vv; |
585 | 581 | } |
586 | - $sql .= implode(' AND ', $tmp) . ' '; |
|
582 | + $sql .= implode(' AND ', $tmp).' '; |
|
587 | 583 | } |
588 | 584 | foreach ($relations as $k => $v) { |
589 | 585 | $table = $v[1] !== $this->definition->getName() ? $getAlias($v[1]) : $v[1]; |
@@ -595,13 +591,13 @@ discard block |
||
595 | 591 | foreach ($v->keymap as $kk => $vv) { |
596 | 592 | $tmp[] = $table.'.'.$kk.' = '.$alias.'.'.$vv.' '; |
597 | 593 | } |
598 | - $sql .= implode(' AND ', $tmp) . ' '; |
|
594 | + $sql .= implode(' AND ', $tmp).' '; |
|
599 | 595 | $sql .= 'LEFT JOIN '.$v->table->getName().' '.$getAlias($k).' ON '; |
600 | 596 | $tmp = []; |
601 | 597 | foreach ($v->pivot_keymap as $kk => $vv) { |
602 | 598 | $tmp[] = $getAlias($k).'.'.$vv.' = '.$alias.'.'.$kk.' '; |
603 | 599 | } |
604 | - $sql .= implode(' AND ', $tmp) . ' '; |
|
600 | + $sql .= implode(' AND ', $tmp).' '; |
|
605 | 601 | } else { |
606 | 602 | $alias = $getAlias($k); |
607 | 603 | $sql .= 'LEFT JOIN '.$v->table->getName().' '.$alias.' ON '; |
@@ -610,30 +606,30 @@ discard block |
||
610 | 606 | $tmp[] = $table.'.'.$kk.' = '.$alias.'.'.$vv.' '; |
611 | 607 | } |
612 | 608 | if ($v->sql) { |
613 | - $tmp[] = $v->sql . ' '; |
|
609 | + $tmp[] = $v->sql.' '; |
|
614 | 610 | $par = array_merge($par, $v->par ?? []); |
615 | 611 | } |
616 | - $sql .= implode(' AND ', $tmp) . ' '; |
|
612 | + $sql .= implode(' AND ', $tmp).' '; |
|
617 | 613 | } |
618 | 614 | } |
619 | 615 | if (count($w)) { |
620 | 616 | $sql .= 'WHERE '; |
621 | 617 | $tmp = []; |
622 | 618 | foreach ($w as $v) { |
623 | - $tmp[] = '(' . $v[0] . ')'; |
|
619 | + $tmp[] = '('.$v[0].')'; |
|
624 | 620 | $par = array_merge($par, $v[1]); |
625 | 621 | } |
626 | 622 | $sql .= implode(' AND ', $tmp).' '; |
627 | 623 | } |
628 | 624 | if (count($g)) { |
629 | - $sql .= 'GROUP BY ' . $g[0] . ' '; |
|
625 | + $sql .= 'GROUP BY '.$g[0].' '; |
|
630 | 626 | $par = array_merge($par, $g[1]); |
631 | 627 | } |
632 | 628 | if (count($h)) { |
633 | 629 | $sql .= 'HAVING '; |
634 | 630 | $tmp = []; |
635 | 631 | foreach ($h as $v) { |
636 | - $tmp[] = '(' . $v[0] . ')'; |
|
632 | + $tmp[] = '('.$v[0].')'; |
|
637 | 633 | $par = array_merge($par, $v[1]); |
638 | 634 | } |
639 | 635 | $sql .= implode(' AND ', $tmp).' '; |
@@ -667,7 +663,7 @@ discard block |
||
667 | 663 | $this->with(implode('.', $temp)); |
668 | 664 | $table = array_reduce( |
669 | 665 | $temp, |
670 | - function ($carry, $item) use (&$table) { |
|
666 | + function($carry, $item) use (&$table) { |
|
671 | 667 | return $table->getRelation($item)->table; |
672 | 668 | } |
673 | 669 | ); |
@@ -676,7 +672,7 @@ discard block |
||
676 | 672 | } |
677 | 673 | unset($fields[$k]); |
678 | 674 | foreach ($cols as $col) { |
679 | - $fields[] = $table . '.' . $col; |
|
675 | + $fields[] = $table.'.'.$col; |
|
680 | 676 | } |
681 | 677 | } |
682 | 678 | } |
@@ -708,9 +704,9 @@ discard block |
||
708 | 704 | return $this->qiterator; |
709 | 705 | } |
710 | 706 | $aliases = []; |
711 | - $getAlias = function ($name) use (&$aliases) { |
|
707 | + $getAlias = function($name) use (&$aliases) { |
|
712 | 708 | // to bypass use: return $name; |
713 | - return $aliases[$name] = $aliases[$name] ?? 'alias' . static::SEP . count($aliases); |
|
709 | + return $aliases[$name] = $aliases[$name] ?? 'alias'.static::SEP.count($aliases); |
|
714 | 710 | }; |
715 | 711 | $table = $this->definition->getName(); |
716 | 712 | if ($fields !== null) { |
@@ -726,7 +722,7 @@ discard block |
||
726 | 722 | $h = $this->having; |
727 | 723 | $o = $this->order; |
728 | 724 | $g = $this->group; |
729 | - $j = array_map(function ($v) { return clone $v; }, $this->joins); |
|
725 | + $j = array_map(function($v) { return clone $v; }, $this->joins); |
|
730 | 726 | |
731 | 727 | $porder = []; |
732 | 728 | foreach ($this->definition->getPrimaryKey() as $field) { |
@@ -735,47 +731,47 @@ discard block |
||
735 | 731 | |
736 | 732 | foreach ($this->definition->getRelations() as $k => $relation) { |
737 | 733 | foreach ($f as $kk => $field) { |
738 | - if (strpos($field, $k . '.') === 0) { |
|
739 | - $relations[$k] = [ $relation, $table ]; |
|
740 | - $f[$kk] = str_replace($k . '.', $getAlias($k) . '.', $field); |
|
734 | + if (strpos($field, $k.'.') === 0) { |
|
735 | + $relations[$k] = [$relation, $table]; |
|
736 | + $f[$kk] = str_replace($k.'.', $getAlias($k).'.', $field); |
|
741 | 737 | } |
742 | 738 | } |
743 | 739 | foreach ($w as $kk => $v) { |
744 | - if (preg_match('(\b'.preg_quote($k . '.'). ')i', $v[0])) { |
|
745 | - $relations[$k] = [ $relation, $table ]; |
|
746 | - $w[$kk][0] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $v[0]); |
|
740 | + if (preg_match('(\b'.preg_quote($k.'.').')i', $v[0])) { |
|
741 | + $relations[$k] = [$relation, $table]; |
|
742 | + $w[$kk][0] = preg_replace('(\b'.preg_quote($k.'.').')i', $getAlias($k).'.', $v[0]); |
|
747 | 743 | } |
748 | 744 | } |
749 | 745 | foreach ($h as $kk => $v) { |
750 | - if (preg_match('(\b'.preg_quote($k . '.'). ')i', $v[0])) { |
|
751 | - $relations[$k] = [ $relation, $table ]; |
|
752 | - $h[$kk][0] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $v[0]); |
|
746 | + if (preg_match('(\b'.preg_quote($k.'.').')i', $v[0])) { |
|
747 | + $relations[$k] = [$relation, $table]; |
|
748 | + $h[$kk][0] = preg_replace('(\b'.preg_quote($k.'.').')i', $getAlias($k).'.', $v[0]); |
|
753 | 749 | } |
754 | 750 | } |
755 | - if (isset($o[0]) && preg_match('(\b'.preg_quote($k . '.'). ')i', $o[0])) { |
|
756 | - $relations[$k] = [ $relation, $table ]; |
|
757 | - $o[0] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $o[0]); |
|
751 | + if (isset($o[0]) && preg_match('(\b'.preg_quote($k.'.').')i', $o[0])) { |
|
752 | + $relations[$k] = [$relation, $table]; |
|
753 | + $o[0] = preg_replace('(\b'.preg_quote($k.'.').')i', $getAlias($k).'.', $o[0]); |
|
758 | 754 | } |
759 | - if (isset($g[0]) && preg_match('(\b'.preg_quote($k . '.'). ')i', $g[0])) { |
|
760 | - $relations[$k] = [ $relation, $table ]; |
|
761 | - $g[0] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $g[0]); |
|
755 | + if (isset($g[0]) && preg_match('(\b'.preg_quote($k.'.').')i', $g[0])) { |
|
756 | + $relations[$k] = [$relation, $table]; |
|
757 | + $g[0] = preg_replace('(\b'.preg_quote($k.'.').')i', $getAlias($k).'.', $g[0]); |
|
762 | 758 | } |
763 | 759 | foreach ($j as $kk => $v) { |
764 | 760 | foreach ($v->keymap as $kkk => $vv) { |
765 | - if (preg_match('(\b'.preg_quote($k . '.'). ')i', $vv)) { |
|
766 | - $relations[$k] = [ $relation, $table ]; |
|
767 | - $j[$k]->keymap[$kkk] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $vv); |
|
761 | + if (preg_match('(\b'.preg_quote($k.'.').')i', $vv)) { |
|
762 | + $relations[$k] = [$relation, $table]; |
|
763 | + $j[$k]->keymap[$kkk] = preg_replace('(\b'.preg_quote($k.'.').')i', $getAlias($k).'.', $vv); |
|
768 | 764 | } |
769 | 765 | } |
770 | 766 | } |
771 | 767 | } |
772 | 768 | $select = []; |
773 | 769 | foreach ($f as $k => $field) { |
774 | - $select[] = $field . (!is_numeric($k) ? ' ' . $k : ''); |
|
770 | + $select[] = $field.(!is_numeric($k) ? ' '.$k : ''); |
|
775 | 771 | } |
776 | 772 | foreach ($this->withr as $name => $relation) { |
777 | 773 | foreach ($relation[0]->table->getColumns() as $column) { |
778 | - $select[] = $getAlias($name) . '.' . $column . ' ' . $getAlias($name . static::SEP . $column); |
|
774 | + $select[] = $getAlias($name).'.'.$column.' '.$getAlias($name.static::SEP.$column); |
|
779 | 775 | } |
780 | 776 | } |
781 | 777 | $sql = 'SELECT '.implode(', ', $select).' FROM '.$table.' '; |
@@ -785,12 +781,12 @@ discard block |
||
785 | 781 | if ($v->many) { |
786 | 782 | $many = true; |
787 | 783 | } |
788 | - $sql .= ($v->many ? 'LEFT ' : '' ) . 'JOIN '.$v->table->getName().' '.$k.' ON '; |
|
784 | + $sql .= ($v->many ? 'LEFT ' : '').'JOIN '.$v->table->getName().' '.$k.' ON '; |
|
789 | 785 | $tmp = []; |
790 | 786 | foreach ($v->keymap as $kk => $vv) { |
791 | 787 | $tmp[] = $kk.' = '.$vv; |
792 | 788 | } |
793 | - $sql .= implode(' AND ', $tmp) . ' '; |
|
789 | + $sql .= implode(' AND ', $tmp).' '; |
|
794 | 790 | } |
795 | 791 | foreach ($relations as $relation => $v) { |
796 | 792 | $table = $v[1] !== $this->definition->getName() ? $getAlias($v[1]) : $v[1]; |
@@ -805,13 +801,13 @@ discard block |
||
805 | 801 | foreach ($v->keymap as $kk => $vv) { |
806 | 802 | $tmp[] = $table.'.'.$kk.' = '.$alias.'.'.$vv.' '; |
807 | 803 | } |
808 | - $sql .= implode(' AND ', $tmp) . ' '; |
|
804 | + $sql .= implode(' AND ', $tmp).' '; |
|
809 | 805 | $sql .= 'LEFT JOIN '.$v->table->getName().' '.$getAlias($relation).' ON '; |
810 | 806 | $tmp = []; |
811 | 807 | foreach ($v->pivot_keymap as $kk => $vv) { |
812 | 808 | $tmp[] = $getAlias($relation).'.'.$vv.' = '.$alias.'.'.$kk.' '; |
813 | 809 | } |
814 | - $sql .= implode(' AND ', $tmp) . ' '; |
|
810 | + $sql .= implode(' AND ', $tmp).' '; |
|
815 | 811 | } else { |
816 | 812 | $alias = $getAlias($relation); |
817 | 813 | |
@@ -821,10 +817,10 @@ discard block |
||
821 | 817 | $tmp[] = $table.'.'.$kk.' = '.$alias.'.'.$vv.' '; |
822 | 818 | } |
823 | 819 | if ($v->sql) { |
824 | - $tmp[] = $v->sql . ' '; |
|
820 | + $tmp[] = $v->sql.' '; |
|
825 | 821 | $par = array_merge($par, $v->par ?? []); |
826 | 822 | } |
827 | - $sql .= implode(' AND ', $tmp) . ' '; |
|
823 | + $sql .= implode(' AND ', $tmp).' '; |
|
828 | 824 | } |
829 | 825 | } |
830 | 826 | |
@@ -834,9 +830,9 @@ discard block |
||
834 | 830 | if (count($porder) > 1) { |
835 | 831 | $pkw = []; |
836 | 832 | foreach ($porder as $name) { |
837 | - $pkw[] = $name . ' = ?'; |
|
833 | + $pkw[] = $name.' = ?'; |
|
838 | 834 | } |
839 | - $pkw = '(' . implode(' AND ', $pkw) . ')'; |
|
835 | + $pkw = '('.implode(' AND ', $pkw).')'; |
|
840 | 836 | $pkp = []; |
841 | 837 | foreach ($ids as $id) { |
842 | 838 | foreach ($id as $p) { |
@@ -848,64 +844,64 @@ discard block |
||
848 | 844 | $pkp |
849 | 845 | ]; |
850 | 846 | } else { |
851 | - $w[] = [ $porder[0] . ' IN ('.implode(',', array_fill(0, count($ids), '?')).')', $ids ]; |
|
847 | + $w[] = [$porder[0].' IN ('.implode(',', array_fill(0, count($ids), '?')).')', $ids]; |
|
852 | 848 | } |
853 | 849 | } else { |
854 | - $w[] = [ '1=0', [] ]; |
|
850 | + $w[] = ['1=0', []]; |
|
855 | 851 | } |
856 | 852 | } |
857 | 853 | if (count($w)) { |
858 | 854 | $sql .= 'WHERE '; |
859 | 855 | $tmp = []; |
860 | 856 | foreach ($w as $v) { |
861 | - $tmp[] = '(' . $v[0] . ')'; |
|
857 | + $tmp[] = '('.$v[0].')'; |
|
862 | 858 | $par = array_merge($par, $v[1]); |
863 | 859 | } |
864 | 860 | $sql .= implode(' AND ', $tmp).' '; |
865 | 861 | } |
866 | 862 | if (count($g)) { |
867 | - $sql .= 'GROUP BY ' . $g[0] . ' '; |
|
863 | + $sql .= 'GROUP BY '.$g[0].' '; |
|
868 | 864 | $par = array_merge($par, $g[1]); |
869 | 865 | } |
870 | 866 | if (count($h)) { |
871 | 867 | $sql .= 'HAVING '; |
872 | 868 | $tmp = []; |
873 | 869 | foreach ($h as $v) { |
874 | - $tmp[] = '(' . $v[0] . ')'; |
|
870 | + $tmp[] = '('.$v[0].')'; |
|
875 | 871 | $par = array_merge($par, $v[1]); |
876 | 872 | } |
877 | 873 | $sql .= implode(' AND ', $tmp).' '; |
878 | 874 | } |
879 | 875 | if (count($o)) { |
880 | - $sql .= 'ORDER BY ' . $o[0] . ' '; |
|
876 | + $sql .= 'ORDER BY '.$o[0].' '; |
|
881 | 877 | $par = array_merge($par, $o[1]); |
882 | 878 | } |
883 | 879 | if (count($porder)) { |
884 | 880 | $pdir = (count($o) && strpos($o[0], 'DESC') !== false) ? 'DESC' : 'ASC'; |
885 | - $porder = array_map(function ($v) use ($pdir) { return $v . ' ' . $pdir; }, $porder); |
|
886 | - $sql .= (count($o) ? ', ' : 'ORDER BY ') . implode(', ', $porder) . ' '; |
|
881 | + $porder = array_map(function($v) use ($pdir) { return $v.' '.$pdir; }, $porder); |
|
882 | + $sql .= (count($o) ? ', ' : 'ORDER BY ').implode(', ', $porder).' '; |
|
887 | 883 | } |
888 | 884 | if ((!$many || $this->li_of[2] === 0 || !count($porder)) && $this->li_of[0]) { |
889 | 885 | if ($this->db->driverName() === 'oracle') { |
890 | - if ((int)$this->db->driverOption('version', 0) >= 12) { |
|
891 | - $sql .= 'OFFSET ' . $this->li_of[1] . ' ROWS FETCH NEXT ' . $this->li_of[0] . ' ROWS ONLY'; |
|
886 | + if ((int) $this->db->driverOption('version', 0) >= 12) { |
|
887 | + $sql .= 'OFFSET '.$this->li_of[1].' ROWS FETCH NEXT '.$this->li_of[0].' ROWS ONLY'; |
|
892 | 888 | } else { |
893 | - $f = array_map(function ($v) { |
|
889 | + $f = array_map(function($v) { |
|
894 | 890 | $v = explode(' ', trim($v), 2); |
895 | 891 | if (count($v) === 2) { return $v[1]; } |
896 | 892 | $v = explode('.', $v[0], 2); |
897 | 893 | return count($v) === 2 ? $v[1] : $v[0]; |
898 | 894 | }, $select); |
899 | - $sql = "SELECT " . implode(', ', $f) . " |
|
895 | + $sql = "SELECT ".implode(', ', $f)." |
|
900 | 896 | FROM ( |
901 | 897 | SELECT tbl__.*, rownum rnum__ FROM ( |
902 | - " . $sql . " |
|
898 | + " . $sql." |
|
903 | 899 | ) tbl__ |
904 | - WHERE rownum <= " . ($this->li_of[0] + $this->li_of[1]) . " |
|
900 | + WHERE rownum <= " . ($this->li_of[0] + $this->li_of[1])." |
|
905 | 901 | ) WHERE rnum__ > " . $this->li_of[1]; |
906 | 902 | } |
907 | 903 | } else { |
908 | - $sql .= 'LIMIT ' . $this->li_of[0] . ' OFFSET ' . $this->li_of[1]; |
|
904 | + $sql .= 'LIMIT '.$this->li_of[0].' OFFSET '.$this->li_of[1]; |
|
909 | 905 | } |
910 | 906 | } |
911 | 907 | return $this->qiterator = new TableQueryIterator( |
@@ -955,7 +951,7 @@ discard block |
||
955 | 951 | $ret[$k] = str_repeat(' ', 255); |
956 | 952 | $par[] = &$ret[$k]; |
957 | 953 | } |
958 | - $sql .= ' RETURNING ' . implode(',', $primary) . ' INTO ' . implode(',', array_fill(0, count($primary), '?')); |
|
954 | + $sql .= ' RETURNING '.implode(',', $primary).' INTO '.implode(',', array_fill(0, count($primary), '?')); |
|
959 | 955 | $this->db->query($sql, $par); |
960 | 956 | return $ret; |
961 | 957 | } else { |
@@ -987,7 +983,7 @@ discard block |
||
987 | 983 | } |
988 | 984 | $sql = 'UPDATE '.$table.' SET '; |
989 | 985 | $par = []; |
990 | - $sql .= implode(', ', array_map(function ($v) { return $v . ' = ?'; }, array_keys($update))) . ' '; |
|
986 | + $sql .= implode(', ', array_map(function($v) { return $v.' = ?'; }, array_keys($update))).' '; |
|
991 | 987 | $par = array_merge($par, array_values($update)); |
992 | 988 | if (count($this->where)) { |
993 | 989 | $sql .= 'WHERE '; |
@@ -996,7 +992,7 @@ discard block |
||
996 | 992 | $tmp[] = $v[0]; |
997 | 993 | $par = array_merge($par, $v[1]); |
998 | 994 | } |
999 | - $sql .= implode(' AND ', $tmp) . ' '; |
|
995 | + $sql .= implode(' AND ', $tmp).' '; |
|
1000 | 996 | } |
1001 | 997 | if (count($this->order)) { |
1002 | 998 | $sql .= $this->order[0]; |
@@ -1020,7 +1016,7 @@ discard block |
||
1020 | 1016 | $tmp[] = $v[0]; |
1021 | 1017 | $par = array_merge($par, $v[1]); |
1022 | 1018 | } |
1023 | - $sql .= implode(' AND ', $tmp) . ' '; |
|
1019 | + $sql .= implode(' AND ', $tmp).' '; |
|
1024 | 1020 | } |
1025 | 1021 | if (count($this->order)) { |
1026 | 1022 | $sql .= $this->order[0]; |
@@ -1040,13 +1036,13 @@ discard block |
||
1040 | 1036 | $table = $this->definition; |
1041 | 1037 | array_reduce( |
1042 | 1038 | $parts, |
1043 | - function ($carry, $item) use (&$table) { |
|
1039 | + function($carry, $item) use (&$table) { |
|
1044 | 1040 | $relation = $table->getRelation($item); |
1045 | 1041 | if (!$relation) { |
1046 | 1042 | throw new DBException('Invalid relation name'); |
1047 | 1043 | } |
1048 | - $name = $carry ? $carry . static::SEP . $item : $item; |
|
1049 | - $this->withr[$name] = [ $relation, $carry ?? $table->getName() ]; |
|
1044 | + $name = $carry ? $carry.static::SEP.$item : $item; |
|
1045 | + $this->withr[$name] = [$relation, $carry ?? $table->getName()]; |
|
1050 | 1046 | $table = $relation->table; |
1051 | 1047 | return $name; |
1052 | 1048 | } |
@@ -1091,9 +1087,9 @@ discard block |
||
1091 | 1087 | } |
1092 | 1088 | |
1093 | 1089 | $aliases = []; |
1094 | - $getAlias = function ($name) use (&$aliases) { |
|
1090 | + $getAlias = function($name) use (&$aliases) { |
|
1095 | 1091 | // to bypass use: return $name; |
1096 | - return $aliases[$name] = $aliases[$name] ?? 'alias' . static::SEP . count($aliases); |
|
1092 | + return $aliases[$name] = $aliases[$name] ?? 'alias'.static::SEP.count($aliases); |
|
1097 | 1093 | }; |
1098 | 1094 | |
1099 | 1095 | $table = $this->definition->getName(); |
@@ -1106,63 +1102,63 @@ discard block |
||
1106 | 1102 | $h = $this->having; |
1107 | 1103 | $o = $this->order; |
1108 | 1104 | $g = $this->group; |
1109 | - $j = array_map(function ($v) { return clone $v; }, $this->joins); |
|
1105 | + $j = array_map(function($v) { return clone $v; }, $this->joins); |
|
1110 | 1106 | foreach ($this->definition->getRelations() as $k => $v) { |
1111 | 1107 | foreach ($w as $kk => $vv) { |
1112 | - if (preg_match('(\b'.preg_quote($k . '.'). ')i', $vv[0])) { |
|
1113 | - $relations[$k] = [ $v, $table ]; |
|
1114 | - $w[$kk][0] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $vv[0]); |
|
1108 | + if (preg_match('(\b'.preg_quote($k.'.').')i', $vv[0])) { |
|
1109 | + $relations[$k] = [$v, $table]; |
|
1110 | + $w[$kk][0] = preg_replace('(\b'.preg_quote($k.'.').')i', $getAlias($k).'.', $vv[0]); |
|
1115 | 1111 | } |
1116 | 1112 | } |
1117 | - if (isset($o[0]) && preg_match('(\b'.preg_quote($k . '.'). ')i', $o[0])) { |
|
1118 | - $relations[$k] = [ $v, $table ]; |
|
1119 | - $o[0] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $o[0]); |
|
1120 | - $o[2] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $o[2]); |
|
1113 | + if (isset($o[0]) && preg_match('(\b'.preg_quote($k.'.').')i', $o[0])) { |
|
1114 | + $relations[$k] = [$v, $table]; |
|
1115 | + $o[0] = preg_replace('(\b'.preg_quote($k.'.').')i', $getAlias($k).'.', $o[0]); |
|
1116 | + $o[2] = preg_replace('(\b'.preg_quote($k.'.').')i', $getAlias($k).'.', $o[2]); |
|
1121 | 1117 | } |
1122 | 1118 | foreach ($h as $kk => $vv) { |
1123 | - if (preg_match('(\b'.preg_quote($k . '.'). ')i', $vv[0])) { |
|
1124 | - $relations[$k] = [ $relation, $table ]; |
|
1125 | - $h[$kk][0] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $vv[0]); |
|
1119 | + if (preg_match('(\b'.preg_quote($k.'.').')i', $vv[0])) { |
|
1120 | + $relations[$k] = [$relation, $table]; |
|
1121 | + $h[$kk][0] = preg_replace('(\b'.preg_quote($k.'.').')i', $getAlias($k).'.', $vv[0]); |
|
1126 | 1122 | } |
1127 | 1123 | } |
1128 | - if (isset($g[0]) && preg_match('(\b'.preg_quote($k . '.'). ')i', $g[0])) { |
|
1129 | - $relations[$k] = [ $relation, $table ]; |
|
1130 | - $g[0] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $g[0]); |
|
1124 | + if (isset($g[0]) && preg_match('(\b'.preg_quote($k.'.').')i', $g[0])) { |
|
1125 | + $relations[$k] = [$relation, $table]; |
|
1126 | + $g[0] = preg_replace('(\b'.preg_quote($k.'.').')i', $getAlias($k).'.', $g[0]); |
|
1131 | 1127 | } |
1132 | 1128 | foreach ($j as $kk => $v) { |
1133 | 1129 | foreach ($v->keymap as $kkk => $vv) { |
1134 | - if (preg_match('(\b'.preg_quote($k . '.'). ')i', $vv)) { |
|
1135 | - $relations[$k] = [ $relation, $table ]; |
|
1136 | - $j[$k]->keymap[$kkk] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $vv); |
|
1130 | + if (preg_match('(\b'.preg_quote($k.'.').')i', $vv)) { |
|
1131 | + $relations[$k] = [$relation, $table]; |
|
1132 | + $j[$k]->keymap[$kkk] = preg_replace('(\b'.preg_quote($k.'.').')i', $getAlias($k).'.', $vv); |
|
1137 | 1133 | } |
1138 | 1134 | } |
1139 | 1135 | } |
1140 | 1136 | } |
1141 | 1137 | |
1142 | - $key = array_map(function ($v) use ($table) { return $table . '.' . $v; }, $this->pkey); |
|
1138 | + $key = array_map(function($v) use ($table) { return $table.'.'.$v; }, $this->pkey); |
|
1143 | 1139 | $own = false; |
1144 | 1140 | $dir = 'ASC'; |
1145 | 1141 | if (count($o)) { |
1146 | 1142 | $dir = strpos($o[0], ' DESC') ? 'DESC' : 'ASC'; |
1147 | - $own = strpos($o[2], $table . '.') === 0; |
|
1143 | + $own = strpos($o[2], $table.'.') === 0; |
|
1148 | 1144 | } |
1149 | 1145 | |
1150 | 1146 | $dst = $key; |
1151 | 1147 | if ($own) { |
1152 | 1148 | // if using own table - do not use max/min in order - that will prevent index usage |
1153 | - $dst[] = $o[2] . ' orderbyfix___'; |
|
1149 | + $dst[] = $o[2].' orderbyfix___'; |
|
1154 | 1150 | } |
1155 | 1151 | $dst = array_unique($dst); |
1156 | 1152 | |
1157 | 1153 | $par = []; |
1158 | - $sql = 'SELECT DISTINCT '.implode(', ', $dst).' FROM '.$table.' '; |
|
1154 | + $sql = 'SELECT DISTINCT '.implode(', ', $dst).' FROM '.$table.' '; |
|
1159 | 1155 | foreach ($j as $k => $v) { |
1160 | - $sql .= ($v->many ? 'LEFT ' : '' ) . 'JOIN '.$v->table->getName().' '.$k.' ON '; |
|
1156 | + $sql .= ($v->many ? 'LEFT ' : '').'JOIN '.$v->table->getName().' '.$k.' ON '; |
|
1161 | 1157 | $tmp = []; |
1162 | 1158 | foreach ($v->keymap as $kk => $vv) { |
1163 | 1159 | $tmp[] = $kk.' = '.$vv; |
1164 | 1160 | } |
1165 | - $sql .= implode(' AND ', $tmp) . ' '; |
|
1161 | + $sql .= implode(' AND ', $tmp).' '; |
|
1166 | 1162 | } |
1167 | 1163 | foreach ($relations as $k => $v) { |
1168 | 1164 | $table = $v[1] !== $this->definition->getName() ? $getAlias($v[1]) : $v[1]; |
@@ -1174,13 +1170,13 @@ discard block |
||
1174 | 1170 | foreach ($v->keymap as $kk => $vv) { |
1175 | 1171 | $tmp[] = $table.'.'.$kk.' = '.$alias.'.'.$vv.' '; |
1176 | 1172 | } |
1177 | - $sql .= implode(' AND ', $tmp) . ' '; |
|
1173 | + $sql .= implode(' AND ', $tmp).' '; |
|
1178 | 1174 | $sql .= 'LEFT JOIN '.$v->table->getName().' '.$getAlias($k).' ON '; |
1179 | 1175 | $tmp = []; |
1180 | 1176 | foreach ($v->pivot_keymap as $kk => $vv) { |
1181 | 1177 | $tmp[] = $getAlias($k).'.'.$vv.' = '.$alias.'.'.$kk.' '; |
1182 | 1178 | } |
1183 | - $sql .= implode(' AND ', $tmp) . ' '; |
|
1179 | + $sql .= implode(' AND ', $tmp).' '; |
|
1184 | 1180 | } else { |
1185 | 1181 | $alias = $getAlias($k); |
1186 | 1182 | $sql .= 'LEFT JOIN '.$v->table->getName().' '.$alias.' ON '; |
@@ -1189,29 +1185,29 @@ discard block |
||
1189 | 1185 | $tmp[] = $table.'.'.$kk.' = '.$alias.'.'.$vv.' '; |
1190 | 1186 | } |
1191 | 1187 | if ($v->sql) { |
1192 | - $tmp[] = $v->sql . ' '; |
|
1188 | + $tmp[] = $v->sql.' '; |
|
1193 | 1189 | $par = array_merge($par, $v->par ?? []); |
1194 | 1190 | } |
1195 | - $sql .= implode(' AND ', $tmp) . ' '; |
|
1191 | + $sql .= implode(' AND ', $tmp).' '; |
|
1196 | 1192 | } |
1197 | 1193 | } |
1198 | 1194 | if (count($w)) { |
1199 | 1195 | $sql .= 'WHERE '; |
1200 | 1196 | $tmp = []; |
1201 | 1197 | foreach ($w as $v) { |
1202 | - $tmp[] = '(' . $v[0] . ')'; |
|
1198 | + $tmp[] = '('.$v[0].')'; |
|
1203 | 1199 | $par = array_merge($par, $v[1]); |
1204 | 1200 | } |
1205 | 1201 | $sql .= implode(' AND ', $tmp).' '; |
1206 | 1202 | } |
1207 | 1203 | if (!$own) { |
1208 | - $sql .= 'GROUP BY ' . implode(', ', $key) . ' '; |
|
1204 | + $sql .= 'GROUP BY '.implode(', ', $key).' '; |
|
1209 | 1205 | } |
1210 | 1206 | if (count($h)) { |
1211 | 1207 | $sql .= 'HAVING '; |
1212 | 1208 | $tmp = []; |
1213 | 1209 | foreach ($h as $v) { |
1214 | - $tmp[] = '(' . $v[0] . ')'; |
|
1210 | + $tmp[] = '('.$v[0].')'; |
|
1215 | 1211 | $par = array_merge($par, $v[1]); |
1216 | 1212 | } |
1217 | 1213 | $sql .= implode(' AND ', $tmp).' '; |
@@ -1219,44 +1215,44 @@ discard block |
||
1219 | 1215 | if (count($o)) { |
1220 | 1216 | $sql .= 'ORDER BY '; |
1221 | 1217 | if ($own) { |
1222 | - $sql .= $o[2] . ' ' . $dir; |
|
1218 | + $sql .= $o[2].' '.$dir; |
|
1223 | 1219 | } else { |
1224 | - $sql .= 'MAX('.$o[2].') ' . $dir; |
|
1220 | + $sql .= 'MAX('.$o[2].') '.$dir; |
|
1225 | 1221 | } |
1226 | 1222 | } |
1227 | 1223 | $porder = []; |
1228 | 1224 | $pdir = (count($o) && strpos($o[0], 'DESC') !== false) ? 'DESC' : 'ASC'; |
1229 | 1225 | foreach ($this->definition->getPrimaryKey() as $field) { |
1230 | - $porder[] = $this->getColumn($field)['name'] . ' ' . $pdir; |
|
1226 | + $porder[] = $this->getColumn($field)['name'].' '.$pdir; |
|
1231 | 1227 | } |
1232 | 1228 | if (count($porder)) { |
1233 | - $sql .= (count($o) ? ', ' : 'ORDER BY ') . implode(', ', $porder) . ' '; |
|
1229 | + $sql .= (count($o) ? ', ' : 'ORDER BY ').implode(', ', $porder).' '; |
|
1234 | 1230 | } |
1235 | 1231 | |
1236 | 1232 | if ($this->li_of[0]) { |
1237 | 1233 | if ($this->db->driverName() === 'oracle') { |
1238 | - if ((int)$this->db->driverOption('version', 0) >= 12) { |
|
1239 | - $sql .= 'OFFSET ' . $this->li_of[1] . ' ROWS FETCH NEXT ' . $this->li_of[0] . ' ROWS ONLY'; |
|
1234 | + if ((int) $this->db->driverOption('version', 0) >= 12) { |
|
1235 | + $sql .= 'OFFSET '.$this->li_of[1].' ROWS FETCH NEXT '.$this->li_of[0].' ROWS ONLY'; |
|
1240 | 1236 | } else { |
1241 | - $f = array_map(function ($v) { |
|
1237 | + $f = array_map(function($v) { |
|
1242 | 1238 | $v = explode(' ', trim($v), 2); |
1243 | 1239 | if (count($v) === 2) { return $v[1]; } |
1244 | 1240 | $v = explode('.', $v[0], 2); |
1245 | 1241 | return count($v) === 2 ? $v[1] : $v[0]; |
1246 | 1242 | }, $select); |
1247 | - $sql = "SELECT " . implode(', ', $dst) . " |
|
1243 | + $sql = "SELECT ".implode(', ', $dst)." |
|
1248 | 1244 | FROM ( |
1249 | 1245 | SELECT tbl__.*, rownum rnum__ FROM ( |
1250 | - " . $sql . " |
|
1246 | + " . $sql." |
|
1251 | 1247 | ) tbl__ |
1252 | - WHERE rownum <= " . ($this->li_of[0] + $this->li_of[1]) . " |
|
1248 | + WHERE rownum <= " . ($this->li_of[0] + $this->li_of[1])." |
|
1253 | 1249 | ) WHERE rnum__ > " . $this->li_of[1]; |
1254 | 1250 | } |
1255 | 1251 | } else { |
1256 | - $sql .= 'LIMIT ' . $this->li_of[0] . ' OFFSET ' . $this->li_of[1]; |
|
1252 | + $sql .= 'LIMIT '.$this->li_of[0].' OFFSET '.$this->li_of[1]; |
|
1257 | 1253 | } |
1258 | 1254 | } |
1259 | - return array_map(function ($v) use ($own) { |
|
1255 | + return array_map(function($v) use ($own) { |
|
1260 | 1256 | if (isset($v['orderbyfix___'])) { |
1261 | 1257 | unset($v['orderbyfix___']); |
1262 | 1258 | } |
@@ -51,7 +51,7 @@ |
||
51 | 51 | $instance->setLength($data['length']); |
52 | 52 | } |
53 | 53 | if ($instance->getBasicType() === 'enum' && strpos($instance->getType(), 'enum(') === 0) { |
54 | - $temp = array_map(function ($v) { |
|
54 | + $temp = array_map(function($v) { |
|
55 | 55 | return str_replace("''", "'", $v); |
56 | 56 | }, explode("','", substr($instance->getType(), 6, -2))); |
57 | 57 | $instance->setValues($temp); |
@@ -35,7 +35,7 @@ discard block |
||
35 | 35 | { |
36 | 36 | if ($this->lnk === null) { |
37 | 37 | $this->lnk = new \mysqli( |
38 | - (isset($this->connection['opts']['persist']) && $this->connection['opts']['persist'] ? 'p:' : '') . |
|
38 | + (isset($this->connection['opts']['persist']) && $this->connection['opts']['persist'] ? 'p:' : ''). |
|
39 | 39 | $this->connection['host'], |
40 | 40 | $this->connection['user'], |
41 | 41 | $this->connection['pass'], |
@@ -111,10 +111,10 @@ discard block |
||
111 | 111 | $comments = Collection::from( |
112 | 112 | $this->query( |
113 | 113 | "SELECT TABLE_NAME, TABLE_COMMENT FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = ?", |
114 | - [ $this->connection['name'] ] |
|
114 | + [$this->connection['name']] |
|
115 | 115 | ) |
116 | 116 | ) |
117 | - ->mapKey(function ($v) { |
|
117 | + ->mapKey(function($v) { |
|
118 | 118 | return $v['TABLE_NAME']; |
119 | 119 | }) |
120 | 120 | ->pluck('TABLE_COMMENT') |
@@ -133,7 +133,7 @@ discard block |
||
133 | 133 | WHERE |
134 | 134 | TABLE_SCHEMA = ? AND TABLE_NAME IS NOT NULL AND |
135 | 135 | REFERENCED_TABLE_SCHEMA = ? AND REFERENCED_TABLE_NAME IS NOT NULL", |
136 | - [ $this->connection['name'], $this->connection['name'] ] |
|
136 | + [$this->connection['name'], $this->connection['name']] |
|
137 | 137 | ) |
138 | 138 | )->toArray(); |
139 | 139 | foreach ($col as $row) { |
@@ -151,8 +151,8 @@ discard block |
||
151 | 151 | ->addColumns( |
152 | 152 | $columns |
153 | 153 | ->clone() |
154 | - ->mapKey(function ($v) { return $v['Field']; }) |
|
155 | - ->map(function ($v) { |
|
154 | + ->mapKey(function($v) { return $v['Field']; }) |
|
155 | + ->map(function($v) { |
|
156 | 156 | $v['length'] = null; |
157 | 157 | if (!isset($v['Type'])) { |
158 | 158 | return $v; |
@@ -174,7 +174,7 @@ discard block |
||
174 | 174 | default: |
175 | 175 | if (strpos($type, 'char') !== false && strpos($type, '(') !== false) { |
176 | 176 | // extract length from varchar |
177 | - $v['length'] = (int)explode(')', explode('(', $type)[1])[0]; |
|
177 | + $v['length'] = (int) explode(')', explode('(', $type)[1])[0]; |
|
178 | 178 | $v['length'] = $v['length'] > 0 ? $v['length'] : null; |
179 | 179 | } |
180 | 180 | break; |
@@ -186,7 +186,7 @@ discard block |
||
186 | 186 | ->setPrimaryKey( |
187 | 187 | $columns |
188 | 188 | ->clone() |
189 | - ->filter(function ($v) { return $v['Key'] === 'PRI'; }) |
|
189 | + ->filter(function($v) { return $v['Key'] === 'PRI'; }) |
|
190 | 190 | ->pluck('Field') |
191 | 191 | ->toArray() |
192 | 192 | ) |
@@ -213,10 +213,10 @@ discard block |
||
213 | 213 | $usedcol = []; |
214 | 214 | if (count($columns)) { |
215 | 215 | foreach (Collection::from($relationsT[$data['table']] ?? []) |
216 | - ->filter(function ($v) use ($columns) { |
|
216 | + ->filter(function($v) use ($columns) { |
|
217 | 217 | return in_array($v['COLUMN_NAME'], $columns); |
218 | 218 | }) |
219 | - ->map(function ($v) { |
|
219 | + ->map(function($v) { |
|
220 | 220 | $new = []; |
221 | 221 | foreach ($v as $kk => $vv) { |
222 | 222 | $new[strtoupper($kk)] = $vv; |
@@ -234,7 +234,7 @@ discard block |
||
234 | 234 | $relname = $foreign['table']; |
235 | 235 | $cntr = 1; |
236 | 236 | while ($definition->hasRelation($relname) || $definition->getName() == $relname) { |
237 | - $relname = $foreign['table'] . '_' . (++ $cntr); |
|
237 | + $relname = $foreign['table'].'_'.(++$cntr); |
|
238 | 238 | } |
239 | 239 | $definition->addRelation( |
240 | 240 | new TableRelation( |
@@ -250,7 +250,7 @@ discard block |
||
250 | 250 | $relname = $data['table']; |
251 | 251 | $cntr = 1; |
252 | 252 | while ($definition->hasRelation($relname) || $definition->getName() == $relname) { |
253 | - $relname = $data['table'] . '_' . (++ $cntr); |
|
253 | + $relname = $data['table'].'_'.(++$cntr); |
|
254 | 254 | } |
255 | 255 | $definition->addRelation( |
256 | 256 | new TableRelation( |
@@ -267,7 +267,7 @@ discard block |
||
267 | 267 | // resulting in a "belongsTo" relationship |
268 | 268 | $relations = []; |
269 | 269 | foreach (Collection::from($relationsT[$table] ?? []) |
270 | - ->map(function ($v) { |
|
270 | + ->map(function($v) { |
|
271 | 271 | $new = []; |
272 | 272 | foreach ($v as $kk => $vv) { |
273 | 273 | $new[strtoupper($kk)] = $vv; |
@@ -282,7 +282,7 @@ discard block |
||
282 | 282 | $relname = $data['table']; |
283 | 283 | $cntr = 1; |
284 | 284 | while ($definition->hasRelation($relname) || $definition->getName() == $relname) { |
285 | - $relname = $data['table'] . '_' . (++ $cntr); |
|
285 | + $relname = $data['table'].'_'.(++$cntr); |
|
286 | 286 | } |
287 | 287 | $definition->addRelation( |
288 | 288 | new TableRelation( |
@@ -303,7 +303,7 @@ discard block |
||
303 | 303 | "SELECT table_name FROM information_schema.tables where table_schema = ?", |
304 | 304 | [$this->connection['name']] |
305 | 305 | )) |
306 | - ->map(function ($v) { |
|
306 | + ->map(function($v) { |
|
307 | 307 | $new = []; |
308 | 308 | foreach ($v as $kk => $vv) { |
309 | 309 | $new[strtoupper($kk)] = $vv; |
@@ -311,7 +311,7 @@ discard block |
||
311 | 311 | return $new; |
312 | 312 | }) |
313 | 313 | ->pluck('TABLE_NAME') |
314 | - ->map(function ($v) { |
|
314 | + ->map(function($v) { |
|
315 | 315 | return $this->table($v); |
316 | 316 | }) |
317 | 317 | ->toArray(); |