1 | <?php |
||
20 | abstract class AbstractQuery |
||
21 | { |
||
22 | /** |
||
23 | * |
||
24 | * Data to be bound to the query. |
||
25 | * |
||
26 | * @var array |
||
27 | * |
||
28 | */ |
||
29 | protected $bind_values = array(); |
||
30 | |||
31 | /** |
||
32 | * |
||
33 | * The list of WHERE conditions. |
||
34 | * |
||
35 | * @var array |
||
36 | * |
||
37 | */ |
||
38 | protected $where = array(); |
||
39 | |||
40 | /** |
||
41 | * |
||
42 | * ORDER BY these columns. |
||
43 | * |
||
44 | * @var array |
||
45 | * |
||
46 | */ |
||
47 | protected $order_by = array(); |
||
48 | |||
49 | /** |
||
50 | * |
||
51 | * The list of flags. |
||
52 | * |
||
53 | * @var array |
||
54 | * |
||
55 | */ |
||
56 | protected $flags = array(); |
||
57 | |||
58 | /** |
||
59 | * |
||
60 | * A helper for quoting identifier names. |
||
61 | * |
||
62 | * @var Quoter |
||
63 | * |
||
64 | */ |
||
65 | protected $quoter; |
||
66 | |||
67 | /** |
||
68 | * |
||
69 | * Prefix to use on placeholders for "sequential" bound values; used for |
||
70 | * deconfliction when merging bound values from sub-selects, etc. |
||
71 | * |
||
72 | * @var mixed |
||
73 | * |
||
74 | */ |
||
75 | protected $seq_bind_prefix = ''; |
||
76 | |||
77 | /** |
||
78 | * |
||
79 | * Constructor. |
||
80 | * |
||
81 | * @param Quoter $quoter A helper for quoting identifier names. |
||
82 | * |
||
83 | * @param string $seq_bind_prefix A prefix for rewritten sequential-binding |
||
84 | * placeholders (@see getSeqPlaceholder()). |
||
85 | * |
||
86 | */ |
||
87 | 408 | public function __construct(Quoter $quoter, $builder, $seq_bind_prefix = '') |
|
93 | |||
94 | /** |
||
95 | * |
||
96 | * Returns the prefix for rewritten sequential-binding placeholders |
||
97 | * (@see getSeqPlaceholder()). |
||
98 | * |
||
99 | * @return string |
||
100 | * |
||
101 | */ |
||
102 | 1 | public function getSeqBindPrefix() |
|
106 | |||
107 | /** |
||
108 | * |
||
109 | * Returns this query object as an SQL statement string. |
||
110 | * |
||
111 | * @return string |
||
112 | * |
||
113 | */ |
||
114 | 243 | public function __toString() |
|
118 | |||
119 | /** |
||
120 | * |
||
121 | * Returns this query object as an SQL statement string. |
||
122 | * |
||
123 | * @return string |
||
124 | * |
||
125 | */ |
||
126 | 69 | public function getStatement() |
|
130 | |||
131 | /** |
||
132 | * |
||
133 | * Builds this query object into a string. |
||
134 | * |
||
135 | * @return string |
||
136 | * |
||
137 | */ |
||
138 | abstract protected function build(); |
||
139 | |||
140 | /** |
||
141 | * |
||
142 | * Returns the prefix to use when quoting identifier names. |
||
143 | * |
||
144 | * @return string |
||
145 | * |
||
146 | */ |
||
147 | 257 | public function getQuoteNamePrefix() |
|
151 | |||
152 | /** |
||
153 | * |
||
154 | * Returns the suffix to use when quoting identifier names. |
||
155 | * |
||
156 | * @return string |
||
157 | * |
||
158 | */ |
||
159 | 257 | public function getQuoteNameSuffix() |
|
163 | |||
164 | /** |
||
165 | * |
||
166 | * Binds multiple values to placeholders; merges with existing values. |
||
167 | * |
||
168 | * @param array $bind_values Values to bind to placeholders. |
||
169 | * |
||
170 | * @return $this |
||
171 | * |
||
172 | */ |
||
173 | 31 | public function bindValues(array $bind_values) |
|
182 | |||
183 | /** |
||
184 | * |
||
185 | * Binds a single value to the query. |
||
186 | * |
||
187 | * @param string $name The placeholder name or number. |
||
188 | * |
||
189 | * @param mixed $value The value to bind to the placeholder. |
||
190 | * |
||
191 | * @return $this |
||
192 | * |
||
193 | */ |
||
194 | 72 | public function bindValue($name, $value) |
|
199 | |||
200 | /** |
||
201 | * |
||
202 | * Gets the values to bind to placeholders. |
||
203 | * |
||
204 | * @return array |
||
205 | * |
||
206 | */ |
||
207 | 126 | public function getBindValues() |
|
211 | |||
212 | /** |
||
213 | * |
||
214 | * Reset all values bound to named placeholders. |
||
215 | * |
||
216 | * @return $this |
||
217 | * |
||
218 | */ |
||
219 | 15 | public function resetBindValues() |
|
224 | |||
225 | /** |
||
226 | * |
||
227 | * Sets or unsets specified flag. |
||
228 | * |
||
229 | * @param string $flag Flag to set or unset |
||
230 | * |
||
231 | * @param bool $enable Flag status - enabled or not (default true) |
||
232 | * |
||
233 | * @return null |
||
234 | * |
||
235 | */ |
||
236 | 43 | protected function setFlag($flag, $enable = true) |
|
244 | |||
245 | /** |
||
246 | * |
||
247 | * Returns true if the specified flag was enabled by setFlag(). |
||
248 | * |
||
249 | * @param string $flag Flag to check |
||
250 | * |
||
251 | * @return bool |
||
252 | * |
||
253 | */ |
||
254 | 10 | protected function hasFlag($flag) |
|
258 | |||
259 | /** |
||
260 | * |
||
261 | * Reset all query flags. |
||
262 | * |
||
263 | * @return $this |
||
264 | * |
||
265 | */ |
||
266 | 20 | public function resetFlags() |
|
271 | |||
272 | /** |
||
273 | * |
||
274 | * Adds a WHERE condition to the query by AND or OR. If the condition has |
||
275 | * ?-placeholders, additional arguments to the method will be bound to |
||
276 | * those placeholders sequentially. |
||
277 | * |
||
278 | * @param string $andor Add the condition using this operator, typically |
||
279 | * 'AND' or 'OR'. |
||
280 | * |
||
281 | * @param string $cond The WHERE condition. |
||
282 | * |
||
283 | * @param array ...$bind arguments to bind to placeholders |
||
284 | * |
||
285 | * @return $this |
||
286 | * |
||
287 | */ |
||
288 | 60 | protected function addWhere($andor, $cond, ...$bind) |
|
293 | |||
294 | /** |
||
295 | * |
||
296 | * Adds conditions and binds values to a clause. |
||
297 | * |
||
298 | * @param string $clause The clause to work with, typically 'where' or |
||
299 | * 'having'. |
||
300 | * |
||
301 | * @param string $andor Add the condition using this operator, typically |
||
302 | * 'AND' or 'OR'. |
||
303 | * |
||
304 | * @param string $cond The WHERE condition. |
||
305 | |||
306 | * @param array $bind arguments to bind to placeholders |
||
307 | * |
||
308 | * @return null |
||
309 | * |
||
310 | */ |
||
311 | 70 | protected function addClauseCondWithBind($clause, $andor, $cond, $bind) |
|
323 | |||
324 | /** |
||
325 | * |
||
326 | * Rebuilds a condition string, replacing sequential placeholders with |
||
327 | * named placeholders, and binding the sequential values to the named |
||
328 | * placeholders. |
||
329 | * |
||
330 | * @param string $cond The condition with sequential placeholders. |
||
331 | * |
||
332 | * @param array $bind_values The values to bind to the sequential |
||
333 | * placeholders under their named versions. |
||
334 | * |
||
335 | * @return string The rebuilt condition string. |
||
336 | * |
||
337 | */ |
||
338 | 120 | protected function rebuildCondAndBindValues($cond, array $bind_values) |
|
369 | |||
370 | /** |
||
371 | * |
||
372 | * Gets the current sequential placeholder name. |
||
373 | * |
||
374 | * @return string |
||
375 | * |
||
376 | */ |
||
377 | 75 | protected function getSeqPlaceholder() |
|
382 | |||
383 | /** |
||
384 | * |
||
385 | * Adds a column order to the query. |
||
386 | * |
||
387 | * @param array $spec The columns and direction to order by. |
||
388 | * |
||
389 | * @return $this |
||
390 | * |
||
391 | */ |
||
392 | 9 | protected function addOrderBy(array $spec) |
|
399 | } |
||
400 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: