summaryrefslogtreecommitdiffstats
path: root/library/Reporting/Database.php
blob: 3dabe174c4b145f733d0f78734b695e918f9c564 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<?php
// Icinga Reporting | (c) 2018 Icinga GmbH | GPLv2

namespace Icinga\Module\Reporting;

use Icinga\Application\Config;
use Icinga\Data\ResourceFactory;
use ipl\Sql;

trait Database
{
    protected function getDb($resource = null)
    {
        $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'";
        }

        $conn = new RetryConnection($config);

        return $conn;
    }

    protected function listTimeframes()
    {
        $select = (new Sql\Select())
            ->from('timeframe')
            ->columns(['id', 'name']);

        $timeframes = [];

        foreach ($this->getDb()->select($select) as $row) {
            $timeframes[$row->id] = $row->name;
        }

        return $timeframes;
    }

    protected function listTemplates()
    {
        $select = (new Sql\Select())
            ->from('template')
            ->columns(['id', 'name']);

        $templates = [];

        foreach ($this->getDb()->select($select) as $row) {
            $templates[$row->id] = $row->name;
        }

        return $templates;
    }
}