@@ -63,6 +63,7 @@ class ORM {
6363 'driver_options ' => null ,
6464 'identifier_quote_character ' => null , // if this is null, will be autodetected
6565 'logging ' => false ,
66+ 'caching ' => false ,
6667 );
6768
6869 // Database connection, instance of the PDO class
@@ -74,6 +75,9 @@ class ORM {
7475 // Log of all queries run, only populated if logging is enabled
7576 protected static $ _query_log = array ();
7677
78+ // Query cache, only used if query caching is enabled
79+ protected static $ _query_cache = array ();
80+
7781 // --------------------------- //
7882 // --- INSTANCE PROPERTIES --- //
7983 // --------------------------- //
@@ -96,6 +100,9 @@ class ORM {
96100 // Join sources
97101 protected $ _join_sources = array ();
98102
103+ // Should the query include a DISTINCT keyword?
104+ protected $ _distinct = false ;
105+
99106 // Is this a raw query?
100107 protected $ _is_raw_query = false ;
101108
@@ -117,6 +124,9 @@ class ORM {
117124 // ORDER BY
118125 protected $ _order_by = array ();
119126
127+ // GROUP BY
128+ protected $ _group_by = array ();
129+
120130 // The data for a hydrated instance of the class
121131 protected $ _data = array ();
122132
@@ -324,6 +334,17 @@ public function use_id_column($id_column) {
324334 return $ this ;
325335 }
326336
337+ /**
338+ * Create an ORM instance from the given row (an associative
339+ * array of data fetched from the database)
340+ */
341+ protected function _create_instance_from_row ($ row ) {
342+ $ instance = self ::for_table ($ this ->_table_name );
343+ $ instance ->use_id_column ($ this ->_instance_id_column );
344+ $ instance ->hydrate ($ row );
345+ return $ instance ;
346+ }
347+
327348 /**
328349 * Tell the ORM that you are expecting a single result
329350 * back from your query, and execute it. Will return
@@ -334,13 +355,17 @@ public function use_id_column($id_column) {
334355 * lookup on the table.
335356 */
336357 public function find_one ($ id =null ) {
337- if (!is_null ($ id )) {
358+ if (!is_null ($ id )) {
338359 $ this ->where_id_is ($ id );
339360 }
340361 $ this ->limit (1 );
341- $ statement = $ this ->_run ();
342- $ result = $ statement ->fetch (PDO ::FETCH_ASSOC );
343- return $ result ? self ::for_table ($ this ->_table_name )->use_id_column ($ this ->_instance_id_column )->hydrate ($ result ) : $ result ;
362+ $ rows = $ this ->_run ();
363+
364+ if (empty ($ rows )) {
365+ return false ;
366+ }
367+
368+ return $ this ->_create_instance_from_row ($ rows [0 ]);
344369 }
345370
346371 /**
@@ -350,12 +375,8 @@ public function find_one($id=null) {
350375 * no rows were returned.
351376 */
352377 public function find_many () {
353- $ statement = $ this ->_run ();
354- $ instances = array ();
355- while ($ row = $ statement ->fetch (PDO ::FETCH_ASSOC )) {
356- $ instances [] = self ::for_table ($ this ->_table_name )->use_id_column ($ this ->_instance_id_column )->hydrate ($ row );
357- }
358- return $ instances ;
378+ $ rows = $ this ->_run ();
379+ return array_map (array ($ this , '_create_instance_from_row ' ), $ rows );
359380 }
360381
361382 /**
@@ -365,9 +386,8 @@ public function find_many() {
365386 */
366387 public function count () {
367388 $ this ->select_expr ('COUNT(*) ' , 'count ' );
368- $ statement = $ this ->_run ();
369- $ result = $ statement ->fetch (PDO ::FETCH_ASSOC );
370- return isset ($ result ['count ' ]) ? (int ) $ result ['count ' ] : 0 ;
389+ $ result = $ this ->find_one ();
390+ return ($ result !== false && isset ($ result ->count )) ? (int ) $ result ->count : 0 ;
371391 }
372392
373393 /**
@@ -450,6 +470,14 @@ public function select_expr($expr, $alias=null) {
450470 return $ this ->_add_result_column ($ expr , $ alias );
451471 }
452472
473+ /**
474+ * Add a DISTINCT keyword before the list of columns in the SELECT query
475+ */
476+ public function distinct () {
477+ $ this ->_distinct = true ;
478+ return $ this ;
479+ }
480+
453481 /**
454482 * Internal method to add a JOIN source to the query.
455483 *
@@ -720,6 +748,15 @@ public function order_by_asc($column_name) {
720748 return $ this ->_add_order_by ($ column_name , 'ASC ' );
721749 }
722750
751+ /**
752+ * Add a column to the list of columns to GROUP BY
753+ */
754+ public function group_by ($ column_name ) {
755+ $ column_name = $ this ->_quote_identifier ($ column_name );
756+ $ this ->_group_by [] = $ column_name ;
757+ return $ this ;
758+ }
759+
723760 /**
724761 * Build a SELECT statement based on the clauses that have
725762 * been passed to this instance by chaining method calls.
@@ -738,6 +775,7 @@ protected function _build_select() {
738775 $ this ->_build_select_start (),
739776 $ this ->_build_join (),
740777 $ this ->_build_where (),
778+ $ this ->_build_group_by (),
741779 $ this ->_build_order_by (),
742780 $ this ->_build_limit (),
743781 $ this ->_build_offset (),
@@ -749,7 +787,13 @@ protected function _build_select() {
749787 */
750788 protected function _build_select_start () {
751789 $ result_columns = join (', ' , $ this ->_result_columns );
790+
791+ if ($ this ->_distinct ) {
792+ $ result_columns = 'DISTINCT ' . $ result_columns ;
793+ }
794+
752795 $ fragment = "SELECT {$ result_columns } FROM " . $ this ->_quote_identifier ($ this ->_table_name );
796+
753797 if (!is_null ($ this ->_table_alias )) {
754798 $ fragment .= " " . $ this ->_quote_identifier ($ this ->_table_alias );
755799 }
@@ -785,6 +829,16 @@ protected function _build_where() {
785829 return "WHERE " . join (" AND " , $ where_conditions );
786830 }
787831
832+ /**
833+ * Build GROUP BY
834+ */
835+ protected function _build_group_by () {
836+ if (count ($ this ->_group_by ) === 0 ) {
837+ return '' ;
838+ }
839+ return "GROUP BY " . join (", " , $ this ->_group_by );
840+ }
841+
788842 /**
789843 * Build ORDER BY
790844 */
@@ -845,24 +899,82 @@ protected function _quote_identifier($identifier) {
845899
846900 /**
847901 * This method performs the actual quoting of a single
848- * part of an identifier. Currently uses backticks, which
849- * are compatible with (at least) MySQL and SQLite .
902+ * part of an identifier, using the identifier quote
903+ * character specified in the config (or autodetected) .
850904 */
851905 protected function _quote_identifier_part ($ part ) {
906+ if ($ part === '* ' ) {
907+ return $ part ;
908+ }
852909 $ quote_character = self ::$ _config ['identifier_quote_character ' ];
853910 return $ quote_character . $ part . $ quote_character ;
854911 }
855912
913+ /**
914+ * Create a cache key for the given query and parameters.
915+ */
916+ protected static function _create_cache_key ($ query , $ parameters ) {
917+ $ parameter_string = join (', ' , $ parameters );
918+ $ key = $ query . ': ' . $ parameter_string ;
919+ return sha1 ($ key );
920+ }
921+
922+ /**
923+ * Check the query cache for the given cache key. If a value
924+ * is cached for the key, return the value. Otherwise, return false.
925+ */
926+ protected static function _check_query_cache ($ cache_key ) {
927+ if (isset (self ::$ _query_cache [$ cache_key ])) {
928+ return self ::$ _query_cache [$ cache_key ];
929+ }
930+ return false ;
931+ }
932+
933+ /**
934+ * Clear the query cache
935+ */
936+ public static function clear_cache () {
937+ self ::$ _query_cache = array ();
938+ }
939+
940+ /**
941+ * Add the given value to the query cache.
942+ */
943+ protected static function _cache_query_result ($ cache_key , $ value ) {
944+ self ::$ _query_cache [$ cache_key ] = $ value ;
945+ }
946+
856947 /**
857948 * Execute the SELECT query that has been built up by chaining methods
858- * on this class. Return the executed PDOStatement object .
949+ * on this class. Return an array of rows as associative arrays .
859950 */
860951 protected function _run () {
861952 $ query = $ this ->_build_select ();
953+ $ caching_enabled = self ::$ _config ['caching ' ];
954+
955+ if ($ caching_enabled ) {
956+ $ cache_key = self ::_create_cache_key ($ query , $ this ->_values );
957+ $ cached_result = self ::_check_query_cache ($ cache_key );
958+
959+ if ($ cached_result !== false ) {
960+ return $ cached_result ;
961+ }
962+ }
963+
862964 self ::_log_query ($ query , $ this ->_values );
863965 $ statement = self ::$ _db ->prepare ($ query );
864966 $ statement ->execute ($ this ->_values );
865- return $ statement ;
967+
968+ $ rows = array ();
969+ while ($ row = $ statement ->fetch (PDO ::FETCH_ASSOC )) {
970+ $ rows [] = $ row ;
971+ }
972+
973+ if ($ caching_enabled ) {
974+ self ::_cache_query_result ($ cache_key , $ rows );
975+ }
976+
977+ return $ rows ;
866978 }
867979
868980 /**
0 commit comments