1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace HexMakina\Crudites\Grammar\Query; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use HexMakina\Crudites\CruditesException; |
7
|
|
|
|
8
|
|
|
// use HexMakina\Crudites\Grammar\Clause\SelectFrom; |
9
|
|
|
use HexMakina\Crudites\Grammar\Deck; |
10
|
|
|
use HexMakina\Crudites\Grammar\Clause\Clause; |
11
|
|
|
use HexMakina\Crudites\Grammar\Grammar; |
12
|
|
|
|
13
|
|
|
class Select extends Query |
14
|
|
|
{ |
15
|
|
|
private ?Deck $deck = null; |
16
|
|
|
public function __construct(array $columns, string $table, $table_alias = null) |
17
|
|
|
{ |
18
|
|
|
|
19
|
|
|
$this->table = $table; |
20
|
|
|
$this->table_alias = $table_alias; |
21
|
|
|
// die('vefore selftform'); |
22
|
|
|
// $this->add(new SelectFrom($table, $table_alias)); |
23
|
|
|
// die('SELECT'); |
24
|
|
|
$this->selectAlso($columns); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function statement(): string |
28
|
|
|
{ |
29
|
|
|
if ($this->table === null) { |
30
|
|
|
throw new CruditesException('NO_TABLE'); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
$schema = Grammar::backtick($this->table); |
34
|
|
|
if (!empty($this->alias)) { |
35
|
|
|
$schema .= ' AS ' . Grammar::backtick($this->alias); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
$ret = sprintf('SELECT %s FROM %s', $this->deck, $schema); |
39
|
|
|
|
40
|
|
|
foreach ( |
41
|
|
|
[ |
42
|
|
|
Clause::JOINS, |
43
|
|
|
Clause::WHERE, |
44
|
|
|
Clause::GROUP, |
45
|
|
|
Clause::HAVING, |
46
|
|
|
Clause::ORDER, |
47
|
|
|
Clause::LIMIT |
48
|
|
|
] as $clause |
49
|
|
|
) { |
50
|
|
|
if($this->clause($clause) === null) |
51
|
|
|
continue; |
52
|
|
|
|
53
|
|
|
$ret .= PHP_EOL . $this->clause($clause); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
return $ret; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function tableLabel($forced_value = null) |
60
|
|
|
{ |
61
|
|
|
return $forced_value ?? $this->table_alias ?? $this->table; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Adds additional columns to the SELECT statement. |
66
|
|
|
* |
67
|
|
|
* @param array $setter An array of column names to be added to the SELECT statement. |
68
|
|
|
* |
69
|
|
|
* $setter = [ |
70
|
|
|
* 'column_alias' => 'column', |
71
|
|
|
* 2 => 'column', |
72
|
|
|
* 'table_column_alias' => ['table', 'column'], |
73
|
|
|
* 5 => ['table', 'column'], |
74
|
|
|
* 'function_alias' => ['GROUP_CONCAT(..)'], |
75
|
|
|
* 6 => ['GROUP_CONCAT(..)'], |
76
|
|
|
* ]; |
77
|
|
|
* |
78
|
|
|
* @return self Returns the current instance of the Select class. |
79
|
|
|
*/ |
80
|
|
|
public function selectAlso(array $setter): self |
81
|
|
|
{ |
82
|
|
|
if (empty($setter)) |
83
|
|
|
throw new \InvalidArgumentException('EMPTY_SETTER_ARRAY'); |
84
|
|
|
|
85
|
|
|
foreach ($setter as $alias => $column) { |
86
|
|
|
|
87
|
|
|
if (is_int($alias)) { |
88
|
|
|
$alias = null; |
89
|
|
|
} |
90
|
|
|
if(!isset($this->deck)){ |
91
|
|
|
$this->deck = new Deck($column, $alias); |
92
|
|
|
} else { |
93
|
|
|
$this->deck->add($column, $alias); |
|
|
|
|
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return $this; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.