diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-14 13:29:16 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-14 13:29:16 +0000 |
commit | 8a985929ed84cdb458a13c66b25f84e41133b24f (patch) | |
tree | 102a3d6e3cb731c6d23263095d0098f99572626d /library/Reporting/Database.php | |
parent | Adding upstream version 0.10.0. (diff) | |
download | icingaweb2-module-reporting-upstream.tar.xz icingaweb2-module-reporting-upstream.zip |
Adding upstream version 1.0.1.upstream/1.0.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r-- | library/Reporting/Database.php | 103 |
1 files changed, 74 insertions, 29 deletions
diff --git a/library/Reporting/Database.php b/library/Reporting/Database.php index 3dabe17..7ea32a7 100644 --- a/library/Reporting/Database.php +++ b/library/Reporting/Database.php @@ -1,4 +1,5 @@ <?php + // Icinga Reporting | (c) 2018 Icinga GmbH | GPLv2 namespace Icinga\Module\Reporting; @@ -6,53 +7,97 @@ namespace Icinga\Module\Reporting; use Icinga\Application\Config; use Icinga\Data\ResourceFactory; use ipl\Sql; +use PDO; +use stdClass; -trait Database +final class Database { - protected function getDb($resource = null) + /** @var RetryConnection Database connection */ + private static $instance; + + private function __construct() { - $config = new Sql\Config(ResourceFactory::getResourceConfig( - $resource ?: Config::module('reporting')->get('backend', 'resource', 'reporting') - )); + } - $config->options = [\PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_OBJ]; - if ($config->db === 'mysql') { - $config->options[\PDO::MYSQL_ATTR_INIT_COMMAND] = "SET SESSION SQL_MODE='STRICT_TRANS_TABLES" - . ",NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION'"; + /** + * Get the database connection + * + * @return RetryConnection + */ + public static function get(): RetryConnection + { + if (self::$instance === null) { + self::$instance = self::getDb(); } - $conn = new RetryConnection($config); - - return $conn; + return self::$instance; } - protected function listTimeframes() + private static function getDb(): RetryConnection { - $select = (new Sql\Select()) - ->from('timeframe') - ->columns(['id', 'name']); - - $timeframes = []; + $config = new Sql\Config( + ResourceFactory::getResourceConfig( + Config::module('reporting')->get('backend', 'resource', 'reporting') + ) + ); - foreach ($this->getDb()->select($select) as $row) { - $timeframes[$row->id] = $row->name; + $config->options = [PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_OBJ]; + if ($config->db === 'mysql') { + $config->options[PDO::MYSQL_ATTR_INIT_COMMAND] = "SET SESSION SQL_MODE='STRICT_TRANS_TABLES" + . ",NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION'"; } - return $timeframes; + return new RetryConnection($config); } - protected function listTemplates() + /** + * List all reporting timeframes + * + * @return array<int, string> + */ + public static function listTimeframes(): array { - $select = (new Sql\Select()) - ->from('template') - ->columns(['id', 'name']); + return self::list( + (new Sql\Select()) + ->from('timeframe') + ->columns(['id', 'name']) + ); + } - $templates = []; + /** + * List all reporting templates + * + * @return array<int, string> + */ + public static function listTemplates(): array + { + return self::list( + (new Sql\Select()) + ->from('template') + ->columns(['id', 'name']) + ); + } + + /** + * Helper method for list templates and timeframes + * + * @param Sql\Select $select + * + * @return array<int, string> + */ + private static function list(Sql\Select $select): array + { + $result = []; + /** @var stdClass $row */ + foreach (self::get()->select($select) as $row) { + /** @var int $id */ + $id = $row->id; + /** @var string $name */ + $name = $row->name; - foreach ($this->getDb()->select($select) as $row) { - $templates[$row->id] = $row->name; + $result[$id] = $name; } - return $templates; + return $result; } } |