From 65f171c80be49fe41d34ef9da8ead0cff6b896fb Mon Sep 17 00:00:00 2001 From: Alejandro Mostajo Date: Sun, 15 Dec 2019 10:13:12 -0600 Subject: [PATCH] Added support for `SQL_CALC_FOUND_ROWS` Support added for mysql SQL_CALC_FOUND_ROWS, and easy pagination. --- src/QueryBuilder.php | 34 ++++++++++++--- tests/cases/QueryBuilderOperationsTest.php | 13 ++++++ tests/cases/QueryBuilderStatementsTest.php | 50 +++++++++++++++++++++- 3 files changed, 89 insertions(+), 8 deletions(-) diff --git a/src/QueryBuilder.php b/src/QueryBuilder.php index 5b25e1e..2fc38da 100644 --- a/src/QueryBuilder.php +++ b/src/QueryBuilder.php @@ -288,10 +288,11 @@ public function having( $statement ) * * @param int $output WPDB output type. * @param callable $callable_mapping Function callable to filter or map results to. + * @param bool $calc_rows Flag that indicates to SQL if rows should be calculated or not. * * @return array */ - public function get( $output = OBJECT, $callable_mapping = null ) + public function get( $output = OBJECT, $callable_mapping = null, $calc_rows = false ) { global $wpdb; $this->builder = apply_filters( 'query_builder_get_builder', $this->builder ); @@ -299,7 +300,7 @@ public function get( $output = OBJECT, $callable_mapping = null ) // Build // Query $query = ''; - $this->_query_select( $query ); + $this->_query_select( $query, $calc_rows ); $this->_query_from( $query ); $this->_query_join( $query ); $this->_query_where( $query ); @@ -423,11 +424,12 @@ public function count( $column = 1, $bypass_limit = true ) * * @global object $wpdb * - * @param int $x Column index number. + * @param int $x Column index number. + * @param bool $calc_rows Flag that indicates to SQL if rows should be calculated or not. * * @return array */ - public function col( $x = 0 ) + public function col( $x = 0, $calc_rows = false ) { global $wpdb; $this->builder = apply_filters( 'query_builder_col_builder', $this->builder ); @@ -435,7 +437,7 @@ public function col( $x = 0 ) // Build // Query $query = ''; - $this->_query_select( $query ); + $this->_query_select( $query, $calc_rows ); $this->_query_from( $query ); $this->_query_join( $query ); $this->_query_where( $query ); @@ -449,15 +451,33 @@ public function col( $x = 0 ) $query = apply_filters( 'query_builder_col_query_' . $this->id, $query ); return $wpdb->get_col( $query, $x ); } + /** + * Retunrs found rows in last query, if SQL_CALC_FOUND_ROWS is used and is supported. + * @since 1.0.6 + * + * @global object $wpdb + * + * @return array + */ + public function rows_found() + { + global $wpdb; + $query = 'SELECT FOUND_ROWS()'; + // Process + $query = apply_filters( 'query_builder_found_rows_query', $query ); + $query = apply_filters( 'query_builder_found_rows_query_' . $this->id, $query ); + return $wpdb->get_var( $query ); + } /** * Builds query's select statement. * @since 1.0.0 * * @param string &$query + * @param bool $calc_rows */ - private function _query_select( &$query ) + private function _query_select( &$query, $calc_rows = false ) { - $query = 'SELECT ' . ( is_array( $this->builder['select'] ) + $query = 'SELECT ' . ( $calc_rows ? 'SQL_CALC_FOUND_ROWS ' : '' ) . ( is_array( $this->builder['select'] ) ? implode( ',' , $this->builder['select'] ) : $this->builder['select'] ); diff --git a/tests/cases/QueryBuilderOperationsTest.php b/tests/cases/QueryBuilderOperationsTest.php index e74fded..b973a72 100644 --- a/tests/cases/QueryBuilderOperationsTest.php +++ b/tests/cases/QueryBuilderOperationsTest.php @@ -168,4 +168,17 @@ public function testCol2() // Assert dummy results $this->assertEquals( ['type','type','type','type'], $columns ); } + /** + * Test query builder + * @since 1.0.0 + */ + public function testRowFound() + { + // Preapre + $builder = QueryBuilder::create( 'test' ); + // Exec + $var = $builder->rows_found(); + // Assert dummy results + $this->assertEquals( 1, $var ); + } } \ No newline at end of file diff --git a/tests/cases/QueryBuilderStatementsTest.php b/tests/cases/QueryBuilderStatementsTest.php index 430ac16..63a6828 100644 --- a/tests/cases/QueryBuilderStatementsTest.php +++ b/tests/cases/QueryBuilderStatementsTest.php @@ -9,7 +9,7 @@ * @author 10 Quality * @license MIT * @package wp-query-builder - * @version 1.0.3 + * @version 1.0.6 */ class QueryBuilderStatementsTest extends PHPUnit_Framework_TestCase { @@ -674,4 +674,52 @@ public function testJoinRawStatement() $wpdb->get_query() ); } + /** + * Test query builder + * @since 1.0.6 + */ + public function testSelectCalcRowsStatement() + { + // Preapre + global $wpdb; + $builder = QueryBuilder::create( 'test' ); + // Prepare + $builder->select( 'test_field' )->get( OBJECT, null, true ); + // Assert + $this->assertEquals( + 'SELECT SQL_CALC_FOUND_ROWS test_field FROM ', + $wpdb->get_query() + ); + } + /** + * Test query builder + * @since 1.0.6 + */ + public function testColCalcRowsStatement() + { + // Preapre + global $wpdb; + $builder = QueryBuilder::create( 'test' ); + // Prepare + $builder->select( 'test_field' )->col( 0, true ); + // Assert + $this->assertEquals( + 'SELECT SQL_CALC_FOUND_ROWS test_field FROM ', + $wpdb->get_query() + ); + } + /** + * Test query builder + * @since 1.0.6 + */ + public function testRowsFoundStatement() + { + // Preapre + global $wpdb; + $builder = QueryBuilder::create( 'test' ); + // Prepare + $builder->select( 'test_field' )->rows_found(); + // Assert + $this->assertEquals('SELECT FOUND_ROWS()', $wpdb->get_query()); + } } \ No newline at end of file