Skip to content

Commit 1824bbc

Browse files
committed
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 awesomemotive#9699
1 parent 715373d commit 1824bbc

File tree

1 file changed

+53
-28
lines changed

1 file changed

+53
-28
lines changed

includes/database/engine/class-date.php

Lines changed: 53 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,19 @@ class Date extends Base {
137137
'AND'
138138
);
139139

140+
141+
/**
142+
* @since 3.2.6
143+
* @var string|null Table name
144+
*/
145+
public $table_name = null;
146+
147+
/**
148+
* @since 3.2.6
149+
* @var string|null Table alias
150+
*/
151+
public $table_alias = null;
152+
140153
/**
141154
* Constructor.
142155
*
@@ -145,79 +158,79 @@ class Date extends Base {
145158
* 'compare'. When 'compare' is 'IN' or 'NOT IN', arrays are accepted; when 'compare' is 'BETWEEN' or 'NOT
146159
* BETWEEN', arrays of two valid values are required. See individual argument descriptions for accepted values.
147160
*
148-
* @since 1.0.0
149-
*
150161
* @param array $date_query {
151162
* Array of date query clauses.
152163
*
153-
* @type array {
154-
* @type string $column Optional. The column to query against. If undefined, inherits the value of
164+
* @type array {
165+
* @type string $column Optional. The column to query against. If undefined, inherits the value of
155166
* 'date_created'. Accepts 'date_created', 'date_created_gmt',
156167
* 'post_modified','post_modified_gmt', 'comment_date', 'comment_date_gmt'.
157168
* Default 'date_created'.
158-
* @type string $compare Optional. The comparison operator. Accepts '=', '!=', '>', '>=', '<', '<=',
169+
* @type string $compare Optional. The comparison operator. Accepts '=', '!=', '>', '>=', '<', '<=',
159170
* 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN'. Default '='.
160-
* @type string $relation Optional. The boolean relationship between the date queries. Accepts 'OR' or 'AND'.
171+
* @type string $relation Optional. The boolean relationship between the date queries. Accepts 'OR' or 'AND'.
161172
* Default 'OR'.
162-
* @type array {
173+
* @type array {
163174
* Optional. An array of first-order clause parameters, or another fully-formed date query.
164175
*
165-
* @type string|array $before {
176+
* @type string|array $before {
166177
* Optional. Date to retrieve posts before. Accepts `strtotime()`-compatible string,
167178
* or array of 'year', 'month', 'day' values.
168179
*
169-
* @type string $year The four-digit year. Default empty. Accepts any four-digit year.
170-
* @type string $month Optional when passing array.The month of the year.
180+
* @type string $year The four-digit year. Default empty. Accepts any four-digit year.
181+
* @type string $month Optional when passing array.The month of the year.
171182
* Default (string:empty)|(array:1). Accepts numbers 1-12.
172-
* @type string $day Optional when passing array.The day of the month.
183+
* @type string $day Optional when passing array.The day of the month.
173184
* Default (string:empty)|(array:1). Accepts numbers 1-31.
174185
* }
175-
* @type string|array $after {
186+
* @type string|array $after {
176187
* Optional. Date to retrieve posts after. Accepts `strtotime()`-compatible string,
177188
* or array of 'year', 'month', 'day' values.
178189
*
179-
* @type string $year The four-digit year. Accepts any four-digit year. Default empty.
180-
* @type string $month Optional when passing array. The month of the year. Accepts numbers 1-12.
190+
* @type string $year The four-digit year. Accepts any four-digit year. Default empty.
191+
* @type string $month Optional when passing array. The month of the year. Accepts numbers 1-12.
181192
* Default (string:empty)|(array:12).
182-
* @type string $day Optional when passing array.The day of the month. Accepts numbers 1-31.
193+
* @type string $day Optional when passing array.The day of the month. Accepts numbers 1-31.
183194
* Default (string:empty)|(array:last day of month).
184195
* }
185-
* @type string $column Optional. Used to add a clause comparing a column other than the
196+
* @type string $column Optional. Used to add a clause comparing a column other than the
186197
* column specified in the top-level `$column` parameter. Accepts
187198
* 'date_created', 'date_created_gmt', 'post_modified', 'post_modified_gmt',
188199
* 'comment_date', 'comment_date_gmt'. Default is the value of
189200
* top-level `$column`.
190-
* @type string $compare Optional. The comparison operator. Accepts '=', '!=', '>', '>=',
201+
* @type string $compare Optional. The comparison operator. Accepts '=', '!=', '>', '>=',
191202
* '<', '<=', 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN'. 'IN',
192203
* 'NOT IN', 'BETWEEN', and 'NOT BETWEEN'. Comparisons support
193204
* arrays in some time-related parameters. Default '='.
194-
* @type bool $inclusive Optional. Include results from dates specified in 'before' or
205+
* @type bool $inclusive Optional. Include results from dates specified in 'before' or
195206
* 'after'. Default false.
196-
* @type int|array $year Optional. The four-digit year number. Accepts any four-digit year
207+
* @type int|array $year Optional. The four-digit year number. Accepts any four-digit year
197208
* or an array of years if `$compare` supports it. Default empty.
198-
* @type int|array $month Optional. The two-digit month number. Accepts numbers 1-12 or an
209+
* @type int|array $month Optional. The two-digit month number. Accepts numbers 1-12 or an
199210
* array of valid numbers if `$compare` supports it. Default empty.
200-
* @type int|array $week Optional. The week number of the year. Accepts numbers 0-53 or an
211+
* @type int|array $week Optional. The week number of the year. Accepts numbers 0-53 or an
201212
* array of valid numbers if `$compare` supports it. Default empty.
202-
* @type int|array $dayofyear Optional. The day number of the year. Accepts numbers 1-366 or an
213+
* @type int|array $dayofyear Optional. The day number of the year. Accepts numbers 1-366 or an
203214
* array of valid numbers if `$compare` supports it.
204-
* @type int|array $day Optional. The day of the month. Accepts numbers 1-31 or an array
215+
* @type int|array $day Optional. The day of the month. Accepts numbers 1-31 or an array
205216
* of valid numbers if `$compare` supports it. Default empty.
206-
* @type int|array $dayofweek Optional. The day number of the week. Accepts numbers 1-7 (1 is
217+
* @type int|array $dayofweek Optional. The day number of the week. Accepts numbers 1-7 (1 is
207218
* Sunday) or an array of valid numbers if `$compare` supports it.
208219
* Default empty.
209-
* @type int|array $dayofweek_iso Optional. The day number of the week (ISO). Accepts numbers 1-7
220+
* @type int|array $dayofweek_iso Optional. The day number of the week (ISO). Accepts numbers 1-7
210221
* (1 is Monday) or an array of valid numbers if `$compare` supports it.
211222
* Default empty.
212-
* @type int|array $hour Optional. The hour of the day. Accepts numbers 0-23 or an array
223+
* @type int|array $hour Optional. The hour of the day. Accepts numbers 0-23 or an array
213224
* of valid numbers if `$compare` supports it. Default empty.
214-
* @type int|array $minute Optional. The minute of the hour. Accepts numbers 0-60 or an array
225+
* @type int|array $minute Optional. The minute of the hour. Accepts numbers 0-60 or an array
215226
* of valid numbers if `$compare` supports it. Default empty.
216-
* @type int|array $second Optional. The second of the minute. Accepts numbers 0-60 or an
227+
* @type int|array $second Optional. The second of the minute. Accepts numbers 0-60 or an
217228
* array of valid numbers if `$compare` supports it. Default empty.
218229
* }
219230
* }
220231
* }
232+
* @since 1.0.0
233+
*
221234
*/
222235
public function __construct( $date_query = array() ) {
223236

@@ -362,6 +375,12 @@ public function get_column( $query = array() ) {
362375
? esc_sql( $this->validate_column( $query['column'] ) )
363376
: $this->column;
364377

378+
if (!empty($this->table_alias)) {
379+
$retval = $this->table_alias . '.' . $retval;
380+
} elseif (!empty($this->table_name)) {
381+
$retval = $this->table_name . '.' . $retval;
382+
}
383+
365384
return $retval;
366385
}
367386

@@ -602,11 +621,17 @@ public function validate_column( $column = '' ) {
602621
* Generate WHERE clause to be appended to a main query.
603622
*
604623
* @since 1.0.0
624+
* @param string $table_name Optional. Table name. Default null.
625+
* @param string $table_alias Optional. Table alias. Default null.
605626
*
606627
* @return string MySQL WHERE clauses.
607628
*/
608629
public function get_sql() {
609630
$sql = $this->get_sql_clauses();
631+
public function get_sql( $table_name = null, $table_alias = null ) {
632+
$this->table_name = $this->sanitize_table_name( $table_name );
633+
$this->table_alias = $this->sanitize_table_name( $table_alias );
634+
$sql = $this->get_sql_clauses();
610635

611636
/**
612637
* Filters the date query clauses.

0 commit comments

Comments
 (0)