Skip to content

Commit

Permalink
use table alias/name when building date query
Browse files Browse the repository at this point in the history
use the received table name & table alias arguments in the `get_sql` function to set those as properties on the Date class.

Then, when building the SQL query, use the alias or name (only if available) to prepend to the column name in the query.

This prevents and fixes the issue described in #9699
  • Loading branch information
jkudish committed Nov 28, 2023
1 parent 715373d commit 9906995
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions includes/database/engine/class-date.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,19 @@ class Date extends Base {
'AND'
);


/**
* @since 3.2.6
* @var string|null Table name
*/
public $table_name = null;

/**
* @since 3.2.6
* @var string|null Table alias
*/
public $table_alias = null;

/**
* Constructor.
*
Expand Down Expand Up @@ -362,6 +375,12 @@ public function get_column( $query = array() ) {
? esc_sql( $this->validate_column( $query['column'] ) )
: $this->column;

if (!empty($this->table_alias)) {
$retval = $this->table_alias . '.' . $retval;
} elseif (!empty($this->table_name)) {
$retval = $this->table_name . '.' . $retval;
}

return $retval;
}

Expand Down Expand Up @@ -602,11 +621,17 @@ public function validate_column( $column = '' ) {
* Generate WHERE clause to be appended to a main query.
*
* @since 1.0.0
* @param string $table_name Optional. Table name. Default null.
* @param string $table_alias Optional. Table alias. Default null.
*
* @return string MySQL WHERE clauses.
*/
public function get_sql() {
$sql = $this->get_sql_clauses();
public function get_sql( $table_name = null, $table_alias = null ) {
$this->table_name = $this->sanitize_table_name( $table_name );
$this->table_alias = $this->sanitize_table_name( $table_alias );
$sql = $this->get_sql_clauses();

/**
* Filters the date query clauses.
Expand Down

0 comments on commit 9906995

Please sign in to comment.