@@ -288,18 +288,19 @@ public function having( $statement )
288288 *
289289 * @param int $output WPDB output type.
290290 * @param callable $callable_mapping Function callable to filter or map results to.
291+ * @param bool $calc_rows Flag that indicates to SQL if rows should be calculated or not.
291292 *
292293 * @return array
293294 */
294- public function get ( $ output = OBJECT , $ callable_mapping = null )
295+ public function get ( $ output = OBJECT , $ callable_mapping = null , $ calc_rows = false )
295296 {
296297 global $ wpdb ;
297298 $ this ->builder = apply_filters ( 'query_builder_get_builder ' , $ this ->builder );
298299 $ this ->builder = apply_filters ( 'query_builder_get_builder_ ' . $ this ->id , $ this ->builder );
299300 // Build
300301 // Query
301302 $ query = '' ;
302- $ this ->_query_select ( $ query );
303+ $ this ->_query_select ( $ query, $ calc_rows );
303304 $ this ->_query_from ( $ query );
304305 $ this ->_query_join ( $ query );
305306 $ this ->_query_where ( $ query );
@@ -423,19 +424,20 @@ public function count( $column = 1, $bypass_limit = true )
423424 *
424425 * @global object $wpdb
425426 *
426- * @param int $x Column index number.
427+ * @param int $x Column index number.
428+ * @param bool $calc_rows Flag that indicates to SQL if rows should be calculated or not.
427429 *
428430 * @return array
429431 */
430- public function col ( $ x = 0 )
432+ public function col ( $ x = 0 , $ calc_rows = false )
431433 {
432434 global $ wpdb ;
433435 $ this ->builder = apply_filters ( 'query_builder_col_builder ' , $ this ->builder );
434436 $ this ->builder = apply_filters ( 'query_builder_col_builder_ ' . $ this ->id , $ this ->builder );
435437 // Build
436438 // Query
437439 $ query = '' ;
438- $ this ->_query_select ( $ query );
440+ $ this ->_query_select ( $ query, $ calc_rows );
439441 $ this ->_query_from ( $ query );
440442 $ this ->_query_join ( $ query );
441443 $ this ->_query_where ( $ query );
@@ -449,15 +451,33 @@ public function col( $x = 0 )
449451 $ query = apply_filters ( 'query_builder_col_query_ ' . $ this ->id , $ query );
450452 return $ wpdb ->get_col ( $ query , $ x );
451453 }
454+ /**
455+ * Retunrs found rows in last query, if SQL_CALC_FOUND_ROWS is used and is supported.
456+ * @since 1.0.6
457+ *
458+ * @global object $wpdb
459+ *
460+ * @return array
461+ */
462+ public function rows_found ()
463+ {
464+ global $ wpdb ;
465+ $ query = 'SELECT FOUND_ROWS() ' ;
466+ // Process
467+ $ query = apply_filters ( 'query_builder_found_rows_query ' , $ query );
468+ $ query = apply_filters ( 'query_builder_found_rows_query_ ' . $ this ->id , $ query );
469+ return $ wpdb ->get_var ( $ query );
470+ }
452471 /**
453472 * Builds query's select statement.
454473 * @since 1.0.0
455474 *
456475 * @param string &$query
476+ * @param bool $calc_rows
457477 */
458- private function _query_select ( &$ query )
478+ private function _query_select ( &$ query, $ calc_rows = false )
459479 {
460- $ query = 'SELECT ' . ( is_array ( $ this ->builder ['select ' ] )
480+ $ query = 'SELECT ' . ( $ calc_rows ? ' SQL_CALC_FOUND_ROWS ' : '' ) . ( is_array ( $ this ->builder ['select ' ] )
461481 ? implode ( ', ' , $ this ->builder ['select ' ] )
462482 : $ this ->builder ['select ' ]
463483 );
0 commit comments