1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the miBadger package. |
5
|
|
|
* |
6
|
|
|
* @author Michael Webbers <[email protected]> |
7
|
|
|
* @license http://opensource.org/licenses/Apache-2.0 Apache v2 License |
8
|
|
|
* @version 1.0.0 |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace miBadger\Query; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* The query class. |
15
|
|
|
* |
16
|
|
|
* @since 1.0.0 |
17
|
|
|
*/ |
18
|
|
|
class Query implements QueryInterface |
19
|
|
|
{ |
20
|
|
|
/* @var \PDO The PDO. */ |
21
|
|
|
private $pdo; |
22
|
|
|
|
23
|
|
|
/* @var array The bindings. */ |
24
|
|
|
private $bindings; |
25
|
|
|
|
26
|
|
|
/* @var QueryBuilder The query builder. */ |
27
|
|
|
private $queryBuilder; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Construct a query object with the given pdo and table. |
31
|
|
|
* |
32
|
|
|
* @param \PDO $pdo |
33
|
|
|
* @param string $table |
34
|
|
|
*/ |
35
|
21 |
|
public function __construct(\PDO $pdo, $table) |
36
|
|
|
{ |
37
|
21 |
|
$this->pdo = $pdo; |
38
|
21 |
|
$this->bindings = []; |
39
|
21 |
|
$this->queryBuilder = new QueryBuilder($table); |
40
|
21 |
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Returns a string representation of the query object. |
44
|
|
|
* |
45
|
|
|
* @return string a string representation of the query object. |
46
|
|
|
*/ |
47
|
21 |
|
public function __toString() |
48
|
|
|
{ |
49
|
21 |
|
return $this->queryBuilder->__toString(); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* {@inheritdoc} |
54
|
|
|
*/ |
55
|
17 |
|
public function select($columns = ['*']) |
56
|
|
|
{ |
57
|
17 |
|
$this->queryBuilder->select(is_array($columns) ? $columns : func_get_args()); |
58
|
|
|
|
59
|
17 |
|
return $this; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* {@inheritdoc} |
64
|
|
|
*/ |
65
|
1 |
|
public function insert(array $values) |
66
|
|
|
{ |
67
|
1 |
|
$this->queryBuilder->insert($this->replaceValuesWithBindings($values, 'insert')); |
68
|
|
|
|
69
|
1 |
|
return $this; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* {@inheritdoc} |
74
|
|
|
*/ |
75
|
1 |
|
public function update(array $values) |
76
|
|
|
{ |
77
|
1 |
|
$this->queryBuilder->update($this->replaceValuesWithBindings($values, 'update')); |
78
|
|
|
|
79
|
1 |
|
return $this; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* {@inheritdoc} |
84
|
|
|
*/ |
85
|
1 |
|
public function delete() |
86
|
|
|
{ |
87
|
1 |
|
$this->queryBuilder->delete(); |
88
|
|
|
|
89
|
1 |
|
return $this; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* {@inheritdoc} |
94
|
|
|
*/ |
95
|
1 |
|
public function join($table, $primary, $operator, $secondary) |
96
|
|
|
{ |
97
|
1 |
|
$this->queryBuilder->join($table, $primary, $operator, $secondary); |
98
|
|
|
|
99
|
1 |
|
return $this; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* {@inheritdoc} |
104
|
|
|
*/ |
105
|
1 |
|
public function leftJoin($table, $primary, $operator, $secondary) |
106
|
|
|
{ |
107
|
1 |
|
$this->queryBuilder->leftJoin($table, $primary, $operator, $secondary); |
108
|
|
|
|
109
|
1 |
|
return $this; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* {@inheritdoc} |
114
|
|
|
*/ |
115
|
1 |
|
public function rightJoin($table, $primary, $operator, $secondary) |
116
|
|
|
{ |
117
|
1 |
|
$this->queryBuilder->rightJoin($table, $primary, $operator, $secondary); |
118
|
|
|
|
119
|
1 |
|
return $this; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* {@inheritdoc} |
124
|
|
|
*/ |
125
|
1 |
|
public function crossJoin($table, $primary, $operator, $secondary) |
126
|
|
|
{ |
127
|
1 |
|
$this->queryBuilder->crossJoin($table, $primary, $operator, $secondary); |
128
|
|
|
|
129
|
1 |
|
return $this; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* {@inheritdoc} |
134
|
|
|
*/ |
135
|
7 |
|
public function where($column, $operator, $value) |
136
|
|
|
{ |
137
|
7 |
|
if ($operator == 'IN') { |
138
|
1 |
|
$this->whereIn($column, is_array($value) ? $value : [$value]); |
139
|
1 |
|
} else { |
140
|
6 |
|
$this->bindings['where'][] = $value; |
141
|
6 |
|
$this->queryBuilder->where($column, $operator, sprintf(':where%s', count($this->bindings['where']))); |
142
|
|
|
} |
143
|
|
|
|
144
|
7 |
|
return $this; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Set an additional where in condition. |
149
|
|
|
* |
150
|
|
|
* @param string $column |
151
|
|
|
* @param array $values |
152
|
|
|
* @return $this |
153
|
|
|
*/ |
154
|
1 |
|
private function whereIn($column, $values) |
155
|
|
|
{ |
156
|
1 |
|
$bindings = []; |
157
|
|
|
|
158
|
1 |
|
foreach ($values as $value) { |
159
|
1 |
|
$this->bindings['where'][] = $value; |
160
|
|
|
|
161
|
1 |
|
$bindings[] = sprintf(':where%s', count($this->bindings['where'])); |
162
|
1 |
|
} |
163
|
|
|
|
164
|
1 |
|
$this->queryBuilder->where($column, 'IN', $bindings); |
165
|
|
|
|
166
|
1 |
|
return $this; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* {@inheritdoc} |
171
|
|
|
*/ |
172
|
1 |
|
public function groupBy($column) |
173
|
|
|
{ |
174
|
1 |
|
$this->queryBuilder->groupBy($column); |
175
|
|
|
|
176
|
1 |
|
return $this; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* {@inheritdoc} |
181
|
|
|
*/ |
182
|
1 |
|
public function orderBy($column, $order = null) |
183
|
|
|
{ |
184
|
1 |
|
$this->queryBuilder->orderBy($column, $order); |
185
|
|
|
|
186
|
1 |
|
return $this; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* {@inheritdoc} |
191
|
|
|
*/ |
192
|
2 |
|
public function limit($limit) |
193
|
|
|
{ |
194
|
2 |
|
$this->bindings['limit'][] = (int) $limit; |
195
|
2 |
|
$this->queryBuilder->limit(':limit1'); |
196
|
|
|
|
197
|
2 |
|
return $this; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* {@inheritdoc} |
202
|
|
|
*/ |
203
|
1 |
|
public function offset($offset) |
204
|
|
|
{ |
205
|
1 |
|
$this->bindings['offset'][] = (int) $offset; |
206
|
1 |
|
$this->queryBuilder->offset(':offset1'); |
207
|
|
|
|
208
|
1 |
|
return $this; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* Returns the result of the executed prepared query. |
213
|
|
|
* |
214
|
|
|
* @return QueryResult the result of the executed prepared query. |
215
|
|
|
*/ |
216
|
4 |
|
public function execute() |
217
|
|
|
{ |
218
|
4 |
|
$pdoStatement = $this->pdo->prepare((string) $this); |
219
|
|
|
|
220
|
4 |
|
foreach ($this->bindings as $clause => $predicate) { |
221
|
1 |
|
foreach ($predicate as $key => $value) { |
222
|
1 |
|
if (is_bool($value)) { |
223
|
1 |
|
$type = \PDO::PARAM_BOOL; |
224
|
1 |
|
} elseif (is_null($value)) { |
225
|
1 |
|
$type = \PDO::PARAM_NULL; |
226
|
1 |
|
} elseif (is_int($value)) { |
227
|
1 |
|
$type = \PDO::PARAM_INT; |
228
|
1 |
|
} else { |
229
|
1 |
|
$type = \PDO::PARAM_STR; |
230
|
|
|
} |
231
|
|
|
|
232
|
1 |
|
$pdoStatement->bindValue(sprintf(':%s%s', $clause, $key + 1), $value, $type); |
233
|
1 |
|
} |
234
|
4 |
|
} |
235
|
|
|
|
236
|
4 |
|
$pdoStatement->execute(); |
237
|
|
|
|
238
|
4 |
|
return new QueryResult($pdoStatement); |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* Returns the values array with bindings instead of values. |
243
|
|
|
* |
244
|
|
|
* @param array $values |
245
|
|
|
* @return array the values array with bindings instead of values. |
246
|
|
|
*/ |
247
|
2 |
|
private function replaceValuesWithBindings(array $values, $clause) |
248
|
|
|
{ |
249
|
2 |
|
$result = []; |
250
|
|
|
|
251
|
2 |
|
foreach ($values as $key => $value) { |
252
|
2 |
|
$this->bindings[$clause][] = $value; |
253
|
|
|
|
254
|
2 |
|
$result[$key] = sprintf(':%s%s', $clause, count($this->bindings[$clause])); |
255
|
2 |
|
} |
256
|
|
|
|
257
|
2 |
|
return $result; |
258
|
|
|
} |
259
|
|
|
} |
260
|
|
|
|