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 awesomemotive/easy-digital-downloads#9699
  • Loading branch information
jkudish authored Dec 5, 2023
1 parent 9c4d1fe commit 85405d7
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions src/Database/Queries/Date.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,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 @@ -388,6 +401,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 @@ -645,8 +664,10 @@ public function validate_column( $column = '' ) {
*
* @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 85405d7

Please sign in to comment.