Fix meaurement type repo

This commit is contained in:
Starbeamrainbowlabs 2019-01-15 16:16:00 +00:00
parent f31989b7eb
commit 57ec980b7d
Signed by: sbrl
GPG Key ID: 1BE5172E637709C2
2 changed files with 14 additions and 9 deletions

View File

@ -36,7 +36,7 @@ class Database
$this->connection->query("SET SESSION TRANSACTION READ ONLY;"); $this->connection->query("SET SESSION TRANSACTION READ ONLY;");
} }
public function query($sql, $variables) { public function query($sql, $variables = []) {
// FUTURE: Optionally cache prepared statements? // FUTURE: Optionally cache prepared statements?
return $this->connection->prepare($sql)->execute($variables); return $this->connection->prepare($sql)->execute($variables);
} }

View File

@ -19,31 +19,36 @@ class MariaDBMeasurementTypeRepository implements IMeasurementTypeRepository {
*/ */
private $database; private $database;
/** Function that gets a static variable by it's name. Useful in preparing SQL queries. */
private $get_static;
function __construct(\AirQuality\Database $in_database) { function __construct(\AirQuality\Database $in_database) {
$this->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) : boolean {
$s = $this->get_static;
return !empty($this->database->query( return !empty($this->database->query(
"SELECT $this->column_id FROM $this->table_name;", "SELECT {$s("column_id")} FROM {$s("table_name")};"
[]
)->fetchColumn()); )->fetchColumn());
} }
public function get_friendly_name(string $type_name) : string { public function get_friendly_name(string $type_name) : string {
// TODO: Cache results here? Maybe https://packagist.org/packages/thumbtack/querycache will be of some use
$s = $this->get_static;
return $this->database->query( return $this->database->query(
"SELECT $this->column_friendly_text FROM $this->table_name WHERE $this->column_id = :type_name;", [ "SELECT {$s("column_friendly_text")} FROM {$s("table_name")} WHERE $this->column_id = :type_name;", [
"type_name" => $type_name "type_name" => $type_name
] ]
)->fetchColumn(); )->fetchColumn();
// TODO: Cache results here? Maybe https://packagist.org/packages/thumbtack/querycache will be of some use
throw new Exception("Error: Not implemented yet :-\\");
} }
public function get_all_types() { public function get_all_types() {
$s = $this->get_static;
return $this->database->query( return $this->database->query(
"SELECT * FROM $this->table_name" "SELECT * FROM {$s("table_name")}"
)->fetchAll(); )->fetchAll();
} }
} }