blob: d54eb253bcb6d2d5b7ab8aa6b0cf110c60e5f5cf (
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
|
<?php
/* Icinga Web 2 | (c) 2020 Icinga GmbH | GPLv2+ */
namespace Icinga\Common;
use Icinga\Application\Config as IcingaConfig;
use Icinga\Data\ResourceFactory;
use ipl\Sql\Config as SqlConfig;
use ipl\Sql\Connection;
use LogicException;
use PDO;
/**
* Trait for accessing the Icinga Web database
*/
trait Database
{
/**
* Get a connection to the Icinga Web database
*
* @return Connection
*
* @throws \Icinga\Exception\ConfigurationError
*/
protected function getDb(): Connection
{
if (! $this->hasDb()) {
throw new LogicException('Please check if a db instance exists at all');
}
$config = new SqlConfig(ResourceFactory::getResourceConfig(
IcingaConfig::app()->get('global', 'config_resource')
));
if ($config->db === 'mysql') {
$config->charset = 'utf8mb4';
}
$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 new Connection($config);
}
/**
* Check if db exists
*
* @return bool true if a database was found otherwise false
*/
protected function hasDb()
{
return (bool) IcingaConfig::app()->get('global', 'config_resource');
}
}
|