2013-06-07 11:44:37 +02:00
|
|
|
<?php
|
2013-08-19 18:06:16 +02:00
|
|
|
// {{{ICINGA_LICENSE_HEADER}}}
|
|
|
|
/**
|
|
|
|
* This file is part of Icinga 2 Web.
|
|
|
|
*
|
|
|
|
* Icinga 2 Web - Head for multiple monitoring backends.
|
|
|
|
* Copyright (C) 2013 Icinga Development Team
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*
|
|
|
|
* @copyright 2013 Icinga Development Team <info@icinga.org>
|
|
|
|
* @license http://www.gnu.org/licenses/gpl-2.0.txt GPL, version 2
|
|
|
|
* @author Icinga Development Team <info@icinga.org>
|
|
|
|
*/
|
|
|
|
// {{{ICINGA_LICENSE_HEADER}}}
|
2013-06-07 11:44:37 +02:00
|
|
|
|
|
|
|
namespace Icinga\Data\Db;
|
|
|
|
|
2013-08-19 18:06:16 +02:00
|
|
|
use \PDO;
|
|
|
|
use \Zend_Config;
|
|
|
|
use \Zend_Db;
|
|
|
|
use \Zend_Db_Adapter_Abstract;
|
|
|
|
use \Icinga\Application\DbAdapterFactory;
|
|
|
|
use \Icinga\Data\DatasourceInterface;
|
|
|
|
use \Icinga\Exception\ConfigurationError;
|
|
|
|
use \Icinga\Application\Logger;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Encapsulate database connections and query creation
|
|
|
|
*/
|
2013-06-07 11:44:37 +02:00
|
|
|
class Connection implements DatasourceInterface
|
|
|
|
{
|
2013-08-19 18:06:16 +02:00
|
|
|
/**
|
|
|
|
* Database connection
|
|
|
|
*
|
|
|
|
* @var Zend_Db_Adapter_Abstract
|
|
|
|
*/
|
2013-06-07 11:44:37 +02:00
|
|
|
protected $db;
|
2013-08-19 18:06:16 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Backend configuration
|
|
|
|
*
|
|
|
|
* @var Zend_Config
|
|
|
|
*/
|
2013-06-07 11:44:37 +02:00
|
|
|
protected $config;
|
|
|
|
|
2013-08-19 18:06:16 +02:00
|
|
|
/**
|
|
|
|
* Database type
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $dbType;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new connection object
|
|
|
|
*
|
|
|
|
* @param Zend_Config $config
|
|
|
|
*/
|
|
|
|
public function __construct(Zend_Config $config = null)
|
2013-06-07 11:44:37 +02:00
|
|
|
{
|
|
|
|
$this->config = $config;
|
|
|
|
$this->connect();
|
|
|
|
}
|
|
|
|
|
2013-08-19 18:06:16 +02:00
|
|
|
/**
|
|
|
|
* Prepare query object
|
|
|
|
*
|
|
|
|
* @return Query
|
|
|
|
*/
|
2013-06-07 11:44:37 +02:00
|
|
|
public function select()
|
|
|
|
{
|
|
|
|
return new Query($this);
|
|
|
|
}
|
|
|
|
|
2013-08-19 18:06:16 +02:00
|
|
|
/**
|
|
|
|
* Getter for database type
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2013-06-07 11:44:37 +02:00
|
|
|
public function getDbType()
|
|
|
|
{
|
2013-08-19 18:06:16 +02:00
|
|
|
return $this->dbType;
|
2013-06-07 11:44:37 +02:00
|
|
|
}
|
|
|
|
|
2013-08-19 18:06:16 +02:00
|
|
|
/**
|
|
|
|
* Getter for database object
|
|
|
|
*
|
|
|
|
* @return Zend_Db_Adapter_Abstract
|
|
|
|
*/
|
2013-06-07 11:44:37 +02:00
|
|
|
public function getDb()
|
|
|
|
{
|
|
|
|
return $this->db;
|
|
|
|
}
|
|
|
|
|
2013-08-19 18:06:16 +02:00
|
|
|
/**
|
|
|
|
* Create a new connection
|
|
|
|
*/
|
|
|
|
private function connect()
|
2013-06-07 11:44:37 +02:00
|
|
|
{
|
2013-08-19 18:06:16 +02:00
|
|
|
$resourceName = $this->config->get('resource');
|
|
|
|
$this->db = DbAdapterFactory::getDbAdapter($resourceName);
|
2013-06-07 11:44:37 +02:00
|
|
|
|
2013-08-19 18:06:16 +02:00
|
|
|
if ($this->db->getConnection() instanceof PDO) {
|
|
|
|
$this->dbType = $this->db->getConnection()->getAttribute(PDO::ATTR_DRIVER_NAME);
|
|
|
|
} else {
|
|
|
|
$this->dbType = strtolower(get_class($this->db->getConnection()));
|
2013-06-07 11:44:37 +02:00
|
|
|
}
|
2013-08-20 23:07:45 +02:00
|
|
|
$this->db->setFetchMode(Zend_Db::FETCH_OBJ);
|
2013-07-29 18:37:59 +02:00
|
|
|
|
2013-08-19 18:06:16 +02:00
|
|
|
if ($this->dbType === null) {
|
|
|
|
Logger::warn('Could not determine database type');
|
|
|
|
}
|
2013-06-07 11:44:37 +02:00
|
|
|
|
2013-08-19 18:06:16 +02:00
|
|
|
if ($this->dbType === 'oci') {
|
|
|
|
$this->dbType = 'oracle';
|
|
|
|
}
|
2013-06-07 11:44:37 +02:00
|
|
|
}
|
|
|
|
}
|