Complex classes like QueryBuilder 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 QueryBuilder, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
27 | class QueryBuilder extends AbstractQueryBuilder implements QueryBuilderInterface { |
||
28 | |||
29 | /** |
||
30 | * String class values to be reset |
||
31 | * |
||
32 | * @var array |
||
33 | */ |
||
34 | private $string_vars = array( |
||
35 | 'select_string', |
||
36 | 'from_string', |
||
37 | 'set_string', |
||
38 | 'order_string', |
||
39 | 'group_string', |
||
40 | 'limit', |
||
41 | 'offset', |
||
42 | 'explain', |
||
43 | ); |
||
44 | |||
45 | /** |
||
46 | * Array class variables to be reset |
||
47 | * |
||
48 | * @var array |
||
49 | */ |
||
50 | private $array_vars = array( |
||
51 | 'set_array_keys', |
||
52 | 'order_array', |
||
53 | 'group_array', |
||
54 | 'values', |
||
55 | 'where_values', |
||
56 | 'query_map', |
||
57 | 'having_map' |
||
58 | ); |
||
59 | |||
60 | // -------------------------------------------------------------------------- |
||
61 | // ! Methods |
||
62 | // -------------------------------------------------------------------------- |
||
63 | |||
64 | /** |
||
65 | * Constructor |
||
66 | * |
||
67 | * @param DriverInterface $db |
||
68 | * @param QueryParser $parser |
||
69 | */ |
||
70 | public function __construct(DriverInterface $db, QueryParser $parser) |
||
82 | |||
83 | // -------------------------------------------------------------------------- |
||
84 | |||
85 | /** |
||
86 | * Destructor |
||
87 | * @codeCoverageIgnore |
||
88 | */ |
||
89 | public function __destruct() |
||
93 | |||
94 | // -------------------------------------------------------------------------- |
||
95 | |||
96 | /** |
||
97 | * Calls a function further down the inheritence chain |
||
98 | * |
||
99 | * @param string $name |
||
100 | * @param array $params |
||
101 | * @return mixed |
||
102 | * @throws \BadMethodCallException |
||
103 | */ |
||
104 | public function __call($name, $params) |
||
123 | |||
124 | // -------------------------------------------------------------------------- |
||
125 | // ! Select Queries |
||
126 | // -------------------------------------------------------------------------- |
||
127 | |||
128 | /** |
||
129 | * Specifies rows to select in a query |
||
130 | * |
||
131 | * @param string $fields |
||
132 | * @return QueryBuilder |
||
133 | */ |
||
134 | public function select($fields) |
||
170 | |||
171 | // -------------------------------------------------------------------------- |
||
172 | |||
173 | /** |
||
174 | * Selects the maximum value of a field from a query |
||
175 | * |
||
176 | * @param string $field |
||
177 | * @param string|FALSE $as |
||
178 | * @return QueryBuilder |
||
179 | */ |
||
180 | public function select_max($field, $as=FALSE) |
||
186 | |||
187 | // -------------------------------------------------------------------------- |
||
188 | |||
189 | /** |
||
190 | * Selects the minimum value of a field from a query |
||
191 | * |
||
192 | * @param string $field |
||
193 | * @param string|bool $as |
||
194 | * @return QueryBuilder |
||
195 | */ |
||
196 | public function select_min($field, $as=FALSE) |
||
202 | |||
203 | // -------------------------------------------------------------------------- |
||
204 | |||
205 | /** |
||
206 | * Selects the average value of a field from a query |
||
207 | * |
||
208 | * @param string $field |
||
209 | * @param string|bool $as |
||
210 | * @return QueryBuilder |
||
211 | */ |
||
212 | public function select_avg($field, $as=FALSE) |
||
218 | |||
219 | // -------------------------------------------------------------------------- |
||
220 | |||
221 | /** |
||
222 | * Selects the sum of a field from a query |
||
223 | * |
||
224 | * @param string $field |
||
225 | * @param string|bool $as |
||
226 | * @return QueryBuilder |
||
227 | */ |
||
228 | public function select_sum($field, $as=FALSE) |
||
234 | |||
235 | // -------------------------------------------------------------------------- |
||
236 | |||
237 | /** |
||
238 | * Adds the 'distinct' keyword to a query |
||
239 | * |
||
240 | * @return QueryBuilder |
||
241 | */ |
||
242 | public function distinct() |
||
248 | |||
249 | // -------------------------------------------------------------------------- |
||
250 | |||
251 | /** |
||
252 | * Tell the database to give you the query plan instead of result set |
||
253 | * |
||
254 | * @return QueryBuilder |
||
255 | */ |
||
256 | public function explain() |
||
261 | |||
262 | // -------------------------------------------------------------------------- |
||
263 | |||
264 | /** |
||
265 | * Specify the database table to select from |
||
266 | * |
||
267 | * @param string $tblname |
||
268 | * @return QueryBuilder |
||
269 | */ |
||
270 | public function from($tblname) |
||
285 | |||
286 | // -------------------------------------------------------------------------- |
||
287 | // ! 'Like' methods |
||
288 | // -------------------------------------------------------------------------- |
||
289 | |||
290 | /** |
||
291 | * Creates a Like clause in the sql statement |
||
292 | * |
||
293 | * @param string $field |
||
294 | * @param mixed $val |
||
295 | * @param string $pos |
||
296 | * @return QueryBuilder |
||
297 | */ |
||
298 | public function like($field, $val, $pos='both') |
||
302 | |||
303 | // -------------------------------------------------------------------------- |
||
304 | |||
305 | /** |
||
306 | * Generates an OR Like clause |
||
307 | * |
||
308 | * @param string $field |
||
309 | * @param mixed $val |
||
310 | * @param string $pos |
||
311 | * @return QueryBuilder |
||
312 | */ |
||
313 | public function or_like($field, $val, $pos='both') |
||
317 | |||
318 | // -------------------------------------------------------------------------- |
||
319 | |||
320 | /** |
||
321 | * Generates a NOT LIKE clause |
||
322 | * |
||
323 | * @param string $field |
||
324 | * @param mixed $val |
||
325 | * @param string $pos |
||
326 | * @return QueryBuilder |
||
327 | */ |
||
328 | public function not_like($field, $val, $pos='both') |
||
332 | |||
333 | // -------------------------------------------------------------------------- |
||
334 | |||
335 | /** |
||
336 | * Generates a OR NOT LIKE clause |
||
337 | * |
||
338 | * @param string $field |
||
339 | * @param mixed $val |
||
340 | * @param string $pos |
||
341 | * @return QueryBuilder |
||
342 | */ |
||
343 | public function or_not_like($field, $val, $pos='both') |
||
347 | |||
348 | // -------------------------------------------------------------------------- |
||
349 | // ! Having methods |
||
350 | // -------------------------------------------------------------------------- |
||
351 | |||
352 | /** |
||
353 | * Generates a 'Having' clause |
||
354 | * |
||
355 | * @param mixed $key |
||
356 | * @param mixed $val |
||
357 | * @return QueryBuilder |
||
358 | */ |
||
359 | public function having($key, $val=array()) |
||
363 | |||
364 | // -------------------------------------------------------------------------- |
||
365 | |||
366 | /** |
||
367 | * Generates a 'Having' clause prefixed with 'OR' |
||
368 | * |
||
369 | * @param mixed $key |
||
370 | * @param mixed $val |
||
371 | * @return QueryBuilder |
||
372 | */ |
||
373 | public function or_having($key, $val=array()) |
||
377 | |||
378 | // -------------------------------------------------------------------------- |
||
379 | // ! 'Where' methods |
||
380 | // -------------------------------------------------------------------------- |
||
381 | |||
382 | /** |
||
383 | * Specify condition(s) in the where clause of a query |
||
384 | * Note: this function works with key / value, or a |
||
385 | * passed array with key / value pairs |
||
386 | * |
||
387 | * @param mixed $key |
||
388 | * @param mixed $val |
||
389 | * @param mixed $escape |
||
390 | * @return QueryBuilder |
||
391 | */ |
||
392 | public function where($key, $val=array(), $escape=NULL) |
||
396 | |||
397 | // -------------------------------------------------------------------------- |
||
398 | |||
399 | /** |
||
400 | * Where clause prefixed with "OR" |
||
401 | * |
||
402 | * @param string $key |
||
403 | * @param mixed $val |
||
404 | * @return QueryBuilder |
||
405 | */ |
||
406 | public function or_where($key, $val=array()) |
||
410 | |||
411 | // -------------------------------------------------------------------------- |
||
412 | |||
413 | /** |
||
414 | * Where clause with 'IN' statement |
||
415 | * |
||
416 | * @param mixed $field |
||
417 | * @param mixed $val |
||
418 | * @return QueryBuilder |
||
419 | */ |
||
420 | public function where_in($field, $val=array()) |
||
424 | |||
425 | // -------------------------------------------------------------------------- |
||
426 | |||
427 | /** |
||
428 | * Where in statement prefixed with "or" |
||
429 | * |
||
430 | * @param string $field |
||
431 | * @param mixed $val |
||
432 | * @return QueryBuilder |
||
433 | */ |
||
434 | public function or_where_in($field, $val=array()) |
||
438 | |||
439 | // -------------------------------------------------------------------------- |
||
440 | |||
441 | /** |
||
442 | * WHERE NOT IN (FOO) clause |
||
443 | * |
||
444 | * @param string $field |
||
445 | * @param mixed $val |
||
446 | * @return QueryBuilder |
||
447 | */ |
||
448 | public function where_not_in($field, $val=array()) |
||
452 | |||
453 | // -------------------------------------------------------------------------- |
||
454 | |||
455 | /** |
||
456 | * OR WHERE NOT IN (FOO) clause |
||
457 | * |
||
458 | * @param string $field |
||
459 | * @param mixed $val |
||
460 | * @return QueryBuilder |
||
461 | */ |
||
462 | public function or_where_not_in($field, $val=array()) |
||
466 | |||
467 | // -------------------------------------------------------------------------- |
||
468 | // ! Other Query Modifier methods |
||
469 | // -------------------------------------------------------------------------- |
||
470 | |||
471 | /** |
||
472 | * Sets values for inserts / updates / deletes |
||
473 | * |
||
474 | * @param mixed $key |
||
475 | * @param mixed $val |
||
476 | * @return QueryBuilder |
||
477 | */ |
||
478 | public function set($key, $val = NULL) |
||
493 | |||
494 | // -------------------------------------------------------------------------- |
||
495 | |||
496 | /** |
||
497 | * Creates a join phrase in a compiled query |
||
498 | * |
||
499 | * @param string $table |
||
500 | * @param string $condition |
||
501 | * @param string $type |
||
502 | * @return QueryBuilder |
||
503 | */ |
||
504 | public function join($table, $condition, $type='') |
||
520 | |||
521 | // -------------------------------------------------------------------------- |
||
522 | |||
523 | /** |
||
524 | * Group the results by the selected field(s) |
||
525 | * |
||
526 | * @param mixed $field |
||
527 | * @return QueryBuilder |
||
528 | */ |
||
529 | public function group_by($field) |
||
545 | |||
546 | // -------------------------------------------------------------------------- |
||
547 | |||
548 | /** |
||
549 | * Order the results by the selected field(s) |
||
550 | * |
||
551 | * @param string $field |
||
552 | * @param string $type |
||
553 | * @return QueryBuilder |
||
554 | */ |
||
555 | public function order_by($field, $type="") |
||
584 | |||
585 | // -------------------------------------------------------------------------- |
||
586 | |||
587 | /** |
||
588 | * Set a limit on the current sql statement |
||
589 | * |
||
590 | * @param int $limit |
||
591 | * @param int|bool $offset |
||
592 | * @return QueryBuilder |
||
593 | */ |
||
594 | public function limit($limit, $offset=FALSE) |
||
601 | |||
602 | // -------------------------------------------------------------------------- |
||
603 | // ! Query Grouping Methods |
||
604 | // -------------------------------------------------------------------------- |
||
605 | |||
606 | /** |
||
607 | * Adds a paren to the current query for query grouping |
||
608 | * |
||
609 | * @return QueryBuilder |
||
610 | */ |
||
611 | public function group_start() |
||
619 | |||
620 | // -------------------------------------------------------------------------- |
||
621 | |||
622 | /** |
||
623 | * Adds a paren to the current query for query grouping, |
||
624 | * prefixed with 'OR' |
||
625 | * |
||
626 | * @return QueryBuilder |
||
627 | */ |
||
628 | public function or_group_start() |
||
634 | |||
635 | // -------------------------------------------------------------------------- |
||
636 | |||
637 | /** |
||
638 | * Adds a paren to the current query for query grouping, |
||
639 | * prefixed with 'OR NOT' |
||
640 | * |
||
641 | * @return QueryBuilder |
||
642 | */ |
||
643 | public function or_not_group_start() |
||
649 | |||
650 | // -------------------------------------------------------------------------- |
||
651 | |||
652 | /** |
||
653 | * Ends a query group |
||
654 | * |
||
655 | * @return QueryBuilder |
||
656 | */ |
||
657 | public function group_end() |
||
663 | |||
664 | // -------------------------------------------------------------------------- |
||
665 | // ! Query execution methods |
||
666 | // -------------------------------------------------------------------------- |
||
667 | |||
668 | /** |
||
669 | * Select and retrieve all records from the current table, and/or |
||
670 | * execute current compiled query |
||
671 | * |
||
672 | * @param $table |
||
673 | * @param int|bool $limit |
||
674 | * @param int|bool $offset |
||
675 | * @return \PDOStatement |
||
676 | */ |
||
677 | public function get($table='', $limit=FALSE, $offset=FALSE) |
||
693 | |||
694 | // -------------------------------------------------------------------------- |
||
695 | |||
696 | /** |
||
697 | * Convenience method for get() with a where clause |
||
698 | * |
||
699 | * @param string $table |
||
700 | * @param array $where |
||
701 | * @param int|bool $limit |
||
702 | * @param int|bool $offset |
||
703 | * @return \PDOStatement |
||
704 | */ |
||
705 | public function get_where($table, $where=array(), $limit=FALSE, $offset=FALSE) |
||
713 | |||
714 | // -------------------------------------------------------------------------- |
||
715 | |||
716 | /** |
||
717 | * Retreive the number of rows in the selected table |
||
718 | * |
||
719 | * @param string $table |
||
720 | * @return int |
||
721 | */ |
||
722 | public function count_all($table) |
||
728 | |||
729 | // -------------------------------------------------------------------------- |
||
730 | |||
731 | /** |
||
732 | * Retrieve the number of results for the generated query - used |
||
733 | * in place of the get() method |
||
734 | * |
||
735 | * @param string $table |
||
736 | * @return int |
||
737 | */ |
||
738 | public function count_all_results($table='') |
||
751 | |||
752 | // -------------------------------------------------------------------------- |
||
753 | |||
754 | /** |
||
755 | * Creates an insert clause, and executes it |
||
756 | * |
||
757 | * @param string $table |
||
758 | * @param mixed $data |
||
759 | * @return \PDOStatement |
||
760 | */ |
||
761 | public function insert($table, $data=array()) |
||
770 | |||
771 | // -------------------------------------------------------------------------- |
||
772 | |||
773 | /** |
||
774 | * Creates and executes a batch insertion query |
||
775 | * |
||
776 | * @param string $table |
||
777 | * @param array $data |
||
778 | * @return \PDOStatement |
||
779 | */ |
||
780 | public function insert_batch($table, $data=array()) |
||
789 | |||
790 | // -------------------------------------------------------------------------- |
||
791 | |||
792 | /** |
||
793 | * Creates an update clause, and executes it |
||
794 | * |
||
795 | * @param string $table |
||
796 | * @param mixed $data |
||
797 | * @return \PDOStatement |
||
798 | */ |
||
799 | public function update($table, $data=array()) |
||
808 | |||
809 | // -------------------------------------------------------------------------- |
||
810 | |||
811 | /** |
||
812 | * Deletes data from a table |
||
813 | * |
||
814 | * @param string $table |
||
815 | * @param mixed $where |
||
816 | * @return \PDOStatement |
||
817 | */ |
||
818 | public function delete($table, $where='') |
||
828 | |||
829 | // -------------------------------------------------------------------------- |
||
830 | // ! SQL Returning Methods |
||
831 | // -------------------------------------------------------------------------- |
||
832 | |||
833 | |||
834 | |||
835 | /** |
||
836 | * Returns the generated 'select' sql query |
||
837 | * |
||
838 | * @param string $table |
||
839 | * @param bool $reset |
||
840 | * @return string |
||
841 | */ |
||
842 | public function get_compiled_select($table='', $reset=TRUE) |
||
852 | |||
853 | // -------------------------------------------------------------------------- |
||
854 | |||
855 | /** |
||
856 | * Returns the generated 'insert' sql query |
||
857 | * |
||
858 | * @param string $table |
||
859 | * @param bool $reset |
||
860 | * @return string |
||
861 | */ |
||
862 | public function get_compiled_insert($table, $reset=TRUE) |
||
866 | |||
867 | // -------------------------------------------------------------------------- |
||
868 | |||
869 | /** |
||
870 | * Returns the generated 'update' sql query |
||
871 | * |
||
872 | * @param string $table |
||
873 | * @param bool $reset |
||
874 | * @return string |
||
875 | */ |
||
876 | public function get_compiled_update($table='', $reset=TRUE) |
||
880 | |||
881 | // -------------------------------------------------------------------------- |
||
882 | |||
883 | /** |
||
884 | * Returns the generated 'delete' sql query |
||
885 | * |
||
886 | * @param string $table |
||
887 | * @param bool $reset |
||
888 | * @return string |
||
889 | */ |
||
890 | public function get_compiled_delete($table="", $reset=TRUE) |
||
894 | |||
895 | |||
896 | // -------------------------------------------------------------------------- |
||
897 | // ! Miscellaneous Methods |
||
898 | // -------------------------------------------------------------------------- |
||
899 | |||
900 | /** |
||
901 | * Clear out the class variables, so the next query can be run |
||
902 | * |
||
903 | * @return void |
||
904 | */ |
||
905 | public function reset_query() |
||
919 | } |
||
920 | // End of query_builder.php |