Fix measurement data query, but it's not quite right yet

This commit is contained in:
Starbeamrainbowlabs 2019-01-15 17:02:24 +00:00
parent 2787ea7c4c
commit 9f6bea64d4
Signed by: sbrl
GPG key ID: 1BE5172E637709C2
8 changed files with 40 additions and 29 deletions

View file

@ -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

View file

@ -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);

View file

@ -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);

View file

@ -3,5 +3,5 @@
namespace AirQuality\Actions;
interface IAction {
public function handle() : boolean;
public function handle() : bool;
}

View file

@ -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() {

View file

@ -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.

View file

@ -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();

View file

@ -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")};"