Merge pull request #32644 from woocommerce/update/reports-datastore-date-column

Replace the date_created column name with a class property in the analytics DataStore
This commit is contained in:
Chi-Hsuan Huang 2022-05-26 09:24:15 +08:00 committed by GitHub
commit 13fe9b4970
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 30 additions and 15 deletions

View File

@ -0,0 +1,4 @@
Significance: minor
Type: update
Replace the date_created column name with a class property in the analytics datastore

View File

@ -37,6 +37,13 @@ class DataStore extends SqlQuery {
*/ */
protected static $table_name = ''; protected static $table_name = '';
/**
* Date field name.
*
* @var string
*/
protected $date_column_name = 'date_created';
/** /**
* Mapping columns to data type to return correct response types. * Mapping columns to data type to return correct response types.
* *
@ -51,18 +58,21 @@ class DataStore extends SqlQuery {
* @var string * @var string
*/ */
private $order_by = ''; private $order_by = '';
/** /**
* Order property, used in the cmp function. * Order property, used in the cmp function.
* *
* @var string * @var string
*/ */
private $order = ''; private $order = '';
/** /**
* Query limit parameters. * Query limit parameters.
* *
* @var array * @var array
*/ */
private $limit_parameters = array(); private $limit_parameters = array();
/** /**
* Data store context used to pass to filters. * Data store context used to pass to filters.
* *
@ -499,8 +509,8 @@ class DataStore extends SqlQuery {
$adj_after = $new_start_date->format( TimeInterval::$sql_datetime_format ); $adj_after = $new_start_date->format( TimeInterval::$sql_datetime_format );
$adj_before = $new_end_date->format( TimeInterval::$sql_datetime_format ); $adj_before = $new_end_date->format( TimeInterval::$sql_datetime_format );
$this->interval_query->clear_sql_clause( array( 'where_time', 'limit' ) ); $this->interval_query->clear_sql_clause( array( 'where_time', 'limit' ) );
$this->interval_query->add_sql_clause( 'where_time', "AND {$table_name}.date_created <= '$adj_before'" ); $this->interval_query->add_sql_clause( 'where_time', "AND {$table_name}.`{$this->date_column_name}` <= '$adj_before'" );
$this->interval_query->add_sql_clause( 'where_time', "AND {$table_name}.date_created >= '$adj_after'" ); $this->interval_query->add_sql_clause( 'where_time', "AND {$table_name}.`{$this->date_column_name}` >= '$adj_after'" );
$this->clear_sql_clause( 'limit' ); $this->clear_sql_clause( 'limit' );
$this->add_sql_clause( 'limit', 'LIMIT 0,' . $params['per_page'] ); $this->add_sql_clause( 'limit', 'LIMIT 0,' . $params['per_page'] );
} else { } else {
@ -694,9 +704,9 @@ class DataStore extends SqlQuery {
$datetime_str = $query_args['before']->format( TimeInterval::$sql_datetime_format ); $datetime_str = $query_args['before']->format( TimeInterval::$sql_datetime_format );
} }
if ( isset( $this->subquery ) ) { if ( isset( $this->subquery ) ) {
$this->subquery->add_sql_clause( 'where_time', "AND {$table_name}.date_created <= '$datetime_str'" ); $this->subquery->add_sql_clause( 'where_time', "AND {$table_name}.`{$this->date_column_name}` <= '$datetime_str'" );
} else { } else {
$this->add_sql_clause( 'where_time', "AND {$table_name}.date_created <= '$datetime_str'" ); $this->add_sql_clause( 'where_time', "AND {$table_name}.`{$this->date_column_name}` <= '$datetime_str'" );
} }
} }
@ -707,9 +717,9 @@ class DataStore extends SqlQuery {
$datetime_str = $query_args['after']->format( TimeInterval::$sql_datetime_format ); $datetime_str = $query_args['after']->format( TimeInterval::$sql_datetime_format );
} }
if ( isset( $this->subquery ) ) { if ( isset( $this->subquery ) ) {
$this->subquery->add_sql_clause( 'where_time', "AND {$table_name}.date_created >= '$datetime_str'" ); $this->subquery->add_sql_clause( 'where_time', "AND {$table_name}.`{$this->date_column_name}` >= '$datetime_str'" );
} else { } else {
$this->add_sql_clause( 'where_time', "AND {$table_name}.date_created >= '$datetime_str'" ); $this->add_sql_clause( 'where_time', "AND {$table_name}.`{$this->date_column_name}` >= '$datetime_str'" );
} }
} }
} }
@ -840,7 +850,7 @@ class DataStore extends SqlQuery {
if ( isset( $query_args['interval'] ) && '' !== $query_args['interval'] ) { if ( isset( $query_args['interval'] ) && '' !== $query_args['interval'] ) {
$interval = $query_args['interval']; $interval = $query_args['interval'];
$this->clear_sql_clause( 'select' ); $this->clear_sql_clause( 'select' );
$this->add_sql_clause( 'select', TimeInterval::db_datetime_format( $interval, $table_name ) ); $this->add_sql_clause( 'select', TimeInterval::db_datetime_format( $interval, $table_name, $this->date_column_name ) );
} }
} }

View File

@ -79,28 +79,29 @@ class TimeInterval {
* *
* @param string $time_interval Time interval. * @param string $time_interval Time interval.
* @param string $table_name Name of the db table relevant for the date constraint. * @param string $table_name Name of the db table relevant for the date constraint.
* @param string $date_column_name Name of the date table column.
* @return mixed * @return mixed
*/ */
public static function db_datetime_format( $time_interval, $table_name ) { public static function db_datetime_format( $time_interval, $table_name, $date_column_name = 'date_created' ) {
$first_day_of_week = absint( get_option( 'start_of_week' ) ); $first_day_of_week = absint( get_option( 'start_of_week' ) );
if ( 1 === $first_day_of_week ) { if ( 1 === $first_day_of_week ) {
// Week begins on Monday, ISO 8601. // Week begins on Monday, ISO 8601.
$week_format = "DATE_FORMAT({$table_name}.date_created, '%x-%v')"; $week_format = "DATE_FORMAT({$table_name}.`{$date_column_name}`, '%x-%v')";
} else { } else {
// Week begins on day other than specified by ISO 8601, needs to be in sync with function simple_week_number. // Week begins on day other than specified by ISO 8601, needs to be in sync with function simple_week_number.
$week_format = "CONCAT(YEAR({$table_name}.date_created), '-', LPAD( FLOOR( ( DAYOFYEAR({$table_name}.date_created) + ( ( DATE_FORMAT(MAKEDATE(YEAR({$table_name}.date_created),1), '%w') - $first_day_of_week + 7 ) % 7 ) - 1 ) / 7 ) + 1 , 2, '0'))"; $week_format = "CONCAT(YEAR({$table_name}.`{$date_column_name}`), '-', LPAD( FLOOR( ( DAYOFYEAR({$table_name}.`{$date_column_name}`) + ( ( DATE_FORMAT(MAKEDATE(YEAR({$table_name}.`{$date_column_name}`),1), '%w') - $first_day_of_week + 7 ) % 7 ) - 1 ) / 7 ) + 1 , 2, '0'))";
} }
// Whenever this is changed, double check method time_interval_id to make sure they are in sync. // Whenever this is changed, double check method time_interval_id to make sure they are in sync.
$mysql_date_format_mapping = array( $mysql_date_format_mapping = array(
'hour' => "DATE_FORMAT({$table_name}.date_created, '%Y-%m-%d %H')", 'hour' => "DATE_FORMAT({$table_name}.`{$date_column_name}`, '%Y-%m-%d %H')",
'day' => "DATE_FORMAT({$table_name}.date_created, '%Y-%m-%d')", 'day' => "DATE_FORMAT({$table_name}.`{$date_column_name}`, '%Y-%m-%d')",
'week' => $week_format, 'week' => $week_format,
'month' => "DATE_FORMAT({$table_name}.date_created, '%Y-%m')", 'month' => "DATE_FORMAT({$table_name}.`{$date_column_name}`, '%Y-%m')",
'quarter' => "CONCAT(YEAR({$table_name}.date_created), '-', QUARTER({$table_name}.date_created))", 'quarter' => "CONCAT(YEAR({$table_name}.`{$date_column_name}`), '-', QUARTER({$table_name}.`{$date_column_name}`))",
'year' => "YEAR({$table_name}.date_created)", 'year' => "YEAR({$table_name}.`{$date_column_name}`)",
); );