icingadb-web/library/Icingadb/Common/Database.php

45 lines
1.2 KiB
PHP
Raw Normal View History

2019-11-27 07:19:06 -05:00
<?php
2020-03-13 03:38:01 -04:00
/* Icinga DB Web | (c) 2020 Icinga GmbH | GPLv2 */
2019-11-27 07:19:06 -05:00
namespace Icinga\Module\Icingadb\Common;
use Icinga\Application\Config as AppConfig;
use Icinga\Data\ResourceFactory;
use Icinga\Exception\ConfigurationError;
use ipl\Sql\Config as SqlConfig;
use ipl\Sql\Connection;
use PDO;
trait Database
{
/** @var Connection Connection to the Icinga database */
private $db;
/**
* Get the connection to the Icinga database
*
* @return Connection
*
* @throws ConfigurationError If the related resource configuration does not exist
*/
public function getDb(): Connection
2019-11-27 07:19:06 -05:00
{
if ($this->db === null) {
$config = new SqlConfig(ResourceFactory::getResourceConfig(
AppConfig::module('icingadb')->get('icingadb', 'resource')
2019-11-27 07:19:06 -05:00
));
$config->options = [
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_OBJ,
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'"
];
$this->db = new Connection($config);
}
return $this->db;
}
}