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 ORM 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 ORM, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
42 | class ORM implements \ArrayAccess |
||
43 | { |
||
44 | |||
45 | // ----------------------- // |
||
46 | // --- CLASS CONSTANTS --- // |
||
47 | // ----------------------- // |
||
48 | |||
49 | // WHERE and HAVING condition array keys |
||
50 | const CONDITION_FRAGMENT = 0; |
||
51 | const CONDITION_VALUES = 1; |
||
52 | const DEFAULT_CONNECTION = 'default'; |
||
53 | |||
54 | // Limit clause style |
||
55 | const LIMIT_STYLE_TOP_N = 'top'; |
||
56 | const LIMIT_STYLE_LIMIT = 'limit'; |
||
57 | |||
58 | // ------------------------ // |
||
59 | // --- CLASS PROPERTIES --- // |
||
60 | // ------------------------ // |
||
61 | |||
62 | /** |
||
63 | * Class configuration |
||
64 | * |
||
65 | * @var array |
||
66 | */ |
||
67 | protected static $_default_config = array( |
||
68 | 'connection_string' => 'sqlite::memory:', |
||
69 | 'id_column' => 'id', |
||
70 | 'id_column_overrides' => array(), |
||
71 | 'error_mode' => \PDO::ERRMODE_EXCEPTION, |
||
72 | 'username' => null, |
||
73 | 'password' => null, |
||
74 | 'driver_options' => null, |
||
75 | 'identifier_quote_character' => null, // if this is null, will be autodetected |
||
76 | 'limit_clause_style' => null, // if this is null, will be autodetected |
||
77 | 'logging' => false, |
||
78 | 'logger' => null, |
||
79 | 'caching' => false, |
||
80 | 'caching_auto_clear' => false, |
||
81 | 'return_result_sets' => false, |
||
82 | ); |
||
83 | |||
84 | /** |
||
85 | * Map of configuration settings |
||
86 | * |
||
87 | * @var array |
||
88 | */ |
||
89 | protected static $_config = array(); |
||
90 | |||
91 | /** |
||
92 | * Map of database connections, instances of the PDO class |
||
93 | * |
||
94 | * @var array |
||
95 | */ |
||
96 | protected static $_db = array(); |
||
97 | |||
98 | /** |
||
99 | * Last query run, only populated if logging is enabled |
||
100 | * |
||
101 | * @var string |
||
102 | */ |
||
103 | protected static $_last_query; |
||
104 | |||
105 | /** |
||
106 | * Log of all queries run, mapped by connection key, only populated if logging is enabled |
||
107 | * |
||
108 | * @var array |
||
109 | */ |
||
110 | protected static $_query_log = array(); |
||
111 | |||
112 | /** |
||
113 | * Query cache, only used if query caching is enabled |
||
114 | * |
||
115 | * @var array |
||
116 | */ |
||
117 | protected static $_query_cache = array(); |
||
118 | |||
119 | /** |
||
120 | * Reference to previously used PDOStatement object to enable low-level access, if needed |
||
121 | * |
||
122 | * @var null|\PDOStatement |
||
123 | */ |
||
124 | protected static $_last_statement = null; |
||
125 | |||
126 | // --------------------------- // |
||
127 | // --- INSTANCE PROPERTIES --- // |
||
128 | // --------------------------- // |
||
129 | |||
130 | /** |
||
131 | * Key name of the connections in static::$_db used by this instance |
||
132 | * |
||
133 | * @var string |
||
134 | */ |
||
135 | protected $_connection_name; |
||
136 | |||
137 | /** |
||
138 | * The name of the table the current ORM instance is associated with |
||
139 | * |
||
140 | * @var string |
||
141 | */ |
||
142 | protected $_table_name; |
||
143 | |||
144 | /** |
||
145 | * Alias for the table to be used in SELECT queries |
||
146 | * |
||
147 | * @var null|string |
||
148 | */ |
||
149 | protected $_table_alias = null; |
||
150 | |||
151 | /** |
||
152 | * Values to be bound to the query |
||
153 | * |
||
154 | * @var array |
||
155 | */ |
||
156 | protected $_values = array(); |
||
157 | |||
158 | /** |
||
159 | * Columns to select in the result |
||
160 | * |
||
161 | * @var array |
||
162 | */ |
||
163 | protected $_result_columns = array('*'); |
||
164 | |||
165 | /** |
||
166 | * Are we using the default result column or have these been manually changed? |
||
167 | * |
||
168 | * @var bool |
||
169 | */ |
||
170 | protected $_using_default_result_columns = true; |
||
171 | |||
172 | /** |
||
173 | * Join sources |
||
174 | * |
||
175 | * @var array |
||
176 | */ |
||
177 | protected $_join_sources = array(); |
||
178 | |||
179 | /** |
||
180 | * Should the query include a DISTINCT keyword? |
||
181 | * |
||
182 | * @var bool |
||
183 | */ |
||
184 | protected $_distinct = false; |
||
185 | |||
186 | /** |
||
187 | * Is this a raw query? |
||
188 | * |
||
189 | * @var bool |
||
190 | */ |
||
191 | protected $_is_raw_query = false; |
||
192 | |||
193 | /** |
||
194 | * The raw query |
||
195 | * |
||
196 | * @var string |
||
197 | */ |
||
198 | protected $_raw_query = ''; |
||
199 | |||
200 | /** |
||
201 | * The raw query parameters |
||
202 | * |
||
203 | * @var array |
||
204 | */ |
||
205 | protected $_raw_parameters = array(); |
||
206 | |||
207 | /** |
||
208 | * Array of WHERE clauses |
||
209 | * |
||
210 | * @var array |
||
211 | */ |
||
212 | protected $_where_conditions = array(); |
||
213 | |||
214 | /** |
||
215 | * LIMIT |
||
216 | * |
||
217 | * @var null|int |
||
218 | */ |
||
219 | protected $_limit = null; |
||
220 | |||
221 | /** |
||
222 | * OFFSET |
||
223 | * |
||
224 | * @var null|int |
||
225 | */ |
||
226 | protected $_offset = null; |
||
227 | |||
228 | /** |
||
229 | * ORDER BY |
||
230 | * |
||
231 | * @var array |
||
232 | */ |
||
233 | protected $_order_by = array(); |
||
234 | |||
235 | /** |
||
236 | * GROUP BY |
||
237 | * |
||
238 | * @var array |
||
239 | */ |
||
240 | protected $_group_by = array(); |
||
241 | |||
242 | /** |
||
243 | * HAVING |
||
244 | * |
||
245 | * @var array |
||
246 | */ |
||
247 | protected $_having_conditions = array(); |
||
248 | |||
249 | /** |
||
250 | * The data for a hydrated instance of the class |
||
251 | * |
||
252 | * @var array |
||
253 | */ |
||
254 | protected $_data = array(); |
||
255 | |||
256 | /** |
||
257 | * Fields that have been modified during the lifetime of the object |
||
258 | * |
||
259 | * @var array |
||
260 | */ |
||
261 | protected $_dirty_fields = array(); |
||
262 | |||
263 | /** |
||
264 | * Fields that are to be inserted in the DB raw |
||
265 | * |
||
266 | * @var array |
||
267 | */ |
||
268 | protected $_expr_fields = array(); |
||
269 | |||
270 | /** |
||
271 | * Is this a new object (has create() been called)? |
||
272 | * |
||
273 | * @var bool |
||
274 | */ |
||
275 | protected $_is_new = false; |
||
276 | |||
277 | /** |
||
278 | * Name of the column to use as the primary key for |
||
279 | * this instance only. Overrides the config settings. |
||
280 | * |
||
281 | * @var null|string |
||
282 | */ |
||
283 | protected $_instance_id_column = null; |
||
284 | |||
285 | /** |
||
286 | * Refresh cache for current query? |
||
287 | * |
||
288 | * @var bool |
||
289 | */ |
||
290 | protected $_refresh_cache = false; |
||
291 | |||
292 | /** |
||
293 | * Disable caching for current query? |
||
294 | * |
||
295 | * @var bool |
||
296 | */ |
||
297 | protected $_no_caching = false; |
||
298 | |||
299 | // ---------------------- // |
||
300 | // --- STATIC METHODS --- // |
||
301 | // ---------------------- // |
||
302 | |||
303 | /** |
||
304 | * Pass configuration settings to the class in the form of |
||
305 | * key/value pairs. As a shortcut, if the second argument |
||
306 | * is omitted and the key is a string, the setting is |
||
307 | * assumed to be the DSN string used by PDO to connect |
||
308 | * to the database (often, this will be the only configuration |
||
309 | * required to use Idiorm). If you have more than one setting |
||
310 | * you wish to configure, another shortcut is to pass an array |
||
311 | * of settings (and omit the second argument). |
||
312 | * |
||
313 | * @param string $key |
||
314 | * @param mixed $value |
||
315 | * @param string $connection_name Which connection to use |
||
316 | */ |
||
317 | 232 | public static function configure($key, $value = null, $connection_name = self::DEFAULT_CONNECTION) |
|
318 | { |
||
319 | 232 | static::_setup_db_config($connection_name); //ensures at least default config is set |
|
320 | |||
321 | 232 | if (is_array($key)) { |
|
322 | // Shortcut: If only one array argument is passed, |
||
323 | // assume it's an array of configuration settings |
||
324 | foreach ($key as $conf_key => $conf_value) { |
||
325 | static::configure($conf_key, $conf_value, $connection_name); |
||
326 | } |
||
327 | } else { |
||
328 | 232 | if (null === $value) { |
|
329 | // Shortcut: If only one string argument is passed, |
||
330 | // assume it's a connection string |
||
331 | 5 | $value = $key; |
|
332 | 5 | $key = 'connection_string'; |
|
333 | 5 | } |
|
334 | 232 | static::$_config[$connection_name][$key] = $value; |
|
335 | } |
||
336 | 232 | } |
|
337 | |||
338 | /** |
||
339 | * Retrieve configuration options by key, or as whole array. |
||
340 | * |
||
341 | * @param string|null $key |
||
342 | * @param string $connection_name Which connection to use |
||
343 | * |
||
344 | * @return mixed |
||
345 | */ |
||
346 | 3 | public static function get_config($key = null, $connection_name = self::DEFAULT_CONNECTION) |
|
354 | |||
355 | /** |
||
356 | * Delete all configs in _config array. |
||
357 | */ |
||
358 | 145 | public static function reset_config() |
|
362 | |||
363 | /** |
||
364 | * Despite its slightly odd name, this is actually the factory |
||
365 | * method used to acquire instances of the class. It is named |
||
366 | * this way for the sake of a readable interface, ie |
||
367 | * ORM::for_table('table_name')->find_one()-> etc. As such, |
||
368 | * this will normally be the first method called in a chain. |
||
369 | * |
||
370 | * @param string $table_name |
||
371 | * @param string $connection_name Which connection to use |
||
372 | * |
||
373 | * @return ORM |
||
374 | */ |
||
375 | 220 | public static function for_table($table_name, $connection_name = self::DEFAULT_CONNECTION) |
|
381 | |||
382 | /** |
||
383 | * Set up the database connection used by the class |
||
384 | * |
||
385 | * @param string $connection_name Which connection to use |
||
386 | */ |
||
387 | 231 | protected static function _setup_db($connection_name = self::DEFAULT_CONNECTION) |
|
404 | |||
405 | /** |
||
406 | * Ensures configuration (multiple connections) is at least set to default. |
||
407 | * |
||
408 | * @param string $connection_name Which connection to use |
||
409 | */ |
||
410 | 232 | protected static function _setup_db_config($connection_name) |
|
416 | |||
417 | /** |
||
418 | * Set the PDO object used by Idiorm to communicate with the database. |
||
419 | * This is public in case the ORM should use a ready-instantiated |
||
420 | * PDO object as its database connection. Accepts an optional string key |
||
421 | * to identify the connection if multiple connections are used. |
||
422 | * |
||
423 | * @param \PDO $db |
||
424 | * @param string $connection_name Which connection to use |
||
425 | */ |
||
426 | 232 | public static function set_db($db, $connection_name = self::DEFAULT_CONNECTION) |
|
436 | |||
437 | /** |
||
438 | * Delete all registered PDO objects in _db array. |
||
439 | */ |
||
440 | 145 | public static function reset_db() |
|
444 | |||
445 | /** |
||
446 | * Detect and initialise the character used to quote identifiers |
||
447 | * (table names, column names etc). If this has been specified |
||
448 | * manually using ORM::configure('identifier_quote_character', 'some-char'), |
||
449 | * this will do nothing. |
||
450 | * |
||
451 | * @param string $connection_name Which connection to use |
||
452 | */ |
||
453 | 232 | protected static function _setup_identifier_quote_character($connection_name) |
|
459 | |||
460 | /** |
||
461 | * Detect and initialise the limit clause style ("SELECT TOP 5" / |
||
462 | * "... LIMIT 5"). If this has been specified manually using |
||
463 | * ORM::configure('limit_clause_style', 'top'), this will do nothing. |
||
464 | * |
||
465 | * @param string $connection_name Which connection to use |
||
466 | */ |
||
467 | 232 | public static function _setup_limit_clause_style($connection_name) |
|
473 | |||
474 | /** |
||
475 | * Return the correct character used to quote identifiers (table |
||
476 | * names, column names etc) by looking at the driver being used by PDO. |
||
477 | * |
||
478 | * @param string $connection_name Which connection to use |
||
479 | * |
||
480 | * @return string |
||
481 | */ |
||
482 | 145 | protected static function _detect_identifier_quote_character($connection_name) |
|
499 | |||
500 | /** |
||
501 | * Returns a constant after determining the appropriate limit clause |
||
502 | * style |
||
503 | * |
||
504 | * @param string $connection_name Which connection to use |
||
505 | * |
||
506 | * @return string Limit clause style keyword/constant |
||
507 | */ |
||
508 | 145 | protected static function _detect_limit_clause_style($connection_name) |
|
519 | |||
520 | /** |
||
521 | * Returns the PDO instance used by the the ORM to communicate with |
||
522 | * the database. This can be called if any low-level DB access is |
||
523 | * required outside the class. If multiple connections are used, |
||
524 | * accepts an optional key name for the connection. |
||
525 | * |
||
526 | * @param string $connection_name Which connection to use |
||
527 | * |
||
528 | * @return \PDO |
||
529 | */ |
||
530 | 231 | public static function get_db($connection_name = self::DEFAULT_CONNECTION) |
|
535 | |||
536 | /** |
||
537 | * Executes a raw query as a wrapper for PDOStatement::execute. |
||
538 | * Useful for queries that can't be accomplished through Idiorm, |
||
539 | * particularly those using engine-specific features. |
||
540 | * |
||
541 | * @example raw_execute('SELECT `name`, AVG(`order`) FROM `customer` GROUP BY `name` HAVING AVG(`order`) > 10') |
||
542 | * @example raw_execute('INSERT OR REPLACE INTO `widget` (`id`, `name`) SELECT `id`, `name` FROM `other_table`') |
||
543 | * |
||
544 | * @param string $query The raw SQL query |
||
545 | * @param array $parameters Optional bound parameters |
||
546 | * @param string $connection_name Which connection to use |
||
547 | * |
||
548 | * @return bool Success |
||
549 | */ |
||
550 | 1 | public static function raw_execute($query, $parameters = array(), $connection_name = self::DEFAULT_CONNECTION) |
|
556 | |||
557 | /** |
||
558 | * Returns the PDOStatement instance last used by any connection wrapped by the ORM. |
||
559 | * Useful for access to PDOStatement::rowCount() or error information |
||
560 | * |
||
561 | * @return \PDOStatement |
||
562 | */ |
||
563 | 199 | public static function get_last_statement() |
|
567 | |||
568 | /** |
||
569 | * Internal helper method for executing statements. Logs queries, and |
||
570 | * stores statement object in ::_last_statement, accessible publicly |
||
571 | * through ::get_last_statement() |
||
572 | * |
||
573 | * @param string $query |
||
574 | * @param array $parameters An array of parameters to be bound in to the query |
||
575 | * @param string $connection_name Which connection to use |
||
576 | * |
||
577 | * @return bool Response of PDOStatement::execute() |
||
578 | * |
||
579 | * @throws \Exception |
||
580 | */ |
||
581 | 211 | protected static function _execute($query, $parameters = array(), $connection_name = self::DEFAULT_CONNECTION) |
|
614 | |||
615 | /** |
||
616 | * Add a query to the internal query log. Only works if the |
||
617 | * 'logging' config option is set to true. |
||
618 | * |
||
619 | * This works by manually binding the parameters to the query - the |
||
620 | * query isn't executed like this (PDO normally passes the query and |
||
621 | * parameters to the database which takes care of the binding) but |
||
622 | * doing it this way makes the logged queries more readable. |
||
623 | * |
||
624 | * @param string $query |
||
625 | * @param array $parameters An array of parameters to be bound in to the query |
||
626 | * @param string $connection_name Which connection to use |
||
627 | * @param float $query_time Query time |
||
628 | * |
||
629 | * @return bool |
||
630 | */ |
||
631 | 211 | protected static function _log_query($query, $parameters, $connection_name, $query_time) |
|
680 | |||
681 | /** |
||
682 | * Get the last query executed. Only works if the |
||
683 | * 'logging' config option is set to true. Otherwise |
||
684 | * this will return null. Returns last query from all connections if |
||
685 | * no connection_name is specified |
||
686 | * |
||
687 | * @param null|string $connection_name Which connection to use |
||
688 | * |
||
689 | * @return string |
||
690 | */ |
||
691 | 200 | public static function get_last_query($connection_name = null) |
|
702 | |||
703 | /** |
||
704 | * Get an array containing all the queries run on a |
||
705 | * specified connection up to now. |
||
706 | * Only works if the 'logging' config option is |
||
707 | * set to true. Otherwise, returned array will be empty. |
||
708 | * |
||
709 | * @param string $connection_name Which connection to use |
||
710 | * |
||
711 | * @return array |
||
712 | */ |
||
713 | public static function get_query_log($connection_name = self::DEFAULT_CONNECTION) |
||
721 | |||
722 | /** |
||
723 | * Get a list of the available connection names |
||
724 | * |
||
725 | * @return array |
||
726 | */ |
||
727 | public static function get_connection_names() |
||
731 | |||
732 | // ------------------------ // |
||
733 | // --- INSTANCE METHODS --- // |
||
734 | // ------------------------ // |
||
735 | |||
736 | /** |
||
737 | * "Private" constructor; shouldn't be called directly. |
||
738 | * Use the ORM::for_table factory method instead. |
||
739 | * |
||
740 | * @param string $table_name |
||
741 | * @param array $data |
||
742 | * @param string $connection_name |
||
743 | */ |
||
744 | 220 | protected function __construct($table_name, $data = array(), $connection_name = self::DEFAULT_CONNECTION) |
|
752 | |||
753 | /** |
||
754 | * Create a new, empty instance of the class. Used |
||
755 | * to add a new row to your database. May optionally |
||
756 | * be passed an associative array of data to populate |
||
757 | * the instance. If so, all fields will be flagged as |
||
758 | * dirty so all will be saved to the database when |
||
759 | * save() is called. |
||
760 | * |
||
761 | * @param mixed $data |
||
762 | * |
||
763 | * @return $this |
||
764 | */ |
||
765 | 19 | public function create($data = null) |
|
774 | |||
775 | /** |
||
776 | * Specify the ID column to use for this instance or array of instances only. |
||
777 | * This overrides the id_column and id_column_overrides settings. |
||
778 | * |
||
779 | * This is mostly useful for libraries built on top of Idiorm, and will |
||
780 | * not normally be used in manually built queries. If you don't know why |
||
781 | * you would want to use this, you should probably just ignore it. |
||
782 | * |
||
783 | * @param string $id_column |
||
784 | * |
||
785 | * @return $this |
||
786 | */ |
||
787 | 200 | public function use_id_column($id_column) |
|
793 | |||
794 | /** |
||
795 | * Create an ORM instance from the given row (an associative |
||
796 | * array of data fetched from the database) |
||
797 | * |
||
798 | * @param array $row |
||
799 | * |
||
800 | * @return ORM |
||
801 | */ |
||
802 | 196 | protected function _create_instance_from_row($row) |
|
810 | |||
811 | /** |
||
812 | * Tell the ORM that you are expecting a single result |
||
813 | * back from your query, and execute it. Will return |
||
814 | * a single instance of the ORM class, or false if no |
||
815 | * rows were returned. |
||
816 | * As a shortcut, you may supply an ID as a parameter |
||
817 | * to this method. This will perform a primary key |
||
818 | * lookup on the table. |
||
819 | * |
||
820 | * @param mixed $id |
||
821 | * |
||
822 | * @return false|ORM false on error |
||
823 | */ |
||
824 | 86 | public function find_one($id = null) |
|
825 | { |
||
826 | 86 | if (null !== $id) { |
|
827 | 31 | $this->where_id_is($id); |
|
828 | 31 | } |
|
829 | 86 | $this->limit(1); |
|
830 | 86 | $rows = $this->_run(); |
|
831 | |||
832 | 86 | if (empty($rows)) { |
|
833 | 2 | return false; |
|
834 | } |
||
835 | |||
836 | 85 | return $this->_create_instance_from_row($rows[0]); |
|
837 | } |
||
838 | |||
839 | /** |
||
840 | * Tell the ORM that you are expecting multiple results |
||
841 | * from your query, and execute it. Will return an array |
||
842 | * of instances of the ORM class, or an empty array if |
||
843 | * no rows were returned. |
||
844 | * |
||
845 | * @return array|IdiormResultSet |
||
846 | */ |
||
847 | 110 | public function find_many() |
|
855 | |||
856 | /** |
||
857 | * Tell the ORM that you are expecting multiple results |
||
858 | * from your query, and execute it. Will return an array |
||
859 | * of instances of the ORM class, or an empty array if |
||
860 | * no rows were returned. |
||
861 | * |
||
862 | * @return array |
||
863 | */ |
||
864 | 112 | protected function _find_many() |
|
870 | |||
871 | /** |
||
872 | * Tell the ORM that you are expecting multiple results |
||
873 | * from your query, and execute it. Will return a result set object |
||
874 | * containing instances of the ORM class. |
||
875 | * |
||
876 | * @return IdiormResultSet |
||
877 | */ |
||
878 | 3 | public function find_result_set() |
|
882 | |||
883 | /** |
||
884 | * Tell the ORM that you are expecting multiple results |
||
885 | * from your query, and execute it. Will return an array, |
||
886 | * or an empty array if no rows were returned. |
||
887 | * |
||
888 | * @return array |
||
889 | */ |
||
890 | 1 | public function find_array() |
|
894 | |||
895 | /** |
||
896 | * Tell the ORM that you wish to execute a COUNT query. |
||
897 | * Will return an integer representing the number of |
||
898 | * rows returned. |
||
899 | * |
||
900 | * @param string $column |
||
901 | * |
||
902 | * @return int |
||
903 | */ |
||
904 | 4 | public function count($column = '*') |
|
908 | |||
909 | /** |
||
910 | * Tell the ORM that you wish to execute a MAX query. |
||
911 | * Will return the max value of the choosen column. |
||
912 | * |
||
913 | * @param string $column |
||
914 | * |
||
915 | * @return int |
||
916 | */ |
||
917 | 2 | public function max($column) |
|
921 | |||
922 | /** |
||
923 | * Tell the ORM that you wish to execute a MIN query. |
||
924 | * Will return the min value of the choosen column. |
||
925 | * |
||
926 | * @param string $column |
||
927 | * |
||
928 | * @return int |
||
929 | */ |
||
930 | 2 | public function min($column) |
|
934 | |||
935 | /** |
||
936 | * Tell the ORM that you wish to execute a AVG query. |
||
937 | * Will return the average value of the choosen column. |
||
938 | * |
||
939 | * @param string $column |
||
940 | * |
||
941 | * @return int |
||
942 | */ |
||
943 | 2 | public function avg($column) |
|
947 | |||
948 | /** |
||
949 | * Tell the ORM that you wish to execute a SUM query. |
||
950 | * Will return the sum of the choosen column. |
||
951 | * |
||
952 | * @param string $column |
||
953 | * |
||
954 | * @return int |
||
955 | */ |
||
956 | 2 | public function sum($column) |
|
960 | |||
961 | /** |
||
962 | * Execute an aggregate query on the current connection. |
||
963 | * |
||
964 | * @param string $sql_function The aggregate function to call eg. MIN, COUNT, etc |
||
965 | * @param string $column The column to execute the aggregate query against |
||
966 | * |
||
967 | * @return int |
||
968 | */ |
||
969 | 12 | protected function _call_aggregate_db_function($sql_function, $column) |
|
995 | |||
996 | /** |
||
997 | * This method can be called to hydrate (populate) this |
||
998 | * instance of the class from an associative array of data. |
||
999 | * This will usually be called only from inside the class, |
||
1000 | * but it's public in case you need to call it directly. |
||
1001 | * |
||
1002 | * @param array $data |
||
1003 | * |
||
1004 | * @return $this |
||
1005 | */ |
||
1006 | 200 | public function hydrate($data = array()) |
|
1012 | |||
1013 | /** |
||
1014 | * Force the ORM to flag all the fields in the $data array |
||
1015 | * as "dirty" and therefore update them when save() is called. |
||
1016 | */ |
||
1017 | 6 | public function force_all_dirty() |
|
1023 | |||
1024 | /** |
||
1025 | * Perform a raw query. The query can contain placeholders in |
||
1026 | * either named or question mark style. If placeholders are |
||
1027 | * used, the parameters should be an array of values which will |
||
1028 | * be bound to the placeholders in the query. If this method |
||
1029 | * is called, all other query building methods will be ignored. |
||
1030 | * |
||
1031 | * @param string $query |
||
1032 | * @param array $parameters |
||
1033 | * |
||
1034 | * @return $this |
||
1035 | */ |
||
1036 | 4 | public function raw_query($query, $parameters = array()) |
|
1044 | |||
1045 | /** |
||
1046 | * Add an alias for the main table to be used in SELECT queries |
||
1047 | * |
||
1048 | * @param string $alias |
||
1049 | * |
||
1050 | * @return $this |
||
1051 | */ |
||
1052 | 4 | public function table_alias($alias) |
|
1058 | |||
1059 | /** |
||
1060 | * Internal method to add an unquoted expression to the set |
||
1061 | * of columns returned by the SELECT query. The second optional |
||
1062 | * argument is the alias to return the expression as. |
||
1063 | * |
||
1064 | * @param string $expr |
||
1065 | * @param mixed $alias |
||
1066 | * |
||
1067 | * @return $this |
||
1068 | */ |
||
1069 | 34 | protected function _add_result_column($expr, $alias = null) |
|
1084 | |||
1085 | /** |
||
1086 | * Counts the number of columns that belong to the primary |
||
1087 | * key and their value is null. |
||
1088 | */ |
||
1089 | 12 | public function count_null_id_columns() |
|
1097 | |||
1098 | /** |
||
1099 | * Add a column to the list of columns returned by the SELECT |
||
1100 | * query. This defaults to '*'. The second optional argument is |
||
1101 | * the alias to return the column as. |
||
1102 | * |
||
1103 | * @param string $column |
||
1104 | * @param mixed $alias |
||
1105 | * |
||
1106 | * @return ORM |
||
1107 | */ |
||
1108 | 20 | public function select($column, $alias = null) |
|
1114 | |||
1115 | /** |
||
1116 | * Add an unquoted expression to the list of columns returned |
||
1117 | * by the SELECT query. The second optional argument is |
||
1118 | * the alias to return the column as. |
||
1119 | * |
||
1120 | * @param string $expr |
||
1121 | * @param mixed $alias |
||
1122 | * |
||
1123 | * @return ORM |
||
1124 | */ |
||
1125 | 16 | public function select_expr($expr, $alias = null) |
|
1129 | |||
1130 | /** |
||
1131 | * Add columns to the list of columns returned by the SELECT |
||
1132 | * query. This defaults to '*'. Many columns can be supplied |
||
1133 | * as either an array or as a list of parameters to the method. |
||
1134 | * |
||
1135 | * Note that the alias must not be numeric - if you want a |
||
1136 | * numeric alias then prepend it with some alpha chars. eg. a1 |
||
1137 | * |
||
1138 | * @example select_many(array('alias' => 'column', 'column2', 'alias2' => 'column3'), 'column4', 'column5'); |
||
1139 | * @example select_many('column', 'column2', 'column3'); |
||
1140 | * @example select_many(array('column', 'column2', 'column3'), 'column4', 'column5'); |
||
1141 | * |
||
1142 | * @return ORM |
||
1143 | */ |
||
1144 | 2 | View Code Duplication | public function select_many() |
1159 | |||
1160 | /** |
||
1161 | * Add an unquoted expression to the list of columns returned |
||
1162 | * by the SELECT query. Many columns can be supplied as either |
||
1163 | * an array or as a list of parameters to the method. |
||
1164 | * |
||
1165 | * Note that the alias must not be numeric - if you want a |
||
1166 | * numeric alias then prepend it with some alpha chars. eg. a1 |
||
1167 | * |
||
1168 | * @example select_many_expr(array('alias' => 'column', 'column2', 'alias2' => 'column3'), 'column4', 'column5') |
||
1169 | * @example select_many_expr('column', 'column2', 'column3') |
||
1170 | * @example select_many_expr(array('column', 'column2', 'column3'), 'column4', 'column5') |
||
1171 | * |
||
1172 | * @return ORM |
||
1173 | */ |
||
1174 | 2 | View Code Duplication | public function select_many_expr() |
1189 | |||
1190 | /** |
||
1191 | * Take a column specification for the select many methods and convert it |
||
1192 | * into a normalised array of columns and aliases. |
||
1193 | * |
||
1194 | * It is designed to turn the following styles into a normalised array: |
||
1195 | * |
||
1196 | * array(array('alias' => 'column', 'column2', 'alias2' => 'column3'), 'column4', 'column5')) |
||
1197 | * |
||
1198 | * @param array $columns |
||
1199 | * |
||
1200 | * @return array |
||
1201 | */ |
||
1202 | 4 | protected function _normalise_select_many_columns($columns) |
|
1221 | |||
1222 | /** |
||
1223 | * Add a DISTINCT keyword before the list of columns in the SELECT query |
||
1224 | */ |
||
1225 | 2 | public function distinct() |
|
1231 | |||
1232 | /** |
||
1233 | * Internal method to add a JOIN source to the query. |
||
1234 | * |
||
1235 | * The join_operator should be one of INNER, LEFT OUTER, CROSS etc - this |
||
1236 | * will be prepended to JOIN. |
||
1237 | * |
||
1238 | * The table should be the name of the table to join to. |
||
1239 | * |
||
1240 | * The constraint may be either a string or an array with three elements. If it |
||
1241 | * is a string, it will be compiled into the query as-is, with no escaping. The |
||
1242 | * recommended way to supply the constraint is as an array with three elements: |
||
1243 | * |
||
1244 | * first_column, operator, second_column |
||
1245 | * |
||
1246 | * Example: array('user.id', '=', 'profile.user_id') |
||
1247 | * |
||
1248 | * will compile to |
||
1249 | * |
||
1250 | * ON `user`.`id` = `profile`.`user_id` |
||
1251 | * |
||
1252 | * The final (optional) argument specifies an alias for the joined table. |
||
1253 | * |
||
1254 | * @param string $join_operator |
||
1255 | * @param string $table |
||
1256 | * @param string $constraint |
||
1257 | * @param string|null $table_alias |
||
1258 | * |
||
1259 | * @return $this |
||
1260 | */ |
||
1261 | 20 | protected function _add_join_source($join_operator, $table, $constraint, $table_alias = null) |
|
1285 | |||
1286 | /** |
||
1287 | * Add a RAW JOIN source to the query |
||
1288 | * |
||
1289 | * @param string $table |
||
1290 | * @param string $constraint |
||
1291 | * @param string $table_alias |
||
1292 | * @param array $parameters |
||
1293 | * |
||
1294 | * @return $this |
||
1295 | */ |
||
1296 | 6 | public function raw_join($table, $constraint, $table_alias, $parameters = array()) |
|
1318 | |||
1319 | /** |
||
1320 | * Add a simple JOIN source to the query |
||
1321 | * |
||
1322 | * @param string $table |
||
1323 | * @param string $constraint |
||
1324 | * @param string|null $table_alias |
||
1325 | * |
||
1326 | * @return ORM |
||
1327 | */ |
||
1328 | 12 | public function join($table, $constraint, $table_alias = null) |
|
1332 | |||
1333 | /** |
||
1334 | * Add an INNER JOIN souce to the query |
||
1335 | */ |
||
1336 | /** |
||
1337 | * @param string $table |
||
1338 | * @param string $constraint |
||
1339 | * @param null|string $table_alias |
||
1340 | * |
||
1341 | * @return ORM |
||
1342 | */ |
||
1343 | 2 | public function inner_join($table, $constraint, $table_alias = null) |
|
1347 | |||
1348 | /** |
||
1349 | * Add an LEFT JOIN source to the query |
||
1350 | */ |
||
1351 | public function left_join($table, $constraint, $table_alias = null) { |
||
1354 | |||
1355 | /** |
||
1356 | * Add an RIGHT JOIN source to the query |
||
1357 | */ |
||
1358 | public function right_join($table, $constraint, $table_alias = null) { |
||
1361 | |||
1362 | /** |
||
1363 | * Add a LEFT OUTER JOIN souce to the query |
||
1364 | * |
||
1365 | * @param string $table |
||
1366 | * @param string $constraint |
||
1367 | * @param null|string $table_alias |
||
1368 | * |
||
1369 | * @return $this|ORM |
||
1370 | */ |
||
1371 | 2 | public function left_outer_join($table, $constraint, $table_alias = null) |
|
1375 | |||
1376 | /** |
||
1377 | * Add an RIGHT OUTER JOIN souce to the query |
||
1378 | * |
||
1379 | * @param string $table |
||
1380 | * @param string $constraint |
||
1381 | * @param null|string $table_alias |
||
1382 | * |
||
1383 | * @return $this|ORM |
||
1384 | */ |
||
1385 | 2 | public function right_outer_join($table, $constraint, $table_alias = null) |
|
1389 | |||
1390 | /** |
||
1391 | * Add an FULL OUTER JOIN souce to the query |
||
1392 | */ |
||
1393 | /** |
||
1394 | * @param string $table |
||
1395 | * @param string $constraint |
||
1396 | * @param null|string $table_alias |
||
1397 | * |
||
1398 | * @return ORM |
||
1399 | */ |
||
1400 | 2 | public function full_outer_join($table, $constraint, $table_alias = null) |
|
1404 | |||
1405 | /** |
||
1406 | * Internal method to add a HAVING condition to the query |
||
1407 | */ |
||
1408 | /** |
||
1409 | * @param string $fragment |
||
1410 | * @param array $values |
||
1411 | * |
||
1412 | * @return ORM |
||
1413 | */ |
||
1414 | 10 | protected function _add_having($fragment, $values = array()) |
|
1418 | |||
1419 | /** |
||
1420 | * Internal method to add a HAVING condition to the query |
||
1421 | */ |
||
1422 | /** |
||
1423 | * @param string|array $column_name |
||
1424 | * @param string $separator |
||
1425 | * @param mixed $value |
||
1426 | * |
||
1427 | * @return $this|ORM |
||
1428 | */ |
||
1429 | 14 | protected function _add_simple_having($column_name, $separator, $value) |
|
1433 | |||
1434 | /** |
||
1435 | * Internal method to add a HAVING clause with multiple values (like IN and NOT IN) |
||
1436 | */ |
||
1437 | /** |
||
1438 | * @param string|array $column_name |
||
1439 | * @param string $separator |
||
1440 | * @param mixed $values |
||
1441 | * |
||
1442 | * @return ORM |
||
1443 | */ |
||
1444 | 4 | View Code Duplication | public function _add_having_placeholder($column_name, $separator, $values) |
1461 | |||
1462 | /** |
||
1463 | * Internal method to add a HAVING clause with no parameters(like IS NULL and IS NOT NULL) |
||
1464 | * |
||
1465 | * @param string|array $column_name |
||
1466 | * @param string $operator |
||
1467 | * |
||
1468 | * @return ORM |
||
1469 | */ |
||
1470 | 4 | View Code Duplication | public function _add_having_no_value($column_name, $operator) |
1486 | |||
1487 | /** |
||
1488 | * Internal method to add a WHERE condition to the query |
||
1489 | * |
||
1490 | * @param string $fragment |
||
1491 | * @param array $values |
||
1492 | * |
||
1493 | * @return ORM |
||
1494 | */ |
||
1495 | 27 | protected function _add_where($fragment, $values = array()) |
|
1499 | |||
1500 | /** |
||
1501 | * Internal method to add a WHERE condition to the query |
||
1502 | * |
||
1503 | * @param string|array $column_name |
||
1504 | * @param string $separator |
||
1505 | * @param mixed $value |
||
1506 | * |
||
1507 | * @return $this|ORM |
||
1508 | */ |
||
1509 | 63 | protected function _add_simple_where($column_name, $separator, $value) |
|
1513 | |||
1514 | /** |
||
1515 | * Add a WHERE clause with multiple values (like IN and NOT IN) |
||
1516 | * |
||
1517 | * @param string|array $column_name |
||
1518 | * @param string $separator |
||
1519 | * @param mixed $values |
||
1520 | * |
||
1521 | * @return ORM |
||
1522 | */ |
||
1523 | 5 | View Code Duplication | public function _add_where_placeholder($column_name, $separator, $values) |
1540 | |||
1541 | /** |
||
1542 | * Add a WHERE clause with no parameters(like IS NULL and IS NOT NULL) |
||
1543 | * |
||
1544 | * @param string|array $column_name |
||
1545 | * @param string $operator |
||
1546 | * |
||
1547 | * @return ORM |
||
1548 | */ |
||
1549 | 4 | View Code Duplication | public function _add_where_no_value($column_name, $operator) |
1565 | |||
1566 | /** |
||
1567 | * Internal method to add a HAVING or WHERE condition to the query |
||
1568 | * |
||
1569 | * @param string $type |
||
1570 | * @param string $fragment |
||
1571 | * @param mixed $values |
||
1572 | * |
||
1573 | * @return $this |
||
1574 | */ |
||
1575 | 112 | protected function _add_condition($type, $fragment, $values = array()) |
|
1593 | |||
1594 | /** |
||
1595 | * Helper method to compile a simple COLUMN SEPARATOR VALUE |
||
1596 | * style HAVING or WHERE condition into a string and value ready to |
||
1597 | * be passed to the _add_condition method. Avoids duplication |
||
1598 | * of the call to _quote_identifier |
||
1599 | * |
||
1600 | * If column_name is an associative array, it will add a condition for each column |
||
1601 | * |
||
1602 | * @param string $type |
||
1603 | * @param string|array $column_name |
||
1604 | * @param string $separator |
||
1605 | * @param string|int $value |
||
1606 | * |
||
1607 | * @return $this|ORM |
||
1608 | */ |
||
1609 | 77 | protected function _add_simple_condition($type, $column_name, $separator, $value) |
|
1636 | |||
1637 | /** |
||
1638 | * Return a string containing the given number of question marks, |
||
1639 | * separated by commas. Eg "?, ?, ?" |
||
1640 | * |
||
1641 | * @param mixed $fields |
||
1642 | * |
||
1643 | * @return string |
||
1644 | */ |
||
1645 | 21 | protected function _create_placeholders($fields) |
|
1663 | |||
1664 | /** |
||
1665 | * Helper method that filters a column/value array returning only those |
||
1666 | * columns that belong to a compound primary key. |
||
1667 | * |
||
1668 | * If the key contains a column that does not exist in the given array, |
||
1669 | * a null value will be returned for it. |
||
1670 | * |
||
1671 | * @param mixed $value |
||
1672 | * |
||
1673 | * @return array |
||
1674 | */ |
||
1675 | 2 | protected function _get_compound_id_column_values($value) |
|
1684 | |||
1685 | /** |
||
1686 | * Helper method that filters an array containing compound column/value |
||
1687 | * arrays. |
||
1688 | * |
||
1689 | * @param array $values |
||
1690 | * |
||
1691 | * @return array |
||
1692 | */ |
||
1693 | 1 | protected function _get_compound_id_column_values_array($values) |
|
1702 | |||
1703 | /** |
||
1704 | * Add a WHERE column = value clause to your query. Each time |
||
1705 | * this is called in the chain, an additional WHERE will be |
||
1706 | * added, and these will be ANDed together when the final query |
||
1707 | * is built. |
||
1708 | * |
||
1709 | * If you use an array in $column_name, a new clause will be |
||
1710 | * added for each element. In this case, $value is ignored. |
||
1711 | * |
||
1712 | * @param string $column_name |
||
1713 | * @param mixed $value |
||
1714 | * |
||
1715 | * @return $this|ORM |
||
1716 | */ |
||
1717 | 49 | public function where($column_name, $value = null) |
|
1721 | |||
1722 | /** |
||
1723 | * More explicitly named version of for the where() method. |
||
1724 | * Can be used if preferred. |
||
1725 | * |
||
1726 | * @param string $column_name |
||
1727 | * @param mixed $value |
||
1728 | * |
||
1729 | * @return $this|ORM |
||
1730 | */ |
||
1731 | 53 | public function where_equal($column_name, $value = null) |
|
1735 | |||
1736 | /** |
||
1737 | * Add a WHERE column != value clause to your query. |
||
1738 | * |
||
1739 | * @param string $column_name |
||
1740 | * @param mixed $value |
||
1741 | * |
||
1742 | * @return $this|ORM |
||
1743 | */ |
||
1744 | 2 | public function where_not_equal($column_name, $value = null) |
|
1748 | |||
1749 | /** |
||
1750 | * Special method to query the table by its primary key |
||
1751 | * |
||
1752 | * If primary key is compound, only the columns that |
||
1753 | * belong to they key will be used for the query |
||
1754 | * |
||
1755 | * @param mixed $id |
||
1756 | * |
||
1757 | * @return $this|ORM |
||
1758 | */ |
||
1759 | 33 | View Code Duplication | public function where_id_is($id) |
1767 | |||
1768 | /** |
||
1769 | * Allows adding a WHERE clause that matches any of the conditions |
||
1770 | * specified in the array. Each element in the associative array will |
||
1771 | * be a different condition, where the key will be the column name. |
||
1772 | * |
||
1773 | * By default, an equal operator will be used against all columns, but |
||
1774 | * it can be overridden for any or every column using the second parameter. |
||
1775 | * |
||
1776 | * Each condition will be ORed together when added to the final query. |
||
1777 | * |
||
1778 | * @param array $values |
||
1779 | * @param string $operator |
||
1780 | * |
||
1781 | * @return $this|ORM |
||
1782 | */ |
||
1783 | 4 | public function where_any_is($values, $operator = '=') |
|
1819 | |||
1820 | /** |
||
1821 | * Similar to where_id_is() but allowing multiple primary keys. |
||
1822 | * |
||
1823 | * If primary key is compound, only the columns that |
||
1824 | * belong to they key will be used for the query |
||
1825 | * |
||
1826 | * @param mixed $ids |
||
1827 | * |
||
1828 | * @return $this|ORM |
||
1829 | */ |
||
1830 | 2 | public function where_id_in($ids) |
|
1838 | |||
1839 | /** |
||
1840 | * Add a WHERE ... LIKE clause to your query. |
||
1841 | * |
||
1842 | * @param string $column_name |
||
1843 | * @param mixed $value |
||
1844 | * |
||
1845 | * @return $this|ORM |
||
1846 | */ |
||
1847 | 2 | public function where_like($column_name, $value = null) |
|
1851 | |||
1852 | /** |
||
1853 | * Add where WHERE ... NOT LIKE clause to your query. |
||
1854 | * |
||
1855 | * @param string $column_name |
||
1856 | * @param mixed $value |
||
1857 | * |
||
1858 | * @return $this|ORM |
||
1859 | */ |
||
1860 | 2 | public function where_not_like($column_name, $value = null) |
|
1864 | |||
1865 | /** |
||
1866 | * Add a WHERE ... > clause to your query |
||
1867 | * |
||
1868 | * @param string $column_name |
||
1869 | * @param mixed $value |
||
1870 | * |
||
1871 | * @return $this|ORM |
||
1872 | */ |
||
1873 | 2 | public function where_gt($column_name, $value = null) |
|
1877 | |||
1878 | /** |
||
1879 | * Add a WHERE ... < clause to your query |
||
1880 | * |
||
1881 | * @param string $column_name |
||
1882 | * @param mixed $value |
||
1883 | * |
||
1884 | * @return $this|ORM |
||
1885 | */ |
||
1886 | 2 | public function where_lt($column_name, $value = null) |
|
1890 | |||
1891 | /** |
||
1892 | * Add a WHERE ... >= clause to your query |
||
1893 | * |
||
1894 | * @param string $column_name |
||
1895 | * @param mixed $value |
||
1896 | * |
||
1897 | * @return $this|ORM |
||
1898 | */ |
||
1899 | 2 | public function where_gte($column_name, $value = null) |
|
1903 | |||
1904 | /** |
||
1905 | * Add a WHERE ... <= clause to your query |
||
1906 | * |
||
1907 | * @param string $column_name |
||
1908 | * @param mixed $value |
||
1909 | * |
||
1910 | * @return $this|ORM |
||
1911 | */ |
||
1912 | 2 | public function where_lte($column_name, $value = null) |
|
1916 | |||
1917 | /** |
||
1918 | * Add a WHERE ... IN clause to your query |
||
1919 | * |
||
1920 | * @param string $column_name |
||
1921 | * @param mixed $values |
||
1922 | * |
||
1923 | * @return $this|ORM |
||
1924 | */ |
||
1925 | 3 | public function where_in($column_name, $values) |
|
1929 | |||
1930 | /** |
||
1931 | * Add a WHERE ... NOT IN clause to your query |
||
1932 | * |
||
1933 | * @param string $column_name |
||
1934 | * @param mixed $values |
||
1935 | * |
||
1936 | * @return $this|ORM |
||
1937 | */ |
||
1938 | 2 | public function where_not_in($column_name, $values) |
|
1942 | |||
1943 | /** |
||
1944 | * Add a WHERE column IS NULL clause to your query |
||
1945 | * |
||
1946 | * @param string $column_name |
||
1947 | * |
||
1948 | * @return $this|ORM |
||
1949 | */ |
||
1950 | 2 | public function where_null($column_name) |
|
1954 | |||
1955 | /** |
||
1956 | * Add a WHERE column IS NOT NULL clause to your query |
||
1957 | * |
||
1958 | * @param string $column_name |
||
1959 | * |
||
1960 | * @return $this|ORM |
||
1961 | */ |
||
1962 | 2 | public function where_not_null($column_name) |
|
1966 | |||
1967 | /** |
||
1968 | * Add a raw WHERE clause to the query. The clause should |
||
1969 | * contain question mark placeholders, which will be bound |
||
1970 | * to the parameters supplied in the second argument. |
||
1971 | * |
||
1972 | * @param string $clause |
||
1973 | * @param array $parameters |
||
1974 | * |
||
1975 | * @return $this|ORM |
||
1976 | */ |
||
1977 | 18 | public function where_raw($clause, $parameters = array()) |
|
1981 | |||
1982 | /** |
||
1983 | * Add a LIMIT to the query |
||
1984 | * |
||
1985 | * @param int $limit |
||
1986 | * |
||
1987 | * @return $this |
||
1988 | */ |
||
1989 | 95 | public function limit($limit) |
|
1995 | |||
1996 | /** |
||
1997 | * Add an OFFSET to the query |
||
1998 | * |
||
1999 | * @param $offset |
||
2000 | * |
||
2001 | * @return $this |
||
2002 | */ |
||
2003 | 4 | public function offset($offset) |
|
2009 | |||
2010 | /** |
||
2011 | * Add an ORDER BY clause to the query |
||
2012 | * |
||
2013 | * @param string $column_name |
||
2014 | * @param string $ordering |
||
2015 | * |
||
2016 | * @return $this |
||
2017 | */ |
||
2018 | 12 | protected function _add_order_by($column_name, $ordering) |
|
2025 | |||
2026 | /** |
||
2027 | * Add an ORDER BY column DESC clause |
||
2028 | * |
||
2029 | * @param string $column_name |
||
2030 | * |
||
2031 | * @return $this|ORM |
||
2032 | */ |
||
2033 | 8 | public function order_by_desc($column_name) |
|
2037 | |||
2038 | /** |
||
2039 | * Add an ORDER BY column ASC clause |
||
2040 | * |
||
2041 | * @param string $column_name |
||
2042 | * |
||
2043 | * @return $this|ORM |
||
2044 | */ |
||
2045 | 6 | public function order_by_asc($column_name) |
|
2049 | |||
2050 | /** |
||
2051 | * Add an unquoted expression as an ORDER BY clause |
||
2052 | * |
||
2053 | * @param $clause |
||
2054 | * |
||
2055 | * @return $this |
||
2056 | */ |
||
2057 | 2 | public function order_by_expr($clause) |
|
2063 | |||
2064 | /** |
||
2065 | * Reset the ORDER BY clause |
||
2066 | */ |
||
2067 | 1 | public function reset_order_by() |
|
2073 | |||
2074 | /** |
||
2075 | * Add a column to the list of columns to GROUP BY |
||
2076 | * |
||
2077 | * @param string $column_name |
||
2078 | * |
||
2079 | * @return $this |
||
2080 | */ |
||
2081 | 28 | public function group_by($column_name) |
|
2088 | |||
2089 | /** |
||
2090 | * Add an unquoted expression to the list of columns to GROUP BY |
||
2091 | * |
||
2092 | * @param string $expr |
||
2093 | * |
||
2094 | * @return $this |
||
2095 | */ |
||
2096 | 2 | public function group_by_expr($expr) |
|
2102 | |||
2103 | /** |
||
2104 | * Add a HAVING column = value clause to your query. Each time |
||
2105 | * this is called in the chain, an additional HAVING will be |
||
2106 | * added, and these will be ANDed together when the final query |
||
2107 | * is built. |
||
2108 | * |
||
2109 | * If you use an array in $column_name, a new clause will be |
||
2110 | * added for each element. In this case, $value is ignored. |
||
2111 | * |
||
2112 | * @param string $column_name |
||
2113 | * @param null $value |
||
2114 | * |
||
2115 | * @return $this|ORM |
||
2116 | */ |
||
2117 | 4 | public function having($column_name, $value = null) |
|
2121 | |||
2122 | /** |
||
2123 | * More explicitly named version of for the having() method. |
||
2124 | * Can be used if preferred. |
||
2125 | * |
||
2126 | * @param string $column_name |
||
2127 | * @param null $value |
||
2128 | * |
||
2129 | * @return $this|ORM |
||
2130 | */ |
||
2131 | 4 | public function having_equal($column_name, $value = null) |
|
2135 | |||
2136 | /** |
||
2137 | * Add a HAVING column != value clause to your query. |
||
2138 | * |
||
2139 | * @param string $column_name |
||
2140 | * @param null $value |
||
2141 | * |
||
2142 | * @return $this|ORM |
||
2143 | */ |
||
2144 | 2 | public function having_not_equal($column_name, $value = null) |
|
2148 | |||
2149 | /** |
||
2150 | * Special method to query the table by its primary key. |
||
2151 | * |
||
2152 | * If primary key is compound, only the columns that |
||
2153 | * belong to they key will be used for the query |
||
2154 | * |
||
2155 | * @param $id |
||
2156 | * |
||
2157 | * @return $this|ORM |
||
2158 | */ |
||
2159 | View Code Duplication | public function having_id_is($id) |
|
2167 | |||
2168 | /** |
||
2169 | * Add a HAVING ... LIKE clause to your query. |
||
2170 | * |
||
2171 | * @param string $column_name |
||
2172 | * @param mixed $value |
||
2173 | * |
||
2174 | * @return $this|ORM |
||
2175 | */ |
||
2176 | 2 | public function having_like($column_name, $value = null) |
|
2180 | |||
2181 | /** |
||
2182 | * Add where HAVING ... NOT LIKE clause to your query. |
||
2183 | * |
||
2184 | * @param string $column_name |
||
2185 | * @param mixed $value |
||
2186 | * |
||
2187 | * @return $this|ORM |
||
2188 | */ |
||
2189 | 2 | public function having_not_like($column_name, $value = null) |
|
2193 | |||
2194 | /** |
||
2195 | * Add a HAVING ... > clause to your query |
||
2196 | * |
||
2197 | * @param string $column_name |
||
2198 | * @param mixed $value |
||
2199 | * |
||
2200 | * @return $this|ORM |
||
2201 | */ |
||
2202 | 2 | public function having_gt($column_name, $value = null) |
|
2206 | |||
2207 | /** |
||
2208 | * Add a HAVING ... < clause to your query |
||
2209 | * |
||
2210 | * @param string $column_name |
||
2211 | * @param mixed $value |
||
2212 | * |
||
2213 | * @return $this|ORM |
||
2214 | */ |
||
2215 | 2 | public function having_lt($column_name, $value = null) |
|
2219 | |||
2220 | /** |
||
2221 | * Add a HAVING ... >= clause to your query |
||
2222 | * |
||
2223 | * @param string $column_name |
||
2224 | * @param mixed $value |
||
2225 | * |
||
2226 | * @return $this|ORM |
||
2227 | */ |
||
2228 | 2 | public function having_gte($column_name, $value = null) |
|
2232 | |||
2233 | /** |
||
2234 | * Add a HAVING ... <= clause to your query |
||
2235 | * |
||
2236 | * @param string $column_name |
||
2237 | * @param mixed $value |
||
2238 | * |
||
2239 | * @return $this|ORM |
||
2240 | */ |
||
2241 | 2 | public function having_lte($column_name, $value = null) |
|
2245 | |||
2246 | /** |
||
2247 | * Add a HAVING ... IN clause to your query |
||
2248 | * |
||
2249 | * @param string $column_name |
||
2250 | * @param mixed $values |
||
2251 | * |
||
2252 | * @return $this|ORM |
||
2253 | */ |
||
2254 | 2 | public function having_in($column_name, $values = null) |
|
2258 | |||
2259 | /** |
||
2260 | * Add a HAVING ... NOT IN clause to your query |
||
2261 | * |
||
2262 | * @param string $column_name |
||
2263 | * @param mixed $values |
||
2264 | * |
||
2265 | * @return $this|ORM |
||
2266 | */ |
||
2267 | 2 | public function having_not_in($column_name, $values = null) |
|
2271 | |||
2272 | /** |
||
2273 | * Add a HAVING column IS NULL clause to your query |
||
2274 | * |
||
2275 | * @param string $column_name |
||
2276 | * |
||
2277 | * @return $this|ORM |
||
2278 | */ |
||
2279 | 2 | public function having_null($column_name) |
|
2283 | |||
2284 | /** |
||
2285 | * Add a HAVING column IS NOT NULL clause to your query |
||
2286 | * |
||
2287 | * @param string $column_name |
||
2288 | * |
||
2289 | * @return $this|ORM |
||
2290 | */ |
||
2291 | 2 | public function having_not_null($column_name) |
|
2295 | |||
2296 | /** |
||
2297 | * Add a raw HAVING clause to the query. The clause should |
||
2298 | * contain question mark placeholders, which will be bound |
||
2299 | * to the parameters supplied in the second argument. |
||
2300 | * |
||
2301 | * @param string $clause |
||
2302 | * @param array $parameters |
||
2303 | * |
||
2304 | * @return $this|ORM |
||
2305 | */ |
||
2306 | 2 | public function having_raw($clause, $parameters = array()) |
|
2310 | |||
2311 | /** |
||
2312 | * Activate cache refreshing for current query |
||
2313 | * |
||
2314 | * @return ORM |
||
2315 | */ |
||
2316 | public function refreshCache() |
||
2322 | |||
2323 | /** |
||
2324 | * Disable caching for current query |
||
2325 | * |
||
2326 | * @return ORM |
||
2327 | */ |
||
2328 | public function noCaching() |
||
2334 | |||
2335 | /** |
||
2336 | * Build a SELECT statement based on the clauses that have |
||
2337 | * been passed to this instance by chaining method calls. |
||
2338 | */ |
||
2339 | 199 | protected function _build_select() |
|
2365 | |||
2366 | /** |
||
2367 | * Build the start of the SELECT statement |
||
2368 | */ |
||
2369 | 195 | protected function _build_select_start() |
|
2394 | |||
2395 | /** |
||
2396 | * Build the JOIN sources |
||
2397 | */ |
||
2398 | 195 | protected function _build_join() |
|
2406 | |||
2407 | /** |
||
2408 | * Build the WHERE clause(s) |
||
2409 | */ |
||
2410 | 197 | protected function _build_where() |
|
2414 | |||
2415 | /** |
||
2416 | * Build the HAVING clause(s) |
||
2417 | */ |
||
2418 | 195 | protected function _build_having() |
|
2422 | |||
2423 | /** |
||
2424 | * Build GROUP BY |
||
2425 | */ |
||
2426 | 195 | protected function _build_group_by() |
|
2434 | |||
2435 | /** |
||
2436 | * Build a WHERE or HAVING clause |
||
2437 | * |
||
2438 | * @param string $type |
||
2439 | * |
||
2440 | * @return string |
||
2441 | */ |
||
2442 | 197 | protected function _build_conditions($type) |
|
2459 | |||
2460 | /** |
||
2461 | * Build ORDER BY |
||
2462 | */ |
||
2463 | 195 | protected function _build_order_by() |
|
2473 | |||
2474 | /** |
||
2475 | * Build LIMIT |
||
2476 | */ |
||
2477 | 195 | protected function _build_limit() |
|
2500 | |||
2501 | /** |
||
2502 | * Build OFFSET |
||
2503 | */ |
||
2504 | 195 | protected function _build_offset() |
|
2519 | |||
2520 | /** |
||
2521 | * Wrapper around PHP's join function which |
||
2522 | * only adds the pieces if they are not empty. |
||
2523 | * |
||
2524 | * @param string $glue |
||
2525 | * @param array $pieces |
||
2526 | * |
||
2527 | * @return string |
||
2528 | */ |
||
2529 | 197 | protected function _join_if_not_empty($glue, $pieces) |
|
2543 | |||
2544 | /** |
||
2545 | * Quote a string that is used as an identifier |
||
2546 | * (table names, column names etc). This method can |
||
2547 | * also deal with dot-separated identifiers eg table.column |
||
2548 | * |
||
2549 | * @param string $identifier |
||
2550 | * |
||
2551 | * @return string |
||
2552 | */ |
||
2553 | 207 | protected function _quote_one_identifier($identifier) |
|
2560 | |||
2561 | /** |
||
2562 | * Quote a string that is used as an identifier |
||
2563 | * (table names, column names etc) or an array containing |
||
2564 | * multiple identifiers. This method can also deal with |
||
2565 | * dot-separated identifiers eg table.column |
||
2566 | * |
||
2567 | * @param array|string $identifier |
||
2568 | * |
||
2569 | * @return string |
||
2570 | */ |
||
2571 | 207 | protected function _quote_identifier($identifier) |
|
2581 | |||
2582 | /** |
||
2583 | * This method performs the actual quoting of a single |
||
2584 | * part of an identifier, using the identifier quote |
||
2585 | * character specified in the config (or autodetected). |
||
2586 | * |
||
2587 | * @param string $part |
||
2588 | * |
||
2589 | * @return string |
||
2590 | */ |
||
2591 | 207 | protected function _quote_identifier_part($part) |
|
2602 | |||
2603 | /** |
||
2604 | * Create a cache key for the given query and parameters. |
||
2605 | * |
||
2606 | * @param string $query |
||
2607 | * @param array $parameters |
||
2608 | * @param null|string $table_name |
||
2609 | * @param string $connection_name |
||
2610 | * |
||
2611 | * @return mixed|string |
||
2612 | */ |
||
2613 | 3 | protected static function _create_cache_key($query, $parameters, $table_name = null, $connection_name = self::DEFAULT_CONNECTION) |
|
2635 | |||
2636 | /** |
||
2637 | * Check the query cache for the given cache key. If a value |
||
2638 | * is cached for the key, return the value. Otherwise, return false. |
||
2639 | * |
||
2640 | * @param string $cache_key |
||
2641 | * @param null|string $table_name |
||
2642 | * @param string $connection_name |
||
2643 | * |
||
2644 | * @return bool|mixed |
||
2645 | */ |
||
2646 | 3 | protected static function _check_query_cache($cache_key, $table_name = null, $connection_name = self::DEFAULT_CONNECTION) |
|
2667 | |||
2668 | /** |
||
2669 | * Clear the query cache |
||
2670 | * |
||
2671 | * @param null|string $table_name |
||
2672 | * @param string $connection_name |
||
2673 | * |
||
2674 | * @return bool|mixed |
||
2675 | */ |
||
2676 | 1 | public static function clear_cache($table_name = null, $connection_name = self::DEFAULT_CONNECTION) |
|
2697 | |||
2698 | /** |
||
2699 | * Add the given value to the query cache. |
||
2700 | * |
||
2701 | * @param string $cache_key |
||
2702 | * @param string $value |
||
2703 | * @param null|string $table_name |
||
2704 | * @param string $connection_name |
||
2705 | * |
||
2706 | * @return bool|mixed |
||
2707 | */ |
||
2708 | 3 | protected static function _cache_query_result($cache_key, $value, $table_name = null, $connection_name = self::DEFAULT_CONNECTION) |
|
2734 | |||
2735 | /** |
||
2736 | * Execute the SELECT query that has been built up by chaining methods |
||
2737 | * on this class. Return an array of rows as associative arrays. |
||
2738 | */ |
||
2739 | 199 | protected function _run() |
|
2778 | |||
2779 | /** |
||
2780 | * Return the raw data wrapped by this ORM |
||
2781 | * instance as an associative array. Column |
||
2782 | * names may optionally be supplied as arguments, |
||
2783 | * if so, only those keys will be returned. |
||
2784 | */ |
||
2785 | 2 | public function as_array() |
|
2794 | |||
2795 | /** |
||
2796 | * Return the value of a property of this object (database row) |
||
2797 | * or null if not present. |
||
2798 | * |
||
2799 | * If a column-names array is passed, it will return a associative array |
||
2800 | * with the value of each column or null if it is not present. |
||
2801 | * |
||
2802 | * @param mixed $key |
||
2803 | * |
||
2804 | * @return mixed |
||
2805 | */ |
||
2806 | 35 | public function get($key) |
|
2819 | |||
2820 | /** |
||
2821 | * Return the name of the column in the database table which contains |
||
2822 | * the primary key ID of the row. |
||
2823 | */ |
||
2824 | 50 | protected function _get_id_column_name() |
|
2836 | |||
2837 | /** |
||
2838 | * Get the primary key ID of this object. |
||
2839 | * |
||
2840 | * @param bool $disallow_null |
||
2841 | * |
||
2842 | * @return mixed |
||
2843 | * |
||
2844 | * @throws \Exception |
||
2845 | */ |
||
2846 | 33 | public function id($disallow_null = false) |
|
2864 | |||
2865 | /** |
||
2866 | * Set a property to a particular value on this object. |
||
2867 | * To set multiple properties at once, pass an associative array |
||
2868 | * as the first parameter and leave out the second parameter. |
||
2869 | * Flags the properties as 'dirty' so they will be saved to the |
||
2870 | * database when save() is called. |
||
2871 | * |
||
2872 | * @param mixed $key |
||
2873 | * @param mixed $value |
||
2874 | * |
||
2875 | * @return $this|ORM |
||
2876 | */ |
||
2877 | 27 | public function set($key, $value = null) |
|
2881 | |||
2882 | /** |
||
2883 | * Set a property to a particular value on this object. |
||
2884 | * To set multiple properties at once, pass an associative array |
||
2885 | * as the first parameter and leave out the second parameter. |
||
2886 | * Flags the properties as 'dirty' so they will be saved to the |
||
2887 | * database when save() is called. |
||
2888 | * |
||
2889 | * @param string|array $key |
||
2890 | * @param string|null $value |
||
2891 | * |
||
2892 | * @return ORM |
||
2893 | */ |
||
2894 | 10 | public function set_expr($key, $value = null) |
|
2898 | |||
2899 | /** |
||
2900 | * Set a property on the ORM object. |
||
2901 | * |
||
2902 | * @param string|array $key |
||
2903 | * @param string|null $value |
||
2904 | * @param bool $expr |
||
2905 | * |
||
2906 | * @return $this |
||
2907 | */ |
||
2908 | 29 | protected function _set_orm_property($key, $value = null, $expr = false) |
|
2927 | |||
2928 | /** |
||
2929 | * Check whether the given field has been changed since this |
||
2930 | * object was saved. |
||
2931 | * |
||
2932 | * @param string $key |
||
2933 | * |
||
2934 | * @return bool |
||
2935 | */ |
||
2936 | 1 | public function is_dirty($key) |
|
2940 | |||
2941 | /** |
||
2942 | * Check whether the model was the result of a call to create() or not |
||
2943 | * |
||
2944 | * @return bool |
||
2945 | */ |
||
2946 | 2 | public function is_new() |
|
2950 | |||
2951 | /** |
||
2952 | * Save any fields which have been modified on this object |
||
2953 | * to the database. |
||
2954 | * |
||
2955 | * @return bool |
||
2956 | * |
||
2957 | * @throws \Exception |
||
2958 | */ |
||
2959 | 28 | public function save() |
|
3031 | |||
3032 | /** |
||
3033 | * Add a WHERE clause for every column that belongs to the primary key |
||
3034 | * |
||
3035 | * @param array $query warning: this is a reference |
||
3036 | */ |
||
3037 | 21 | public function _add_id_column_conditions(&$query) |
|
3060 | |||
3061 | /** |
||
3062 | * Build an UPDATE query |
||
3063 | */ |
||
3064 | 17 | protected function _build_update() |
|
3084 | |||
3085 | /** |
||
3086 | * Build an INSERT query |
||
3087 | */ |
||
3088 | 12 | protected function _build_insert() |
|
3105 | |||
3106 | /** |
||
3107 | * Delete this record from the database |
||
3108 | */ |
||
3109 | 4 | public function delete() |
|
3123 | |||
3124 | /** |
||
3125 | * Delete many records from the database |
||
3126 | */ |
||
3127 | 2 | public function delete_many() |
|
3142 | |||
3143 | // --------------------- // |
||
3144 | // --- ArrayAccess --- // |
||
3145 | // --------------------- // |
||
3146 | |||
3147 | 14 | public function offsetExists($key) |
|
3151 | |||
3152 | 4 | public function offsetGet($key) |
|
3156 | |||
3157 | 16 | public function offsetSet($key, $value) |
|
3164 | |||
3165 | 1 | public function offsetUnset($key) |
|
3170 | |||
3171 | // --------------------- // |
||
3172 | // --- MAGIC METHODS --- // |
||
3173 | // --------------------- // |
||
3174 | 1 | public function __get($key) |
|
3178 | |||
3179 | 13 | public function __set($key, $value) |
|
3183 | |||
3184 | public function __unset($key) |
||
3188 | |||
3189 | 13 | public function __isset($key) |
|
3193 | |||
3194 | /** |
||
3195 | * Magic method to capture calls to undefined class methods. |
||
3196 | * In this case we are attempting to convert camel case formatted |
||
3197 | * methods into underscore formatted methods. |
||
3198 | * |
||
3199 | * This allows us to call ORM methods using camel case and remain |
||
3200 | * backwards compatible. |
||
3201 | * |
||
3202 | * @param string $name |
||
3203 | * @param array $arguments |
||
3204 | * |
||
3205 | * @return ORM |
||
3206 | * |
||
3207 | * @throws IdiormMethodMissingException |
||
3208 | */ |
||
3209 | 80 | public function __call($name, $arguments) |
|
3219 | |||
3220 | /** |
||
3221 | * Magic method to capture calls to undefined static class methods. |
||
3222 | * In this case we are attempting to convert camel case formatted |
||
3223 | * methods into underscore formatted methods. |
||
3224 | * |
||
3225 | * This allows us to call ORM methods using camel case and remain |
||
3226 | * backwards compatible. |
||
3227 | * |
||
3228 | * @param string $name |
||
3229 | * @param array $arguments |
||
3230 | * |
||
3231 | * @return ORM |
||
3232 | */ |
||
3233 | 86 | public static function __callStatic($name, $arguments) |
|
3239 | |||
3240 | } |
||
3241 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: