1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace HexMakina\Crudites\Grammar\Query; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use HexMakina\Crudites\Grammar\Grammar; |
7
|
|
|
use HexMakina\Crudites\Grammar\Deck; |
8
|
|
|
|
9
|
|
|
use HexMakina\Crudites\Grammar\Clause\Clause; |
10
|
|
|
use HexMakina\Crudites\Grammar\Clause\Where; |
11
|
|
|
use HexMakina\Crudites\Grammar\Clause\Joins; |
12
|
|
|
use HexMakina\Crudites\Grammar\Clause\Join; |
13
|
|
|
use HexMakina\Crudites\Grammar\Clause\GroupBy; |
14
|
|
|
use HexMakina\Crudites\Grammar\Clause\OrderBy; |
15
|
|
|
use HexMakina\Crudites\Grammar\Clause\Limit; |
16
|
|
|
|
17
|
|
|
class Select extends Query |
18
|
|
|
{ |
19
|
|
|
// decks handle the list of columns and expressions to be selected |
20
|
|
|
private ?Deck $deck = null; |
21
|
|
|
|
22
|
|
|
public function __construct(array $selection, string $table, $table_alias = null) |
23
|
|
|
{ |
24
|
|
|
$this->table = $table; |
25
|
|
|
$this->alias = $table_alias; |
26
|
|
|
$this->selectAlso($selection); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function statement(): string |
30
|
|
|
{ |
31
|
|
|
$schema = Grammar::identifier($this->table()); |
32
|
|
|
if (!empty($this->alias())) { |
33
|
|
|
$schema .= ' ' . Grammar::identifier($this->alias()); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
$ret = sprintf('SELECT %s FROM %s', $this->deck, $schema); |
37
|
|
|
|
38
|
|
|
foreach ( |
39
|
|
|
[ |
40
|
|
|
Clause::JOINS, |
41
|
|
|
Clause::JOIN, |
42
|
|
|
Clause::WHERE, |
43
|
|
|
Clause::GROUP, |
44
|
|
|
Clause::HAVING, |
45
|
|
|
Clause::ORDER, |
46
|
|
|
Clause::LIMIT |
47
|
|
|
] as $clause |
48
|
|
|
) { |
49
|
|
|
if($this->clause($clause) === null) |
50
|
|
|
continue; |
51
|
|
|
|
52
|
|
|
$ret .= ' ' . $this->clause($clause); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
return $ret; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function tableLabel($forced_value = null) |
59
|
|
|
{ |
60
|
|
|
return $forced_value ?? $this->table_alias ?? $this->table; |
|
|
|
|
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Adds additional columns to the SELECT statement. |
65
|
|
|
* |
66
|
|
|
* @param array $setter An array of column names to be added to the SELECT statement. |
67
|
|
|
* |
68
|
|
|
* $setter = [ |
69
|
|
|
* 'column_alias' => 'column', |
70
|
|
|
* 2 => 'column', |
71
|
|
|
* 'table_column_alias' => ['table', 'column'], |
72
|
|
|
* 5 => ['table', 'column'], |
73
|
|
|
* 'function_alias' => ['GROUP_CONCAT(..)'], |
74
|
|
|
* 6 => ['GROUP_CONCAT(..)'], |
75
|
|
|
* ]; |
76
|
|
|
* |
77
|
|
|
* @return self Returns the current instance of the Select class. |
78
|
|
|
*/ |
79
|
|
|
public function selectAlso(array $setter): self |
80
|
|
|
{ |
81
|
|
|
if (empty($setter)) |
82
|
|
|
throw new \InvalidArgumentException('EMPTY_SETTER_ARRAY'); |
83
|
|
|
|
84
|
|
|
foreach ($setter as $alias => $column) { |
85
|
|
|
|
86
|
|
|
if (is_int($alias)) { |
87
|
|
|
$alias = null; |
88
|
|
|
} |
89
|
|
|
if(!isset($this->deck)){ |
90
|
|
|
$this->deck = new Deck($column, $alias); |
91
|
|
|
} else { |
92
|
|
|
$this->deck->add($column, $alias); |
|
|
|
|
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return $this; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public function where(array $predicates): Where |
100
|
|
|
{ |
101
|
|
|
$where = new Where($predicates); |
102
|
|
|
$this->add($where); |
103
|
|
|
return $where; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function join(string $table, ?string $alias=null): Join |
107
|
|
|
{ |
108
|
|
|
$join = new Join($table, $alias); |
109
|
|
|
|
110
|
|
|
if($this->clause(Clause::JOINS) === null){ |
111
|
|
|
$joins = new Joins([$join]); |
112
|
|
|
$this->add($joins); |
113
|
|
|
} |
114
|
|
|
else{ |
115
|
|
|
$this->clause(Clause::JOINS)->add($join); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
return $join; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
public function groupBy($selected): GroupBy |
122
|
|
|
{ |
123
|
|
|
$group = new GroupBy($selected); |
124
|
|
|
$this->add($group); |
125
|
|
|
return $group; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
public function orderBy($selected, string $direction): OrderBy |
129
|
|
|
{ |
130
|
|
|
$order = new OrderBy($selected, $direction); |
131
|
|
|
$this->add($order); |
132
|
|
|
return $order; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
public function limit(int $number, int $offset = 0): Limit |
136
|
|
|
{ |
137
|
|
|
$limit = new Limit($number, $offset); |
138
|
|
|
$this->add($limit); |
139
|
|
|
return $limit; |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|