1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the Phalcon Framework. |
5
|
|
|
* |
6
|
|
|
* (c) Phalcon Team <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE.txt |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
* |
11
|
|
|
* Implementation of this file has been influenced by AtlasPHP |
12
|
|
|
* |
13
|
|
|
* @link https://github.com/atlasphp/Atlas.Query |
14
|
|
|
* @license https://github.com/atlasphp/Atlas.Query/blob/1.x/LICENSE.md |
15
|
|
|
*/ |
16
|
|
|
|
17
|
|
|
declare(strict_types=1); |
18
|
|
|
|
19
|
|
|
namespace Phalcon\DataMapper\Query; |
20
|
|
|
|
21
|
|
|
use PDO; |
22
|
|
|
use PDOStatement; |
23
|
|
|
use Phalcon\DataMapper\Pdo\Connection; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Class AbstractQuery |
27
|
|
|
* |
28
|
|
|
* @property Bind $bind |
29
|
|
|
* @property Connection $connection |
30
|
|
|
* @property array $store |
31
|
|
|
*/ |
32
|
|
|
abstract class AbstractQuery |
33
|
|
|
{ |
34
|
|
|
/** |
35
|
|
|
* @var Bind |
36
|
|
|
*/ |
37
|
|
|
protected $bind; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var Connection |
41
|
|
|
*/ |
42
|
|
|
protected $connection; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var array |
46
|
|
|
*/ |
47
|
|
|
protected $store = []; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* AbstractQuery constructor. |
51
|
|
|
* |
52
|
|
|
* @param Connection $connection |
53
|
|
|
* @param Bind $bind |
54
|
|
|
*/ |
55
|
|
|
public function __construct(Connection $connection, Bind $bind) |
56
|
|
|
{ |
57
|
|
|
$this->bind = $bind; |
58
|
|
|
$this->connection = $connection; |
59
|
|
|
$this->store["UNION"] = []; |
60
|
|
|
|
61
|
|
|
$this->reset(); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Binds a value inline |
66
|
|
|
* |
67
|
|
|
* @param mixed $value |
68
|
|
|
* @param int $type |
69
|
|
|
* |
70
|
|
|
* @return string |
71
|
|
|
*/ |
72
|
|
|
public function bindInline($value, int $type = -1): string |
73
|
|
|
{ |
74
|
|
|
return $this->bind->bindInline($value, $type); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Binds a value - auto-detects the type if necessary |
79
|
|
|
* |
80
|
|
|
* @param string $key |
81
|
|
|
* @param mixed $value |
82
|
|
|
* @param int $type |
83
|
|
|
* |
84
|
|
|
* @return AbstractQuery |
85
|
|
|
*/ |
86
|
|
|
public function bindValue( |
87
|
|
|
string $key, |
88
|
|
|
$value, |
89
|
|
|
int $type = -1 |
90
|
|
|
): AbstractQuery { |
91
|
|
|
$this->bind->setValue($key, $value, $type); |
92
|
|
|
|
93
|
|
|
return $this; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Binds an array of values |
98
|
|
|
* |
99
|
|
|
* @param array $values |
100
|
|
|
* |
101
|
|
|
* @return AbstractQuery |
102
|
|
|
*/ |
103
|
|
|
public function bindValues(array $values): AbstractQuery |
104
|
|
|
{ |
105
|
|
|
$this->bind->setValues($values); |
106
|
|
|
|
107
|
|
|
return $this; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Returns all the bound values |
112
|
|
|
* |
113
|
|
|
* @return array |
114
|
|
|
*/ |
115
|
|
|
public function getBindValues(): array |
116
|
|
|
{ |
117
|
|
|
return $this->bind->toArray(); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Return the generated statement |
122
|
|
|
* |
123
|
|
|
* @return string |
124
|
|
|
*/ |
125
|
|
|
abstract public function getStatement(): string; |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Performs a statement in the connection |
129
|
|
|
* |
130
|
|
|
* @return PDOStatement |
131
|
|
|
*/ |
132
|
|
|
public function perform() |
133
|
|
|
{ |
134
|
|
|
return $this->connection->perform( |
135
|
|
|
$this->getStatement(), |
136
|
|
|
$this->getBindValues() |
137
|
|
|
); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Sets a flag for the query such as "DISTINCT" |
142
|
|
|
* |
143
|
|
|
* @param string $flag |
144
|
|
|
* @param bool $enable |
145
|
|
|
*/ |
146
|
|
|
public function setFlag(string $flag, bool $enable = true): void |
147
|
|
|
{ |
148
|
|
|
if ($enable) { |
149
|
|
|
$this->store["FLAGS"][$flag] = true; |
150
|
|
|
} else { |
151
|
|
|
$flags = $this->store["FLAGS"]; |
152
|
|
|
|
153
|
|
|
unset($flags[$flag]); |
154
|
|
|
|
155
|
|
|
$this->store["FLAGS"] = $flags; |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Quotes the identifier |
161
|
|
|
* |
162
|
|
|
* @param string $name |
163
|
|
|
* @param int $type |
164
|
|
|
* |
165
|
|
|
* @return string |
166
|
|
|
*/ |
167
|
|
|
public function quoteIdentifier( |
168
|
|
|
string $name, |
169
|
|
|
int $type = PDO::PARAM_STR |
170
|
|
|
): string { |
171
|
|
|
return $this->connection->quote($name, $type); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Resets the internal array |
176
|
|
|
*/ |
177
|
|
|
public function reset() |
178
|
|
|
{ |
179
|
|
|
$this->store["COLUMNS"] = []; |
180
|
|
|
$this->store["FLAGS"] = []; |
181
|
|
|
$this->store["FROM"] = []; |
182
|
|
|
$this->store["GROUP"] = []; |
183
|
|
|
$this->store["HAVING"] = []; |
184
|
|
|
$this->store["LIMIT"] = 0; |
185
|
|
|
$this->store["ORDER"] = []; |
186
|
|
|
$this->store["OFFSET"] = 0; |
187
|
|
|
$this->store["WHERE"] = []; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Builds the flags statement(s) |
192
|
|
|
* |
193
|
|
|
* @return string |
194
|
|
|
*/ |
195
|
|
|
protected function buildFlags() |
196
|
|
|
{ |
197
|
|
|
if (empty($this->store["FLAGS"])) { |
198
|
|
|
return ""; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
return " " . implode(" ", array_keys($this->store["FLAGS"])); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* Builds the `RETURNING` clause |
206
|
|
|
* |
207
|
|
|
* @return string |
208
|
|
|
*/ |
209
|
|
|
protected function buildReturning(): string |
210
|
|
|
{ |
211
|
|
|
if (empty($this->store["RETURNING"])) { |
212
|
|
|
return ""; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
return " RETURNING" . $this->indent($this->store["RETURNING"], ","); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* Indents a collection |
220
|
|
|
* |
221
|
|
|
* @param array $collection |
222
|
|
|
* @param string $glue |
223
|
|
|
* |
224
|
|
|
* @return string |
225
|
|
|
*/ |
226
|
|
|
protected function indent(array $collection, string $glue = ""): string |
227
|
|
|
{ |
228
|
|
|
if (empty($collection)) { |
229
|
|
|
return ""; |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
return " " . implode($glue . " ", $collection); |
233
|
|
|
} |
234
|
|
|
} |
235
|
|
|
|