Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like SphinxQL often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use SphinxQL, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
13 | class SphinxQL |
||
14 | { |
||
15 | /** |
||
16 | * A non-static connection for the current SphinxQL object |
||
17 | * |
||
18 | * @var ConnectionInterface |
||
19 | */ |
||
20 | protected $connection = null; |
||
21 | |||
22 | /** |
||
23 | * The last result object. |
||
24 | * |
||
25 | * @var array |
||
26 | */ |
||
27 | protected $last_result = null; |
||
28 | |||
29 | /** |
||
30 | * The last compiled query. |
||
31 | * |
||
32 | * @var string |
||
33 | */ |
||
34 | protected $last_compiled = null; |
||
35 | |||
36 | /** |
||
37 | * The last chosen method (select, insert, replace, update, delete). |
||
38 | * |
||
39 | * @var string |
||
40 | */ |
||
41 | protected $type = null; |
||
42 | |||
43 | /** |
||
44 | * An SQL query that is not yet executed or "compiled" |
||
45 | * |
||
46 | * @var string |
||
47 | */ |
||
48 | protected $query = null; |
||
49 | |||
50 | /** |
||
51 | * Array of select elements that will be comma separated. |
||
52 | * |
||
53 | * @var array |
||
54 | */ |
||
55 | protected $select = array(); |
||
56 | |||
57 | /** |
||
58 | * From in SphinxQL is the list of indexes that will be used |
||
59 | * |
||
60 | * @var array |
||
61 | */ |
||
62 | protected $from = array(); |
||
63 | |||
64 | /** |
||
65 | * The list of where and parenthesis, must be inserted in order |
||
66 | * |
||
67 | * @var array |
||
68 | */ |
||
69 | protected $where = array(); |
||
70 | |||
71 | /** |
||
72 | * The list of matches for the MATCH function in SphinxQL |
||
73 | * |
||
74 | * @var array |
||
75 | */ |
||
76 | protected $match = array(); |
||
77 | |||
78 | /** |
||
79 | * GROUP BY array to be comma separated |
||
80 | * |
||
81 | * @var array |
||
82 | */ |
||
83 | protected $group_by = array(); |
||
84 | |||
85 | /** |
||
86 | * ORDER BY array |
||
87 | * |
||
88 | * @var array |
||
89 | */ |
||
90 | protected $within_group_order_by = array(); |
||
91 | |||
92 | /** |
||
93 | * The list of where and parenthesis, must be inserted in order |
||
94 | * |
||
95 | * @var array |
||
96 | */ |
||
97 | protected $having = array(); |
||
98 | |||
99 | /** |
||
100 | * ORDER BY array |
||
101 | * |
||
102 | * @var array |
||
103 | */ |
||
104 | protected $order_by = array(); |
||
105 | |||
106 | /** |
||
107 | * When not null it adds an offset |
||
108 | * |
||
109 | * @var null|int |
||
110 | */ |
||
111 | protected $offset = null; |
||
112 | |||
113 | /** |
||
114 | * When not null it adds a limit |
||
115 | * |
||
116 | * @var null|int |
||
117 | */ |
||
118 | protected $limit = null; |
||
119 | |||
120 | /** |
||
121 | * Value of INTO query for INSERT or REPLACE |
||
122 | * |
||
123 | * @var null|string |
||
124 | */ |
||
125 | protected $into = null; |
||
126 | |||
127 | /** |
||
128 | * Array of columns for INSERT or REPLACE |
||
129 | * |
||
130 | * @var array |
||
131 | */ |
||
132 | protected $columns = array(); |
||
133 | |||
134 | /** |
||
135 | * Array OF ARRAYS of values for INSERT or REPLACE |
||
136 | * |
||
137 | * @var array |
||
138 | */ |
||
139 | protected $values = array(); |
||
140 | |||
141 | /** |
||
142 | * Array arrays containing column and value for SET in UPDATE |
||
143 | * |
||
144 | * @var array |
||
145 | */ |
||
146 | protected $set = array(); |
||
147 | |||
148 | /** |
||
149 | * Array of OPTION specific to SphinxQL |
||
150 | * |
||
151 | * @var array |
||
152 | */ |
||
153 | protected $options = array(); |
||
154 | |||
155 | /** |
||
156 | * Array of FACETs |
||
157 | * |
||
158 | * @var Facet[] |
||
159 | */ |
||
160 | protected $facets = array(); |
||
161 | |||
162 | /** |
||
163 | * The reference to the object that queued itself and created this object |
||
164 | * |
||
165 | * @var null|SphinxQL |
||
166 | */ |
||
167 | protected $queue_prev = null; |
||
168 | |||
169 | /** |
||
170 | * An array of escaped characters for escapeMatch() |
||
171 | * @var array |
||
172 | */ |
||
173 | protected $escape_full_chars = array( |
||
174 | '\\' => '\\\\', |
||
175 | '(' => '\(', |
||
176 | ')' => '\)', |
||
177 | '|' => '\|', |
||
178 | '-' => '\-', |
||
179 | '!' => '\!', |
||
180 | '@' => '\@', |
||
181 | '~' => '\~', |
||
182 | '"' => '\"', |
||
183 | '&' => '\&', |
||
184 | '/' => '\/', |
||
185 | '^' => '\^', |
||
186 | '$' => '\$', |
||
187 | '=' => '\=', |
||
188 | '<' => '\<', |
||
189 | ); |
||
190 | |||
191 | /** |
||
192 | * An array of escaped characters for fullEscapeMatch() |
||
193 | * @var array |
||
194 | */ |
||
195 | protected $escape_half_chars = array( |
||
196 | '\\' => '\\\\', |
||
197 | '(' => '\(', |
||
198 | ')' => '\)', |
||
199 | '!' => '\!', |
||
200 | '@' => '\@', |
||
201 | '~' => '\~', |
||
202 | '&' => '\&', |
||
203 | '/' => '\/', |
||
204 | '^' => '\^', |
||
205 | '$' => '\$', |
||
206 | '=' => '\=', |
||
207 | '<' => '\<', |
||
208 | ); |
||
209 | |||
210 | public function __construct(ConnectionInterface $connection = null, $static = false) |
||
|
|||
211 | { |
||
212 | $this->connection = $connection; |
||
213 | } |
||
214 | |||
215 | /** |
||
216 | * Creates and setups a SphinxQL object |
||
217 | * |
||
218 | * @param ConnectionInterface $connection |
||
219 | * |
||
220 | * @return SphinxQL |
||
221 | */ |
||
222 | public static function create(ConnectionInterface $connection) |
||
226 | |||
227 | /** |
||
228 | * Returns the currently attached connection |
||
229 | * |
||
230 | * @returns ConnectionInterface |
||
231 | */ |
||
232 | public function getConnection() |
||
236 | |||
237 | /** |
||
238 | * Avoids having the expressions escaped |
||
239 | * |
||
240 | * Examples: |
||
241 | * $query->where('time', '>', SphinxQL::expr('CURRENT_TIMESTAMP')); |
||
242 | * // WHERE time > CURRENT_TIMESTAMP |
||
243 | * |
||
244 | * @param string $string The string to keep unaltered |
||
245 | * |
||
246 | * @return Expression The new Expression |
||
247 | */ |
||
248 | public static function expr($string = '') |
||
252 | |||
253 | /** |
||
254 | * Runs the query built |
||
255 | * |
||
256 | * @return ResultSetInterface The result of the query |
||
257 | */ |
||
258 | public function execute() |
||
259 | { |
||
260 | // pass the object so execute compiles it by itself |
||
261 | return $this->last_result = $this->getConnection()->query($this->compile()->getCompiled()); |
||
262 | } |
||
263 | |||
264 | /** |
||
265 | * Executes a batch of queued queries |
||
266 | * |
||
267 | * @return MultiResultSetInterface The array of results |
||
268 | * @throws SphinxQLException In case no query is in queue |
||
269 | */ |
||
270 | public function executeBatch() |
||
271 | { |
||
272 | if (count($this->getQueue()) == 0) { |
||
273 | throw new SphinxQLException('There is no Queue present to execute.'); |
||
274 | } |
||
275 | |||
276 | $queue = array(); |
||
277 | |||
278 | foreach ($this->getQueue() as $query) { |
||
279 | $queue[] = $query->compile()->getCompiled(); |
||
280 | } |
||
281 | |||
282 | return $this->last_result = $this->getConnection()->multiQuery($queue); |
||
283 | } |
||
284 | |||
285 | /** |
||
286 | * Enqueues the current object and returns a new one or the supplied one |
||
287 | * |
||
288 | * @param SphinxQL|null $next |
||
289 | * |
||
290 | * @return SphinxQL A new SphinxQL object with the current object referenced |
||
291 | */ |
||
292 | public function enqueue(SphinxQL $next = null) |
||
293 | { |
||
294 | if ($next === null) { |
||
295 | $next = new static($this->getConnection()); |
||
296 | } |
||
297 | |||
298 | $next->setQueuePrev($this); |
||
299 | |||
300 | return $next; |
||
301 | } |
||
302 | |||
303 | /** |
||
304 | * Returns the ordered array of enqueued objects |
||
305 | * |
||
306 | * @return SphinxQL[] The ordered array of enqueued objects |
||
307 | */ |
||
308 | public function getQueue() |
||
309 | { |
||
310 | $queue = array(); |
||
311 | $curr = $this; |
||
312 | |||
313 | do { |
||
314 | if ($curr->type != null) { |
||
315 | $queue[] = $curr; |
||
316 | } |
||
317 | } while ($curr = $curr->getQueuePrev()); |
||
318 | |||
319 | return array_reverse($queue); |
||
320 | } |
||
321 | |||
322 | /** |
||
323 | * Gets the enqueued object |
||
324 | * |
||
325 | * @return SphinxQL|null |
||
326 | */ |
||
327 | public function getQueuePrev() |
||
328 | { |
||
329 | return $this->queue_prev; |
||
330 | } |
||
331 | |||
332 | /** |
||
333 | * Sets the reference to the enqueued object |
||
334 | * |
||
335 | * @param SphinxQL $query The object to set as previous |
||
336 | * |
||
337 | * @return SphinxQL |
||
338 | */ |
||
339 | public function setQueuePrev($query) |
||
340 | { |
||
341 | $this->queue_prev = $query; |
||
342 | |||
343 | return $this; |
||
344 | } |
||
345 | |||
346 | /** |
||
347 | * Returns the result of the last query |
||
348 | * |
||
349 | * @return array The result of the last query |
||
350 | */ |
||
351 | public function getResult() |
||
355 | |||
356 | /** |
||
357 | * Returns the latest compiled query |
||
358 | * |
||
359 | * @return string The last compiled query |
||
360 | */ |
||
361 | public function getCompiled() |
||
365 | |||
366 | /** |
||
367 | * Begins transaction |
||
368 | */ |
||
369 | public function transactionBegin() |
||
373 | |||
374 | /** |
||
375 | * Commits transaction |
||
376 | */ |
||
377 | public function transactionCommit() |
||
381 | |||
382 | /** |
||
383 | * Rollbacks transaction |
||
384 | */ |
||
385 | public function transactionRollback() |
||
389 | |||
390 | /** |
||
391 | * Runs the compile function |
||
392 | * |
||
393 | * @return SphinxQL |
||
394 | */ |
||
395 | public function compile() |
||
396 | { |
||
397 | switch ($this->type) { |
||
398 | case 'select': |
||
399 | $this->compileSelect(); |
||
400 | break; |
||
401 | case 'insert': |
||
402 | case 'replace': |
||
403 | $this->compileInsert(); |
||
404 | break; |
||
405 | case 'update': |
||
406 | $this->compileUpdate(); |
||
407 | break; |
||
408 | case 'delete': |
||
409 | $this->compileDelete(); |
||
410 | break; |
||
411 | case 'query': |
||
412 | $this->compileQuery(); |
||
413 | break; |
||
414 | } |
||
415 | |||
416 | return $this; |
||
417 | } |
||
418 | |||
419 | public function compileQuery() |
||
420 | { |
||
421 | $this->last_compiled = $this->query; |
||
422 | |||
423 | return $this; |
||
424 | } |
||
425 | |||
426 | /** |
||
427 | * Compiles the MATCH part of the queries |
||
428 | * Used by: SELECT, DELETE, UPDATE |
||
429 | * |
||
430 | * @return string The compiled MATCH |
||
431 | */ |
||
432 | public function compileMatch() |
||
433 | { |
||
434 | $query = ''; |
||
435 | |||
436 | if (!empty($this->match)) { |
||
437 | $query .= 'WHERE MATCH('; |
||
438 | |||
439 | $matched = array(); |
||
440 | |||
441 | foreach ($this->match as $match) { |
||
442 | $pre = ''; |
||
443 | if ($match['column'] instanceof \Closure) { |
||
444 | $sub = new Match($this); |
||
445 | call_user_func($match['column'], $sub); |
||
446 | $pre .= $sub->compile()->getCompiled(); |
||
447 | } elseif ($match['column'] instanceof Match) { |
||
448 | $pre .= $match['column']->compile()->getCompiled(); |
||
449 | } elseif (empty($match['column'])) { |
||
450 | $pre .= ''; |
||
451 | } elseif (is_array($match['column'])) { |
||
452 | $pre .= '@('.implode(',', $match['column']).') '; |
||
453 | } else { |
||
454 | $pre .= '@'.$match['column'].' '; |
||
455 | } |
||
456 | |||
457 | if ($match['half']) { |
||
458 | $pre .= $this->halfEscapeMatch($match['value']); |
||
459 | } else { |
||
460 | $pre .= $this->escapeMatch($match['value']); |
||
461 | } |
||
462 | |||
463 | $matched[] = '('.$pre.')'; |
||
464 | } |
||
465 | |||
466 | $matched = implode(' ', $matched); |
||
467 | $query .= $this->getConnection()->escape(trim($matched)).') '; |
||
468 | } |
||
469 | return $query; |
||
470 | } |
||
471 | |||
472 | /** |
||
473 | * Compiles the WHERE part of the queries |
||
474 | * It interacts with the MATCH() and of course isn't usable stand-alone |
||
475 | * Used by: SELECT, DELETE, UPDATE |
||
476 | * |
||
477 | * @return string The compiled WHERE |
||
478 | */ |
||
479 | public function compileWhere() |
||
480 | { |
||
481 | $query = ''; |
||
482 | |||
483 | if (empty($this->match) && !empty($this->where)) { |
||
484 | $query .= 'WHERE '; |
||
485 | } |
||
486 | |||
487 | if (!empty($this->where)) { |
||
488 | foreach ($this->where as $key => $where) { |
||
489 | if ($key > 0 || !empty($this->match)) { |
||
490 | $query .= 'AND '; |
||
491 | } |
||
492 | $query .= $this->compileFilterCondition($where); |
||
493 | } |
||
494 | } |
||
495 | |||
496 | return $query; |
||
497 | } |
||
498 | |||
499 | public function compileFilterCondition($filter) |
||
500 | { |
||
501 | $query = ''; |
||
502 | |||
503 | if (!empty($filter)) { |
||
504 | if (strtoupper($filter['operator']) === 'BETWEEN') { |
||
505 | $query .= $this->getConnection()->quoteIdentifier($filter['column']); |
||
506 | $query .= ' BETWEEN '; |
||
507 | $query .= $this->getConnection()->quote($filter['value'][0]).' AND ' |
||
508 | .$this->getConnection()->quote($filter['value'][1]).' '; |
||
509 | } else { |
||
510 | // id can't be quoted! |
||
511 | if ($filter['column'] === 'id') { |
||
512 | $query .= 'id '; |
||
513 | } else { |
||
514 | $query .= $this->getConnection()->quoteIdentifier($filter['column']).' '; |
||
515 | } |
||
516 | |||
517 | if (in_array(strtoupper($filter['operator']), array('IN', 'NOT IN'), true)) { |
||
518 | $query .= strtoupper($filter['operator']).' ('.implode(', ', $this->getConnection()->quoteArr($filter['value'])).') '; |
||
519 | } else { |
||
520 | $query .= $filter['operator'].' '.$this->getConnection()->quote($filter['value']).' '; |
||
521 | } |
||
522 | } |
||
523 | } |
||
524 | |||
525 | return $query; |
||
526 | } |
||
527 | |||
528 | /** |
||
529 | * Compiles the statements for SELECT |
||
530 | * |
||
531 | * @return SphinxQL |
||
532 | */ |
||
533 | public function compileSelect() |
||
665 | |||
666 | /** |
||
667 | * Compiles the statements for INSERT or REPLACE |
||
668 | * |
||
669 | * @return SphinxQL |
||
670 | */ |
||
671 | public function compileInsert() |
||
672 | { |
||
673 | if ($this->type == 'insert') { |
||
674 | $query = 'INSERT '; |
||
675 | } else { |
||
676 | $query = 'REPLACE '; |
||
677 | } |
||
678 | |||
679 | if ($this->into !== null) { |
||
680 | $query .= 'INTO '.$this->into.' '; |
||
681 | } |
||
682 | |||
683 | View Code Duplication | if (!empty($this->columns)) { |
|
1 ignored issue
–
show
|
|||
684 | $query .= '('.implode(', ', $this->getConnection()->quoteIdentifierArr($this->columns)).') '; |
||
685 | } |
||
686 | |||
687 | if (!empty($this->values)) { |
||
688 | $query .= 'VALUES '; |
||
689 | $query_sub = ''; |
||
690 | |||
691 | foreach ($this->values as $value) { |
||
692 | $query_sub[] = '('.implode(', ', $this->getConnection()->quoteArr($value)).')'; |
||
693 | } |
||
694 | |||
695 | $query .= implode(', ', $query_sub); |
||
696 | } |
||
697 | |||
698 | $query = trim($query); |
||
699 | $this->last_compiled = $query; |
||
700 | |||
701 | return $this; |
||
702 | } |
||
703 | |||
704 | /** |
||
705 | * Compiles the statements for UPDATE |
||
706 | * |
||
707 | * @return SphinxQL |
||
708 | */ |
||
709 | public function compileUpdate() |
||
710 | { |
||
711 | $query = 'UPDATE '; |
||
712 | |||
713 | if ($this->into !== null) { |
||
714 | $query .= $this->into.' '; |
||
715 | } |
||
716 | |||
717 | if (!empty($this->set)) { |
||
718 | $query .= 'SET '; |
||
719 | |||
720 | $query_sub = array(); |
||
721 | |||
722 | foreach ($this->set as $column => $value) { |
||
723 | // MVA support |
||
724 | if (is_array($value)) { |
||
725 | $query_sub[] = $this->getConnection()->quoteIdentifier($column) |
||
726 | .' = ('.implode(', ', $this->getConnection()->quoteArr($value)).')'; |
||
727 | } else { |
||
728 | $query_sub[] = $this->getConnection()->quoteIdentifier($column) |
||
729 | .' = '.$this->getConnection()->quote($value); |
||
730 | } |
||
731 | } |
||
732 | |||
733 | $query .= implode(', ', $query_sub).' '; |
||
734 | } |
||
735 | |||
736 | $query .= $this->compileMatch().$this->compileWhere(); |
||
737 | |||
738 | $query = trim($query); |
||
739 | $this->last_compiled = $query; |
||
740 | |||
741 | return $this; |
||
742 | } |
||
743 | |||
744 | /** |
||
745 | * Compiles the statements for DELETE |
||
746 | * |
||
747 | * @return SphinxQL |
||
748 | */ |
||
749 | public function compileDelete() |
||
750 | { |
||
751 | $query = 'DELETE '; |
||
752 | |||
753 | if (!empty($this->from)) { |
||
754 | $query .= 'FROM '.$this->from[0].' '; |
||
755 | } |
||
756 | |||
757 | if (!empty($this->where)) { |
||
758 | $query .= $this->compileWhere(); |
||
759 | } |
||
760 | |||
761 | $query = trim($query); |
||
762 | $this->last_compiled = $query; |
||
763 | |||
764 | return $this; |
||
765 | } |
||
766 | |||
767 | /** |
||
768 | * Sets a query to be executed |
||
769 | * |
||
770 | * @param string $sql A SphinxQL query to execute |
||
771 | * |
||
772 | * @return SphinxQL |
||
773 | */ |
||
774 | public function query($sql) |
||
775 | { |
||
776 | $this->type = 'query'; |
||
777 | $this->query = $sql; |
||
778 | |||
779 | return $this; |
||
780 | } |
||
781 | |||
782 | /** |
||
783 | * Select the columns |
||
784 | * |
||
785 | * Gets the arguments passed as $sphinxql->select('one', 'two') |
||
786 | * Using it without arguments equals to having '*' as argument |
||
787 | * Using it with array maps values as column names |
||
788 | * |
||
789 | * Examples: |
||
790 | * $query->select('title'); |
||
791 | * // SELECT title |
||
792 | * |
||
793 | * $query->select('title', 'author', 'date'); |
||
794 | * // SELECT title, author, date |
||
795 | * |
||
796 | * $query->select(['id', 'title']); |
||
797 | * // SELECT id, title |
||
798 | * |
||
799 | * @param array|string $columns Array or multiple string arguments containing column names |
||
800 | * |
||
801 | * @return SphinxQL |
||
802 | */ |
||
803 | public function select($columns = null) |
||
804 | { |
||
805 | $this->reset(); |
||
806 | $this->type = 'select'; |
||
807 | |||
808 | if (is_array($columns)) { |
||
809 | $this->select = $columns; |
||
810 | } else { |
||
811 | $this->select = \func_get_args(); |
||
812 | } |
||
813 | |||
814 | return $this; |
||
815 | } |
||
816 | |||
817 | /** |
||
818 | * Activates the INSERT mode |
||
819 | * |
||
820 | * @return SphinxQL |
||
821 | */ |
||
822 | public function insert() |
||
823 | { |
||
824 | $this->reset(); |
||
825 | $this->type = 'insert'; |
||
826 | |||
827 | return $this; |
||
828 | } |
||
829 | |||
830 | /** |
||
831 | * Activates the REPLACE mode |
||
832 | * |
||
833 | * @return SphinxQL |
||
834 | */ |
||
835 | public function replace() |
||
836 | { |
||
837 | $this->reset(); |
||
838 | $this->type = 'replace'; |
||
839 | |||
840 | return $this; |
||
841 | } |
||
842 | |||
843 | /** |
||
844 | * Activates the UPDATE mode |
||
845 | * |
||
846 | * @param string $index The index to update into |
||
847 | * |
||
848 | * @return SphinxQL |
||
849 | */ |
||
850 | public function update($index) |
||
851 | { |
||
852 | $this->reset(); |
||
853 | $this->type = 'update'; |
||
854 | $this->into($index); |
||
855 | |||
856 | return $this; |
||
857 | } |
||
858 | |||
859 | /** |
||
860 | * Activates the DELETE mode |
||
861 | * |
||
862 | * @return SphinxQL |
||
863 | */ |
||
864 | public function delete() |
||
865 | { |
||
866 | $this->reset(); |
||
867 | $this->type = 'delete'; |
||
868 | |||
869 | return $this; |
||
870 | } |
||
871 | |||
872 | /** |
||
873 | * FROM clause (Sphinx-specific since it works with multiple indexes) |
||
874 | * func_get_args()-enabled |
||
875 | * |
||
876 | * @param array $array An array of indexes to use |
||
877 | * |
||
878 | * @return SphinxQL |
||
879 | */ |
||
880 | public function from($array = null) |
||
881 | { |
||
882 | if (is_string($array)) { |
||
883 | $this->from = \func_get_args(); |
||
884 | } |
||
885 | |||
886 | if (is_array($array) || $array instanceof \Closure || $array instanceof SphinxQL) { |
||
887 | $this->from = $array; |
||
888 | } |
||
889 | |||
890 | return $this; |
||
891 | } |
||
892 | |||
893 | /** |
||
894 | * MATCH clause (Sphinx-specific) |
||
895 | * |
||
896 | * @param mixed $column The column name (can be array, string, Closure, or Match) |
||
897 | * @param string $value The value |
||
898 | * @param boolean $half Exclude ", |, - control characters from being escaped |
||
899 | * |
||
900 | * @return SphinxQL |
||
901 | */ |
||
902 | public function match($column, $value = null, $half = false) |
||
912 | |||
913 | /** |
||
914 | * WHERE clause |
||
915 | * |
||
916 | * Examples: |
||
917 | * $query->where('column', 'value'); |
||
918 | * // WHERE column = 'value' |
||
919 | * |
||
920 | * $query->where('column', '=', 'value'); |
||
921 | * // WHERE column = 'value' |
||
922 | * |
||
923 | * $query->where('column', '>=', 'value') |
||
924 | * // WHERE column >= 'value' |
||
925 | * |
||
926 | * $query->where('column', 'IN', array('value1', 'value2', 'value3')); |
||
927 | * // WHERE column IN ('value1', 'value2', 'value3') |
||
928 | * |
||
929 | * $query->where('column', 'BETWEEN', array('value1', 'value2')) |
||
930 | * // WHERE column BETWEEN 'value1' AND 'value2' |
||
931 | * // WHERE example BETWEEN 10 AND 100 |
||
932 | * |
||
933 | * @param string $column The column name |
||
934 | * @param string $operator The operator to use |
||
935 | * @param string $value The value to check against |
||
936 | * |
||
937 | * @return SphinxQL |
||
938 | */ |
||
939 | View Code Duplication | public function where($column, $operator, $value = null) |
|
954 | |||
955 | /** |
||
956 | * GROUP BY clause |
||
957 | * Adds to the previously added columns |
||
958 | * |
||
959 | * @param string $column A column to group by |
||
960 | * |
||
961 | * @return SphinxQL |
||
962 | */ |
||
963 | public function groupBy($column) |
||
969 | |||
970 | /** |
||
971 | * WITHIN GROUP ORDER BY clause (SphinxQL-specific) |
||
972 | * Adds to the previously added columns |
||
973 | * Works just like a classic ORDER BY |
||
974 | * |
||
975 | * @param string $column The column to group by |
||
976 | * @param string $direction The group by direction (asc/desc) |
||
977 | * |
||
978 | * @return SphinxQL |
||
979 | */ |
||
980 | public function withinGroupOrderBy($column, $direction = null) |
||
986 | |||
987 | /** |
||
988 | * HAVING clause |
||
989 | * |
||
990 | * Examples: |
||
991 | * $sq->having('column', 'value'); |
||
992 | * // HAVING column = 'value' |
||
993 | * |
||
994 | * $sq->having('column', '=', 'value'); |
||
995 | * // HAVING column = 'value' |
||
996 | * |
||
997 | * $sq->having('column', '>=', 'value') |
||
998 | * // HAVING column >= 'value' |
||
999 | * |
||
1000 | * $sq->having('column', 'IN', array('value1', 'value2', 'value3')); |
||
1001 | * // HAVING column IN ('value1', 'value2', 'value3') |
||
1002 | * |
||
1003 | * $sq->having('column', 'BETWEEN', array('value1', 'value2')) |
||
1004 | * // HAVING column BETWEEN 'value1' AND 'value2' |
||
1005 | * // HAVING example BETWEEN 10 AND 100 |
||
1006 | * |
||
1007 | * @param string $column The column name |
||
1008 | * @param string $operator The operator to use |
||
1009 | * @param string $value The value to check against |
||
1010 | * |
||
1011 | * @return SphinxQL The current object |
||
1012 | */ |
||
1013 | View Code Duplication | public function having($column, $operator, $value = null) |
|
1028 | |||
1029 | /** |
||
1030 | * ORDER BY clause |
||
1031 | * Adds to the previously added columns |
||
1032 | * |
||
1033 | * @param string $column The column to order on |
||
1034 | * @param string $direction The ordering direction (asc/desc) |
||
1035 | * |
||
1036 | * @return SphinxQL |
||
1037 | */ |
||
1038 | public function orderBy($column, $direction = null) |
||
1044 | |||
1045 | /** |
||
1046 | * LIMIT clause |
||
1047 | * Supports also LIMIT offset, limit |
||
1048 | * |
||
1049 | * @param int $offset Offset if $limit is specified, else limit |
||
1050 | * @param null|int $limit The limit to set, null for no limit |
||
1051 | * |
||
1052 | * @return SphinxQL |
||
1053 | */ |
||
1054 | View Code Duplication | public function limit($offset, $limit = null) |
|
1066 | |||
1067 | /** |
||
1068 | * OFFSET clause |
||
1069 | * |
||
1070 | * @param int $offset The offset |
||
1071 | * |
||
1072 | * @return SphinxQL |
||
1073 | */ |
||
1074 | public function offset($offset) |
||
1080 | |||
1081 | /** |
||
1082 | * OPTION clause (SphinxQL-specific) |
||
1083 | * Used by: SELECT |
||
1084 | * |
||
1085 | * @param string $name Option name |
||
1086 | * @param string $value Option value |
||
1087 | * |
||
1088 | * @return SphinxQL |
||
1089 | */ |
||
1090 | public function option($name, $value) |
||
1096 | |||
1097 | /** |
||
1098 | * INTO clause |
||
1099 | * Used by: INSERT, REPLACE |
||
1100 | * |
||
1101 | * @param string $index The index to insert/replace into |
||
1102 | * |
||
1103 | * @return SphinxQL |
||
1104 | */ |
||
1105 | public function into($index) |
||
1111 | |||
1112 | /** |
||
1113 | * Set columns |
||
1114 | * Used in: INSERT, REPLACE |
||
1115 | * func_get_args()-enabled |
||
1116 | * |
||
1117 | * @param array $array The array of columns |
||
1118 | * |
||
1119 | * @return SphinxQL |
||
1120 | */ |
||
1121 | public function columns($array = array()) |
||
1131 | |||
1132 | /** |
||
1133 | * Set VALUES |
||
1134 | * Used in: INSERT, REPLACE |
||
1135 | * func_get_args()-enabled |
||
1136 | * |
||
1137 | * @param array $array The array of values matching the columns from $this->columns() |
||
1138 | * |
||
1139 | * @return SphinxQL |
||
1140 | */ |
||
1141 | public function values($array) |
||
1151 | |||
1152 | /** |
||
1153 | * Set column and relative value |
||
1154 | * Used in: INSERT, REPLACE |
||
1155 | * |
||
1156 | * @param string $column The column name |
||
1157 | * @param string $value The value |
||
1158 | * |
||
1159 | * @return SphinxQL |
||
1160 | */ |
||
1161 | public function value($column, $value) |
||
1172 | |||
1173 | /** |
||
1174 | * Allows passing an array with the key as column and value as value |
||
1175 | * Used in: INSERT, REPLACE, UPDATE |
||
1176 | * |
||
1177 | * @param array $array Array of key-values |
||
1178 | * |
||
1179 | * @return SphinxQL |
||
1180 | */ |
||
1181 | public function set($array) |
||
1189 | |||
1190 | /** |
||
1191 | * Allows passing an array with the key as column and value as value |
||
1192 | * Used in: INSERT, REPLACE, UPDATE |
||
1193 | * |
||
1194 | * @param Facet $facet |
||
1195 | * @return SphinxQL |
||
1196 | */ |
||
1197 | public function facet($facet) |
||
1203 | |||
1204 | /** |
||
1205 | * Sets the characters used for escapeMatch(). |
||
1206 | * |
||
1207 | * @param array $array The array of characters to escape |
||
1208 | * |
||
1209 | * @return SphinxQL The escaped characters |
||
1210 | */ |
||
1211 | public function setFullEscapeChars($array = array()) |
||
1212 | { |
||
1213 | if (!empty($array)) { |
||
1214 | $this->escape_full_chars = $this->compileEscapeChars($array); |
||
1215 | } |
||
1216 | |||
1217 | return $this; |
||
1218 | } |
||
1219 | |||
1220 | /** |
||
1221 | * Sets the characters used for halfEscapeMatch(). |
||
1222 | * |
||
1223 | * @param array $array The array of characters to escape |
||
1224 | * |
||
1225 | * @return SphinxQL The escaped characters |
||
1226 | */ |
||
1227 | public function setHalfEscapeChars($array = array()) |
||
1235 | |||
1236 | /** |
||
1237 | * Compiles an array containing the characters and escaped characters into a key/value configuration. |
||
1238 | * |
||
1239 | * @param array $array The array of characters to escape |
||
1240 | * |
||
1241 | * @return array An array of the characters and it's escaped counterpart |
||
1242 | */ |
||
1243 | public function compileEscapeChars($array = array()) |
||
1252 | |||
1253 | /** |
||
1254 | * Escapes the query for the MATCH() function |
||
1255 | * |
||
1256 | * @param string $string The string to escape for the MATCH |
||
1257 | * |
||
1258 | * @return string The escaped string |
||
1259 | */ |
||
1260 | public function escapeMatch($string) |
||
1268 | |||
1269 | /** |
||
1270 | * Escapes the query for the MATCH() function |
||
1271 | * Allows some of the control characters to pass through for use with a search field: -, |, " |
||
1272 | * It also does some tricks to wrap/unwrap within " the string and prevents errors |
||
1273 | * |
||
1274 | * @param string $string The string to escape for the MATCH |
||
1275 | * |
||
1276 | * @return string The escaped string |
||
1277 | */ |
||
1278 | public function halfEscapeMatch($string) |
||
1304 | |||
1305 | /** |
||
1306 | * Clears the existing query build for new query when using the same SphinxQL instance. |
||
1307 | * |
||
1308 | * @return SphinxQL |
||
1309 | */ |
||
1310 | public function reset() |
||
1331 | |||
1332 | public function resetWhere() |
||
1338 | |||
1339 | public function resetMatch() |
||
1345 | |||
1346 | public function resetGroupBy() |
||
1352 | |||
1353 | public function resetWithinGroupOrderBy() |
||
1359 | |||
1360 | public function resetHaving() |
||
1366 | |||
1367 | public function resetOrderBy() |
||
1373 | |||
1374 | public function resetOptions() |
||
1380 | |||
1381 | /** |
||
1382 | * @param array $select |
||
1383 | */ |
||
1384 | public function setSelect(array $select) |
||
1388 | } |
||
1389 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.