From 9906995447cd517ca0c7d8a3c8f05702f1ccb573 Mon Sep 17 00:00:00 2001 From: Joey Kudish Date: Mon, 27 Nov 2023 19:51:15 -0800 Subject: [PATCH] use table alias/name when building date query 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 https://github.com/awesomemotive/easy-digital-downloads/issues/9699 --- includes/database/engine/class-date.php | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/includes/database/engine/class-date.php b/includes/database/engine/class-date.php index d5b2e0d2a2..0fe7bbc8e4 100644 --- a/includes/database/engine/class-date.php +++ b/includes/database/engine/class-date.php @@ -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. * @@ -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; } @@ -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.