1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright 2021 Aleksandar Panic |
4
|
|
|
* |
5
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
6
|
|
|
* you may not use this file except in compliance with the License. |
7
|
|
|
* You may obtain a copy of the License at |
8
|
|
|
* |
9
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
10
|
|
|
* |
11
|
|
|
* Unless required by applicable law or agreed to in writing, software |
12
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
13
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
14
|
|
|
* See the License for the specific language governing permissions and |
15
|
|
|
* limitations under the License. |
16
|
|
|
*/ |
17
|
|
|
|
18
|
|
|
namespace ArekX\PQL\Sql\Query; |
19
|
|
|
|
20
|
|
|
use ArekX\PQL\Contracts\StructuredQuery; |
21
|
|
|
use ArekX\PQL\Query; |
22
|
|
|
use ArekX\PQL\Sql\Query\Traits\ConditionTrait; |
23
|
|
|
use ArekX\PQL\Sql\Query\Traits\ConfigureTrait; |
24
|
|
|
use ArekX\PQL\Sql\Query\Traits\JoinTrait; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Class representing a select query for retrieving data from |
28
|
|
|
* database. |
29
|
|
|
*/ |
30
|
|
|
class Select extends Query |
31
|
|
|
{ |
32
|
|
|
use ConditionTrait; |
33
|
|
|
use JoinTrait; |
34
|
|
|
use ConfigureTrait; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Sets a data source from which to pull the data from |
38
|
|
|
* such as a table of data, depending on the driver. |
39
|
|
|
* |
40
|
|
|
* **SQL Injection Warning**: Value in this function is not usually escaped in the driver |
41
|
|
|
* and should not be used to pass values from the user input to it. If you need to pass |
42
|
|
|
* and escape query use Raw query. |
43
|
|
|
* |
44
|
|
|
* If the source is passed as an array then the key of this source |
45
|
|
|
* is used as an alias if the driver supports it and values is used |
46
|
|
|
* as the source name. |
47
|
|
|
* |
48
|
|
|
* If null is passed it means that this query part is not to be used. |
49
|
|
|
* |
50
|
|
|
* @param string|array|StructuredQuery|null $source Source to be passed |
51
|
|
|
* @return $this |
52
|
|
|
*/ |
53
|
1 |
|
public function from($source): self |
54
|
|
|
{ |
55
|
1 |
|
$this->use('from', $source); |
56
|
1 |
|
return $this; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Set columns to be retrieved from data source |
61
|
|
|
* |
62
|
|
|
* **SQL Injection Warning**: Value in this function is not usually escaped in the driver |
63
|
|
|
* and should not be used to pass values from the user input to it. If you need to pass |
64
|
|
|
* and escape query use Raw query. |
65
|
|
|
* |
66
|
|
|
* If columns are passed as an array then the key of the array |
67
|
|
|
* is used as an alias if the driver supports it and values is used |
68
|
|
|
* as the column name. |
69
|
|
|
* |
70
|
|
|
* If null is passed it means that this query part is not to be used. |
71
|
|
|
* |
72
|
|
|
* @param array|string|StructuredQuery|null $columns |
73
|
|
|
* @return $this |
74
|
|
|
*/ |
75
|
3 |
|
public function columns($columns) |
76
|
|
|
{ |
77
|
3 |
|
$this->use('columns', $columns); |
78
|
3 |
|
return $this; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Add a column to the columns list. |
83
|
|
|
* |
84
|
|
|
* **SQL Injection Warning**: Value in this function is not usually escaped in the driver |
85
|
|
|
* and should not be used to pass values from the user input to it. If you need to pass |
86
|
|
|
* and escape query use Raw query. |
87
|
|
|
* |
88
|
|
|
* If passed column is an array it will be merged |
89
|
|
|
* with the current column list. |
90
|
|
|
* |
91
|
|
|
* @see Select::columns() |
92
|
|
|
* @param array|string|StructuredQuery $column Column to be added |
93
|
|
|
* @return $this |
94
|
|
|
*/ |
95
|
2 |
|
public function addColumns($column) |
96
|
|
|
{ |
97
|
2 |
|
$columns = $this->get('columns'); |
98
|
|
|
|
99
|
2 |
|
if (!is_array($columns)) { |
100
|
2 |
|
$columns = [$columns]; |
101
|
|
|
} |
102
|
|
|
|
103
|
2 |
|
if (is_array($column)) { |
104
|
1 |
|
foreach ($column as $key => $value) { |
105
|
1 |
|
$columns[$key] = $value; |
106
|
|
|
} |
107
|
|
|
} else { |
108
|
1 |
|
$columns[] = $column; |
109
|
|
|
} |
110
|
|
|
|
111
|
2 |
|
$this->use('columns', $columns); |
112
|
2 |
|
return $this; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Order results by a specific value. |
117
|
|
|
* |
118
|
|
|
* **SQL Injection Warning**: Value in this function is not usually escaped in the driver |
119
|
|
|
* and should not be used to pass values from the user input to it. If you need to pass |
120
|
|
|
* and escape query use Raw query. |
121
|
|
|
* |
122
|
|
|
* If the array is passed this function expects an associative array where key is the |
123
|
|
|
* column to sort by and value is how to sort by. |
124
|
|
|
* |
125
|
|
|
* If structured query is passed it is processed as is. |
126
|
|
|
* |
127
|
|
|
* @param array|StructuredQuery|null $orderBy |
128
|
|
|
* @return $this |
129
|
|
|
*/ |
130
|
1 |
|
public function orderBy($orderBy) |
131
|
|
|
{ |
132
|
1 |
|
$this->use('orderBy', $orderBy); |
133
|
1 |
|
return $this; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Group results by specified value. |
138
|
|
|
* |
139
|
|
|
* **SQL Injection Warning**: Value in this function is not usually escaped in the driver |
140
|
|
|
* and should not be used to pass values from the user input to it. If you need to pass |
141
|
|
|
* and escape query use Raw query. |
142
|
|
|
* |
143
|
|
|
* If array is passed here, every element of the array is used as the grouping column. |
144
|
|
|
* |
145
|
|
|
* If structured query is passed it is processed as is. |
146
|
|
|
* |
147
|
|
|
* @param array|StructuredQuery|null $groupBy |
148
|
|
|
*/ |
149
|
1 |
|
public function groupBy($groupBy) |
150
|
|
|
{ |
151
|
1 |
|
$this->use('groupBy', $groupBy); |
152
|
1 |
|
return $this; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Add a HAVING condition to the query. |
158
|
|
|
* |
159
|
|
|
* Condition logic is the same as with where method. |
160
|
|
|
* |
161
|
|
|
* @param array|StructuredQuery $having |
162
|
|
|
* @return $this |
163
|
|
|
* @see ConditionTrait::where() |
164
|
|
|
*/ |
165
|
1 |
|
public function having($having) |
166
|
|
|
{ |
167
|
1 |
|
$this->use('having', $having); |
168
|
1 |
|
return $this; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Append having to the existing one by AND-ing the previous having |
173
|
|
|
* filter to the value specified in having part. |
174
|
|
|
* |
175
|
|
|
* Condition accepts the same format as where method. |
176
|
|
|
* |
177
|
|
|
* If the current condition in part is an `['and', condition]`, new condition |
178
|
|
|
* will be appended to it, otherwise the previous condition and this condition will |
179
|
|
|
* be wrapped as `['and', previous, current]` |
180
|
|
|
* |
181
|
|
|
* @param array|StructuredQuery $having |
182
|
|
|
* @return $this |
183
|
|
|
* @see ConditionTrait::appendConditionPart() |
184
|
|
|
* @see ConditionTrait::where() |
185
|
|
|
*/ |
186
|
1 |
|
public function andHaving($having) |
187
|
|
|
{ |
188
|
1 |
|
return $this->appendConditionPart('having', 'and', $having); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Append having to the existing one by OR-ing the previous having |
193
|
|
|
* filter to the value specified in having part. |
194
|
|
|
* |
195
|
|
|
* Condition accepts the same format as where method. |
196
|
|
|
* |
197
|
|
|
* If the current condition in part is an `['or', condition]`, new condition |
198
|
|
|
* will be appended to it, otherwise the previous condition and this condition will |
199
|
|
|
* be wrapped as `['or', previous, current]` |
200
|
|
|
* |
201
|
|
|
* @param array|StructuredQuery $having |
202
|
|
|
* @return $this |
203
|
|
|
* @see ConditionTrait::appendConditionPart() |
204
|
|
|
* @see ConditionTrait::where() |
205
|
|
|
*/ |
206
|
1 |
|
public function orHaving($having) |
207
|
|
|
{ |
208
|
1 |
|
return $this->appendConditionPart('having', 'or', $having); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
} |