Skip to content

Commit

Permalink
Use table alias/name when building date query (#164)
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
* Update get_sql params as per WP_Meta_Query
* Update get_column to only use the table_name
* Removed the table alias as that's not something WP_Meta_Query uses
  • Loading branch information
jkudish authored Sep 19, 2024
1 parent 8c4fcd9 commit 32f2d98
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/Database/Queries/Date.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,13 @@ class Date extends Base {
'AND'
);


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

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

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

return $retval;
}

Expand Down Expand Up @@ -645,8 +656,9 @@ public function validate_column( $column = '' ) {
*
* @return string MySQL WHERE clauses.
*/
public function get_sql() {
$sql = $this->get_sql_clauses();
public function get_sql( $type, $primary_table, $primary_id_column, $context = null ) {
$this->table_name = $this->sanitize_table_name( $primary_table );
$sql = $this->get_sql_clauses();

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

0 comments on commit 32f2d98

Please # to comment.