Skip to content

Commit

Permalink
Raw statement
Browse files Browse the repository at this point in the history
Tested raw condition for `where` and `join`.
  • Loading branch information
amostajo committed Sep 21, 2019
1 parent b2c4a49 commit 897d400
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 15 deletions.
30 changes: 16 additions & 14 deletions 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.0
* @version 1.0.3
*/
class QueryBuilder
{
Expand Down Expand Up @@ -138,19 +138,21 @@ public function where( $args )
: true;
if ( $sanitize_callback )
$arg_value = $this->sanitize_value( $sanitize_callback, $arg_value );
$statement = [
$key,
is_array( $value ) && isset( $value['operator'] ) ? $value['operator'] : ( is_null( $arg_value ) ? 'is' : '=' ),
is_array( $value ) && isset( $value['key'] )
? '`' . $value['key'] . '`'
: ( is_array( $arg_value )
? ( '(\'' . implode( '\',\'', $arg_value ) . '\')' )
: ( is_null( $arg_value )
? 'null'
: $wpdb->prepare( is_numeric( $arg_value ) ? '%d' : '%s' , $arg_value )
)
),
];
$statement = $key === 'raw'
? [sanitize_text_field( $value )]
: [
$key,
is_array( $value ) && isset( $value['operator'] ) ? $value['operator'] : ( is_null( $arg_value ) ? 'is' : '=' ),
is_array( $value ) && isset( $value['key'] )
? '`' . $value['key'] . '`'
: ( is_array( $arg_value )
? ( '(\'' . implode( '\',\'', $arg_value ) . '\')' )
: ( is_null( $arg_value )
? 'null'
: $wpdb->prepare( is_numeric( $arg_value ) ? '%d' : '%s' , $arg_value )
)
),
];
$this->builder['where'][] = [
'joint' => is_array( $value ) && isset( $value['joint'] ) ? $value['joint'] : 'AND',
'condition' => implode( ' ', $statement ),
Expand Down
45 changes: 44 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.0
* @version 1.0.3
*/
class QueryBuilderStatementsTest extends PHPUnit_Framework_TestCase
{
Expand Down Expand Up @@ -628,4 +628,47 @@ public function testAllStatements()
$wpdb->get_query()
);
}
/**
* Test query builder
* @since 1.0.3
*/
public function testWhereRawStatement()
{
// Preapre
global $wpdb;
$builder = QueryBuilder::create( 'test' );
// Prepare
$builder->select( '*' )
->from( 'test_table' )
->where([
'test_field' => 1,
'raw' => 'a = b',
])
->get();
// Assert
$this->assertEquals(
'SELECT * FROM prefix_test_table WHERE test_field = %d AND a = b',
$wpdb->get_query()
);
}
/**
* Test query builder
* @since 1.0.3
*/
public function testJoinRawStatement()
{
// Preapre
global $wpdb;
$builder = QueryBuilder::create( 'test' );
// Prepare
$builder->select( '*' )
->from( 'test_table' )
->join( 'test_join', [ ['raw' => 'a = b'], ['key' => 'field_a', 'value' => 4] ] )
->get();
// Assert
$this->assertEquals(
'SELECT * FROM prefix_test_table JOIN prefix_test_join ON a = b AND field_a = %d',
$wpdb->get_query()
);
}
}

0 comments on commit 897d400

Please sign in to comment.