* @license http://www.gnu.org/licenses/gpl-2.0.txt GPL, version 2 * @author Icinga Development Team */ // {{{ICINGA_LICENSE_HEADER}}} namespace Icinga\Data\Db; 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 */ class Connection implements DatasourceInterface { /** * Database connection * * @var Zend_Db_Adapter_Abstract */ protected $db; /** * Backend configuration * * @var Zend_Config */ protected $config; /** * Database type * * @var string */ protected $dbType; /** * Create a new connection object * * @param Zend_Config $config */ public function __construct(Zend_Config $config = null) { $this->config = $config; $this->connect(); } /** * Prepare query object * * @return Query */ public function select() { return new Query($this); } /** * Getter for database type * * @return string */ public function getDbType() { return $this->dbType; } /** * Getter for database object * * @return Zend_Db_Adapter_Abstract */ public function getDb() { return $this->db; } /** * Create a new connection */ private function connect() { $resourceName = $this->config->get('resource'); $this->db = DbAdapterFactory::getDbAdapter($resourceName); 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())); } $this->db->setFetchMode(Zend_Db::FETCH_OBJ); if ($this->dbType === null) { Logger::warn('Could not determine database type'); } if ($this->dbType === 'oci') { $this->dbType = 'oracle'; } } }