From 9f6bea64d4105e8a628311731724103ce3a3c563 Mon Sep 17 00:00:00 2001 From: Starbeamrainbowlabs Date: Tue, 15 Jan 2019 17:02:24 +0000 Subject: [PATCH] Fix measurement data query, but it's not quite right yet --- api.php | 3 +- lib/SBRL/TomlConfig.php | 6 +-- logic/Actions/FetchData.php | 2 +- logic/Actions/IAction.php | 2 +- logic/Database.php | 6 +-- .../IMeasurementTypeRepository.php | 4 +- .../MariaDBMeasurementDataRepository.php | 43 ++++++++++++------- .../MariaDBMeasurementTypeRepository.php | 3 +- 8 files changed, 40 insertions(+), 29 deletions(-) diff --git a/api.php b/api.php index c8c7ca0..9bc5076 100644 --- a/api.php +++ b/api.php @@ -30,7 +30,8 @@ $settings = $di_container->get(\SBRL\TomlConfig::class); // 4: Database -// Done via a static factory method & PHP-DI +// Done automagically by PHP-DI. +// PHP-DI autowires it, and doesn't create more than 1 instance of it either // 5: Action diff --git a/lib/SBRL/TomlConfig.php b/lib/SBRL/TomlConfig.php index 874e511..8c5df71 100644 --- a/lib/SBRL/TomlConfig.php +++ b/lib/SBRL/TomlConfig.php @@ -88,7 +88,7 @@ class TomlConfig /** * Determines whether the given property has been customised or explicitly specified in the customised settings file. * @param string $key The property, in dotted notation, to check. - * @return boolean Whether the specified properties file has been explicitly specified in the custom settings file. + * @return bool Whether the specified properties file has been explicitly specified in the custom settings file. */ public function hasChanged($key) { return static::hasPropertyByPath($this->customSettings, $key); @@ -112,7 +112,7 @@ class TomlConfig /** * Saves the customised settings back to the disk. - * @return boolean Whether the save was successful or not. + * @return bool Whether the save was successful or not. */ public function save() { $output_builder = new TomlBuilder(); @@ -139,7 +139,7 @@ class TomlConfig * Works out whether a given dotted path to a property exists on a given object. * @param stdClass $obj The object to search. * @param string $path The dotted path to search with. e.g. `Network.Firewall.OpenPorts` - * @return boolean Whether the given property is present in the given object. + * @return bool Whether the given property is present in the given object. */ public static function hasPropertyByPath($obj, $path) { $pathParts = explode(".", $path); diff --git a/logic/Actions/FetchData.php b/logic/Actions/FetchData.php index 9dce0cb..192cb46 100644 --- a/logic/Actions/FetchData.php +++ b/logic/Actions/FetchData.php @@ -38,7 +38,7 @@ class FetchData implements IAction { $this->validator = new Validator($_GET); } - public function handle() : boolean { + public function handle() : bool { global $start_time; $start_handle = microtime(true); diff --git a/logic/Actions/IAction.php b/logic/Actions/IAction.php index f8f6f8d..1aef415 100644 --- a/logic/Actions/IAction.php +++ b/logic/Actions/IAction.php @@ -3,5 +3,5 @@ namespace AirQuality\Actions; interface IAction { - public function handle() : boolean; + public function handle() : bool; } diff --git a/logic/Database.php b/logic/Database.php index e1932c1..48d1f0f 100644 --- a/logic/Database.php +++ b/logic/Database.php @@ -24,14 +24,12 @@ class Database ]; function __construct(TomlConfig $in_settings) { - error_log("Created database instance"); $this->settings = $in_settings; $this->connect(); // Connect automagically } public function connect() { - error_log($this->get_connection_string()); $this->connection = new \PDO( $this->get_connection_string(), $this->settings->get("database.username"), @@ -45,7 +43,9 @@ class Database public function query($sql, $variables = []) { // FUTURE: Optionally cache prepared statements? - return $this->connection->prepare($sql)->execute($variables); + $statement = $this->connection->prepare($sql); + $statement->execute($variables); + return $statement; // fetchColumn(), fetchAll(), etc. are defined on the statement, not the return value of execute() } private function get_connection_string() { diff --git a/logic/Repositories/IMeasurementTypeRepository.php b/logic/Repositories/IMeasurementTypeRepository.php index 10d36ff..e255204 100644 --- a/logic/Repositories/IMeasurementTypeRepository.php +++ b/logic/Repositories/IMeasurementTypeRepository.php @@ -6,9 +6,9 @@ interface IMeasurementTypeRepository { /** * Returns whether the specified type is valid or not. * @param string $type_name The name of the type to validate. - * @return boolean Whether the specified type name is valid or not. + * @return bool Whether the specified type name is valid or not. */ - public function is_valid_type(string $type_name) : boolean; + public function is_valid_type(string $type_name) : bool; /** * Gets the friendly name for the specified type name. diff --git a/logic/Repositories/MariaDBMeasurementDataRepository.php b/logic/Repositories/MariaDBMeasurementDataRepository.php index 542519f..6c6f5d2 100644 --- a/logic/Repositories/MariaDBMeasurementDataRepository.php +++ b/logic/Repositories/MariaDBMeasurementDataRepository.php @@ -29,36 +29,47 @@ class MariaDBMeasurementDataRepository implements IMeasurementDataRepository { */ private $database; + /** Function that gets a static variable by it's name. Useful in preparing SQL queries. */ + private $get_static; + + private $get_static_extra; + function __construct(\AirQuality\Database $in_database) { $this->database = $in_database; + $this->get_static = function($name) { return self::$$name; }; + $this->get_static_extra = function($class_name, $name) { + return $class_name::$$name; + }; } public function get_readings_by_date(\DateTime $datetime, string $reading_type) { + $s = $this->get_static; + $o = $this->get_static_extra; return $this->database->query( "SELECT - $this->table_name_values.*, - $this->table_name_metadata.device_id, + {$s("table_name_values")}.*, + {$s("table_name_metadata")}.device_id, COALESCE( - $this->table_name_metadata.$this->column_metadata_recordedon, - $this->table_name_metadata.$this->column_metadata_storedon + {$s("table_name_metadata")}.{$s("column_metadata_recordedon")}, + {$s("table_name_metadata")}.{$s("column_metadata_storedon")} ) AS datetime, COALESCE( - $this->table_name_metadata.$this->column_metadata_lat, - {MariaDBDeviceRepository::$table_name}.device_latitude + {$s("table_name_metadata")}.{$s("column_metadata_lat")}, + {$o(MariaDBDeviceRepository::class, "table_name")}.{$o(MariaDBDeviceRepository::class, "column_lat")} ) AS latitude, COALESCE( - $this->table_name_metadata.$this->column_metadata_long, - devices.device_longitude + {$s("table_name_metadata")}.{$s("column_metadata_long")}, + {$o(MariaDBDeviceRepository::class, "table_name")}.{$o(MariaDBDeviceRepository::class, "column_long")} ) AS longitude - FROM $this->table_name_values - JOIN $this->table_name_metadata ON $this->table_name_values.$this->column_values_reading_id = $this->table_name_metadata.id - JOIN devices ON $this->table_name_metadata.$this->column_metadata_device_id = devices.device_id + 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 + JOIN {$o(MariaDBDeviceRepository::class, "table_name")} ON {$s("table_name_metadata")}.{$s("column_metadata_device_id")} = {$o(MariaDBDeviceRepository::class, "table_name")}.{$o(MariaDBDeviceRepository::class, "column_device_id")} WHERE COALESCE( - $this->table_name_metadata.$this->column_metadata_recordedon, - $this->table_name_metadata.$this->column_metadata_storedon - ) = :datetime", - [ - "datetime" => $datetime, + {$s("table_name_metadata")}.{$s("column_metadata_recordedon")}, + {$s("table_name_metadata")}.{$s("column_metadata_storedon")} + ) = :datetime", [ + // The database likes strings, not PHP DateTime() instances + "datetime" => $datetime->format(\DateTime::ISO8601), "reading_type" => $reading_type ] )->fetchAll(); diff --git a/logic/Repositories/MariaDBMeasurementTypeRepository.php b/logic/Repositories/MariaDBMeasurementTypeRepository.php index dcd86ca..8fa4617 100644 --- a/logic/Repositories/MariaDBMeasurementTypeRepository.php +++ b/logic/Repositories/MariaDBMeasurementTypeRepository.php @@ -24,11 +24,10 @@ class MariaDBMeasurementTypeRepository implements IMeasurementTypeRepository { function __construct(\AirQuality\Database $in_database) { $this->database = $in_database; - $this->get_static = function($name) { return self::$$name; }; } - public function is_valid_type(string $type_name) : boolean { + public function is_valid_type(string $type_name) : bool { $s = $this->get_static; return !empty($this->database->query( "SELECT {$s("column_id")} FROM {$s("table_name")};"