Skip to content

Commit

Permalink
Improve the where building procedure
Browse files Browse the repository at this point in the history
  • Loading branch information
gdarko committed Sep 16, 2023
1 parent 2655b52 commit 3b37a4f
Showing 1 changed file with 22 additions and 11 deletions.
33 changes: 22 additions & 11 deletions src/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace IgniteKit\WP\QueryBuilder;

use Exception;
use function GuzzleHttp\default_user_agent;

/**
* Database query builder.
Expand Down Expand Up @@ -168,21 +169,31 @@ public function where( $args ) {
$arg_value = $this->sanitize_value( $sanitize_callback, $arg_value );
}


// Create statement
$prepared_operator = is_array( $value ) && isset( $value['operator'] ) ? strtoupper( $value['operator'] ) : ( $arg_value === null ? 'is' : '=' );
if ( is_array( $value ) && array_key_exists( 'key', $value ) ) {
if ( strtoupper($value['operator']) === 'BETWEEN' ) {
$prepared_value = $value['key'];
} else {
$prepared_value = ! is_numeric( $arg_value ) ? sprintf( "'%s'", $arg_value ) : $arg_value;
}
} else {
if ( is_array( $arg_value ) ) {
$prepared_value = '(\'' . implode( '\',\'', $arg_value ) . '\')';
} else {
if ( is_null( $arg_value ) ) {
$prepared_value = 'null';
} else {
$prepared_value = $wpdb->prepare( ( ! is_array( $value ) || ! array_key_exists( 'force_string', $value ) || ! $value['force_string'] ) && is_numeric( $arg_value ) ? '%d' : '%s', $arg_value );
}
}
}
$statement = $key === 'raw'
? [ $arg_value ]
: [
$key,
is_array( $value ) && isset( $value['operator'] ) ? strtoupper( $value['operator'] ) : ( $arg_value === null ? 'is' : '=' ),
is_array( $value ) && array_key_exists( 'key', $value )
? $value['key']
: ( is_array( $arg_value )
? ( '(\'' . implode( '\',\'', $arg_value ) . '\')' )
: ( $arg_value === null
? 'null'
: $wpdb->prepare( ( ! is_array( $value ) || ! array_key_exists( 'force_string', $value ) || ! $value['force_string'] ) && is_numeric( $arg_value ) ? '%d' : '%s', $arg_value )
)
),
$prepared_operator,
$prepared_value
];

// Between?
Expand Down

0 comments on commit 3b37a4f

Please sign in to comment.