From 030d0cb9be2e4062af4ff5e6b65b3722a32e8001 Mon Sep 17 00:00:00 2001
From: Starbeamrainbowlabs <sbrl@starbeamrainbowlabs.com>
Date: Sun, 23 Jun 2019 14:39:24 +0100
Subject: [PATCH] Revert "Don't use the new datetime computed column until the
 issues are worked out"

This reverts commit 97e234b920c4a6676e6532b773e672bf0b780b19.
---
 .../MariaDBMeasurementDataRepository.php      | 29 ++++++++-----------
 1 file changed, 12 insertions(+), 17 deletions(-)

diff --git a/logic/Repositories/MariaDBMeasurementDataRepository.php b/logic/Repositories/MariaDBMeasurementDataRepository.php
index 9f9cdca..54925e4 100644
--- a/logic/Repositories/MariaDBMeasurementDataRepository.php
+++ b/logic/Repositories/MariaDBMeasurementDataRepository.php
@@ -30,8 +30,6 @@ class MariaDBMeasurementDataRepository implements IMeasurementDataRepository {
 	public static $column_metadata_lat = "reading_latitude";
 	public static $column_metadata_long = "reading_longitude";
 	
-	public static $column_metadata_datetime_poly;
-	
 	// ------------------------------------------------------------------------
 	
 	/** @var TomlConfig */
@@ -64,9 +62,6 @@ class MariaDBMeasurementDataRepository implements IMeasurementDataRepository {
 		$this->database = $in_database;
 		$this->settings = $in_settings;
 		
-		self::$column_metadata_datetime_poly = "COALESCE(".self::$table_name_metadata.".".self::$column_metadata_recordedon.",".self::$table_name_metadata.".".self::$column_metadata_storedon.")";
-		
-		
 		$this->get_static = function($name) { return self::$$name; };
 		$this->get_static_extra = function($class_name, $name) {
 			return $class_name::$$name;
@@ -89,17 +84,17 @@ class MariaDBMeasurementDataRepository implements IMeasurementDataRepository {
 				{$s("table_name_values")}.{$s("column_values_reading_id")},
 				
 				{$s("table_name_metadata")}.{$s("column_metadata_device_id")},
-				{$s("column_metadata_datetime_poly")} AS datetime,
+				{$s("table_name_metadata")}.{$s("column_metadata_datetime")} AS datetime,
 				COUNT({$s("table_name_metadata")}.{$s("column_metadata_device_id")}) AS record_count
 			FROM {$s("table_name_values")}
 			JOIN {$s("table_name_metadata")} ON {$s("table_name_values")}.{$s("column_values_reading_id")} = {$s("table_name_metadata")}.id
 			WHERE
-			{$s("column_metadata_datetime_poly")} >= :start_datetime AND
-			{$s("column_metadata_datetime_poly")} <= :end_datetime
+			{$s("table_name_metadata")}.{$s("column_metadata_datetime")} >= :start_datetime AND
+			{$s("table_name_metadata")}.{$s("column_metadata_datetime")} <= :end_datetime
 			AND 
 				{$s("table_name_values")}.{$s("column_values_reading_type")} = :reading_type
 			GROUP BY {$s("table_name_metadata")}.{$s("column_metadata_device_id")}
-			ORDER BY {$s("column_metadata_datetime_poly")}
+			ORDER BY {$s("table_name_metadata")}.{$s("column_metadata_datetime")}
 				", [
 				"reading_type" => $type_id,
 				// The database likes strings, not PHP DateTime() instances
@@ -113,8 +108,8 @@ class MariaDBMeasurementDataRepository implements IMeasurementDataRepository {
 		$s = $this->get_static;
 		return $this->database->query(
 			"SELECT
-				MIN({$s("column_metadata_datetime_poly")}) AS start,
-				MAX({$s("column_metadata_datetime_poly")}) AS end
+				MIN({$s("table_name_metadata")}.{$s("column_metadata_datetime")}) AS start,
+				MAX({$s("table_name_metadata")}.{$s("column_metadata_datetime")}) AS end
 			FROM {$s("table_name_metadata")}
 			WHERE {$s("table_name_metadata")}.{$s("column_metadata_device_id")} = :device_id;", [
 				"device_id" => $device_id
@@ -131,7 +126,7 @@ class MariaDBMeasurementDataRepository implements IMeasurementDataRepository {
 				AVG({$s("table_name_values")}.{$s("column_values_value")}) AS {$s("column_values_value")},
 				MIN({$s("table_name_values")}.{$s("column_values_reading_id")}) AS {$s("column_values_reading_id")},
 				
-				MIN({$s("column_metadata_datetime_poly")}) AS datetime
+				MIN({$s("table_name_metadata")}.{$s("column_metadata_datetime")}) AS datetime
 			FROM {$s("table_name_values")}
 			JOIN {$s("table_name_metadata")} ON
 				{$s("table_name_metadata")}.{$s("column_metadata_id")} = {$s("table_name_values")}.{$s("column_values_reading_id")}
@@ -139,9 +134,9 @@ class MariaDBMeasurementDataRepository implements IMeasurementDataRepository {
 				{$s("table_name_metadata")}.{$s("column_metadata_device_id")} = :device_id AND
 				{$s("table_name_values")}.{$s("column_values_reading_type")} = :reading_type AND
 				
-				{$s("column_metadata_datetime_poly")} >= :start_datetime AND
-				{$s("column_metadata_datetime_poly")} <= :end_datetime
-			GROUP BY CEIL(UNIX_TIMESTAMP({$s("column_metadata_datetime_poly")}) / :average_seconds);", [
+				{$s("table_name_metadata")}.{$s("column_metadata_datetime")} >= :start_datetime AND
+				{$s("table_name_metadata")}.{$s("column_metadata_datetime")} <= :end_datetime
+			GROUP BY CEIL(UNIX_TIMESTAMP({$s("table_name_metadata")}.{$s("column_metadata_datetime")}) / :average_seconds);", [
 				"device_id" => $device_id,
 				"reading_type" => $type_id,
 				"start_datetime" => $start->format(Constants::DATETIME_FORMAT_SQL),
@@ -160,14 +155,14 @@ class MariaDBMeasurementDataRepository implements IMeasurementDataRepository {
 				{$s("table_name_values")}.{$s("column_values_value")} AS {$s("column_values_value")},
 				{$s("table_name_values")}.{$s("column_values_reading_id")} AS {$s("column_values_reading_id")},
 				
-				{$s("column_metadata_datetime_poly")} AS datetime
+				{$s("table_name_metadata")}.{$s("column_metadata_datetime")} AS datetime
 			FROM {$s("table_name_values")}
 			JOIN {$s("table_name_metadata")} ON
 				{$s("table_name_metadata")}.{$s("column_metadata_id")} = {$s("table_name_values")}.{$s("column_values_reading_id")}
 			WHERE
 				{$s("table_name_metadata")}.{$s("column_metadata_device_id")} = :device_id AND
 				{$s("table_name_values")}.{$s("column_values_reading_type")} = :reading_type
-			ORDER BY {$s("column_metadata_datetime_poly")} DESC
+			ORDER BY {$s("table_name_metadata")}.{$s("column_metadata_datetime")} DESC
 			LIMIT :count;", [
 				"device_id" => $device_id,
 				"reading_type" => $type_id,
