Source of file Connection.php
Size: 6,937 Bytes - Last Modified: 2014-03-12T23:21:18+01:00
/home/theseer/Downloads/ZendFramework-2.3.0/library/Zend/Db/Adapter/Driver/IbmDb2/Connection.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284 | <?php /** * Zend Framework (http://framework.zend.com/) * * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ namespace Zend\Db\Adapter\Driver\IbmDb2; use Zend\Db\Adapter\Driver\ConnectionInterface; use Zend\Db\Adapter\Exception; use Zend\Db\Adapter\Profiler; class Connection implements ConnectionInterface, Profiler\ProfilerAwareInterface { /** @var IbmDb2 */ protected $driver = null; /** * @var array */ protected $connectionParameters = null; /** * @var resource */ protected $resource = null; /** * @var Profiler\ProfilerInterface */ protected $profiler = null; /** * Constructor * * @param array|resource|null $connectionParameters (ibm_db2 connection resource) * @throws Exception\InvalidArgumentException */ public function __construct($connectionParameters = null) { if (is_array($connectionParameters)) { $this->setConnectionParameters($connectionParameters); } elseif (is_resource($connectionParameters)) { $this->setResource($connectionParameters); } elseif (null !== $connectionParameters) { throw new Exception\InvalidArgumentException( '$connection must be an array of parameters, a db2 connection resource or null' ); } } /** * Set driver * * @param IbmDb2 $driver * @return Connection */ public function setDriver(IbmDb2 $driver) { $this->driver = $driver; return $this; } /** * @param Profiler\ProfilerInterface $profiler * @return Connection */ public function setProfiler(Profiler\ProfilerInterface $profiler) { $this->profiler = $profiler; return $this; } /** * @return null|Profiler\ProfilerInterface */ public function getProfiler() { return $this->profiler; } /** * @param array $connectionParameters * @return Connection */ public function setConnectionParameters(array $connectionParameters) { $this->connectionParameters = $connectionParameters; return $this; } /** * @return array */ public function getConnectionParameters() { return $this->connectionParameters; } /** * @param resource $resource DB2 resource * @return Connection */ public function setResource($resource) { if (!is_resource($resource) || get_resource_type($resource) !== 'DB2 Connection') { throw new Exception\InvalidArgumentException('The resource provided must be of type "DB2 Connection"'); } $this->resource = $resource; return $this; } /** * Get current schema * * @return string */ public function getCurrentSchema() { if (!$this->isConnected()) { $this->connect(); } $info = db2_server_info($this->resource); return (isset($info->DB_NAME) ? $info->DB_NAME : ''); } /** * Get resource * * @return mixed */ public function getResource() { return $this->resource; } /** * Connect * * @return ConnectionInterface */ public function connect() { if (is_resource($this->resource)) { return $this; } // localize $p = $this->connectionParameters; // given a list of key names, test for existence in $p $findParameterValue = function (array $names) use ($p) { foreach ($names as $name) { if (isset($p[$name])) { return $p[$name]; } } return null; }; $database = $findParameterValue(array('database', 'db')); $username = $findParameterValue(array('username', 'uid', 'UID')); $password = $findParameterValue(array('password', 'pwd', 'PWD')); $isPersistent = $findParameterValue(array('persistent', 'PERSISTENT', 'Persistent')); $options = (isset($p['driver_options']) ? $p['driver_options'] : array()); if ($isPersistent) { $this->resource = db2_pconnect($database, $username, $password, $options); } else { $this->resource = db2_connect($database, $username, $password, $options); } if ($this->resource === false) { throw new Exception\RuntimeException(sprintf( '%s: Unable to connect to database', __METHOD__ )); } return $this; } /** * Is connected * * @return bool */ public function isConnected() { return ($this->resource !== null); } /** * Disconnect * * @return ConnectionInterface */ public function disconnect() { if ($this->resource) { db2_close($this->resource); $this->resource = null; } return $this; } /** * Begin transaction * * @return ConnectionInterface */ public function beginTransaction() { // TODO: Implement beginTransaction() method. } /** * Commit * * @return ConnectionInterface */ public function commit() { // TODO: Implement commit() method. } /** * Rollback * * @return ConnectionInterface */ public function rollback() { // TODO: Implement rollback() method. } /** * Execute * * @param string $sql * @return Result */ public function execute($sql) { if (!$this->isConnected()) { $this->connect(); } if ($this->profiler) { $this->profiler->profilerStart($sql); } set_error_handler(function () {}, E_WARNING); // suppress warnings $resultResource = db2_exec($this->resource, $sql); restore_error_handler(); if ($this->profiler) { $this->profiler->profilerFinish($sql); } // if the returnValue is something other than a pg result resource, bypass wrapping it if ($resultResource === false) { throw new Exception\InvalidQueryException(db2_stmt_errormsg()); } $resultPrototype = $this->driver->createResult(($resultResource === true) ? $this->resource : $resultResource); return $resultPrototype; } /** * Get last generated id * * @param null $name Ignored * @return int */ public function getLastGeneratedValue($name = null) { return db2_last_insert_id($this->resource); } } |