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:
commit
13fe9b4970
|
@ -0,0 +1,4 @@
|
|||
Significance: minor
|
||||
Type: update
|
||||
|
||||
Replace the date_created column name with a class property in the analytics datastore
|
|
@ -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 ) );
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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 = 'date_created' ) {
|
||||
$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}`)",
|
||||
|
||||
);
|
||||
|
||||
|
|
Loading…
Reference in New Issue