From 7ed7ad2fde2d1f59f2fce9680fd65d50d6d56d6d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9CChris?= Date: Fri, 15 Apr 2022 18:06:32 +0300 Subject: [PATCH 1/3] Introduce date_column_name in reports datastore --- .../src/Admin/API/Reports/DataStore.php | 24 +++++++++++++------ .../src/Admin/API/Reports/TimeInterval.php | 17 ++++++------- 2 files changed, 26 insertions(+), 15 deletions(-) diff --git a/plugins/woocommerce/src/Admin/API/Reports/DataStore.php b/plugins/woocommerce/src/Admin/API/Reports/DataStore.php index bf2a95cf2ac..d0e38156893 100644 --- a/plugins/woocommerce/src/Admin/API/Reports/DataStore.php +++ b/plugins/woocommerce/src/Admin/API/Reports/DataStore.php @@ -37,6 +37,13 @@ class DataStore extends SqlQuery { */ 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. * @@ -51,18 +58,21 @@ class DataStore extends SqlQuery { * @var string */ private $order_by = ''; + /** * Order property, used in the cmp function. * * @var string */ private $order = ''; + /** * Query limit parameters. * * @var array */ private $limit_parameters = array(); + /** * 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_before = $new_end_date->format( TimeInterval::$sql_datetime_format ); $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}.date_created >= '$adj_after'" ); + $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}.`{$this->date_column_name}` >= '$adj_after'" ); $this->clear_sql_clause( 'limit' ); $this->add_sql_clause( 'limit', 'LIMIT 0,' . $params['per_page'] ); } else { @@ -694,9 +704,9 @@ class DataStore extends SqlQuery { $datetime_str = $query_args['before']->format( TimeInterval::$sql_datetime_format ); } 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 { - $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 ); } 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 { - $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'] ) { $interval = $query_args['interval']; $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 ) ); } } diff --git a/plugins/woocommerce/src/Admin/API/Reports/TimeInterval.php b/plugins/woocommerce/src/Admin/API/Reports/TimeInterval.php index 31d74ae458b..7a2c59a27b4 100644 --- a/plugins/woocommerce/src/Admin/API/Reports/TimeInterval.php +++ b/plugins/woocommerce/src/Admin/API/Reports/TimeInterval.php @@ -79,28 +79,29 @@ class TimeInterval { * * @param string $time_interval Time interval. * @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 */ - public static function db_datetime_format( $time_interval, $table_name ) { + public static function db_datetime_format( $time_interval, $table_name, $date_column_name ) { $first_day_of_week = absint( get_option( 'start_of_week' ) ); if ( 1 === $first_day_of_week ) { // 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 { // 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. $mysql_date_format_mapping = array( - 'hour' => "DATE_FORMAT({$table_name}.date_created, '%Y-%m-%d %H')", - 'day' => "DATE_FORMAT({$table_name}.date_created, '%Y-%m-%d')", + 'hour' => "DATE_FORMAT({$table_name}.`{$date_column_name}`, '%Y-%m-%d %H')", + 'day' => "DATE_FORMAT({$table_name}.`{$date_column_name}`, '%Y-%m-%d')", 'week' => $week_format, - 'month' => "DATE_FORMAT({$table_name}.date_created, '%Y-%m')", - 'quarter' => "CONCAT(YEAR({$table_name}.date_created), '-', QUARTER({$table_name}.date_created))", - 'year' => "YEAR({$table_name}.date_created)", + 'month' => "DATE_FORMAT({$table_name}.`{$date_column_name}`, '%Y-%m')", + 'quarter' => "CONCAT(YEAR({$table_name}.`{$date_column_name}`), '-', QUARTER({$table_name}.`{$date_column_name}`))", + 'year' => "YEAR({$table_name}.`{$date_column_name}`)", ); From d057225e07964d590b8b7c8228bec5a94d9b364c Mon Sep 17 00:00:00 2001 From: Christos Lilitsas Date: Wed, 25 May 2022 16:26:20 +0300 Subject: [PATCH 2/3] Add default value to `db_datetime_format` for back compatibility Co-authored-by: Chi-Hsuan Huang --- plugins/woocommerce/src/Admin/API/Reports/TimeInterval.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/woocommerce/src/Admin/API/Reports/TimeInterval.php b/plugins/woocommerce/src/Admin/API/Reports/TimeInterval.php index 7a2c59a27b4..9686e737967 100644 --- a/plugins/woocommerce/src/Admin/API/Reports/TimeInterval.php +++ b/plugins/woocommerce/src/Admin/API/Reports/TimeInterval.php @@ -82,7 +82,7 @@ class TimeInterval { * @param string $date_column_name Name of the date table column. * @return mixed */ - public static function db_datetime_format( $time_interval, $table_name, $date_column_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' ) ); if ( 1 === $first_day_of_week ) { From 58abc685cbe446368436dd64ed2ca594d3d45f90 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9CChris?= Date: Wed, 25 May 2022 16:36:22 +0300 Subject: [PATCH 3/3] Add changelog --- .../changelog/update-reports-datastore-date-column | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 plugins/woocommerce/changelog/update-reports-datastore-date-column diff --git a/plugins/woocommerce/changelog/update-reports-datastore-date-column b/plugins/woocommerce/changelog/update-reports-datastore-date-column new file mode 100644 index 00000000000..7c4431a0d07 --- /dev/null +++ b/plugins/woocommerce/changelog/update-reports-datastore-date-column @@ -0,0 +1,4 @@ +Significance: minor +Type: update + +Replace the date_created column name with a class property in the analytics datastore