2019-01-15 15:46:24 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace AirQuality\Repositories;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetches measurement readings from a MariaDB database.
|
|
|
|
*/
|
|
|
|
class MariaDBMeasurementDataRepository implements IMeasurementDataRepository {
|
|
|
|
public static $table_name_metadata = "readings";
|
|
|
|
public static $table_name_values = "reading_values";
|
|
|
|
|
|
|
|
public static $column_values_id = "id";
|
|
|
|
public static $column_values_reading_id = "reading_id";
|
|
|
|
public static $column_values_value = "value";
|
|
|
|
public static $column_values_reading_value_types_id = "reading_value_types_id";
|
|
|
|
|
|
|
|
public static $column_metadata_id = "id";
|
|
|
|
public static $column_metadata_storedon = "storedon";
|
|
|
|
public static $column_metadata_recordedon = "recordedon";
|
|
|
|
public static $column_metadata_device_id = "device_id";
|
|
|
|
public static $column_metadata_lat = "reading_latitude";
|
|
|
|
public static $column_metadata_long = "reading_longitude";
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The database connection.
|
|
|
|
* @var \AirQuality\Database
|
|
|
|
*/
|
|
|
|
private $database;
|
|
|
|
|
2019-01-15 17:02:24 +00:00
|
|
|
/** Function that gets a static variable by it's name. Useful in preparing SQL queries. */
|
|
|
|
private $get_static;
|
|
|
|
|
|
|
|
private $get_static_extra;
|
|
|
|
|
2019-01-15 15:46:24 +00:00
|
|
|
function __construct(\AirQuality\Database $in_database) {
|
|
|
|
$this->database = $in_database;
|
2019-01-15 17:02:24 +00:00
|
|
|
$this->get_static = function($name) { return self::$$name; };
|
|
|
|
$this->get_static_extra = function($class_name, $name) {
|
|
|
|
return $class_name::$$name;
|
|
|
|
};
|
2019-01-15 15:46:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function get_readings_by_date(\DateTime $datetime, string $reading_type) {
|
2019-01-15 17:02:24 +00:00
|
|
|
$s = $this->get_static;
|
|
|
|
$o = $this->get_static_extra;
|
2019-01-15 15:46:24 +00:00
|
|
|
return $this->database->query(
|
|
|
|
"SELECT
|
2019-01-15 17:02:24 +00:00
|
|
|
{$s("table_name_values")}.*,
|
|
|
|
{$s("table_name_metadata")}.device_id,
|
2019-01-15 15:46:24 +00:00
|
|
|
COALESCE(
|
2019-01-15 17:02:24 +00:00
|
|
|
{$s("table_name_metadata")}.{$s("column_metadata_recordedon")},
|
|
|
|
{$s("table_name_metadata")}.{$s("column_metadata_storedon")}
|
2019-01-15 15:46:24 +00:00
|
|
|
) AS datetime,
|
|
|
|
COALESCE(
|
2019-01-15 17:02:24 +00:00
|
|
|
{$s("table_name_metadata")}.{$s("column_metadata_lat")},
|
|
|
|
{$o(MariaDBDeviceRepository::class, "table_name")}.{$o(MariaDBDeviceRepository::class, "column_lat")}
|
2019-01-15 15:46:24 +00:00
|
|
|
) AS latitude,
|
|
|
|
COALESCE(
|
2019-01-15 17:02:24 +00:00
|
|
|
{$s("table_name_metadata")}.{$s("column_metadata_long")},
|
|
|
|
{$o(MariaDBDeviceRepository::class, "table_name")}.{$o(MariaDBDeviceRepository::class, "column_long")}
|
2019-01-15 15:46:24 +00:00
|
|
|
) AS longitude
|
2019-01-15 17:02:24 +00:00
|
|
|
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")}
|
2019-01-15 15:46:24 +00:00
|
|
|
WHERE COALESCE(
|
2019-01-15 17:02:24 +00:00
|
|
|
{$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),
|
2019-01-15 15:46:24 +00:00
|
|
|
"reading_type" => $reading_type
|
|
|
|
]
|
2019-01-15 15:57:39 +00:00
|
|
|
)->fetchAll();
|
2019-01-15 15:46:24 +00:00
|
|
|
}
|
|
|
|
}
|