@@ -35,7 +35,6 @@ discard block |
||
| 35 | 35 | /** |
| 36 | 36 | * Run a query (prepare & execute). |
| 37 | 37 | * @param string $sql SQL query |
| 38 | - * @param array $data parameters (optional) |
|
| 39 | 38 | * @return ResultInterface the result of the execution |
| 40 | 39 | */ |
| 41 | 40 | public function query(string $sql, $par = null) : ResultInterface |
@@ -50,6 +49,13 @@ discard block |
||
| 50 | 49 | { |
| 51 | 50 | return $this->connection['name']; |
| 52 | 51 | } |
| 52 | + |
|
| 53 | + /** |
|
| 54 | + * @param string $key |
|
| 55 | + * @param string $default |
|
| 56 | + * |
|
| 57 | + * @return string |
|
| 58 | + */ |
|
| 53 | 59 | public function option($key, $default = null) |
| 54 | 60 | { |
| 55 | 61 | return isset($this->connection['opts'][$key]) ? $this->connection['opts'][$key] : $default; |
@@ -12,7 +12,7 @@ discard block |
||
| 12 | 12 | $new = ''; |
| 13 | 13 | $par = array_values($par); |
| 14 | 14 | if (substr_count($sql, '?') === 2 && !is_array($par[0])) { |
| 15 | - $par = [ $par ]; |
|
| 15 | + $par = [$par]; |
|
| 16 | 16 | } |
| 17 | 17 | $parts = explode('??', $sql); |
| 18 | 18 | $index = 0; |
@@ -22,7 +22,7 @@ discard block |
||
| 22 | 22 | $index += count($tmp) - 1; |
| 23 | 23 | if (isset($par[$index])) { |
| 24 | 24 | if (!is_array($par[$index])) { |
| 25 | - $par[$index] = [ $par[$index] ]; |
|
| 25 | + $par[$index] = [$par[$index]]; |
|
| 26 | 26 | } |
| 27 | 27 | $params = $par[$index]; |
| 28 | 28 | array_splice($par, $index, 1, $params); |
@@ -30,7 +30,7 @@ discard block |
||
| 30 | 30 | $new .= implode(',', array_fill(0, count($params), '?')); |
| 31 | 31 | } |
| 32 | 32 | } |
| 33 | - return [ $new, $par ]; |
|
| 33 | + return [$new, $par]; |
|
| 34 | 34 | } |
| 35 | 35 | /** |
| 36 | 36 | * Run a query (prepare & execute). |
@@ -3,7 +3,6 @@ |
||
| 3 | 3 | |
| 4 | 4 | use vakata\database\DBInterface; |
| 5 | 5 | use vakata\database\DBException; |
| 6 | -use vakata\database\ResultInterface; |
|
| 7 | 6 | |
| 8 | 7 | /** |
| 9 | 8 | * A database query class |
@@ -60,7 +60,7 @@ discard block |
||
| 60 | 60 | /** |
| 61 | 61 | * Create an instance |
| 62 | 62 | * @param DBInterface $db the database connection |
| 63 | - * @param Table|string $table the name or definition of the main table in the query |
|
| 63 | + * @param Table $table the name or definition of the main table in the query |
|
| 64 | 64 | */ |
| 65 | 65 | public function __construct(DBInterface $db, $table) |
| 66 | 66 | { |
@@ -239,7 +239,7 @@ discard block |
||
| 239 | 239 | } |
| 240 | 240 | /** |
| 241 | 241 | * Group by a column (or columns) |
| 242 | - * @param string|array $column the column name (or names) to group by |
|
| 242 | + * @param string $column the column name (or names) to group by |
|
| 243 | 243 | * @return $this |
| 244 | 244 | */ |
| 245 | 245 | public function group($column) : TableQuery |
@@ -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 | */ |
@@ -65,7 +65,7 @@ discard block |
||
| 65 | 65 | public function __construct(DBInterface $db, $table) |
| 66 | 66 | { |
| 67 | 67 | $this->db = $db; |
| 68 | - $this->definition = $table instanceof Table ? $table : $this->db->definition((string)$table); |
|
| 68 | + $this->definition = $table instanceof Table ? $table : $this->db->definition((string) $table); |
|
| 69 | 69 | $this->columns($this->definition->getColumns()); |
| 70 | 70 | } |
| 71 | 71 | public function __clone() |
@@ -85,7 +85,7 @@ discard block |
||
| 85 | 85 | { |
| 86 | 86 | $column = explode('.', $column); |
| 87 | 87 | if (count($column) === 1) { |
| 88 | - $column = [ $this->definition->getName(), $column[0] ]; |
|
| 88 | + $column = [$this->definition->getName(), $column[0]]; |
|
| 89 | 89 | $col = $this->definition->getColumn($column[1]); |
| 90 | 90 | } elseif (count($column) === 2) { |
| 91 | 91 | if ($column[0] === $this->definition->getName()) { |
@@ -105,7 +105,7 @@ discard block |
||
| 105 | 105 | throw new DBException('Invalid column name in related table'); |
| 106 | 106 | } |
| 107 | 107 | } else { |
| 108 | - throw new DBException('Invalid foreign table name: ' . implode(',', $column)); |
|
| 108 | + throw new DBException('Invalid foreign table name: '.implode(',', $column)); |
|
| 109 | 109 | } |
| 110 | 110 | } |
| 111 | 111 | } else { |
@@ -114,15 +114,15 @@ discard block |
||
| 114 | 114 | $table = $this->definition; |
| 115 | 115 | $table = array_reduce( |
| 116 | 116 | $column, |
| 117 | - function ($carry, $item) use (&$table) { |
|
| 117 | + function($carry, $item) use (&$table) { |
|
| 118 | 118 | $table = $table->getRelation($item)->table; |
| 119 | 119 | return $table; |
| 120 | 120 | } |
| 121 | 121 | ); |
| 122 | 122 | $col = $table->getColumn($name); |
| 123 | - $column = [ implode(static::SEP, $column), $name ]; |
|
| 123 | + $column = [implode(static::SEP, $column), $name]; |
|
| 124 | 124 | } |
| 125 | - return [ 'name' => implode('.', $column), 'data' => $col ]; |
|
| 125 | + return ['name' => implode('.', $column), 'data' => $col]; |
|
| 126 | 126 | } |
| 127 | 127 | protected function normalizeValue(TableColumn $col, $value) |
| 128 | 128 | { |
@@ -169,7 +169,7 @@ discard block |
||
| 169 | 169 | } |
| 170 | 170 | return $value; |
| 171 | 171 | case 'int': |
| 172 | - return (int)$value; |
|
| 172 | + return (int) $value; |
|
| 173 | 173 | default: |
| 174 | 174 | return $value; |
| 175 | 175 | } |
@@ -186,18 +186,16 @@ discard block |
||
| 186 | 186 | list($name, $column) = array_values($this->getColumn($column)); |
| 187 | 187 | if (is_null($value)) { |
| 188 | 188 | return $negate ? |
| 189 | - $this->where($name . ' IS NOT NULL') : |
|
| 190 | - $this->where($name . ' IS NULL'); |
|
| 189 | + $this->where($name.' IS NOT NULL') : $this->where($name.' IS NULL'); |
|
| 191 | 190 | } |
| 192 | 191 | if (!is_array($value)) { |
| 193 | 192 | return $negate ? |
| 194 | 193 | $this->where( |
| 195 | - $name . ' <> ?', |
|
| 196 | - [ $this->normalizeValue($column, $value) ] |
|
| 197 | - ) : |
|
| 198 | - $this->where( |
|
| 199 | - $name . ' = ?', |
|
| 200 | - [ $this->normalizeValue($column, $value) ] |
|
| 194 | + $name.' <> ?', |
|
| 195 | + [$this->normalizeValue($column, $value)] |
|
| 196 | + ) : $this->where( |
|
| 197 | + $name.' = ?', |
|
| 198 | + [$this->normalizeValue($column, $value)] |
|
| 201 | 199 | ); |
| 202 | 200 | } |
| 203 | 201 | if (isset($value['beg']) && isset($value['end'])) { |
@@ -208,8 +206,7 @@ discard block |
||
| 208 | 206 | $this->normalizeValue($column, $value['beg']), |
| 209 | 207 | $this->normalizeValue($column, $value['end']) |
| 210 | 208 | ] |
| 211 | - ) : |
|
| 212 | - $this->where( |
|
| 209 | + ) : $this->where( |
|
| 213 | 210 | $name.' BETWEEN ? AND ?', |
| 214 | 211 | [ |
| 215 | 212 | $this->normalizeValue($column, $value['beg']), |
@@ -219,12 +216,12 @@ discard block |
||
| 219 | 216 | } |
| 220 | 217 | return $negate ? |
| 221 | 218 | $this->where( |
| 222 | - $name . ' NOT IN (??)', |
|
| 223 | - [ array_map(function ($v) use ($column) { return $this->normalizeValue($column, $v); }, $value) ] |
|
| 219 | + $name.' NOT IN (??)', |
|
| 220 | + [array_map(function($v) use ($column) { return $this->normalizeValue($column, $v); }, $value)] |
|
| 224 | 221 | ) : |
| 225 | 222 | $this->where( |
| 226 | - $name . ' IN (??)', |
|
| 227 | - [ array_map(function ($v) use ($column) { return $this->normalizeValue($column, $v); }, $value) ] |
|
| 223 | + $name.' IN (??)', |
|
| 224 | + [array_map(function($v) use ($column) { return $this->normalizeValue($column, $v); }, $value)] |
|
| 228 | 225 | ); |
| 229 | 226 | } |
| 230 | 227 | /** |
@@ -235,7 +232,7 @@ discard block |
||
| 235 | 232 | */ |
| 236 | 233 | public function sort(string $column, bool $desc = false) : TableQuery |
| 237 | 234 | { |
| 238 | - return $this->order($this->getColumn($column)['name'] . ' ' . ($desc ? 'DESC' : 'ASC')); |
|
| 235 | + return $this->order($this->getColumn($column)['name'].' '.($desc ? 'DESC' : 'ASC')); |
|
| 239 | 236 | } |
| 240 | 237 | /** |
| 241 | 238 | * Group by a column (or columns) |
@@ -245,7 +242,7 @@ discard block |
||
| 245 | 242 | public function group($column) : TableQuery |
| 246 | 243 | { |
| 247 | 244 | if (!is_array($column)) { |
| 248 | - $column = [ $column ]; |
|
| 245 | + $column = [$column]; |
|
| 249 | 246 | } |
| 250 | 247 | foreach ($column as $k => $v) { |
| 251 | 248 | $column[$k] = $this->getColumn($v)['name']; |
@@ -286,7 +283,7 @@ discard block |
||
| 286 | 283 | $this->withr = []; |
| 287 | 284 | $this->order = []; |
| 288 | 285 | $this->having = []; |
| 289 | - $this->li_of = [0,0]; |
|
| 286 | + $this->li_of = [0, 0]; |
|
| 290 | 287 | $this->qiterator = null; |
| 291 | 288 | return $this; |
| 292 | 289 | } |
@@ -299,7 +296,7 @@ discard block |
||
| 299 | 296 | public function groupBy(string $sql, array $params = []) : TableQuery |
| 300 | 297 | { |
| 301 | 298 | $this->qiterator = null; |
| 302 | - $this->group = [ $sql, $params ]; |
|
| 299 | + $this->group = [$sql, $params]; |
|
| 303 | 300 | return $this; |
| 304 | 301 | } |
| 305 | 302 | /** |
@@ -312,7 +309,7 @@ discard block |
||
| 312 | 309 | */ |
| 313 | 310 | public function join($table, array $fields, string $name = null, bool $multiple = true) |
| 314 | 311 | { |
| 315 | - $table = $table instanceof Table ? $table : $this->db->definition((string)$table); |
|
| 312 | + $table = $table instanceof Table ? $table : $this->db->definition((string) $table); |
|
| 316 | 313 | $name = $name ?? $table->getName(); |
| 317 | 314 | if (isset($this->joins[$name]) || $this->definition->hasRelation($name)) { |
| 318 | 315 | throw new DBException('Alias / table name already in use'); |
@@ -321,7 +318,7 @@ discard block |
||
| 321 | 318 | foreach ($fields as $k => $v) { |
| 322 | 319 | $k = explode('.', $k, 2); |
| 323 | 320 | $k = count($k) == 2 ? $k[1] : $k[0]; |
| 324 | - $this->joins[$name]->keymap[$this->getColumn($name . '.' . $k)['name']] = $this->getColumn($v)['name']; |
|
| 321 | + $this->joins[$name]->keymap[$this->getColumn($name.'.'.$k)['name']] = $this->getColumn($v)['name']; |
|
| 325 | 322 | } |
| 326 | 323 | return $this; |
| 327 | 324 | } |
@@ -334,7 +331,7 @@ discard block |
||
| 334 | 331 | public function where(string $sql, array $params = []) : TableQuery |
| 335 | 332 | { |
| 336 | 333 | $this->qiterator = null; |
| 337 | - $this->where[] = [ $sql, $params ]; |
|
| 334 | + $this->where[] = [$sql, $params]; |
|
| 338 | 335 | return $this; |
| 339 | 336 | } |
| 340 | 337 | /** |
@@ -346,7 +343,7 @@ discard block |
||
| 346 | 343 | public function having(string $sql, array $params = []) : TableQuery |
| 347 | 344 | { |
| 348 | 345 | $this->qiterator = null; |
| 349 | - $this->having[] = [ $sql, $params ]; |
|
| 346 | + $this->having[] = [$sql, $params]; |
|
| 350 | 347 | return $this; |
| 351 | 348 | } |
| 352 | 349 | /** |
@@ -358,7 +355,7 @@ discard block |
||
| 358 | 355 | public function order(string $sql, array $params = []) : TableQuery |
| 359 | 356 | { |
| 360 | 357 | $this->qiterator = null; |
| 361 | - $this->order = [ $sql, $params ]; |
|
| 358 | + $this->order = [$sql, $params]; |
|
| 362 | 359 | return $this; |
| 363 | 360 | } |
| 364 | 361 | /** |
@@ -370,7 +367,7 @@ discard block |
||
| 370 | 367 | public function limit(int $limit, int $offset = 0) : TableQuery |
| 371 | 368 | { |
| 372 | 369 | $this->qiterator = null; |
| 373 | - $this->li_of = [ $limit, $offset ]; |
|
| 370 | + $this->li_of = [$limit, $offset]; |
|
| 374 | 371 | return $this; |
| 375 | 372 | } |
| 376 | 373 | /** |
@@ -387,22 +384,22 @@ discard block |
||
| 387 | 384 | $relations = $this->withr; |
| 388 | 385 | foreach ($this->definition->getRelations() as $k => $v) { |
| 389 | 386 | foreach ($this->where as $vv) { |
| 390 | - if (strpos($vv[0], $k . '.') !== false) { |
|
| 391 | - $relations[$k] = [ $v, $table ]; |
|
| 387 | + if (strpos($vv[0], $k.'.') !== false) { |
|
| 388 | + $relations[$k] = [$v, $table]; |
|
| 392 | 389 | } |
| 393 | 390 | } |
| 394 | - if (isset($this->order[0]) && strpos($this->order[0], $k . '.') !== false) { |
|
| 395 | - $relations[$k] = [ $v, $table ]; |
|
| 391 | + if (isset($this->order[0]) && strpos($this->order[0], $k.'.') !== false) { |
|
| 392 | + $relations[$k] = [$v, $table]; |
|
| 396 | 393 | } |
| 397 | 394 | } |
| 398 | 395 | |
| 399 | 396 | foreach ($this->joins as $k => $v) { |
| 400 | - $sql .= ($v->many ? 'LEFT ' : '' ) . 'JOIN '.$v->table->getName().' '.$k.' ON '; |
|
| 397 | + $sql .= ($v->many ? 'LEFT ' : '').'JOIN '.$v->table->getName().' '.$k.' ON '; |
|
| 401 | 398 | $tmp = []; |
| 402 | 399 | foreach ($v->keymap as $kk => $vv) { |
| 403 | 400 | $tmp[] = $kk.' = '.$vv; |
| 404 | 401 | } |
| 405 | - $sql .= implode(' AND ', $tmp) . ' '; |
|
| 402 | + $sql .= implode(' AND ', $tmp).' '; |
|
| 406 | 403 | } |
| 407 | 404 | foreach ($relations as $k => $v) { |
| 408 | 405 | $table = $v[1]; |
@@ -413,13 +410,13 @@ discard block |
||
| 413 | 410 | foreach ($v->keymap as $kk => $vv) { |
| 414 | 411 | $tmp[] = $table.'.'.$kk.' = '.$k.'_pivot.'.$vv.' '; |
| 415 | 412 | } |
| 416 | - $sql .= implode(' AND ', $tmp) . ' '; |
|
| 413 | + $sql .= implode(' AND ', $tmp).' '; |
|
| 417 | 414 | $sql .= 'LEFT JOIN '.$v->table->getName().' '.$k.' ON '; |
| 418 | 415 | $tmp = []; |
| 419 | 416 | foreach ($v->pivot_keymap as $kk => $vv) { |
| 420 | 417 | $tmp[] = $k.'.'.$vv.' = '.$k.'_pivot.'.$kk.' '; |
| 421 | 418 | } |
| 422 | - $sql .= implode(' AND ', $tmp) . ' '; |
|
| 419 | + $sql .= implode(' AND ', $tmp).' '; |
|
| 423 | 420 | } else { |
| 424 | 421 | $sql .= 'LEFT JOIN '.$v->table->getName().' '.$k.' ON '; |
| 425 | 422 | $tmp = []; |
@@ -427,30 +424,30 @@ discard block |
||
| 427 | 424 | $tmp[] = $table.'.'.$kk.' = '.$k.'.'.$vv.' '; |
| 428 | 425 | } |
| 429 | 426 | if ($v->sql) { |
| 430 | - $tmp[] = $v->sql . ' '; |
|
| 427 | + $tmp[] = $v->sql.' '; |
|
| 431 | 428 | $par = array_merge($par, $v->par ?? []); |
| 432 | 429 | } |
| 433 | - $sql .= implode(' AND ', $tmp) . ' '; |
|
| 430 | + $sql .= implode(' AND ', $tmp).' '; |
|
| 434 | 431 | } |
| 435 | 432 | } |
| 436 | 433 | if (count($this->where)) { |
| 437 | 434 | $sql .= 'WHERE '; |
| 438 | 435 | $tmp = []; |
| 439 | 436 | foreach ($this->where as $v) { |
| 440 | - $tmp[] = '(' . $v[0] . ')'; |
|
| 437 | + $tmp[] = '('.$v[0].')'; |
|
| 441 | 438 | $par = array_merge($par, $v[1]); |
| 442 | 439 | } |
| 443 | 440 | $sql .= implode(' AND ', $tmp).' '; |
| 444 | 441 | } |
| 445 | 442 | if (count($this->group)) { |
| 446 | - $sql .= 'GROUP BY ' . $this->group[0] . ' '; |
|
| 443 | + $sql .= 'GROUP BY '.$this->group[0].' '; |
|
| 447 | 444 | $par = array_merge($par, $this->group[1]); |
| 448 | 445 | } |
| 449 | 446 | if (count($this->having)) { |
| 450 | 447 | $sql .= 'HAVING '; |
| 451 | 448 | $tmp = []; |
| 452 | 449 | foreach ($this->having as $v) { |
| 453 | - $tmp[] = '(' . $v[0] . ')'; |
|
| 450 | + $tmp[] = '('.$v[0].')'; |
|
| 454 | 451 | $par = array_merge($par, $v[1]); |
| 455 | 452 | } |
| 456 | 453 | $sql .= implode(' AND ', $tmp).' '; |
@@ -486,7 +483,7 @@ discard block |
||
| 486 | 483 | $this->with(implode('.', $temp)); |
| 487 | 484 | $table = array_reduce( |
| 488 | 485 | $temp, |
| 489 | - function ($carry, $item) use (&$table) { |
|
| 486 | + function($carry, $item) use (&$table) { |
|
| 490 | 487 | return $table->getRelation($item)->table; |
| 491 | 488 | } |
| 492 | 489 | ); |
@@ -495,7 +492,7 @@ discard block |
||
| 495 | 492 | } |
| 496 | 493 | unset($fields[$k]); |
| 497 | 494 | foreach ($cols as $col) { |
| 498 | - $fields[] = $table . '.' . $col; |
|
| 495 | + $fields[] = $table.'.'.$col; |
|
| 499 | 496 | } |
| 500 | 497 | } |
| 501 | 498 | } |
@@ -534,37 +531,37 @@ discard block |
||
| 534 | 531 | $relations = $this->withr; |
| 535 | 532 | foreach ($this->definition->getRelations() as $k => $relation) { |
| 536 | 533 | foreach ($this->fields as $field) { |
| 537 | - if (strpos($field, $k . '.') === 0) { |
|
| 538 | - $relations[$k] = [ $relation, $table ]; |
|
| 534 | + if (strpos($field, $k.'.') === 0) { |
|
| 535 | + $relations[$k] = [$relation, $table]; |
|
| 539 | 536 | } |
| 540 | 537 | } |
| 541 | 538 | foreach ($this->where as $v) { |
| 542 | - if (strpos($v[0], $k . '.') !== false) { |
|
| 543 | - $relations[$k] = [ $relation, $table ]; |
|
| 539 | + if (strpos($v[0], $k.'.') !== false) { |
|
| 540 | + $relations[$k] = [$relation, $table]; |
|
| 544 | 541 | } |
| 545 | 542 | } |
| 546 | - if (isset($this->order[0]) && strpos($this->order[0], $k . '.') !== false) { |
|
| 547 | - $relations[$k] = [ $relation, $table ]; |
|
| 543 | + if (isset($this->order[0]) && strpos($this->order[0], $k.'.') !== false) { |
|
| 544 | + $relations[$k] = [$relation, $table]; |
|
| 548 | 545 | } |
| 549 | 546 | } |
| 550 | 547 | $select = []; |
| 551 | 548 | foreach ($this->fields as $k => $field) { |
| 552 | - $select[] = $field . (!is_numeric($k) ? ' ' . $k : ''); |
|
| 549 | + $select[] = $field.(!is_numeric($k) ? ' '.$k : ''); |
|
| 553 | 550 | } |
| 554 | 551 | foreach ($this->withr as $name => $relation) { |
| 555 | 552 | foreach ($relation[0]->table->getColumns() as $column) { |
| 556 | - $select[] = $name . '.' . $column . ' ' . $name . static::SEP . $column; |
|
| 553 | + $select[] = $name.'.'.$column.' '.$name.static::SEP.$column; |
|
| 557 | 554 | } |
| 558 | 555 | } |
| 559 | 556 | $sql = 'SELECT '.implode(', ', $select).' FROM '.$table.' '; |
| 560 | 557 | $par = []; |
| 561 | 558 | foreach ($this->joins as $k => $v) { |
| 562 | - $sql .= ($v->many ? 'LEFT ' : '' ) . 'JOIN '.$v->table->getName().' '.$k.' ON '; |
|
| 559 | + $sql .= ($v->many ? 'LEFT ' : '').'JOIN '.$v->table->getName().' '.$k.' ON '; |
|
| 563 | 560 | $tmp = []; |
| 564 | 561 | foreach ($v->keymap as $kk => $vv) { |
| 565 | 562 | $tmp[] = $kk.' = '.$vv; |
| 566 | 563 | } |
| 567 | - $sql .= implode(' AND ', $tmp) . ' '; |
|
| 564 | + $sql .= implode(' AND ', $tmp).' '; |
|
| 568 | 565 | } |
| 569 | 566 | foreach ($relations as $relation => $v) { |
| 570 | 567 | $table = $v[1]; |
@@ -575,13 +572,13 @@ discard block |
||
| 575 | 572 | foreach ($v->keymap as $kk => $vv) { |
| 576 | 573 | $tmp[] = $table.'.'.$kk.' = '.$relation.'_pivot.'.$vv.' '; |
| 577 | 574 | } |
| 578 | - $sql .= implode(' AND ', $tmp) . ' '; |
|
| 575 | + $sql .= implode(' AND ', $tmp).' '; |
|
| 579 | 576 | $sql .= 'LEFT JOIN '.$v->table->getName().' '.$relation.' ON '; |
| 580 | 577 | $tmp = []; |
| 581 | 578 | foreach ($v->pivot_keymap as $kk => $vv) { |
| 582 | 579 | $tmp[] = $relation.'.'.$vv.' = '.$relation.'_pivot.'.$kk.' '; |
| 583 | 580 | } |
| 584 | - $sql .= implode(' AND ', $tmp) . ' '; |
|
| 581 | + $sql .= implode(' AND ', $tmp).' '; |
|
| 585 | 582 | } else { |
| 586 | 583 | $sql .= 'LEFT JOIN '.$v->table->getName().' '.$relation.' ON '; |
| 587 | 584 | $tmp = []; |
@@ -589,65 +586,65 @@ discard block |
||
| 589 | 586 | $tmp[] = $table.'.'.$kk.' = '.$relation.'.'.$vv.' '; |
| 590 | 587 | } |
| 591 | 588 | if ($v->sql) { |
| 592 | - $tmp[] = $v->sql . ' '; |
|
| 589 | + $tmp[] = $v->sql.' '; |
|
| 593 | 590 | $par = array_merge($par, $v->par ?? []); |
| 594 | 591 | } |
| 595 | - $sql .= implode(' AND ', $tmp) . ' '; |
|
| 592 | + $sql .= implode(' AND ', $tmp).' '; |
|
| 596 | 593 | } |
| 597 | 594 | } |
| 598 | 595 | if (count($this->where)) { |
| 599 | 596 | $sql .= 'WHERE '; |
| 600 | 597 | $tmp = []; |
| 601 | 598 | foreach ($this->where as $v) { |
| 602 | - $tmp[] = '(' . $v[0] . ')'; |
|
| 599 | + $tmp[] = '('.$v[0].')'; |
|
| 603 | 600 | $par = array_merge($par, $v[1]); |
| 604 | 601 | } |
| 605 | 602 | $sql .= implode(' AND ', $tmp).' '; |
| 606 | 603 | } |
| 607 | 604 | if (count($this->group)) { |
| 608 | - $sql .= 'GROUP BY ' . $this->group[0] . ' '; |
|
| 605 | + $sql .= 'GROUP BY '.$this->group[0].' '; |
|
| 609 | 606 | $par = array_merge($par, $this->group[1]); |
| 610 | 607 | } |
| 611 | 608 | if (count($this->having)) { |
| 612 | 609 | $sql .= 'HAVING '; |
| 613 | 610 | $tmp = []; |
| 614 | 611 | foreach ($this->having as $v) { |
| 615 | - $tmp[] = '(' . $v[0] . ')'; |
|
| 612 | + $tmp[] = '('.$v[0].')'; |
|
| 616 | 613 | $par = array_merge($par, $v[1]); |
| 617 | 614 | } |
| 618 | 615 | $sql .= implode(' AND ', $tmp).' '; |
| 619 | 616 | } |
| 620 | 617 | if (count($this->order)) { |
| 621 | - $sql .= 'ORDER BY ' . $this->order[0] . ' '; |
|
| 618 | + $sql .= 'ORDER BY '.$this->order[0].' '; |
|
| 622 | 619 | $par = array_merge($par, $this->order[1]); |
| 623 | 620 | } |
| 624 | 621 | $porder = []; |
| 625 | 622 | foreach ($primary as $field) { |
| 626 | 623 | $porder[] = $this->getColumn($field)['name']; |
| 627 | 624 | } |
| 628 | - $sql .= (count($this->order) ? ', ' : 'ORDER BY ') . implode(', ', $porder) . ' '; |
|
| 625 | + $sql .= (count($this->order) ? ', ' : 'ORDER BY ').implode(', ', $porder).' '; |
|
| 629 | 626 | |
| 630 | 627 | if ($this->li_of[0]) { |
| 631 | 628 | if ($this->db->driver() === 'oracle') { |
| 632 | - if ((int)($this->db->settings()->options['version'] ?? 0) >= 12) { |
|
| 633 | - $sql .= 'OFFSET ' . $this->li_of[1] . ' ROWS FETCH NEXT ' . $this->li_of[0] . ' ROWS ONLY'; |
|
| 629 | + if ((int) ($this->db->settings()->options['version'] ?? 0) >= 12) { |
|
| 630 | + $sql .= 'OFFSET '.$this->li_of[1].' ROWS FETCH NEXT '.$this->li_of[0].' ROWS ONLY'; |
|
| 634 | 631 | } else { |
| 635 | - $f = array_map(function ($v) { |
|
| 632 | + $f = array_map(function($v) { |
|
| 636 | 633 | $v = explode(' ', trim($v), 2); |
| 637 | 634 | if (count($v) === 2) { return $v[1]; } |
| 638 | 635 | $v = explode('.', $v[0], 2); |
| 639 | 636 | return count($v) === 2 ? $v[1] : $v[0]; |
| 640 | 637 | }, $select); |
| 641 | - $sql = "SELECT " . implode(', ', $f) . " |
|
| 638 | + $sql = "SELECT ".implode(', ', $f)." |
|
| 642 | 639 | FROM ( |
| 643 | 640 | SELECT tbl__.*, rownum rnum__ FROM ( |
| 644 | - " . $sql . " |
|
| 641 | + " . $sql." |
|
| 645 | 642 | ) tbl__ |
| 646 | - WHERE rownum <= " . ($this->li_of[0] + $this->li_of[1]) . " |
|
| 643 | + WHERE rownum <= " . ($this->li_of[0] + $this->li_of[1])." |
|
| 647 | 644 | ) WHERE rnum__ > " . $this->li_of[1]; |
| 648 | 645 | } |
| 649 | 646 | } else { |
| 650 | - $sql .= 'LIMIT ' . $this->li_of[0] . ' OFFSET ' . $this->li_of[1]; |
|
| 647 | + $sql .= 'LIMIT '.$this->li_of[0].' OFFSET '.$this->li_of[1]; |
|
| 651 | 648 | } |
| 652 | 649 | } |
| 653 | 650 | return $this->qiterator = new TableQueryIterator( |
@@ -692,7 +689,7 @@ discard block |
||
| 692 | 689 | $ret[$k] = str_repeat(' ', 255); |
| 693 | 690 | $par[] = &$ret[$k]; |
| 694 | 691 | } |
| 695 | - $sql .= ' RETURNING ' . implode(',', $primary) . ' INTO ' . implode(',', array_fill(0, count($primary), '?')); |
|
| 692 | + $sql .= ' RETURNING '.implode(',', $primary).' INTO '.implode(',', array_fill(0, count($primary), '?')); |
|
| 696 | 693 | $this->db->query($sql, $par); |
| 697 | 694 | return $ret; |
| 698 | 695 | } else { |
@@ -724,7 +721,7 @@ discard block |
||
| 724 | 721 | } |
| 725 | 722 | $sql = 'UPDATE '.$table.' SET '; |
| 726 | 723 | $par = []; |
| 727 | - $sql .= implode(', ', array_map(function ($v) { return $v . ' = ?'; }, array_keys($update))) . ' '; |
|
| 724 | + $sql .= implode(', ', array_map(function($v) { return $v.' = ?'; }, array_keys($update))).' '; |
|
| 728 | 725 | $par = array_merge($par, array_values($update)); |
| 729 | 726 | if (count($this->where)) { |
| 730 | 727 | $sql .= 'WHERE '; |
@@ -733,7 +730,7 @@ discard block |
||
| 733 | 730 | $tmp[] = $v[0]; |
| 734 | 731 | $par = array_merge($par, $v[1]); |
| 735 | 732 | } |
| 736 | - $sql .= implode(' AND ', $tmp) . ' '; |
|
| 733 | + $sql .= implode(' AND ', $tmp).' '; |
|
| 737 | 734 | } |
| 738 | 735 | if (count($this->order)) { |
| 739 | 736 | $sql .= $this->order[0]; |
@@ -757,7 +754,7 @@ discard block |
||
| 757 | 754 | $tmp[] = $v[0]; |
| 758 | 755 | $par = array_merge($par, $v[1]); |
| 759 | 756 | } |
| 760 | - $sql .= implode(' AND ', $tmp) . ' '; |
|
| 757 | + $sql .= implode(' AND ', $tmp).' '; |
|
| 761 | 758 | } |
| 762 | 759 | if (count($this->order)) { |
| 763 | 760 | $sql .= $this->order[0]; |
@@ -777,13 +774,13 @@ discard block |
||
| 777 | 774 | $table = $this->definition; |
| 778 | 775 | array_reduce( |
| 779 | 776 | $parts, |
| 780 | - function ($carry, $item) use (&$table) { |
|
| 777 | + function($carry, $item) use (&$table) { |
|
| 781 | 778 | $relation = $table->getRelation($item); |
| 782 | 779 | if (!$relation) { |
| 783 | 780 | throw new DBException('Invalid relation name'); |
| 784 | 781 | } |
| 785 | - $name = $carry ? $carry . static::SEP . $item : $item; |
|
| 786 | - $this->withr[$name] = [ $relation, $carry ?? $table->getName() ]; |
|
| 782 | + $name = $carry ? $carry.static::SEP.$item : $item; |
|
| 783 | + $this->withr[$name] = [$relation, $carry ?? $table->getName()]; |
|
| 787 | 784 | $table = $relation->table; |
| 788 | 785 | return $name; |
| 789 | 786 | } |
@@ -23,8 +23,7 @@ |
||
| 23 | 23 | $data = array(); |
| 24 | 24 | } |
| 25 | 25 | $temp = (is_array($data) && count($data)) ? |
| 26 | - \pg_query_params($this->driver, $this->statement, $data) : |
|
| 27 | - \pg_query_params($this->driver, $this->statement, array()); |
|
| 26 | + \pg_query_params($this->driver, $this->statement, $data) : \pg_query_params($this->driver, $this->statement, array()); |
|
| 28 | 27 | if (!$temp) { |
| 29 | 28 | throw new DBException('Could not execute query : '.\pg_last_error($this->driver).' <'.$this->statement.'>'); |
| 30 | 29 | } |
@@ -62,7 +62,7 @@ |
||
| 62 | 62 | } |
| 63 | 63 | public function next() |
| 64 | 64 | { |
| 65 | - $this->fetched ++; |
|
| 65 | + $this->fetched++; |
|
| 66 | 66 | $this->last = \pg_fetch_array($this->statement, null, \PGSQL_ASSOC); |
| 67 | 67 | } |
| 68 | 68 | public function valid() |
@@ -64,7 +64,7 @@ |
||
| 64 | 64 | } |
| 65 | 65 | public function next() |
| 66 | 66 | { |
| 67 | - $this->fetched ++; |
|
| 67 | + $this->fetched++; |
|
| 68 | 68 | $this->last = \oci_fetch_array($this->statement, \OCI_ASSOC + \OCI_RETURN_NULLS + \OCI_RETURN_LOBS); |
| 69 | 69 | } |
| 70 | 70 | public function valid() |
@@ -75,7 +75,7 @@ discard block |
||
| 75 | 75 | |
| 76 | 76 | public function begin() : bool |
| 77 | 77 | { |
| 78 | - return $this->transaction = true; |
|
| 78 | + return $this->transaction = true; |
|
| 79 | 79 | } |
| 80 | 80 | public function commit() : bool |
| 81 | 81 | { |
@@ -199,7 +199,7 @@ discard block |
||
| 199 | 199 | } |
| 200 | 200 | return $new; |
| 201 | 201 | }) |
| 202 | - as $relation |
|
| 202 | + as $relation |
|
| 203 | 203 | ) { |
| 204 | 204 | $relations[$relation['CONSTRAINT_NAME']]['table'] = $relation['TABLE_NAME']; |
| 205 | 205 | $relations[$relation['CONSTRAINT_NAME']]['keymap'][$primary[(int)$relation['POSITION']-1]] = $relation['COLUMN_NAME']; |
@@ -125,16 +125,16 @@ discard block |
||
| 125 | 125 | $columns = Collection::from($this |
| 126 | 126 | ->query( |
| 127 | 127 | "SELECT * FROM all_tab_cols WHERE table_name = ? AND owner = ?", |
| 128 | - [ strtoupper($table), $this->name() ] |
|
| 128 | + [strtoupper($table), $this->name()] |
|
| 129 | 129 | )) |
| 130 | - ->map(function ($v) { |
|
| 130 | + ->map(function($v) { |
|
| 131 | 131 | $new = []; |
| 132 | 132 | foreach ($v as $kk => $vv) { |
| 133 | 133 | $new[strtoupper($kk)] = $vv; |
| 134 | 134 | } |
| 135 | 135 | return $new; |
| 136 | 136 | }) |
| 137 | - ->mapKey(function ($v) { return $v['COLUMN_NAME']; }) |
|
| 137 | + ->mapKey(function($v) { return $v['COLUMN_NAME']; }) |
|
| 138 | 138 | ->toArray(); |
| 139 | 139 | if (!count($columns)) { |
| 140 | 140 | throw new DBException('Table not found by name'); |
@@ -144,9 +144,9 @@ discard block |
||
| 144 | 144 | ->query( |
| 145 | 145 | "SELECT constraint_name FROM all_constraints |
| 146 | 146 | WHERE table_name = ? AND constraint_type = ? AND owner = ?", |
| 147 | - [ strtoupper($table), 'P', $owner ] |
|
| 147 | + [strtoupper($table), 'P', $owner] |
|
| 148 | 148 | )) |
| 149 | - ->map(function ($v) { |
|
| 149 | + ->map(function($v) { |
|
| 150 | 150 | $new = []; |
| 151 | 151 | foreach ($v as $kk => $vv) { |
| 152 | 152 | $new[strtoupper($kk)] = $vv; |
@@ -161,9 +161,9 @@ discard block |
||
| 161 | 161 | ->query( |
| 162 | 162 | "SELECT column_name FROM all_cons_columns |
| 163 | 163 | WHERE table_name = ? AND constraint_name = ? AND owner = ?", |
| 164 | - [ strtoupper($table), $pkname, $owner ] |
|
| 164 | + [strtoupper($table), $pkname, $owner] |
|
| 165 | 165 | )) |
| 166 | - ->map(function ($v) { |
|
| 166 | + ->map(function($v) { |
|
| 167 | 167 | $new = []; |
| 168 | 168 | foreach ($v as $kk => $vv) { |
| 169 | 169 | $new[strtoupper($kk)] = $vv; |
@@ -190,9 +190,9 @@ discard block |
||
| 190 | 190 | LEFT JOIN all_cons_columns cc ON cc.OWNER = ac.OWNER AND cc.CONSTRAINT_NAME = ac.CONSTRAINT_NAME |
| 191 | 191 | WHERE ac.OWNER = ? AND ac.R_OWNER = ? AND ac.R_CONSTRAINT_NAME = ? AND ac.CONSTRAINT_TYPE = ? |
| 192 | 192 | ORDER BY cc.POSITION", |
| 193 | - [ $owner, $owner, $pkname, 'R' ] |
|
| 193 | + [$owner, $owner, $pkname, 'R'] |
|
| 194 | 194 | )) |
| 195 | - ->map(function ($v) { |
|
| 195 | + ->map(function($v) { |
|
| 196 | 196 | $new = []; |
| 197 | 197 | foreach ($v as $kk => $vv) { |
| 198 | 198 | $new[strtoupper($kk)] = $vv; |
@@ -202,7 +202,7 @@ discard block |
||
| 202 | 202 | as $relation |
| 203 | 203 | ) { |
| 204 | 204 | $relations[$relation['CONSTRAINT_NAME']]['table'] = $relation['TABLE_NAME']; |
| 205 | - $relations[$relation['CONSTRAINT_NAME']]['keymap'][$primary[(int)$relation['POSITION']-1]] = $relation['COLUMN_NAME']; |
|
| 205 | + $relations[$relation['CONSTRAINT_NAME']]['keymap'][$primary[(int) $relation['POSITION'] - 1]] = $relation['COLUMN_NAME']; |
|
| 206 | 206 | } |
| 207 | 207 | foreach ($relations as $data) { |
| 208 | 208 | $rtable = $this->table($data['table'], true); // ?? $this->addTableByName($data['table'], false); |
@@ -226,9 +226,9 @@ discard block |
||
| 226 | 226 | ac.OWNER = ? AND ac.R_OWNER = ? AND ac.TABLE_NAME = ? AND ac.CONSTRAINT_TYPE = ? AND |
| 227 | 227 | cc.COLUMN_NAME IN (??) |
| 228 | 228 | ORDER BY POSITION", |
| 229 | - [ $owner, $owner, $data['table'], 'R', $columns ] |
|
| 229 | + [$owner, $owner, $data['table'], 'R', $columns] |
|
| 230 | 230 | )) |
| 231 | - ->map(function ($v) { |
|
| 231 | + ->map(function($v) { |
|
| 232 | 232 | $new = []; |
| 233 | 233 | foreach ($v as $kk => $vv) { |
| 234 | 234 | $new[strtoupper($kk)] = $vv; |
@@ -246,9 +246,9 @@ discard block |
||
| 246 | 246 | $rcolumns = Collection::from($this |
| 247 | 247 | ->query( |
| 248 | 248 | "SELECT COLUMN_NAME FROM all_cons_columns WHERE OWNER = ? AND CONSTRAINT_NAME = ? ORDER BY POSITION", |
| 249 | - [ $owner, current($foreign['keymap']) ] |
|
| 249 | + [$owner, current($foreign['keymap'])] |
|
| 250 | 250 | )) |
| 251 | - ->map(function ($v) { |
|
| 251 | + ->map(function($v) { |
|
| 252 | 252 | $new = []; |
| 253 | 253 | foreach ($v as $kk => $vv) { |
| 254 | 254 | $new[strtoupper($kk)] = $vv; |
@@ -263,7 +263,7 @@ discard block |
||
| 263 | 263 | $relname = $foreign['table']; |
| 264 | 264 | $cntr = 1; |
| 265 | 265 | while ($definition->hasRelation($relname) || $definition->getName() == $relname) { |
| 266 | - $relname = $foreign['table'] . '_' . (++ $cntr); |
|
| 266 | + $relname = $foreign['table'].'_'.(++$cntr); |
|
| 267 | 267 | } |
| 268 | 268 | $definition->addRelation( |
| 269 | 269 | new TableRelation( |
@@ -279,7 +279,7 @@ discard block |
||
| 279 | 279 | $relname = $data['table']; |
| 280 | 280 | $cntr = 1; |
| 281 | 281 | while ($definition->hasRelation($relname) || $definition->getName() == $relname) { |
| 282 | - $relname = $data['table'] . '_' . (++ $cntr); |
|
| 282 | + $relname = $data['table'].'_'.(++$cntr); |
|
| 283 | 283 | } |
| 284 | 284 | $definition->addRelation( |
| 285 | 285 | new TableRelation( |
@@ -303,9 +303,9 @@ discard block |
||
| 303 | 303 | LEFT JOIN all_cons_columns cc ON cc.OWNER = ac.OWNER AND cc.CONSTRAINT_NAME = ac.CONSTRAINT_NAME |
| 304 | 304 | WHERE ac.OWNER = ? AND ac.R_OWNER = ? AND ac.TABLE_NAME = ? AND ac.CONSTRAINT_TYPE = ? |
| 305 | 305 | ORDER BY cc.POSITION", |
| 306 | - [ $owner, $owner, strtoupper($table), 'R' ] |
|
| 306 | + [$owner, $owner, strtoupper($table), 'R'] |
|
| 307 | 307 | )) |
| 308 | - ->map(function ($v) { |
|
| 308 | + ->map(function($v) { |
|
| 309 | 309 | $new = []; |
| 310 | 310 | foreach ($v as $kk => $vv) { |
| 311 | 311 | $new[strtoupper($kk)] = $vv; |
@@ -321,9 +321,9 @@ discard block |
||
| 321 | 321 | $rcolumns = Collection::from($this |
| 322 | 322 | ->query( |
| 323 | 323 | "SELECT COLUMN_NAME FROM all_cons_columns WHERE OWNER = ? AND CONSTRAINT_NAME = ? ORDER BY POSITION", |
| 324 | - [ $owner, current($data['keymap']) ] |
|
| 324 | + [$owner, current($data['keymap'])] |
|
| 325 | 325 | )) |
| 326 | - ->map(function ($v) { |
|
| 326 | + ->map(function($v) { |
|
| 327 | 327 | $new = []; |
| 328 | 328 | foreach ($v as $kk => $vv) { |
| 329 | 329 | $new[strtoupper($kk)] = $vv; |
@@ -338,7 +338,7 @@ discard block |
||
| 338 | 338 | $relname = $data['table']; |
| 339 | 339 | $cntr = 1; |
| 340 | 340 | while ($definition->hasRelation($relname) || $definition->getName() == $relname) { |
| 341 | - $relname = $data['table'] . '_' . (++ $cntr); |
|
| 341 | + $relname = $data['table'].'_'.(++$cntr); |
|
| 342 | 342 | } |
| 343 | 343 | $definition->addRelation( |
| 344 | 344 | new TableRelation( |
@@ -359,7 +359,7 @@ discard block |
||
| 359 | 359 | "SELECT TABLE_NAME FROM ALL_TABLES where OWNER = ?", |
| 360 | 360 | [$this->connection['name']] |
| 361 | 361 | )) |
| 362 | - ->map(function ($v) { |
|
| 362 | + ->map(function($v) { |
|
| 363 | 363 | $new = []; |
| 364 | 364 | foreach ($v as $kk => $vv) { |
| 365 | 365 | $new[strtoupper($kk)] = $vv; |
@@ -367,7 +367,7 @@ discard block |
||
| 367 | 367 | return $new; |
| 368 | 368 | }) |
| 369 | 369 | ->pluck('TABLE_NAME') |
| 370 | - ->map(function ($v) { |
|
| 370 | + ->map(function($v) { |
|
| 371 | 371 | return $this->table($v); |
| 372 | 372 | }) |
| 373 | 373 | ->toArray(); |
@@ -62,7 +62,7 @@ |
||
| 62 | 62 | } |
| 63 | 63 | public function next() |
| 64 | 64 | { |
| 65 | - $this->fetched ++; |
|
| 65 | + $this->fetched++; |
|
| 66 | 66 | $this->last = $this->statement->fetchArray(\SQLITE3_ASSOC); |
| 67 | 67 | } |
| 68 | 68 | public function valid() |
@@ -22,7 +22,7 @@ |
||
| 22 | 22 | parse_str($temp[1], $connection['opts']); |
| 23 | 23 | $connection['name'] = $temp[0]; |
| 24 | 24 | if (!is_file($connection['name']) && is_file('/'.$connection['name'])) { |
| 25 | - $connection['name'] = '/'.$connection['name']; |
|
| 25 | + $connection['name'] = '/'.$connection['name']; |
|
| 26 | 26 | } |
| 27 | 27 | $this->connection = $connection; |
| 28 | 28 | } |
@@ -29,7 +29,7 @@ discard block |
||
| 29 | 29 | if ($temp) { |
| 30 | 30 | $temp = $temp->fetch_fields(); |
| 31 | 31 | if ($temp) { |
| 32 | - $columns = array_map(function ($v) { return $v->name; }, $temp); |
|
| 32 | + $columns = array_map(function($v) { return $v->name; }, $temp); |
|
| 33 | 33 | } |
| 34 | 34 | } |
| 35 | 35 | if (count($columns)) { |
@@ -67,7 +67,7 @@ discard block |
||
| 67 | 67 | } |
| 68 | 68 | public function current() |
| 69 | 69 | { |
| 70 | - return $this->nativeDriver ? $this->last : array_map(function ($v) { return $v; }, $this->row); |
|
| 70 | + return $this->nativeDriver ? $this->last : array_map(function($v) { return $v; }, $this->row); |
|
| 71 | 71 | } |
| 72 | 72 | public function rewind() |
| 73 | 73 | { |
@@ -84,7 +84,7 @@ discard block |
||
| 84 | 84 | } |
| 85 | 85 | public function next() |
| 86 | 86 | { |
| 87 | - $this->fetched ++; |
|
| 87 | + $this->fetched++; |
|
| 88 | 88 | $this->last = $this->nativeDriver ? $this->result->fetch_assoc() : $this->statement->fetch(); |
| 89 | 89 | } |
| 90 | 90 | public function valid() |