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 CI_DB_query_builder 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 CI_DB_query_builder, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
52 | abstract class CI_DB_query_builder extends CI_DB_driver { |
||
53 | |||
54 | /** |
||
55 | * Return DELETE SQL flag |
||
56 | * |
||
57 | * @var bool |
||
58 | */ |
||
59 | protected $return_delete_sql = FALSE; |
||
60 | |||
61 | /** |
||
62 | * Reset DELETE data flag |
||
63 | * |
||
64 | * @var bool |
||
65 | */ |
||
66 | protected $reset_delete_data = FALSE; |
||
67 | |||
68 | /** |
||
69 | * QB SELECT data |
||
70 | * |
||
71 | * @var array |
||
72 | */ |
||
73 | protected $qb_select = array(); |
||
74 | |||
75 | /** |
||
76 | * QB DISTINCT flag |
||
77 | * |
||
78 | * @var bool |
||
79 | */ |
||
80 | protected $qb_distinct = FALSE; |
||
81 | |||
82 | /** |
||
83 | * QB FROM data |
||
84 | * |
||
85 | * @var array |
||
86 | */ |
||
87 | protected $qb_from = array(); |
||
88 | |||
89 | /** |
||
90 | * QB JOIN data |
||
91 | * |
||
92 | * @var array |
||
93 | */ |
||
94 | protected $qb_join = array(); |
||
95 | |||
96 | /** |
||
97 | * QB WHERE data |
||
98 | * |
||
99 | * @var array |
||
100 | */ |
||
101 | protected $qb_where = array(); |
||
102 | |||
103 | /** |
||
104 | * QB GROUP BY data |
||
105 | * |
||
106 | * @var array |
||
107 | */ |
||
108 | protected $qb_groupby = array(); |
||
109 | |||
110 | /** |
||
111 | * QB HAVING data |
||
112 | * |
||
113 | * @var array |
||
114 | */ |
||
115 | protected $qb_having = array(); |
||
116 | |||
117 | /** |
||
118 | * QB keys |
||
119 | * |
||
120 | * @var array |
||
121 | */ |
||
122 | protected $qb_keys = array(); |
||
123 | |||
124 | /** |
||
125 | * QB LIMIT data |
||
126 | * |
||
127 | * @var int |
||
128 | */ |
||
129 | protected $qb_limit = FALSE; |
||
130 | |||
131 | /** |
||
132 | * QB OFFSET data |
||
133 | * |
||
134 | * @var int |
||
135 | */ |
||
136 | protected $qb_offset = FALSE; |
||
137 | |||
138 | /** |
||
139 | * QB ORDER BY data |
||
140 | * |
||
141 | * @var array |
||
142 | */ |
||
143 | protected $qb_orderby = array(); |
||
144 | |||
145 | /** |
||
146 | * QB data sets |
||
147 | * |
||
148 | * @var array |
||
149 | */ |
||
150 | protected $qb_set = array(); |
||
151 | |||
152 | /** |
||
153 | * QB aliased tables list |
||
154 | * |
||
155 | * @var array |
||
156 | */ |
||
157 | protected $qb_aliased_tables = array(); |
||
158 | |||
159 | /** |
||
160 | * QB WHERE group started flag |
||
161 | * |
||
162 | * @var bool |
||
163 | */ |
||
164 | protected $qb_where_group_started = FALSE; |
||
165 | |||
166 | /** |
||
167 | * QB WHERE group count |
||
168 | * |
||
169 | * @var int |
||
170 | */ |
||
171 | protected $qb_where_group_count = 0; |
||
172 | |||
173 | // Query Builder Caching variables |
||
174 | |||
175 | /** |
||
176 | * QB Caching flag |
||
177 | * |
||
178 | * @var bool |
||
179 | */ |
||
180 | protected $qb_caching = FALSE; |
||
181 | |||
182 | /** |
||
183 | * QB Cache exists list |
||
184 | * |
||
185 | * @var array |
||
186 | */ |
||
187 | protected $qb_cache_exists = array(); |
||
188 | |||
189 | /** |
||
190 | * QB Cache SELECT data |
||
191 | * |
||
192 | * @var array |
||
193 | */ |
||
194 | protected $qb_cache_select = array(); |
||
195 | |||
196 | /** |
||
197 | * QB Cache FROM data |
||
198 | * |
||
199 | * @var array |
||
200 | */ |
||
201 | protected $qb_cache_from = array(); |
||
202 | |||
203 | /** |
||
204 | * QB Cache JOIN data |
||
205 | * |
||
206 | * @var array |
||
207 | */ |
||
208 | protected $qb_cache_join = array(); |
||
209 | |||
210 | /** |
||
211 | * QB Cache WHERE data |
||
212 | * |
||
213 | * @var array |
||
214 | */ |
||
215 | protected $qb_cache_where = array(); |
||
216 | |||
217 | /** |
||
218 | * QB Cache GROUP BY data |
||
219 | * |
||
220 | * @var array |
||
221 | */ |
||
222 | protected $qb_cache_groupby = array(); |
||
223 | |||
224 | /** |
||
225 | * QB Cache HAVING data |
||
226 | * |
||
227 | * @var array |
||
228 | */ |
||
229 | protected $qb_cache_having = array(); |
||
230 | |||
231 | /** |
||
232 | * QB Cache ORDER BY data |
||
233 | * |
||
234 | * @var array |
||
235 | */ |
||
236 | protected $qb_cache_orderby = array(); |
||
237 | |||
238 | /** |
||
239 | * QB Cache data sets |
||
240 | * |
||
241 | * @var array |
||
242 | */ |
||
243 | protected $qb_cache_set = array(); |
||
244 | |||
245 | /** |
||
246 | * QB No Escape data |
||
247 | * |
||
248 | * @var array |
||
249 | */ |
||
250 | protected $qb_no_escape = array(); |
||
251 | |||
252 | /** |
||
253 | * QB Cache No Escape data |
||
254 | * |
||
255 | * @var array |
||
256 | */ |
||
257 | protected $qb_cache_no_escape = array(); |
||
258 | |||
259 | // -------------------------------------------------------------------- |
||
260 | |||
261 | /** |
||
262 | * Select |
||
263 | * |
||
264 | * Generates the SELECT portion of the query |
||
265 | * |
||
266 | * @param string |
||
267 | * @param mixed |
||
268 | * @return CI_DB_query_builder |
||
269 | */ |
||
270 | public function select($select = '*', $escape = NULL) |
||
300 | |||
301 | // -------------------------------------------------------------------- |
||
302 | |||
303 | /** |
||
304 | * Select Max |
||
305 | * |
||
306 | * Generates a SELECT MAX(field) portion of a query |
||
307 | * |
||
308 | * @param string the field |
||
309 | * @param string an alias |
||
310 | * @return CI_DB_query_builder |
||
311 | */ |
||
312 | public function select_max($select = '', $alias = '') |
||
316 | |||
317 | // -------------------------------------------------------------------- |
||
318 | |||
319 | /** |
||
320 | * Select Min |
||
321 | * |
||
322 | * Generates a SELECT MIN(field) portion of a query |
||
323 | * |
||
324 | * @param string the field |
||
325 | * @param string an alias |
||
326 | * @return CI_DB_query_builder |
||
327 | */ |
||
328 | public function select_min($select = '', $alias = '') |
||
332 | |||
333 | // -------------------------------------------------------------------- |
||
334 | |||
335 | /** |
||
336 | * Select Average |
||
337 | * |
||
338 | * Generates a SELECT AVG(field) portion of a query |
||
339 | * |
||
340 | * @param string the field |
||
341 | * @param string an alias |
||
342 | * @return CI_DB_query_builder |
||
343 | */ |
||
344 | public function select_avg($select = '', $alias = '') |
||
348 | |||
349 | // -------------------------------------------------------------------- |
||
350 | |||
351 | /** |
||
352 | * Select Sum |
||
353 | * |
||
354 | * Generates a SELECT SUM(field) portion of a query |
||
355 | * |
||
356 | * @param string the field |
||
357 | * @param string an alias |
||
358 | * @return CI_DB_query_builder |
||
359 | */ |
||
360 | public function select_sum($select = '', $alias = '') |
||
364 | |||
365 | // -------------------------------------------------------------------- |
||
366 | |||
367 | /** |
||
368 | * SELECT [MAX|MIN|AVG|SUM]() |
||
369 | * |
||
370 | * @used-by select_max() |
||
371 | * @used-by select_min() |
||
372 | * @used-by select_avg() |
||
373 | * @used-by select_sum() |
||
374 | * |
||
375 | * @param string $select Field name |
||
376 | * @param string $alias |
||
377 | * @param string $type |
||
378 | * @return CI_DB_query_builder |
||
379 | */ |
||
380 | protected function _max_min_avg_sum($select = '', $alias = '', $type = 'MAX') |
||
412 | |||
413 | // -------------------------------------------------------------------- |
||
414 | |||
415 | /** |
||
416 | * Determines the alias name based on the table |
||
417 | * |
||
418 | * @param string $item |
||
419 | * @return string |
||
420 | */ |
||
421 | protected function _create_alias_from_table($item) |
||
431 | |||
432 | // -------------------------------------------------------------------- |
||
433 | |||
434 | /** |
||
435 | * DISTINCT |
||
436 | * |
||
437 | * Sets a flag which tells the query string compiler to add DISTINCT |
||
438 | * |
||
439 | * @param bool $val |
||
440 | * @return CI_DB_query_builder |
||
441 | */ |
||
442 | public function distinct($val = TRUE) |
||
447 | |||
448 | // -------------------------------------------------------------------- |
||
449 | |||
450 | /** |
||
451 | * From |
||
452 | * |
||
453 | * Generates the FROM portion of the query |
||
454 | * |
||
455 | * @param mixed $from can be a string or array |
||
456 | * @return CI_DB_query_builder |
||
457 | */ |
||
458 | public function from($from) |
||
498 | |||
499 | // -------------------------------------------------------------------- |
||
500 | |||
501 | /** |
||
502 | * JOIN |
||
503 | * |
||
504 | * Generates the JOIN portion of the query |
||
505 | * |
||
506 | * @param string |
||
507 | * @param string the join condition |
||
508 | * @param string the type of join |
||
509 | * @param string whether not to try to escape identifiers |
||
510 | * @return CI_DB_query_builder |
||
511 | */ |
||
512 | public function join($table, $cond, $type = '', $escape = NULL) |
||
586 | |||
587 | // -------------------------------------------------------------------- |
||
588 | |||
589 | /** |
||
590 | * WHERE |
||
591 | * |
||
592 | * Generates the WHERE portion of the query. |
||
593 | * Separates multiple calls with 'AND'. |
||
594 | * |
||
595 | * @param mixed |
||
596 | * @param mixed |
||
597 | * @param bool |
||
598 | * @return CI_DB_query_builder |
||
599 | */ |
||
600 | public function where($key, $value = NULL, $escape = NULL) |
||
604 | |||
605 | // -------------------------------------------------------------------- |
||
606 | |||
607 | /** |
||
608 | * OR WHERE |
||
609 | * |
||
610 | * Generates the WHERE portion of the query. |
||
611 | * Separates multiple calls with 'OR'. |
||
612 | * |
||
613 | * @param mixed |
||
614 | * @param mixed |
||
615 | * @param bool |
||
616 | * @return CI_DB_query_builder |
||
617 | */ |
||
618 | public function or_where($key, $value = NULL, $escape = NULL) |
||
622 | |||
623 | // -------------------------------------------------------------------- |
||
624 | |||
625 | /** |
||
626 | * WHERE, HAVING |
||
627 | * |
||
628 | * @used-by where() |
||
629 | * @used-by or_where() |
||
630 | * @used-by having() |
||
631 | * @used-by or_having() |
||
632 | * |
||
633 | * @param string $qb_key 'qb_where' or 'qb_having' |
||
634 | * @param mixed $key |
||
635 | * @param mixed $value |
||
636 | * @param string $type |
||
637 | * @param bool $escape |
||
638 | * @return CI_DB_query_builder |
||
639 | */ |
||
640 | protected function _wh($qb_key, $key, $value = NULL, $type = 'AND ', $escape = NULL) |
||
691 | |||
692 | // -------------------------------------------------------------------- |
||
693 | |||
694 | /** |
||
695 | * WHERE IN |
||
696 | * |
||
697 | * Generates a WHERE field IN('item', 'item') SQL query, |
||
698 | * joined with 'AND' if appropriate. |
||
699 | * |
||
700 | * @param string $key The field to search |
||
701 | * @param array $values The values searched on |
||
702 | * @param bool $escape |
||
703 | * @return CI_DB_query_builder |
||
704 | */ |
||
705 | public function where_in($key = NULL, $values = NULL, $escape = NULL) |
||
709 | |||
710 | // -------------------------------------------------------------------- |
||
711 | |||
712 | /** |
||
713 | * OR WHERE IN |
||
714 | * |
||
715 | * Generates a WHERE field IN('item', 'item') SQL query, |
||
716 | * joined with 'OR' if appropriate. |
||
717 | * |
||
718 | * @param string $key The field to search |
||
719 | * @param array $values The values searched on |
||
720 | * @param bool $escape |
||
721 | * @return CI_DB_query_builder |
||
722 | */ |
||
723 | public function or_where_in($key = NULL, $values = NULL, $escape = NULL) |
||
727 | |||
728 | // -------------------------------------------------------------------- |
||
729 | |||
730 | /** |
||
731 | * WHERE NOT IN |
||
732 | * |
||
733 | * Generates a WHERE field NOT IN('item', 'item') SQL query, |
||
734 | * joined with 'AND' if appropriate. |
||
735 | * |
||
736 | * @param string $key The field to search |
||
737 | * @param array $values The values searched on |
||
738 | * @param bool $escape |
||
739 | * @return CI_DB_query_builder |
||
740 | */ |
||
741 | public function where_not_in($key = NULL, $values = NULL, $escape = NULL) |
||
745 | |||
746 | // -------------------------------------------------------------------- |
||
747 | |||
748 | /** |
||
749 | * OR WHERE NOT IN |
||
750 | * |
||
751 | * Generates a WHERE field NOT IN('item', 'item') SQL query, |
||
752 | * joined with 'OR' if appropriate. |
||
753 | * |
||
754 | * @param string $key The field to search |
||
755 | * @param array $values The values searched on |
||
756 | * @param bool $escape |
||
757 | * @return CI_DB_query_builder |
||
758 | */ |
||
759 | public function or_where_not_in($key = NULL, $values = NULL, $escape = NULL) |
||
763 | |||
764 | // -------------------------------------------------------------------- |
||
765 | |||
766 | /** |
||
767 | * Internal WHERE IN |
||
768 | * |
||
769 | * @used-by where_in() |
||
770 | * @used-by or_where_in() |
||
771 | * @used-by where_not_in() |
||
772 | * @used-by or_where_not_in() |
||
773 | * |
||
774 | * @param string $key The field to search |
||
775 | * @param array $values The values searched on |
||
776 | * @param bool $not If the statement would be IN or NOT IN |
||
777 | * @param string $type |
||
778 | * @param bool $escape |
||
779 | * @return CI_DB_query_builder |
||
780 | */ |
||
781 | protected function _where_in($key = NULL, $values = NULL, $not = FALSE, $type = 'AND ', $escape = NULL) |
||
828 | |||
829 | // -------------------------------------------------------------------- |
||
830 | |||
831 | /** |
||
832 | * LIKE |
||
833 | * |
||
834 | * Generates a %LIKE% portion of the query. |
||
835 | * Separates multiple calls with 'AND'. |
||
836 | * |
||
837 | * @param mixed $field |
||
838 | * @param string $match |
||
839 | * @param string $side |
||
840 | * @param bool $escape |
||
841 | * @return CI_DB_query_builder |
||
842 | */ |
||
843 | public function like($field, $match = '', $side = 'both', $escape = NULL) |
||
847 | |||
848 | // -------------------------------------------------------------------- |
||
849 | |||
850 | /** |
||
851 | * NOT LIKE |
||
852 | * |
||
853 | * Generates a NOT LIKE portion of the query. |
||
854 | * Separates multiple calls with 'AND'. |
||
855 | * |
||
856 | * @param mixed $field |
||
857 | * @param string $match |
||
858 | * @param string $side |
||
859 | * @param bool $escape |
||
860 | * @return CI_DB_query_builder |
||
861 | */ |
||
862 | public function not_like($field, $match = '', $side = 'both', $escape = NULL) |
||
866 | |||
867 | // -------------------------------------------------------------------- |
||
868 | |||
869 | /** |
||
870 | * OR LIKE |
||
871 | * |
||
872 | * Generates a %LIKE% portion of the query. |
||
873 | * Separates multiple calls with 'OR'. |
||
874 | * |
||
875 | * @param mixed $field |
||
876 | * @param string $match |
||
877 | * @param string $side |
||
878 | * @param bool $escape |
||
879 | * @return CI_DB_query_builder |
||
880 | */ |
||
881 | public function or_like($field, $match = '', $side = 'both', $escape = NULL) |
||
885 | |||
886 | // -------------------------------------------------------------------- |
||
887 | |||
888 | /** |
||
889 | * OR NOT LIKE |
||
890 | * |
||
891 | * Generates a NOT LIKE portion of the query. |
||
892 | * Separates multiple calls with 'OR'. |
||
893 | * |
||
894 | * @param mixed $field |
||
895 | * @param string $match |
||
896 | * @param string $side |
||
897 | * @param bool $escape |
||
898 | * @return CI_DB_query_builder |
||
899 | */ |
||
900 | public function or_not_like($field, $match = '', $side = 'both', $escape = NULL) |
||
904 | |||
905 | // -------------------------------------------------------------------- |
||
906 | |||
907 | /** |
||
908 | * Internal LIKE |
||
909 | * |
||
910 | * @used-by like() |
||
911 | * @used-by or_like() |
||
912 | * @used-by not_like() |
||
913 | * @used-by or_not_like() |
||
914 | * |
||
915 | * @param mixed $field |
||
916 | * @param string $match |
||
917 | * @param string $type |
||
918 | * @param string $side |
||
919 | * @param string $not |
||
920 | * @param bool $escape |
||
921 | * @return CI_DB_query_builder |
||
922 | */ |
||
923 | protected function _like($field, $match = '', $type = 'AND ', $side = 'both', $not = '', $escape = NULL) |
||
977 | |||
978 | // -------------------------------------------------------------------- |
||
979 | |||
980 | /** |
||
981 | * Starts a query group. |
||
982 | * |
||
983 | * @param string $not (Internal use only) |
||
984 | * @param string $type (Internal use only) |
||
985 | * @return CI_DB_query_builder |
||
986 | */ |
||
987 | public function group_start($not = '', $type = 'AND ') |
||
1006 | |||
1007 | // -------------------------------------------------------------------- |
||
1008 | |||
1009 | /** |
||
1010 | * Starts a query group, but ORs the group |
||
1011 | * |
||
1012 | * @return CI_DB_query_builder |
||
1013 | */ |
||
1014 | public function or_group_start() |
||
1018 | |||
1019 | // -------------------------------------------------------------------- |
||
1020 | |||
1021 | /** |
||
1022 | * Starts a query group, but NOTs the group |
||
1023 | * |
||
1024 | * @return CI_DB_query_builder |
||
1025 | */ |
||
1026 | public function not_group_start() |
||
1030 | |||
1031 | // -------------------------------------------------------------------- |
||
1032 | |||
1033 | /** |
||
1034 | * Starts a query group, but OR NOTs the group |
||
1035 | * |
||
1036 | * @return CI_DB_query_builder |
||
1037 | */ |
||
1038 | public function or_not_group_start() |
||
1042 | |||
1043 | // -------------------------------------------------------------------- |
||
1044 | |||
1045 | /** |
||
1046 | * Ends a query group |
||
1047 | * |
||
1048 | * @return CI_DB_query_builder |
||
1049 | */ |
||
1050 | public function group_end() |
||
1066 | |||
1067 | // -------------------------------------------------------------------- |
||
1068 | |||
1069 | /** |
||
1070 | * Group_get_type |
||
1071 | * |
||
1072 | * @used-by group_start() |
||
1073 | * @used-by _like() |
||
1074 | * @used-by _wh() |
||
1075 | * @used-by _where_in() |
||
1076 | * |
||
1077 | * @param string $type |
||
1078 | * @return string |
||
1079 | */ |
||
1080 | protected function _group_get_type($type) |
||
1090 | |||
1091 | // -------------------------------------------------------------------- |
||
1092 | |||
1093 | /** |
||
1094 | * GROUP BY |
||
1095 | * |
||
1096 | * @param string $by |
||
1097 | * @param bool $escape |
||
1098 | * @return CI_DB_query_builder |
||
1099 | */ |
||
1100 | public function group_by($by, $escape = NULL) |
||
1130 | |||
1131 | // -------------------------------------------------------------------- |
||
1132 | |||
1133 | /** |
||
1134 | * HAVING |
||
1135 | * |
||
1136 | * Separates multiple calls with 'AND'. |
||
1137 | * |
||
1138 | * @param string $key |
||
1139 | * @param string $value |
||
1140 | * @param bool $escape |
||
1141 | * @return object |
||
1142 | */ |
||
1143 | public function having($key, $value = NULL, $escape = NULL) |
||
1147 | |||
1148 | // -------------------------------------------------------------------- |
||
1149 | |||
1150 | /** |
||
1151 | * OR HAVING |
||
1152 | * |
||
1153 | * Separates multiple calls with 'OR'. |
||
1154 | * |
||
1155 | * @param string $key |
||
1156 | * @param string $value |
||
1157 | * @param bool $escape |
||
1158 | * @return object |
||
1159 | */ |
||
1160 | public function or_having($key, $value = NULL, $escape = NULL) |
||
1164 | |||
1165 | // -------------------------------------------------------------------- |
||
1166 | |||
1167 | /** |
||
1168 | * ORDER BY |
||
1169 | * |
||
1170 | * @param string $orderby |
||
1171 | * @param string $direction ASC, DESC or RANDOM |
||
1172 | * @param bool $escape |
||
1173 | * @return CI_DB_query_builder |
||
1174 | */ |
||
1175 | public function order_by($orderby, $direction = '', $escape = NULL) |
||
1223 | |||
1224 | // -------------------------------------------------------------------- |
||
1225 | |||
1226 | /** |
||
1227 | * LIMIT |
||
1228 | * |
||
1229 | * @param int $value LIMIT value |
||
1230 | * @param int $offset OFFSET value |
||
1231 | * @return CI_DB_query_builder |
||
1232 | */ |
||
1233 | public function limit($value, $offset = 0) |
||
1240 | |||
1241 | // -------------------------------------------------------------------- |
||
1242 | |||
1243 | /** |
||
1244 | * Sets the OFFSET value |
||
1245 | * |
||
1246 | * @param int $offset OFFSET value |
||
1247 | * @return CI_DB_query_builder |
||
1248 | */ |
||
1249 | public function offset($offset) |
||
1254 | |||
1255 | // -------------------------------------------------------------------- |
||
1256 | |||
1257 | /** |
||
1258 | * LIMIT string |
||
1259 | * |
||
1260 | * Generates a platform-specific LIMIT clause. |
||
1261 | * |
||
1262 | * @param string $sql SQL Query |
||
1263 | * @return string |
||
1264 | */ |
||
1265 | protected function _limit($sql) |
||
1269 | |||
1270 | // -------------------------------------------------------------------- |
||
1271 | |||
1272 | /** |
||
1273 | * The "set" function. |
||
1274 | * |
||
1275 | * Allows key/value pairs to be set for inserting or updating |
||
1276 | * |
||
1277 | * @param mixed |
||
1278 | * @param string |
||
1279 | * @param bool |
||
1280 | * @return CI_DB_query_builder |
||
1281 | */ |
||
1282 | public function set($key, $value = '', $escape = NULL) |
||
1301 | |||
1302 | // -------------------------------------------------------------------- |
||
1303 | |||
1304 | /** |
||
1305 | * Get SELECT query string |
||
1306 | * |
||
1307 | * Compiles a SELECT query string and returns the sql. |
||
1308 | * |
||
1309 | * @param string the table name to select from (optional) |
||
1310 | * @param bool TRUE: resets QB values; FALSE: leave QB values alone |
||
1311 | * @return string |
||
1312 | */ |
||
1313 | public function get_compiled_select($table = '', $reset = TRUE) |
||
1330 | |||
1331 | // -------------------------------------------------------------------- |
||
1332 | |||
1333 | /** |
||
1334 | * Get |
||
1335 | * |
||
1336 | * Compiles the select statement based on the other functions called |
||
1337 | * and runs the query |
||
1338 | * |
||
1339 | * @param string the table |
||
1340 | * @param string the limit clause |
||
1341 | * @param string the offset clause |
||
1342 | * @return object |
||
1343 | */ |
||
1344 | public function get($table = '', $limit = NULL, $offset = NULL) |
||
1361 | |||
1362 | // -------------------------------------------------------------------- |
||
1363 | |||
1364 | /** |
||
1365 | * "Count All Results" query |
||
1366 | * |
||
1367 | * Generates a platform-specific query string that counts all records |
||
1368 | * returned by an Query Builder query. |
||
1369 | * |
||
1370 | * @param string |
||
1371 | * @param bool the reset clause |
||
1372 | * @return int |
||
1373 | */ |
||
1374 | public function count_all_results($table = '', $reset = TRUE) |
||
1399 | |||
1400 | // -------------------------------------------------------------------- |
||
1401 | |||
1402 | /** |
||
1403 | * Get_Where |
||
1404 | * |
||
1405 | * Allows the where clause, limit and offset to be added directly |
||
1406 | * |
||
1407 | * @param string $table |
||
1408 | * @param string $where |
||
1409 | * @param int $limit |
||
1410 | * @param int $offset |
||
1411 | * @return object |
||
1412 | */ |
||
1413 | public function get_where($table = '', $where = NULL, $limit = NULL, $offset = NULL) |
||
1434 | |||
1435 | // -------------------------------------------------------------------- |
||
1436 | |||
1437 | /** |
||
1438 | * Insert_Batch |
||
1439 | * |
||
1440 | * Compiles batch insert strings and runs the queries |
||
1441 | * |
||
1442 | * @param string $table Table to insert into |
||
1443 | * @param array $set An associative array of insert values |
||
1444 | * @param bool $escape Whether to escape values and identifiers |
||
1445 | * @return int Number of rows inserted or FALSE on failure |
||
1446 | */ |
||
1447 | public function insert_batch($table = '', $set = NULL, $escape = NULL) |
||
1481 | |||
1482 | // -------------------------------------------------------------------- |
||
1483 | |||
1484 | /** |
||
1485 | * Insert batch statement |
||
1486 | * |
||
1487 | * Generates a platform-specific insert string from the supplied data. |
||
1488 | * |
||
1489 | * @param string $table Table name |
||
1490 | * @param array $keys INSERT keys |
||
1491 | * @param array $values INSERT values |
||
1492 | * @return string |
||
1493 | */ |
||
1494 | protected function _insert_batch($table, $keys, $values) |
||
1498 | |||
1499 | // -------------------------------------------------------------------- |
||
1500 | |||
1501 | /** |
||
1502 | * The "set_insert_batch" function. Allows key/value pairs to be set for batch inserts |
||
1503 | * |
||
1504 | * @param mixed |
||
1505 | * @param string |
||
1506 | * @param bool |
||
1507 | * @return CI_DB_query_builder |
||
1508 | */ |
||
1509 | public function set_insert_batch($key, $value = '', $escape = NULL) |
||
1556 | |||
1557 | // -------------------------------------------------------------------- |
||
1558 | |||
1559 | /** |
||
1560 | * Get INSERT query string |
||
1561 | * |
||
1562 | * Compiles an insert query and returns the sql |
||
1563 | * |
||
1564 | * @param string the table to insert into |
||
1565 | * @param bool TRUE: reset QB values; FALSE: leave QB values alone |
||
1566 | * @return string |
||
1567 | */ |
||
1568 | public function get_compiled_insert($table = '', $reset = TRUE) |
||
1590 | |||
1591 | // -------------------------------------------------------------------- |
||
1592 | |||
1593 | /** |
||
1594 | * Insert |
||
1595 | * |
||
1596 | * Compiles an insert string and runs the query |
||
1597 | * |
||
1598 | * @param string the table to insert data into |
||
1599 | * @param array an associative array of insert values |
||
1600 | * @param bool $escape Whether to escape values and identifiers |
||
1601 | * @return object |
||
1602 | */ |
||
1603 | public function insert($table = '', $set = NULL, $escape = NULL) |
||
1626 | |||
1627 | // -------------------------------------------------------------------- |
||
1628 | |||
1629 | /** |
||
1630 | * Validate Insert |
||
1631 | * |
||
1632 | * This method is used by both insert() and get_compiled_insert() to |
||
1633 | * validate that the there data is actually being set and that table |
||
1634 | * has been chosen to be inserted into. |
||
1635 | * |
||
1636 | * @param string the table to insert data into |
||
1637 | * @return string |
||
1638 | */ |
||
1639 | protected function _validate_insert($table = '') |
||
1657 | |||
1658 | // -------------------------------------------------------------------- |
||
1659 | |||
1660 | /** |
||
1661 | * Replace |
||
1662 | * |
||
1663 | * Compiles an replace into string and runs the query |
||
1664 | * |
||
1665 | * @param string the table to replace data into |
||
1666 | * @param array an associative array of insert values |
||
1667 | * @return object |
||
1668 | */ |
||
1669 | public function replace($table = '', $set = NULL) |
||
1696 | |||
1697 | // -------------------------------------------------------------------- |
||
1698 | |||
1699 | /** |
||
1700 | * Replace statement |
||
1701 | * |
||
1702 | * Generates a platform-specific replace string from the supplied data |
||
1703 | * |
||
1704 | * @param string the table name |
||
1705 | * @param array the insert keys |
||
1706 | * @param array the insert values |
||
1707 | * @return string |
||
1708 | */ |
||
1709 | protected function _replace($table, $keys, $values) |
||
1713 | |||
1714 | // -------------------------------------------------------------------- |
||
1715 | |||
1716 | /** |
||
1717 | * FROM tables |
||
1718 | * |
||
1719 | * Groups tables in FROM clauses if needed, so there is no confusion |
||
1720 | * about operator precedence. |
||
1721 | * |
||
1722 | * Note: This is only used (and overridden) by MySQL and CUBRID. |
||
1723 | * |
||
1724 | * @return string |
||
1725 | */ |
||
1726 | protected function _from_tables() |
||
1730 | |||
1731 | // -------------------------------------------------------------------- |
||
1732 | |||
1733 | /** |
||
1734 | * Get UPDATE query string |
||
1735 | * |
||
1736 | * Compiles an update query and returns the sql |
||
1737 | * |
||
1738 | * @param string the table to update |
||
1739 | * @param bool TRUE: reset QB values; FALSE: leave QB values alone |
||
1740 | * @return string |
||
1741 | */ |
||
1742 | public function get_compiled_update($table = '', $reset = TRUE) |
||
1761 | |||
1762 | // -------------------------------------------------------------------- |
||
1763 | |||
1764 | /** |
||
1765 | * UPDATE |
||
1766 | * |
||
1767 | * Compiles an update string and runs the query. |
||
1768 | * |
||
1769 | * @param string $table |
||
1770 | * @param array $set An associative array of update values |
||
1771 | * @param mixed $where |
||
1772 | * @param int $limit |
||
1773 | * @return object |
||
1774 | */ |
||
1775 | public function update($table = '', $set = NULL, $where = NULL, $limit = NULL) |
||
1804 | |||
1805 | // -------------------------------------------------------------------- |
||
1806 | |||
1807 | /** |
||
1808 | * Validate Update |
||
1809 | * |
||
1810 | * This method is used by both update() and get_compiled_update() to |
||
1811 | * validate that data is actually being set and that a table has been |
||
1812 | * chosen to be update. |
||
1813 | * |
||
1814 | * @param string the table to update data on |
||
1815 | * @return bool |
||
1816 | */ |
||
1817 | protected function _validate_update($table) |
||
1835 | |||
1836 | // -------------------------------------------------------------------- |
||
1837 | |||
1838 | /** |
||
1839 | * Update_Batch |
||
1840 | * |
||
1841 | * Compiles an update string and runs the query |
||
1842 | * |
||
1843 | * @param string the table to retrieve the results from |
||
1844 | * @param array an associative array of update values |
||
1845 | * @param string the where key |
||
1846 | * @return int number of rows affected or FALSE on failure |
||
1847 | */ |
||
1848 | public function update_batch($table = '', $set = NULL, $index = NULL) |
||
1890 | |||
1891 | // -------------------------------------------------------------------- |
||
1892 | |||
1893 | /** |
||
1894 | * Update_Batch statement |
||
1895 | * |
||
1896 | * Generates a platform-specific batch update string from the supplied data |
||
1897 | * |
||
1898 | * @param string $table Table name |
||
1899 | * @param array $values Update data |
||
1900 | * @param string $index WHERE key |
||
1901 | * @return string |
||
1902 | */ |
||
1903 | View Code Duplication | protected function _update_batch($table, $values, $index) |
|
1931 | |||
1932 | // -------------------------------------------------------------------- |
||
1933 | |||
1934 | /** |
||
1935 | * The "set_update_batch" function. Allows key/value pairs to be set for batch updating |
||
1936 | * |
||
1937 | * @param array |
||
1938 | * @param string |
||
1939 | * @param bool |
||
1940 | * @return CI_DB_query_builder |
||
1941 | */ |
||
1942 | public function set_update_batch($key, $index = '', $escape = NULL) |
||
1977 | |||
1978 | // -------------------------------------------------------------------- |
||
1979 | |||
1980 | /** |
||
1981 | * Empty Table |
||
1982 | * |
||
1983 | * Compiles a delete string and runs "DELETE FROM table" |
||
1984 | * |
||
1985 | * @param string the table to empty |
||
1986 | * @return object |
||
1987 | */ |
||
1988 | View Code Duplication | public function empty_table($table = '') |
|
2008 | |||
2009 | // -------------------------------------------------------------------- |
||
2010 | |||
2011 | /** |
||
2012 | * Truncate |
||
2013 | * |
||
2014 | * Compiles a truncate string and runs the query |
||
2015 | * If the database does not support the truncate() command |
||
2016 | * This function maps to "DELETE FROM table" |
||
2017 | * |
||
2018 | * @param string the table to truncate |
||
2019 | * @return object |
||
2020 | */ |
||
2021 | View Code Duplication | public function truncate($table = '') |
|
2041 | |||
2042 | // -------------------------------------------------------------------- |
||
2043 | |||
2044 | /** |
||
2045 | * Truncate statement |
||
2046 | * |
||
2047 | * Generates a platform-specific truncate string from the supplied data |
||
2048 | * |
||
2049 | * If the database does not support the truncate() command, |
||
2050 | * then this method maps to 'DELETE FROM table' |
||
2051 | * |
||
2052 | * @param string the table name |
||
2053 | * @return string |
||
2054 | */ |
||
2055 | protected function _truncate($table) |
||
2059 | |||
2060 | // -------------------------------------------------------------------- |
||
2061 | |||
2062 | /** |
||
2063 | * Get DELETE query string |
||
2064 | * |
||
2065 | * Compiles a delete query string and returns the sql |
||
2066 | * |
||
2067 | * @param string the table to delete from |
||
2068 | * @param bool TRUE: reset QB values; FALSE: leave QB values alone |
||
2069 | * @return string |
||
2070 | */ |
||
2071 | public function get_compiled_delete($table = '', $reset = TRUE) |
||
2078 | |||
2079 | // -------------------------------------------------------------------- |
||
2080 | |||
2081 | /** |
||
2082 | * Delete |
||
2083 | * |
||
2084 | * Compiles a delete string and runs the query |
||
2085 | * |
||
2086 | * @param mixed the table(s) to delete from. String or array |
||
2087 | * @param mixed the where clause |
||
2088 | * @param mixed the limit clause |
||
2089 | * @param bool |
||
2090 | * @return mixed |
||
2091 | */ |
||
2092 | public function delete($table = '', $where = '', $limit = NULL, $reset_data = TRUE) |
||
2145 | |||
2146 | // -------------------------------------------------------------------- |
||
2147 | |||
2148 | /** |
||
2149 | * Delete statement |
||
2150 | * |
||
2151 | * Generates a platform-specific delete string from the supplied data |
||
2152 | * |
||
2153 | * @param string the table name |
||
2154 | * @return string |
||
2155 | */ |
||
2156 | protected function _delete($table) |
||
2161 | |||
2162 | // -------------------------------------------------------------------- |
||
2163 | |||
2164 | /** |
||
2165 | * DB Prefix |
||
2166 | * |
||
2167 | * Prepends a database prefix if one exists in configuration |
||
2168 | * |
||
2169 | * @param string the table |
||
2170 | * @return string |
||
2171 | */ |
||
2172 | public function dbprefix($table = '') |
||
2181 | |||
2182 | // -------------------------------------------------------------------- |
||
2183 | |||
2184 | /** |
||
2185 | * Set DB Prefix |
||
2186 | * |
||
2187 | * Set's the DB Prefix to something new without needing to reconnect |
||
2188 | * |
||
2189 | * @param string the prefix |
||
2190 | * @return string |
||
2191 | */ |
||
2192 | public function set_dbprefix($prefix = '') |
||
2196 | |||
2197 | // -------------------------------------------------------------------- |
||
2198 | |||
2199 | /** |
||
2200 | * Track Aliases |
||
2201 | * |
||
2202 | * Used to track SQL statements written with aliased tables. |
||
2203 | * |
||
2204 | * @param string The table to inspect |
||
2205 | * @return string |
||
2206 | */ |
||
2207 | protected function _track_aliases($table) |
||
2241 | |||
2242 | // -------------------------------------------------------------------- |
||
2243 | |||
2244 | /** |
||
2245 | * Compile the SELECT statement |
||
2246 | * |
||
2247 | * Generates a query string based on which functions were used. |
||
2248 | * Should not be called directly. |
||
2249 | * |
||
2250 | * @param bool $select_override |
||
2251 | * @return string |
||
2252 | */ |
||
2253 | protected function _compile_select($select_override = FALSE) |
||
2311 | |||
2312 | // -------------------------------------------------------------------- |
||
2313 | |||
2314 | /** |
||
2315 | * Compile WHERE, HAVING statements |
||
2316 | * |
||
2317 | * Escapes identifiers in WHERE and HAVING statements at execution time. |
||
2318 | * |
||
2319 | * Required so that aliases are tracked properly, regardless of wether |
||
2320 | * where(), or_where(), having(), or_having are called prior to from(), |
||
2321 | * join() and dbprefix is added only if needed. |
||
2322 | * |
||
2323 | * @param string $qb_key 'qb_where' or 'qb_having' |
||
2324 | * @return string SQL statement |
||
2325 | */ |
||
2326 | protected function _compile_wh($qb_key) |
||
2387 | |||
2388 | // -------------------------------------------------------------------- |
||
2389 | |||
2390 | /** |
||
2391 | * Compile GROUP BY |
||
2392 | * |
||
2393 | * Escapes identifiers in GROUP BY statements at execution time. |
||
2394 | * |
||
2395 | * Required so that aliases are tracked properly, regardless of wether |
||
2396 | * group_by() is called prior to from(), join() and dbprefix is added |
||
2397 | * only if needed. |
||
2398 | * |
||
2399 | * @return string SQL statement |
||
2400 | */ |
||
2401 | protected function _compile_group_by() |
||
2423 | |||
2424 | // -------------------------------------------------------------------- |
||
2425 | |||
2426 | /** |
||
2427 | * Compile ORDER BY |
||
2428 | * |
||
2429 | * Escapes identifiers in ORDER BY statements at execution time. |
||
2430 | * |
||
2431 | * Required so that aliases are tracked properly, regardless of wether |
||
2432 | * order_by() is called prior to from(), join() and dbprefix is added |
||
2433 | * only if needed. |
||
2434 | * |
||
2435 | * @return string SQL statement |
||
2436 | */ |
||
2437 | protected function _compile_order_by() |
||
2460 | |||
2461 | // -------------------------------------------------------------------- |
||
2462 | |||
2463 | /** |
||
2464 | * Object to Array |
||
2465 | * |
||
2466 | * Takes an object as input and converts the class variables to array key/vals |
||
2467 | * |
||
2468 | * @param object |
||
2469 | * @return array |
||
2470 | */ |
||
2471 | protected function _object_to_array($object) |
||
2490 | |||
2491 | // -------------------------------------------------------------------- |
||
2492 | |||
2493 | /** |
||
2494 | * Object to Array |
||
2495 | * |
||
2496 | * Takes an object as input and converts the class variables to array key/vals |
||
2497 | * |
||
2498 | * @param object |
||
2499 | * @return array |
||
2500 | */ |
||
2501 | protected function _object_to_array_batch($object) |
||
2527 | |||
2528 | // -------------------------------------------------------------------- |
||
2529 | |||
2530 | /** |
||
2531 | * Start Cache |
||
2532 | * |
||
2533 | * Starts QB caching |
||
2534 | * |
||
2535 | * @return CI_DB_query_builder |
||
2536 | */ |
||
2537 | public function start_cache() |
||
2542 | |||
2543 | // -------------------------------------------------------------------- |
||
2544 | |||
2545 | /** |
||
2546 | * Stop Cache |
||
2547 | * |
||
2548 | * Stops QB caching |
||
2549 | * |
||
2550 | * @return CI_DB_query_builder |
||
2551 | */ |
||
2552 | public function stop_cache() |
||
2557 | |||
2558 | // -------------------------------------------------------------------- |
||
2559 | |||
2560 | /** |
||
2561 | * Flush Cache |
||
2562 | * |
||
2563 | * Empties the QB cache |
||
2564 | * |
||
2565 | * @return CI_DB_query_builder |
||
2566 | */ |
||
2567 | public function flush_cache() |
||
2584 | |||
2585 | // -------------------------------------------------------------------- |
||
2586 | |||
2587 | /** |
||
2588 | * Merge Cache |
||
2589 | * |
||
2590 | * When called, this function merges any cached QB arrays with |
||
2591 | * locally called ones. |
||
2592 | * |
||
2593 | * @return void |
||
2594 | */ |
||
2595 | protected function _merge_cache() |
||
2638 | |||
2639 | // -------------------------------------------------------------------- |
||
2640 | |||
2641 | /** |
||
2642 | * Is literal |
||
2643 | * |
||
2644 | * Determines if a string represents a literal value or a field name |
||
2645 | * |
||
2646 | * @param string $str |
||
2647 | * @return bool |
||
2648 | */ |
||
2649 | protected function _is_literal($str) |
||
2668 | |||
2669 | // -------------------------------------------------------------------- |
||
2670 | |||
2671 | /** |
||
2672 | * Reset Query Builder values. |
||
2673 | * |
||
2674 | * Publicly-visible method to reset the QB values. |
||
2675 | * |
||
2676 | * @return CI_DB_query_builder |
||
2677 | */ |
||
2678 | public function reset_query() |
||
2684 | |||
2685 | // -------------------------------------------------------------------- |
||
2686 | |||
2687 | /** |
||
2688 | * Resets the query builder values. Called by the get() function |
||
2689 | * |
||
2690 | * @param array An array of fields to reset |
||
2691 | * @return void |
||
2692 | */ |
||
2693 | protected function _reset_run($qb_reset_items) |
||
2700 | |||
2701 | // -------------------------------------------------------------------- |
||
2702 | |||
2703 | /** |
||
2704 | * Resets the query builder values. Called by the get() function |
||
2705 | * |
||
2706 | * @return void |
||
2707 | */ |
||
2708 | protected function _reset_select() |
||
2725 | |||
2726 | // -------------------------------------------------------------------- |
||
2727 | |||
2728 | /** |
||
2729 | * Resets the query builder "write" values. |
||
2730 | * |
||
2731 | * Called by the insert() update() insert_batch() update_batch() and delete() functions |
||
2732 | * |
||
2733 | * @return void |
||
2734 | */ |
||
2735 | protected function _reset_write() |
||
2747 | |||
2748 | } |
||
2749 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.