2013-06-07 11:44:37 +02:00
|
|
|
<?php
|
2013-08-19 18:06:16 +02:00
|
|
|
// {{{ICINGA_LICENSE_HEADER}}}
|
|
|
|
// {{{ICINGA_LICENSE_HEADER}}}
|
2013-06-07 11:44:37 +02:00
|
|
|
|
|
|
|
namespace Icinga\Data\Db;
|
|
|
|
|
2014-04-15 17:44:34 +02:00
|
|
|
use Icinga\Application\Benchmark;
|
2014-06-06 08:43:13 +02:00
|
|
|
use Icinga\Data\Db\DbQuery;
|
2014-06-17 11:51:37 +02:00
|
|
|
use Icinga\Data\ResourceFactory;
|
2014-04-15 17:44:34 +02:00
|
|
|
use Icinga\Data\Selectable;
|
2013-09-24 15:26:10 +02:00
|
|
|
use Icinga\Exception\ConfigurationError;
|
2014-06-06 08:43:13 +02:00
|
|
|
use PDO;
|
|
|
|
use Zend_Config;
|
|
|
|
use Zend_Db;
|
2013-08-19 18:06:16 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Encapsulate database connections and query creation
|
|
|
|
*/
|
2014-06-06 08:43:13 +02:00
|
|
|
class DbConnection implements Selectable
|
2013-06-07 11:44:37 +02:00
|
|
|
{
|
2013-08-19 18:06:16 +02:00
|
|
|
/**
|
2013-09-24 15:26:10 +02:00
|
|
|
* Connection config
|
2013-08-19 18:06:16 +02:00
|
|
|
*
|
|
|
|
* @var Zend_Config
|
|
|
|
*/
|
2013-09-24 15:26:10 +02:00
|
|
|
private $config;
|
2013-06-07 11:44:37 +02:00
|
|
|
|
2013-08-19 18:06:16 +02:00
|
|
|
/**
|
|
|
|
* Database type
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2013-09-24 15:26:10 +02:00
|
|
|
private $dbType;
|
|
|
|
|
2014-04-15 17:37:44 +02:00
|
|
|
/**
|
|
|
|
* @var Zend_Db_Adapter_Abstract
|
|
|
|
*/
|
|
|
|
private $dbAdapter;
|
2013-09-24 15:26:10 +02:00
|
|
|
|
2014-04-15 17:37:44 +02:00
|
|
|
/**
|
|
|
|
* Table prefix
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2013-09-24 15:26:10 +02:00
|
|
|
private $tablePrefix = '';
|
|
|
|
|
|
|
|
private static $genericAdapterOptions = array(
|
|
|
|
Zend_Db::AUTO_QUOTE_IDENTIFIERS => false,
|
|
|
|
Zend_Db::CASE_FOLDING => Zend_Db::CASE_LOWER
|
|
|
|
);
|
|
|
|
|
|
|
|
private static $driverOptions = array(
|
2014-06-17 11:53:59 +02:00
|
|
|
PDO::ATTR_TIMEOUT => 10,
|
|
|
|
PDO::ATTR_CASE => PDO::CASE_LOWER,
|
|
|
|
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
|
|
|
|
// TODO: allow configurable PDO::ATTR_PERSISTENT => true
|
2013-09-24 15:26:10 +02:00
|
|
|
);
|
2013-08-19 18:06:16 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
/**
|
2014-04-15 17:37:44 +02:00
|
|
|
* Provide a query on this connection
|
2013-08-19 18:06:16 +02:00
|
|
|
*
|
|
|
|
* @return Query
|
|
|
|
*/
|
2013-06-07 11:44:37 +02:00
|
|
|
public function select()
|
|
|
|
{
|
2014-06-17 11:52:32 +02:00
|
|
|
return new DbQuery($this);
|
2013-06-07 11:44:37 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
/**
|
2014-04-15 17:37:44 +02:00
|
|
|
* Getter for the Zend_Db_Adapter
|
2013-08-19 18:06:16 +02:00
|
|
|
*
|
|
|
|
* @return Zend_Db_Adapter_Abstract
|
|
|
|
*/
|
2014-04-15 17:37:44 +02:00
|
|
|
public function getDbAdapter()
|
2013-06-07 11:44:37 +02:00
|
|
|
{
|
2014-04-15 17:37:44 +02:00
|
|
|
return $this->dbAdapter;
|
2013-06-07 11:44:37 +02:00
|
|
|
}
|
|
|
|
|
2013-08-19 18:06:16 +02:00
|
|
|
/**
|
|
|
|
* Create a new connection
|
|
|
|
*/
|
|
|
|
private function connect()
|
2013-06-07 11:44:37 +02:00
|
|
|
{
|
2013-09-24 15:26:10 +02:00
|
|
|
$genericAdapterOptions = self::$genericAdapterOptions;
|
|
|
|
$driverOptions = self::$driverOptions;
|
|
|
|
$adapterParamaters = array(
|
|
|
|
'host' => $this->config->host,
|
|
|
|
'username' => $this->config->username,
|
|
|
|
'password' => $this->config->password,
|
|
|
|
'dbname' => $this->config->dbname,
|
|
|
|
'options' => & $genericAdapterOptions,
|
|
|
|
'driver_options' => & $driverOptions
|
|
|
|
);
|
|
|
|
$this->dbType = strtolower($this->config->get('db', 'mysql'));
|
|
|
|
switch ($this->dbType) {
|
|
|
|
case 'mysql':
|
|
|
|
$adapter = 'Pdo_Mysql';
|
|
|
|
/*
|
|
|
|
* Set MySQL server SQL modes to behave as closely as possible to Oracle and PostgreSQL. Note that the
|
2014-02-21 14:07:32 +01:00
|
|
|
* ONLY_FULL_GROUP_BY mode is left on purpose because MySQL requires you to specify all non-aggregate
|
|
|
|
* columns in the group by list even if the query is grouped by the master table's primary key which is
|
|
|
|
* valid ANSI SQL though. Further in that case the query plan would suffer if you add more columns to
|
|
|
|
* the group by list.
|
2013-09-24 15:26:10 +02:00
|
|
|
*/
|
|
|
|
$driverOptions[PDO::MYSQL_ATTR_INIT_COMMAND] =
|
|
|
|
'SET SESSION SQL_MODE=\'STRICT_ALL_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,'
|
|
|
|
. 'NO_AUTO_CREATE_USER,ANSI_QUOTES,PIPES_AS_CONCAT,NO_ENGINE_SUBSTITUTION\';';
|
|
|
|
$adapterParamaters['port'] = $this->config->get('port', 3306);
|
|
|
|
break;
|
|
|
|
case 'pgsql':
|
|
|
|
$adapter = 'Pdo_Pgsql';
|
|
|
|
$adapterParamaters['port'] = $this->config->get('port', 5432);
|
|
|
|
break;
|
2014-02-21 14:07:32 +01:00
|
|
|
/*case 'oracle':
|
|
|
|
if ($this->dbtype === 'oracle') {
|
|
|
|
$attributes['persistent'] = true;
|
|
|
|
}
|
|
|
|
$this->db = ZfDb::factory($adapter, $attributes);
|
|
|
|
if ($adapter === 'Oracle') {
|
|
|
|
$this->db->setLobAsString(false);
|
|
|
|
}
|
|
|
|
break;*/
|
2013-09-24 15:26:10 +02:00
|
|
|
default:
|
2014-02-21 14:07:32 +01:00
|
|
|
throw new ConfigurationError(sprintf('Backend "%s" is not supported', $this->dbType));
|
2013-06-07 11:44:37 +02:00
|
|
|
}
|
2014-04-15 17:37:44 +02:00
|
|
|
$this->dbAdapter = Zend_Db::factory($adapter, $adapterParamaters);
|
|
|
|
$this->dbAdapter->setFetchMode(Zend_Db::FETCH_OBJ);
|
|
|
|
// TODO(el/tg): The profiler is disabled per default, why do we disable the profiler explicitly?
|
|
|
|
$this->dbAdapter->getProfiler()->setEnabled(false);
|
2013-09-24 15:26:10 +02:00
|
|
|
}
|
2013-07-29 18:37:59 +02:00
|
|
|
|
2014-06-17 11:51:37 +02:00
|
|
|
public static function fromResourceName($name)
|
|
|
|
{
|
|
|
|
return new static(ResourceFactory::getResourceConfig($name));
|
|
|
|
}
|
|
|
|
|
2014-04-15 17:37:44 +02:00
|
|
|
/**
|
|
|
|
* @deprecated Use Connection::getDbAdapter() instead
|
|
|
|
*/
|
2013-09-24 15:26:10 +02:00
|
|
|
public function getConnection()
|
|
|
|
{
|
2014-04-15 17:37:44 +02:00
|
|
|
return $this->dbAdapter;
|
2013-09-24 15:26:10 +02:00
|
|
|
}
|
2013-06-07 11:44:37 +02:00
|
|
|
|
2014-04-15 17:37:44 +02:00
|
|
|
/**
|
|
|
|
* Getter for the table prefix
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2013-09-24 15:26:10 +02:00
|
|
|
public function getTablePrefix()
|
|
|
|
{
|
|
|
|
return $this->tablePrefix;
|
|
|
|
}
|
|
|
|
|
2014-04-15 17:37:44 +02:00
|
|
|
/**
|
|
|
|
* Setter for the table prefix
|
|
|
|
*
|
|
|
|
* @param string $prefix
|
|
|
|
*
|
|
|
|
* @return self
|
|
|
|
*/
|
2013-09-24 15:26:10 +02:00
|
|
|
public function setTablePrefix($prefix)
|
|
|
|
{
|
|
|
|
$this->tablePrefix = $prefix;
|
|
|
|
return $this;
|
2013-06-07 11:44:37 +02:00
|
|
|
}
|
2014-04-15 17:44:34 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieve an array containing all rows of the result set
|
|
|
|
*
|
2014-06-06 08:43:13 +02:00
|
|
|
* @param DbQuery $query
|
2014-04-15 17:44:34 +02:00
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2014-06-06 08:43:13 +02:00
|
|
|
public function fetchAll(DbQuery $query)
|
2014-04-15 17:44:34 +02:00
|
|
|
{
|
|
|
|
Benchmark::measure('DB is fetching All');
|
|
|
|
$result = $this->dbAdapter->fetchAll($query->getSelectQuery());
|
|
|
|
Benchmark::measure('DB fetch done');
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetch the first row of the result set
|
|
|
|
*
|
2014-06-06 08:43:13 +02:00
|
|
|
* @param DbQuery $query
|
2014-04-15 17:44:34 +02:00
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2014-06-06 08:43:13 +02:00
|
|
|
public function fetchRow(DbQuery $query)
|
2014-04-15 17:44:34 +02:00
|
|
|
{
|
|
|
|
return $this->dbAdapter->fetchRow($query->getSelectQuery());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetch a column of all rows of the result set as an array
|
|
|
|
*
|
2014-06-06 08:43:13 +02:00
|
|
|
* @param DbQuery $query
|
2014-04-15 17:44:34 +02:00
|
|
|
* @param int $columnIndex Index of the column to fetch
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2014-06-06 08:43:13 +02:00
|
|
|
public function fetchColumn(DbQuery $query, $columnIndex = 0)
|
2014-04-15 17:44:34 +02:00
|
|
|
{
|
|
|
|
return $this->dbAdapter->fetchCol($query->getSelectQuery());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetch the first column of the first row of the result set
|
|
|
|
*
|
2014-06-06 08:43:13 +02:00
|
|
|
* @param DbQuery $query
|
2014-04-15 17:44:34 +02:00
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2014-06-06 08:43:13 +02:00
|
|
|
public function fetchOne(DbQuery $query)
|
2014-04-15 17:44:34 +02:00
|
|
|
{
|
|
|
|
return $this->dbAdapter->fetchOne($query->getSelectQuery());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetch all rows of the result set as an array of key-value pairs
|
|
|
|
*
|
|
|
|
* The first column is the key, the second column is the value.
|
|
|
|
*
|
2014-06-06 08:43:13 +02:00
|
|
|
* @param DbQuery $query
|
2014-04-15 17:44:34 +02:00
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2014-06-06 08:43:13 +02:00
|
|
|
public function fetchPairs(DbQuery $query)
|
2014-04-15 17:44:34 +02:00
|
|
|
{
|
|
|
|
return $this->dbAdapter->fetchPairs($query->getSelectQuery());
|
|
|
|
}
|
2013-06-07 11:44:37 +02:00
|
|
|
}
|