@@ -43,7 +43,7 @@ discard block |
||
| 43 | 43 | /** |
| 44 | 44 | * @var int[] |
| 45 | 45 | */ |
| 46 | - protected $li_of = [0,0]; |
|
| 46 | + protected $li_of = [0, 0]; |
|
| 47 | 47 | /** |
| 48 | 48 | * @var array |
| 49 | 49 | */ |
@@ -73,7 +73,7 @@ discard block |
||
| 73 | 73 | public function __construct(DBInterface $db, $table) |
| 74 | 74 | { |
| 75 | 75 | $this->db = $db; |
| 76 | - $this->definition = $table instanceof Table ? $table : $this->db->definition((string)$table); |
|
| 76 | + $this->definition = $table instanceof Table ? $table : $this->db->definition((string) $table); |
|
| 77 | 77 | $primary = $this->definition->getPrimaryKey(); |
| 78 | 78 | $columns = $this->definition->getColumns(); |
| 79 | 79 | $this->pkey = count($primary) ? $primary : $columns; |
@@ -96,7 +96,7 @@ discard block |
||
| 96 | 96 | { |
| 97 | 97 | $column = explode('.', $column); |
| 98 | 98 | if (count($column) === 1) { |
| 99 | - $column = [ $this->definition->getName(), $column[0] ]; |
|
| 99 | + $column = [$this->definition->getName(), $column[0]]; |
|
| 100 | 100 | $col = $this->definition->getColumn($column[1]); |
| 101 | 101 | if (!$col) { |
| 102 | 102 | throw new DBException('Invalid column name in own table'); |
@@ -119,7 +119,7 @@ discard block |
||
| 119 | 119 | throw new DBException('Invalid column name in related table'); |
| 120 | 120 | } |
| 121 | 121 | } else { |
| 122 | - throw new DBException('Invalid foreign table name: ' . implode(',', $column)); |
|
| 122 | + throw new DBException('Invalid foreign table name: '.implode(',', $column)); |
|
| 123 | 123 | } |
| 124 | 124 | } |
| 125 | 125 | } else { |
@@ -128,15 +128,15 @@ discard block |
||
| 128 | 128 | $table = $this->definition; |
| 129 | 129 | $table = array_reduce( |
| 130 | 130 | $column, |
| 131 | - function ($carry, $item) use (&$table) { |
|
| 131 | + function($carry, $item) use (&$table) { |
|
| 132 | 132 | $table = $table->getRelation($item)->table; |
| 133 | 133 | return $table; |
| 134 | 134 | } |
| 135 | 135 | ); |
| 136 | 136 | $col = $table->getColumn($name); |
| 137 | - $column = [ implode(static::SEP, $column), $name ]; |
|
| 137 | + $column = [implode(static::SEP, $column), $name]; |
|
| 138 | 138 | } |
| 139 | - return [ 'name' => implode('.', $column), 'data' => $col ]; |
|
| 139 | + return ['name' => implode('.', $column), 'data' => $col]; |
|
| 140 | 140 | } |
| 141 | 141 | protected function normalizeValue(TableColumn $col, $value) |
| 142 | 142 | { |
@@ -183,7 +183,7 @@ discard block |
||
| 183 | 183 | } |
| 184 | 184 | return $value; |
| 185 | 185 | case 'int': |
| 186 | - return (int)$value; |
|
| 186 | + return (int) $value; |
|
| 187 | 187 | default: |
| 188 | 188 | return $value; |
| 189 | 189 | } |
@@ -201,18 +201,16 @@ discard block |
||
| 201 | 201 | list($name, $column) = array_values($this->getColumn($column)); |
| 202 | 202 | if (is_null($value)) { |
| 203 | 203 | return $negate ? |
| 204 | - $this->where($name . ' IS NOT NULL') : |
|
| 205 | - $this->where($name . ' IS NULL'); |
|
| 204 | + $this->where($name.' IS NOT NULL') : $this->where($name.' IS NULL'); |
|
| 206 | 205 | } |
| 207 | 206 | if (!is_array($value)) { |
| 208 | 207 | return $negate ? |
| 209 | 208 | $this->where( |
| 210 | - $name . ' <> ?', |
|
| 211 | - [ $this->normalizeValue($column, $value) ] |
|
| 212 | - ) : |
|
| 213 | - $this->where( |
|
| 214 | - $name . ' = ?', |
|
| 215 | - [ $this->normalizeValue($column, $value) ] |
|
| 209 | + $name.' <> ?', |
|
| 210 | + [$this->normalizeValue($column, $value)] |
|
| 211 | + ) : $this->where( |
|
| 212 | + $name.' = ?', |
|
| 213 | + [$this->normalizeValue($column, $value)] |
|
| 216 | 214 | ); |
| 217 | 215 | } |
| 218 | 216 | if (isset($value['beg']) && isset($value['end'])) { |
@@ -223,8 +221,7 @@ discard block |
||
| 223 | 221 | $this->normalizeValue($column, $value['beg']), |
| 224 | 222 | $this->normalizeValue($column, $value['end']) |
| 225 | 223 | ] |
| 226 | - ) : |
|
| 227 | - $this->where( |
|
| 224 | + ) : $this->where( |
|
| 228 | 225 | $name.' BETWEEN ? AND ?', |
| 229 | 226 | [ |
| 230 | 227 | $this->normalizeValue($column, $value['beg']), |
@@ -235,38 +232,38 @@ discard block |
||
| 235 | 232 | if (isset($value['gt']) || isset($value['lt']) || isset($value['gte']) || isset($value['lte'])) { |
| 236 | 233 | if (isset($value['gt'])) { |
| 237 | 234 | $this->where( |
| 238 | - $name. ' ' . ($negate ? '<=' : '>') . ' ?', |
|
| 239 | - [ $this->normalizeValue($column, $value['gt']) ] |
|
| 235 | + $name.' '.($negate ? '<=' : '>').' ?', |
|
| 236 | + [$this->normalizeValue($column, $value['gt'])] |
|
| 240 | 237 | ); |
| 241 | 238 | } |
| 242 | 239 | if (isset($value['gte'])) { |
| 243 | 240 | $this->where( |
| 244 | - $name. ' ' . ($negate ? '<' : '>=') . ' ?', |
|
| 245 | - [ $this->normalizeValue($column, $value['gte']) ] |
|
| 241 | + $name.' '.($negate ? '<' : '>=').' ?', |
|
| 242 | + [$this->normalizeValue($column, $value['gte'])] |
|
| 246 | 243 | ); |
| 247 | 244 | } |
| 248 | 245 | if (isset($value['lt'])) { |
| 249 | 246 | $this->where( |
| 250 | - $name. ' ' . ($negate ? '>=' : '<') . ' ?', |
|
| 251 | - [ $this->normalizeValue($column, $value['lt']) ] |
|
| 247 | + $name.' '.($negate ? '>=' : '<').' ?', |
|
| 248 | + [$this->normalizeValue($column, $value['lt'])] |
|
| 252 | 249 | ); |
| 253 | 250 | } |
| 254 | 251 | if (isset($value['lte'])) { |
| 255 | 252 | $this->where( |
| 256 | - $name. ' ' . ($negate ? '>' : '<=') . ' ?', |
|
| 257 | - [ $this->normalizeValue($column, $value['lte']) ] |
|
| 253 | + $name.' '.($negate ? '>' : '<=').' ?', |
|
| 254 | + [$this->normalizeValue($column, $value['lte'])] |
|
| 258 | 255 | ); |
| 259 | 256 | } |
| 260 | 257 | return $this; |
| 261 | 258 | } |
| 262 | 259 | return $negate ? |
| 263 | 260 | $this->where( |
| 264 | - $name . ' NOT IN (??)', |
|
| 265 | - [ array_map(function ($v) use ($column) { return $this->normalizeValue($column, $v); }, $value) ] |
|
| 261 | + $name.' NOT IN (??)', |
|
| 262 | + [array_map(function($v) use ($column) { return $this->normalizeValue($column, $v); }, $value)] |
|
| 266 | 263 | ) : |
| 267 | 264 | $this->where( |
| 268 | - $name . ' IN (??)', |
|
| 269 | - [ array_map(function ($v) use ($column) { return $this->normalizeValue($column, $v); }, $value) ] |
|
| 265 | + $name.' IN (??)', |
|
| 266 | + [array_map(function($v) use ($column) { return $this->normalizeValue($column, $v); }, $value)] |
|
| 270 | 267 | ); |
| 271 | 268 | } |
| 272 | 269 | /** |
@@ -277,7 +274,7 @@ discard block |
||
| 277 | 274 | */ |
| 278 | 275 | public function sort(string $column, bool $desc = false) : TableQuery |
| 279 | 276 | { |
| 280 | - return $this->order($this->getColumn($column)['name'] . ' ' . ($desc ? 'DESC' : 'ASC')); |
|
| 277 | + return $this->order($this->getColumn($column)['name'].' '.($desc ? 'DESC' : 'ASC')); |
|
| 281 | 278 | } |
| 282 | 279 | /** |
| 283 | 280 | * Group by a column (or columns) |
@@ -287,7 +284,7 @@ discard block |
||
| 287 | 284 | public function group($column) : TableQuery |
| 288 | 285 | { |
| 289 | 286 | if (!is_array($column)) { |
| 290 | - $column = [ $column ]; |
|
| 287 | + $column = [$column]; |
|
| 291 | 288 | } |
| 292 | 289 | foreach ($column as $k => $v) { |
| 293 | 290 | $column[$k] = $this->getColumn($v)['name']; |
@@ -329,7 +326,7 @@ discard block |
||
| 329 | 326 | $this->order = []; |
| 330 | 327 | $this->having = []; |
| 331 | 328 | $this->aliases = []; |
| 332 | - $this->li_of = [0,0]; |
|
| 329 | + $this->li_of = [0, 0]; |
|
| 333 | 330 | $this->qiterator = null; |
| 334 | 331 | return $this; |
| 335 | 332 | } |
@@ -342,7 +339,7 @@ discard block |
||
| 342 | 339 | public function groupBy(string $sql, array $params = []) : TableQuery |
| 343 | 340 | { |
| 344 | 341 | $this->qiterator = null; |
| 345 | - $this->group = [ $sql, $params ]; |
|
| 342 | + $this->group = [$sql, $params]; |
|
| 346 | 343 | return $this; |
| 347 | 344 | } |
| 348 | 345 | /** |
@@ -355,7 +352,7 @@ discard block |
||
| 355 | 352 | */ |
| 356 | 353 | public function join($table, array $fields, string $name = null, bool $multiple = true) |
| 357 | 354 | { |
| 358 | - $table = $table instanceof Table ? $table : $this->db->definition((string)$table); |
|
| 355 | + $table = $table instanceof Table ? $table : $this->db->definition((string) $table); |
|
| 359 | 356 | $name = $name ?? $table->getName(); |
| 360 | 357 | if (isset($this->joins[$name]) || $this->definition->hasRelation($name)) { |
| 361 | 358 | throw new DBException('Alias / table name already in use'); |
@@ -364,7 +361,7 @@ discard block |
||
| 364 | 361 | foreach ($fields as $k => $v) { |
| 365 | 362 | $k = explode('.', $k, 2); |
| 366 | 363 | $k = count($k) == 2 ? $k[1] : $k[0]; |
| 367 | - $this->joins[$name]->keymap[$this->getColumn($name . '.' . $k)['name']] = $this->getColumn($v)['name']; |
|
| 364 | + $this->joins[$name]->keymap[$this->getColumn($name.'.'.$k)['name']] = $this->getColumn($v)['name']; |
|
| 368 | 365 | } |
| 369 | 366 | return $this; |
| 370 | 367 | } |
@@ -377,7 +374,7 @@ discard block |
||
| 377 | 374 | public function where(string $sql, array $params = []) : TableQuery |
| 378 | 375 | { |
| 379 | 376 | $this->qiterator = null; |
| 380 | - $this->where[] = [ $sql, $params ]; |
|
| 377 | + $this->where[] = [$sql, $params]; |
|
| 381 | 378 | return $this; |
| 382 | 379 | } |
| 383 | 380 | /** |
@@ -389,7 +386,7 @@ discard block |
||
| 389 | 386 | public function having(string $sql, array $params = []) : TableQuery |
| 390 | 387 | { |
| 391 | 388 | $this->qiterator = null; |
| 392 | - $this->having[] = [ $sql, $params ]; |
|
| 389 | + $this->having[] = [$sql, $params]; |
|
| 393 | 390 | return $this; |
| 394 | 391 | } |
| 395 | 392 | /** |
@@ -401,7 +398,7 @@ discard block |
||
| 401 | 398 | public function order(string $sql, array $params = []) : TableQuery |
| 402 | 399 | { |
| 403 | 400 | $this->qiterator = null; |
| 404 | - $this->order = [ $sql, $params ]; |
|
| 401 | + $this->order = [$sql, $params]; |
|
| 405 | 402 | return $this; |
| 406 | 403 | } |
| 407 | 404 | /** |
@@ -413,7 +410,7 @@ discard block |
||
| 413 | 410 | public function limit(int $limit, int $offset = 0) : TableQuery |
| 414 | 411 | { |
| 415 | 412 | $this->qiterator = null; |
| 416 | - $this->li_of = [ $limit, $offset ]; |
|
| 413 | + $this->li_of = [$limit, $offset]; |
|
| 417 | 414 | return $this; |
| 418 | 415 | } |
| 419 | 416 | /** |
@@ -423,9 +420,9 @@ discard block |
||
| 423 | 420 | public function count() : int |
| 424 | 421 | { |
| 425 | 422 | $aliases = []; |
| 426 | - $getAlias = function ($name) use (&$aliases) { |
|
| 423 | + $getAlias = function($name) use (&$aliases) { |
|
| 427 | 424 | // to bypass use: return $name; |
| 428 | - return $aliases[$name] = $aliases[$name] ?? 'alias' . static::SEP . count($aliases); |
|
| 425 | + return $aliases[$name] = $aliases[$name] ?? 'alias'.static::SEP.count($aliases); |
|
| 429 | 426 | }; |
| 430 | 427 | $table = $this->definition->getName(); |
| 431 | 428 | $sql = 'SELECT COUNT(DISTINCT '.$table.'.'.implode(', '.$table.'.', $this->pkey).') FROM '.$table.' '; |
@@ -440,44 +437,44 @@ discard block |
||
| 440 | 437 | $h = $this->having; |
| 441 | 438 | $o = $this->order; |
| 442 | 439 | $g = $this->group; |
| 443 | - $j = array_map(function ($v) { return clone $v; }, $this->joins); |
|
| 440 | + $j = array_map(function($v) { return clone $v; }, $this->joins); |
|
| 444 | 441 | foreach ($this->definition->getRelations() as $k => $v) { |
| 445 | 442 | foreach ($w as $kk => $vv) { |
| 446 | - if (strpos($vv[0], $k . '.') !== false) { |
|
| 447 | - $relations[$k] = [ $v, $table ]; |
|
| 448 | - $w[$kk][0] = str_replace($k . '.', $getAlias($k) . '.', $vv[0]); |
|
| 443 | + if (strpos($vv[0], $k.'.') !== false) { |
|
| 444 | + $relations[$k] = [$v, $table]; |
|
| 445 | + $w[$kk][0] = str_replace($k.'.', $getAlias($k).'.', $vv[0]); |
|
| 449 | 446 | } |
| 450 | 447 | } |
| 451 | - if (isset($o[0]) && strpos($o[0], $k . '.') !== false) { |
|
| 452 | - $relations[$k] = [ $v, $table ]; |
|
| 448 | + if (isset($o[0]) && strpos($o[0], $k.'.') !== false) { |
|
| 449 | + $relations[$k] = [$v, $table]; |
|
| 453 | 450 | } |
| 454 | 451 | foreach ($h as $kk => $vv) { |
| 455 | - if (strpos($vv[0], $k . '.') !== false) { |
|
| 456 | - $relations[$k] = [ $relation, $table ]; |
|
| 457 | - $h[$kk][0] = str_replace($k . '.', $getAlias($k) . '.', $vv[0]); |
|
| 452 | + if (strpos($vv[0], $k.'.') !== false) { |
|
| 453 | + $relations[$k] = [$relation, $table]; |
|
| 454 | + $h[$kk][0] = str_replace($k.'.', $getAlias($k).'.', $vv[0]); |
|
| 458 | 455 | } |
| 459 | 456 | } |
| 460 | - if (isset($g[0]) && strpos($g[0], $k . '.') !== false) { |
|
| 461 | - $relations[$k] = [ $relation, $table ]; |
|
| 462 | - $g[0] = str_replace($k . '.', $getAlias($k) . '.', $g[0]); |
|
| 457 | + if (isset($g[0]) && strpos($g[0], $k.'.') !== false) { |
|
| 458 | + $relations[$k] = [$relation, $table]; |
|
| 459 | + $g[0] = str_replace($k.'.', $getAlias($k).'.', $g[0]); |
|
| 463 | 460 | } |
| 464 | 461 | foreach ($j as $kk => $v) { |
| 465 | 462 | foreach ($v->keymap as $kkk => $vv) { |
| 466 | - if (strpos($vv, $k . '.') !== false) { |
|
| 467 | - $relations[$k] = [ $relation, $table ]; |
|
| 468 | - $j[$k]->keymap[$kkk] = str_replace($k . '.', $getAlias($k) . '.', $vv); |
|
| 463 | + if (strpos($vv, $k.'.') !== false) { |
|
| 464 | + $relations[$k] = [$relation, $table]; |
|
| 465 | + $j[$k]->keymap[$kkk] = str_replace($k.'.', $getAlias($k).'.', $vv); |
|
| 469 | 466 | } |
| 470 | 467 | } |
| 471 | 468 | } |
| 472 | 469 | } |
| 473 | 470 | |
| 474 | 471 | foreach ($j as $k => $v) { |
| 475 | - $sql .= ($v->many ? 'LEFT ' : '' ) . 'JOIN '.$v->table->getName().' '.$k.' ON '; |
|
| 472 | + $sql .= ($v->many ? 'LEFT ' : '').'JOIN '.$v->table->getName().' '.$k.' ON '; |
|
| 476 | 473 | $tmp = []; |
| 477 | 474 | foreach ($v->keymap as $kk => $vv) { |
| 478 | 475 | $tmp[] = $kk.' = '.$vv; |
| 479 | 476 | } |
| 480 | - $sql .= implode(' AND ', $tmp) . ' '; |
|
| 477 | + $sql .= implode(' AND ', $tmp).' '; |
|
| 481 | 478 | } |
| 482 | 479 | foreach ($relations as $k => $v) { |
| 483 | 480 | $table = $v[1] !== $this->definition->getName() ? $getAlias($v[1]) : $v[1]; |
@@ -489,13 +486,13 @@ discard block |
||
| 489 | 486 | foreach ($v->keymap as $kk => $vv) { |
| 490 | 487 | $tmp[] = $table.'.'.$kk.' = '.$alias.'.'.$vv.' '; |
| 491 | 488 | } |
| 492 | - $sql .= implode(' AND ', $tmp) . ' '; |
|
| 489 | + $sql .= implode(' AND ', $tmp).' '; |
|
| 493 | 490 | $sql .= 'LEFT JOIN '.$v->table->getName().' '.$getAlias($k).' ON '; |
| 494 | 491 | $tmp = []; |
| 495 | 492 | foreach ($v->pivot_keymap as $kk => $vv) { |
| 496 | 493 | $tmp[] = $getAlias($k).'.'.$vv.' = '.$alias.'.'.$kk.' '; |
| 497 | 494 | } |
| 498 | - $sql .= implode(' AND ', $tmp) . ' '; |
|
| 495 | + $sql .= implode(' AND ', $tmp).' '; |
|
| 499 | 496 | } else { |
| 500 | 497 | $alias = $getAlias($k); |
| 501 | 498 | $sql .= 'LEFT JOIN '.$v->table->getName().' '.$alias.' ON '; |
@@ -504,30 +501,30 @@ discard block |
||
| 504 | 501 | $tmp[] = $table.'.'.$kk.' = '.$alias.'.'.$vv.' '; |
| 505 | 502 | } |
| 506 | 503 | if ($v->sql) { |
| 507 | - $tmp[] = $v->sql . ' '; |
|
| 504 | + $tmp[] = $v->sql.' '; |
|
| 508 | 505 | $par = array_merge($par, $v->par ?? []); |
| 509 | 506 | } |
| 510 | - $sql .= implode(' AND ', $tmp) . ' '; |
|
| 507 | + $sql .= implode(' AND ', $tmp).' '; |
|
| 511 | 508 | } |
| 512 | 509 | } |
| 513 | 510 | if (count($w)) { |
| 514 | 511 | $sql .= 'WHERE '; |
| 515 | 512 | $tmp = []; |
| 516 | 513 | foreach ($w as $v) { |
| 517 | - $tmp[] = '(' . $v[0] . ')'; |
|
| 514 | + $tmp[] = '('.$v[0].')'; |
|
| 518 | 515 | $par = array_merge($par, $v[1]); |
| 519 | 516 | } |
| 520 | 517 | $sql .= implode(' AND ', $tmp).' '; |
| 521 | 518 | } |
| 522 | 519 | if (count($g)) { |
| 523 | - $sql .= 'GROUP BY ' . $g[0] . ' '; |
|
| 520 | + $sql .= 'GROUP BY '.$g[0].' '; |
|
| 524 | 521 | $par = array_merge($par, $g[1]); |
| 525 | 522 | } |
| 526 | 523 | if (count($h)) { |
| 527 | 524 | $sql .= 'HAVING '; |
| 528 | 525 | $tmp = []; |
| 529 | 526 | foreach ($h as $v) { |
| 530 | - $tmp[] = '(' . $v[0] . ')'; |
|
| 527 | + $tmp[] = '('.$v[0].')'; |
|
| 531 | 528 | $par = array_merge($par, $v[1]); |
| 532 | 529 | } |
| 533 | 530 | $sql .= implode(' AND ', $tmp).' '; |
@@ -561,7 +558,7 @@ discard block |
||
| 561 | 558 | $this->with(implode('.', $temp)); |
| 562 | 559 | $table = array_reduce( |
| 563 | 560 | $temp, |
| 564 | - function ($carry, $item) use (&$table) { |
|
| 561 | + function($carry, $item) use (&$table) { |
|
| 565 | 562 | return $table->getRelation($item)->table; |
| 566 | 563 | } |
| 567 | 564 | ); |
@@ -570,7 +567,7 @@ discard block |
||
| 570 | 567 | } |
| 571 | 568 | unset($fields[$k]); |
| 572 | 569 | foreach ($cols as $col) { |
| 573 | - $fields[] = $table . '.' . $col; |
|
| 570 | + $fields[] = $table.'.'.$col; |
|
| 574 | 571 | } |
| 575 | 572 | } |
| 576 | 573 | } |
@@ -602,9 +599,9 @@ discard block |
||
| 602 | 599 | return $this->qiterator; |
| 603 | 600 | } |
| 604 | 601 | $aliases = []; |
| 605 | - $getAlias = function ($name) use (&$aliases) { |
|
| 602 | + $getAlias = function($name) use (&$aliases) { |
|
| 606 | 603 | // to bypass use: return $name; |
| 607 | - return $aliases[$name] = $aliases[$name] ?? 'alias' . static::SEP . count($aliases); |
|
| 604 | + return $aliases[$name] = $aliases[$name] ?? 'alias'.static::SEP.count($aliases); |
|
| 608 | 605 | }; |
| 609 | 606 | $table = $this->definition->getName(); |
| 610 | 607 | if ($fields !== null) { |
@@ -620,61 +617,61 @@ discard block |
||
| 620 | 617 | $h = $this->having; |
| 621 | 618 | $o = $this->order; |
| 622 | 619 | $g = $this->group; |
| 623 | - $j = array_map(function ($v) { return clone $v; }, $this->joins); |
|
| 620 | + $j = array_map(function($v) { return clone $v; }, $this->joins); |
|
| 624 | 621 | foreach ($this->definition->getRelations() as $k => $relation) { |
| 625 | 622 | foreach ($f as $kk => $field) { |
| 626 | - if (strpos($field, $k . '.') === 0) { |
|
| 627 | - $relations[$k] = [ $relation, $table ]; |
|
| 628 | - $f[$kk] = str_replace($k . '.', $getAlias($k) . '.', $field); |
|
| 623 | + if (strpos($field, $k.'.') === 0) { |
|
| 624 | + $relations[$k] = [$relation, $table]; |
|
| 625 | + $f[$kk] = str_replace($k.'.', $getAlias($k).'.', $field); |
|
| 629 | 626 | } |
| 630 | 627 | } |
| 631 | 628 | foreach ($w as $kk => $v) { |
| 632 | - if (strpos($v[0], $k . '.') !== false) { |
|
| 633 | - $relations[$k] = [ $relation, $table ]; |
|
| 634 | - $w[$kk][0] = str_replace($k . '.', $getAlias($k) . '.', $v[0]); |
|
| 629 | + if (strpos($v[0], $k.'.') !== false) { |
|
| 630 | + $relations[$k] = [$relation, $table]; |
|
| 631 | + $w[$kk][0] = str_replace($k.'.', $getAlias($k).'.', $v[0]); |
|
| 635 | 632 | } |
| 636 | 633 | } |
| 637 | 634 | foreach ($h as $kk => $v) { |
| 638 | - if (strpos($v[0], $k . '.') !== false) { |
|
| 639 | - $relations[$k] = [ $relation, $table ]; |
|
| 640 | - $h[$kk][0] = str_replace($k . '.', $getAlias($k) . '.', $v[0]); |
|
| 635 | + if (strpos($v[0], $k.'.') !== false) { |
|
| 636 | + $relations[$k] = [$relation, $table]; |
|
| 637 | + $h[$kk][0] = str_replace($k.'.', $getAlias($k).'.', $v[0]); |
|
| 641 | 638 | } |
| 642 | 639 | } |
| 643 | - if (isset($o[0]) && strpos($o[0], $k . '.') !== false) { |
|
| 644 | - $relations[$k] = [ $relation, $table ]; |
|
| 645 | - $o[0] = str_replace($k . '.', $getAlias($k) . '.', $o[0]); |
|
| 640 | + if (isset($o[0]) && strpos($o[0], $k.'.') !== false) { |
|
| 641 | + $relations[$k] = [$relation, $table]; |
|
| 642 | + $o[0] = str_replace($k.'.', $getAlias($k).'.', $o[0]); |
|
| 646 | 643 | } |
| 647 | - if (isset($g[0]) && strpos($g[0], $k . '.') !== false) { |
|
| 648 | - $relations[$k] = [ $relation, $table ]; |
|
| 649 | - $g[0] = str_replace($k . '.', $getAlias($k) . '.', $g[0]); |
|
| 644 | + if (isset($g[0]) && strpos($g[0], $k.'.') !== false) { |
|
| 645 | + $relations[$k] = [$relation, $table]; |
|
| 646 | + $g[0] = str_replace($k.'.', $getAlias($k).'.', $g[0]); |
|
| 650 | 647 | } |
| 651 | 648 | foreach ($j as $kk => $v) { |
| 652 | 649 | foreach ($v->keymap as $kkk => $vv) { |
| 653 | - if (strpos($vv, $k . '.') !== false) { |
|
| 654 | - $relations[$k] = [ $relation, $table ]; |
|
| 655 | - $j[$k]->keymap[$kkk] = str_replace($k . '.', $getAlias($k) . '.', $vv); |
|
| 650 | + if (strpos($vv, $k.'.') !== false) { |
|
| 651 | + $relations[$k] = [$relation, $table]; |
|
| 652 | + $j[$k]->keymap[$kkk] = str_replace($k.'.', $getAlias($k).'.', $vv); |
|
| 656 | 653 | } |
| 657 | 654 | } |
| 658 | 655 | } |
| 659 | 656 | } |
| 660 | 657 | $select = []; |
| 661 | 658 | foreach ($f as $k => $field) { |
| 662 | - $select[] = $field . (!is_numeric($k) ? ' ' . $k : ''); |
|
| 659 | + $select[] = $field.(!is_numeric($k) ? ' '.$k : ''); |
|
| 663 | 660 | } |
| 664 | 661 | foreach ($this->withr as $name => $relation) { |
| 665 | 662 | foreach ($relation[0]->table->getColumns() as $column) { |
| 666 | - $select[] = $getAlias($name) . '.' . $column . ' ' . $getAlias($name . static::SEP . $column); |
|
| 663 | + $select[] = $getAlias($name).'.'.$column.' '.$getAlias($name.static::SEP.$column); |
|
| 667 | 664 | } |
| 668 | 665 | } |
| 669 | 666 | $sql = 'SELECT '.implode(', ', $select).' FROM '.$table.' '; |
| 670 | 667 | $par = []; |
| 671 | 668 | foreach ($j as $k => $v) { |
| 672 | - $sql .= ($v->many ? 'LEFT ' : '' ) . 'JOIN '.$v->table->getName().' '.$k.' ON '; |
|
| 669 | + $sql .= ($v->many ? 'LEFT ' : '').'JOIN '.$v->table->getName().' '.$k.' ON '; |
|
| 673 | 670 | $tmp = []; |
| 674 | 671 | foreach ($v->keymap as $kk => $vv) { |
| 675 | 672 | $tmp[] = $kk.' = '.$vv; |
| 676 | 673 | } |
| 677 | - $sql .= implode(' AND ', $tmp) . ' '; |
|
| 674 | + $sql .= implode(' AND ', $tmp).' '; |
|
| 678 | 675 | } |
| 679 | 676 | foreach ($relations as $relation => $v) { |
| 680 | 677 | $table = $v[1] !== $this->definition->getName() ? $getAlias($v[1]) : $v[1]; |
@@ -686,13 +683,13 @@ discard block |
||
| 686 | 683 | foreach ($v->keymap as $kk => $vv) { |
| 687 | 684 | $tmp[] = $table.'.'.$kk.' = '.$alias.'.'.$vv.' '; |
| 688 | 685 | } |
| 689 | - $sql .= implode(' AND ', $tmp) . ' '; |
|
| 686 | + $sql .= implode(' AND ', $tmp).' '; |
|
| 690 | 687 | $sql .= 'LEFT JOIN '.$v->table->getName().' '.$getAlias($relation).' ON '; |
| 691 | 688 | $tmp = []; |
| 692 | 689 | foreach ($v->pivot_keymap as $kk => $vv) { |
| 693 | 690 | $tmp[] = $getAlias($relation).'.'.$vv.' = '.$alias.'.'.$kk.' '; |
| 694 | 691 | } |
| 695 | - $sql .= implode(' AND ', $tmp) . ' '; |
|
| 692 | + $sql .= implode(' AND ', $tmp).' '; |
|
| 696 | 693 | } else { |
| 697 | 694 | $alias = $getAlias($relation); |
| 698 | 695 | |
@@ -702,36 +699,36 @@ discard block |
||
| 702 | 699 | $tmp[] = $table.'.'.$kk.' = '.$alias.'.'.$vv.' '; |
| 703 | 700 | } |
| 704 | 701 | if ($v->sql) { |
| 705 | - $tmp[] = $v->sql . ' '; |
|
| 702 | + $tmp[] = $v->sql.' '; |
|
| 706 | 703 | $par = array_merge($par, $v->par ?? []); |
| 707 | 704 | } |
| 708 | - $sql .= implode(' AND ', $tmp) . ' '; |
|
| 705 | + $sql .= implode(' AND ', $tmp).' '; |
|
| 709 | 706 | } |
| 710 | 707 | } |
| 711 | 708 | if (count($w)) { |
| 712 | 709 | $sql .= 'WHERE '; |
| 713 | 710 | $tmp = []; |
| 714 | 711 | foreach ($w as $v) { |
| 715 | - $tmp[] = '(' . $v[0] . ')'; |
|
| 712 | + $tmp[] = '('.$v[0].')'; |
|
| 716 | 713 | $par = array_merge($par, $v[1]); |
| 717 | 714 | } |
| 718 | 715 | $sql .= implode(' AND ', $tmp).' '; |
| 719 | 716 | } |
| 720 | 717 | if (count($g)) { |
| 721 | - $sql .= 'GROUP BY ' . $g[0] . ' '; |
|
| 718 | + $sql .= 'GROUP BY '.$g[0].' '; |
|
| 722 | 719 | $par = array_merge($par, $g[1]); |
| 723 | 720 | } |
| 724 | 721 | if (count($h)) { |
| 725 | 722 | $sql .= 'HAVING '; |
| 726 | 723 | $tmp = []; |
| 727 | 724 | foreach ($h as $v) { |
| 728 | - $tmp[] = '(' . $v[0] . ')'; |
|
| 725 | + $tmp[] = '('.$v[0].')'; |
|
| 729 | 726 | $par = array_merge($par, $v[1]); |
| 730 | 727 | } |
| 731 | 728 | $sql .= implode(' AND ', $tmp).' '; |
| 732 | 729 | } |
| 733 | 730 | if (count($o)) { |
| 734 | - $sql .= 'ORDER BY ' . $o[0] . ' '; |
|
| 731 | + $sql .= 'ORDER BY '.$o[0].' '; |
|
| 735 | 732 | $par = array_merge($par, $o[1]); |
| 736 | 733 | } |
| 737 | 734 | $porder = []; |
@@ -739,30 +736,30 @@ discard block |
||
| 739 | 736 | $porder[] = $this->getColumn($field)['name']; |
| 740 | 737 | } |
| 741 | 738 | if (count($porder)) { |
| 742 | - $sql .= (count($o) ? ', ' : 'ORDER BY ') . implode(', ', $porder) . ' '; |
|
| 739 | + $sql .= (count($o) ? ', ' : 'ORDER BY ').implode(', ', $porder).' '; |
|
| 743 | 740 | } |
| 744 | 741 | |
| 745 | 742 | if ($this->li_of[0]) { |
| 746 | 743 | if ($this->db->driverName() === 'oracle') { |
| 747 | - if ((int)$this->db->driverOption('version', 0) >= 12) { |
|
| 748 | - $sql .= 'OFFSET ' . $this->li_of[1] . ' ROWS FETCH NEXT ' . $this->li_of[0] . ' ROWS ONLY'; |
|
| 744 | + if ((int) $this->db->driverOption('version', 0) >= 12) { |
|
| 745 | + $sql .= 'OFFSET '.$this->li_of[1].' ROWS FETCH NEXT '.$this->li_of[0].' ROWS ONLY'; |
|
| 749 | 746 | } else { |
| 750 | - $f = array_map(function ($v) { |
|
| 747 | + $f = array_map(function($v) { |
|
| 751 | 748 | $v = explode(' ', trim($v), 2); |
| 752 | 749 | if (count($v) === 2) { return $v[1]; } |
| 753 | 750 | $v = explode('.', $v[0], 2); |
| 754 | 751 | return count($v) === 2 ? $v[1] : $v[0]; |
| 755 | 752 | }, $select); |
| 756 | - $sql = "SELECT " . implode(', ', $f) . " |
|
| 753 | + $sql = "SELECT ".implode(', ', $f)." |
|
| 757 | 754 | FROM ( |
| 758 | 755 | SELECT tbl__.*, rownum rnum__ FROM ( |
| 759 | - " . $sql . " |
|
| 756 | + " . $sql." |
|
| 760 | 757 | ) tbl__ |
| 761 | - WHERE rownum <= " . ($this->li_of[0] + $this->li_of[1]) . " |
|
| 758 | + WHERE rownum <= " . ($this->li_of[0] + $this->li_of[1])." |
|
| 762 | 759 | ) WHERE rnum__ > " . $this->li_of[1]; |
| 763 | 760 | } |
| 764 | 761 | } else { |
| 765 | - $sql .= 'LIMIT ' . $this->li_of[0] . ' OFFSET ' . $this->li_of[1]; |
|
| 762 | + $sql .= 'LIMIT '.$this->li_of[0].' OFFSET '.$this->li_of[1]; |
|
| 766 | 763 | } |
| 767 | 764 | } |
| 768 | 765 | return $this->qiterator = new TableQueryIterator( |
@@ -812,7 +809,7 @@ discard block |
||
| 812 | 809 | $ret[$k] = str_repeat(' ', 255); |
| 813 | 810 | $par[] = &$ret[$k]; |
| 814 | 811 | } |
| 815 | - $sql .= ' RETURNING ' . implode(',', $primary) . ' INTO ' . implode(',', array_fill(0, count($primary), '?')); |
|
| 812 | + $sql .= ' RETURNING '.implode(',', $primary).' INTO '.implode(',', array_fill(0, count($primary), '?')); |
|
| 816 | 813 | $this->db->query($sql, $par); |
| 817 | 814 | return $ret; |
| 818 | 815 | } else { |
@@ -844,7 +841,7 @@ discard block |
||
| 844 | 841 | } |
| 845 | 842 | $sql = 'UPDATE '.$table.' SET '; |
| 846 | 843 | $par = []; |
| 847 | - $sql .= implode(', ', array_map(function ($v) { return $v . ' = ?'; }, array_keys($update))) . ' '; |
|
| 844 | + $sql .= implode(', ', array_map(function($v) { return $v.' = ?'; }, array_keys($update))).' '; |
|
| 848 | 845 | $par = array_merge($par, array_values($update)); |
| 849 | 846 | if (count($this->where)) { |
| 850 | 847 | $sql .= 'WHERE '; |
@@ -853,7 +850,7 @@ discard block |
||
| 853 | 850 | $tmp[] = $v[0]; |
| 854 | 851 | $par = array_merge($par, $v[1]); |
| 855 | 852 | } |
| 856 | - $sql .= implode(' AND ', $tmp) . ' '; |
|
| 853 | + $sql .= implode(' AND ', $tmp).' '; |
|
| 857 | 854 | } |
| 858 | 855 | if (count($this->order)) { |
| 859 | 856 | $sql .= $this->order[0]; |
@@ -877,7 +874,7 @@ discard block |
||
| 877 | 874 | $tmp[] = $v[0]; |
| 878 | 875 | $par = array_merge($par, $v[1]); |
| 879 | 876 | } |
| 880 | - $sql .= implode(' AND ', $tmp) . ' '; |
|
| 877 | + $sql .= implode(' AND ', $tmp).' '; |
|
| 881 | 878 | } |
| 882 | 879 | if (count($this->order)) { |
| 883 | 880 | $sql .= $this->order[0]; |
@@ -897,13 +894,13 @@ discard block |
||
| 897 | 894 | $table = $this->definition; |
| 898 | 895 | array_reduce( |
| 899 | 896 | $parts, |
| 900 | - function ($carry, $item) use (&$table) { |
|
| 897 | + function($carry, $item) use (&$table) { |
|
| 901 | 898 | $relation = $table->getRelation($item); |
| 902 | 899 | if (!$relation) { |
| 903 | 900 | throw new DBException('Invalid relation name'); |
| 904 | 901 | } |
| 905 | - $name = $carry ? $carry . static::SEP . $item : $item; |
|
| 906 | - $this->withr[$name] = [ $relation, $carry ?? $table->getName() ]; |
|
| 902 | + $name = $carry ? $carry.static::SEP.$item : $item; |
|
| 903 | + $this->withr[$name] = [$relation, $carry ?? $table->getName()]; |
|
| 907 | 904 | $table = $relation->table; |
| 908 | 905 | return $name; |
| 909 | 906 | } |