Skip to content

Commit

Permalink
Added support for SQL_CALC_FOUND_ROWS
Browse files Browse the repository at this point in the history
Support added for mysql SQL_CALC_FOUND_ROWS, and easy pagination.
  • Loading branch information
amostajo committed Dec 15, 2019
1 parent 4a81b59 commit 65f171c
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 8 deletions.
34 changes: 27 additions & 7 deletions src/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -288,18 +288,19 @@ 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 );
$this->builder = apply_filters( 'query_builder_get_builder_' . $this->id, $this->builder );
// 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 );
Expand Down Expand Up @@ -423,19 +424,20 @@ 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 );
$this->builder = apply_filters( 'query_builder_col_builder_' . $this->id, $this->builder );
// 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 );
Expand All @@ -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']
);
Expand Down
13 changes: 13 additions & 0 deletions tests/cases/QueryBuilderOperationsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}
}
50 changes: 49 additions & 1 deletion tests/cases/QueryBuilderStatementsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* @author 10 Quality <[email protected]>
* @license MIT
* @package wp-query-builder
* @version 1.0.3
* @version 1.0.6
*/
class QueryBuilderStatementsTest extends PHPUnit_Framework_TestCase
{
Expand Down Expand Up @@ -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());
}
}

0 comments on commit 65f171c

Please sign in to comment.