3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
10 namespace Zend\Db\Adapter\Driver\IbmDb2
;
12 use Zend\Db\Adapter\Driver\ConnectionInterface
;
13 use Zend\Db\Adapter\Exception
;
14 use Zend\Db\Adapter\Profiler
;
16 class Connection
implements ConnectionInterface
, Profiler\ProfilerAwareInterface
19 protected $driver = null;
24 protected $connectionParameters = null;
29 protected $resource = null;
32 * @var Profiler\ProfilerInterface
34 protected $profiler = null;
39 * @param array|resource|null $connectionParameters (ibm_db2 connection resource)
40 * @throws Exception\InvalidArgumentException
42 public function __construct($connectionParameters = null)
44 if (is_array($connectionParameters)) {
45 $this->setConnectionParameters($connectionParameters);
46 } elseif (is_resource($connectionParameters)) {
47 $this->setResource($connectionParameters);
48 } elseif (null !== $connectionParameters) {
49 throw new Exception\
InvalidArgumentException(
50 '$connection must be an array of parameters, a db2 connection resource or null'
58 * @param IbmDb2 $driver
61 public function setDriver(IbmDb2
$driver)
63 $this->driver
= $driver;
68 * @param Profiler\ProfilerInterface $profiler
71 public function setProfiler(Profiler\ProfilerInterface
$profiler)
73 $this->profiler
= $profiler;
78 * @return null|Profiler\ProfilerInterface
80 public function getProfiler()
82 return $this->profiler
;
86 * @param array $connectionParameters
89 public function setConnectionParameters(array $connectionParameters)
91 $this->connectionParameters
= $connectionParameters;
98 public function getConnectionParameters()
100 return $this->connectionParameters
;
104 * @param resource $resource DB2 resource
107 public function setResource($resource)
109 if (!is_resource($resource) ||
get_resource_type($resource) !== 'DB2 Connection') {
110 throw new Exception\
InvalidArgumentException('The resource provided must be of type "DB2 Connection"');
112 $this->resource = $resource;
121 public function getCurrentSchema()
123 if (!$this->isConnected()) {
127 $info = db2_server_info($this->resource);
128 return (isset($info->DB_NAME
) ?
$info->DB_NAME
: '');
136 public function getResource()
138 return $this->resource;
144 * @return ConnectionInterface
146 public function connect()
148 if (is_resource($this->resource)) {
153 $p = $this->connectionParameters
;
155 // given a list of key names, test for existence in $p
156 $findParameterValue = function (array $names) use ($p) {
157 foreach ($names as $name) {
158 if (isset($p[$name])) {
165 $connection = array();
166 $connection['database'] = $findParameterValue(array('database', 'db'));
167 $connection['username'] = $findParameterValue(array('username', 'uid', 'UID'));
168 $connection['password'] = $findParameterValue(array('password', 'pwd', 'PWD'));
169 $connection['options'] = (isset($p['driver_options']) ?
$p['driver_options'] : array());
171 $this->resource = db2_connect(
172 $connection['database'],
173 $connection['username'],
174 $connection['password'],
175 $connection['options']
178 if ($this->resource === false) {
179 throw new Exception\
RuntimeException(sprintf(
180 '%s: Unable to connect to database',
192 public function isConnected()
194 return ($this->resource !== null);
200 * @return ConnectionInterface
202 public function disconnect()
204 if ($this->resource) {
205 db2_close($this->resource);
206 $this->resource = null;
214 * @return ConnectionInterface
216 public function beginTransaction()
218 // TODO: Implement beginTransaction() method.
224 * @return ConnectionInterface
226 public function commit()
228 // TODO: Implement commit() method.
234 * @return ConnectionInterface
236 public function rollback()
238 // TODO: Implement rollback() method.
247 public function execute($sql)
249 if (!$this->isConnected()) {
253 if ($this->profiler
) {
254 $this->profiler
->profilerStart($sql);
257 set_error_handler(function () {}, E_WARNING
); // suppress warnings
258 $resultResource = db2_exec($this->resource, $sql);
259 restore_error_handler();
261 if ($this->profiler
) {
262 $this->profiler
->profilerFinish($sql);
265 // if the returnValue is something other than a pg result resource, bypass wrapping it
266 if ($resultResource === false) {
267 throw new Exception\
InvalidQueryException(db2_stmt_errormsg());
270 $resultPrototype = $this->driver
->createResult(($resultResource === true) ?
$this->resource : $resultResource);
271 return $resultPrototype;
275 * Get last generated id
277 * @param null $name Ignored
280 public function getLastGeneratedValue($name = null)
282 return db2_last_insert_id($this->resource);