Skip to content

Commit

Permalink
WPDB get_col() support added.
Browse files Browse the repository at this point in the history
Support added.
  • Loading branch information
amostajo committed Dec 15, 2019
1 parent fb8d5a6 commit 4a81b59
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 2 deletions.
34 changes: 33 additions & 1 deletion src/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* @author 10 Quality <[email protected]>
* @license MIT
* @package wp-query-builder
* @version 1.0.5.2
* @version 1.0.6
*/
class QueryBuilder
{
Expand Down Expand Up @@ -417,6 +417,38 @@ public function count( $column = 1, $bypass_limit = true )
$query = apply_filters( 'query_builder_count_query_' . $this->id, $query );
return intval( $wpdb->get_var( $query ) );
}
/**
* Retunrs column results from builder statements.
* @since 1.0.6
*
* @global object $wpdb
*
* @param int $x Column index number.
*
* @return array
*/
public function col( $x = 0 )
{
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_from( $query );
$this->_query_join( $query );
$this->_query_where( $query );
$this->_query_group( $query );
$this->_query_having( $query );
$this->_query_order( $query );
$this->_query_limit( $query );
$this->_query_offset( $query );
// Process
$query = apply_filters( 'query_builder_col_query', $query );
$query = apply_filters( 'query_builder_col_query_' . $this->id, $query );
return $wpdb->get_col( $query, $x );
}
/**
* Builds query's select statement.
* @since 1.0.0
Expand Down
28 changes: 27 additions & 1 deletion tests/cases/QueryBuilderOperationsTest.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.0
* @version 1.0.6
*/
class QueryBuilderOperationsTest extends PHPUnit_Framework_TestCase
{
Expand Down Expand Up @@ -142,4 +142,30 @@ public function testValueY()
// Assert dummy results
$this->assertEquals( 2, $var );
}
/**
* Test query builder
* @since 1.0.6
*/
public function testCol()
{
// Preapre
$builder = QueryBuilder::create( 'test' );
// Exec
$columns = $builder->col();
// Assert dummy results
$this->assertEquals( [1,2,3,4], $columns );
}
/**
* Test query builder
* @since 1.0.6
*/
public function testCol2()
{
// Preapre
$builder = QueryBuilder::create( 'test' );
// Exec
$columns = $builder->col(1);
// Assert dummy results
$this->assertEquals( ['type','type','type','type'], $columns );
}
}
26 changes: 26 additions & 0 deletions tests/framework/wpdb.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,32 @@ public function get_var( $query, $x = 0, $y = 0 )
}
return null;
}
public function get_col( $query, $x = 0 )
{
static::$query = $query;
$results = [];
for ( $i = 0; $i < 4; $i++ ) {
switch ( $x ) {
case 1:
$results[] = 'type';
break;
case 2:
$results[] = uniqid();
break;
case 3:
$results[] = time();
break;
case 4:
$results[] = date( 'Y-m-d H:i' );
break;
case 0:
default;
$results[] = $i + 1;
break;
}
}
return $results;
}
public function insert( $table )
{
static::$table = $table;
Expand Down

0 comments on commit 4a81b59

Please sign in to comment.