Added the zend framework 2 library, the path is specified in line no.26 in zend_modul...
[openemr.git] / interface / modules / zend_modules / library / Zend / Db / Adapter / Driver / IbmDb2 / Connection.php
blob0d3441506226c1fc1173d71813f97cec9da4e161
1 <?php
2 /**
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
8 */
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
18 /** @var IbmDb2 */
19 protected $driver = null;
21 /**
22 * @var array
24 protected $connectionParameters = null;
26 /**
27 * @var resource
29 protected $resource = null;
31 /**
32 * @var Profiler\ProfilerInterface
34 protected $profiler = null;
36 /**
37 * Constructor
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'
55 /**
56 * Set driver
58 * @param IbmDb2 $driver
59 * @return Connection
61 public function setDriver(IbmDb2 $driver)
63 $this->driver = $driver;
64 return $this;
67 /**
68 * @param Profiler\ProfilerInterface $profiler
69 * @return Connection
71 public function setProfiler(Profiler\ProfilerInterface $profiler)
73 $this->profiler = $profiler;
74 return $this;
77 /**
78 * @return null|Profiler\ProfilerInterface
80 public function getProfiler()
82 return $this->profiler;
85 /**
86 * @param array $connectionParameters
87 * @return Connection
89 public function setConnectionParameters(array $connectionParameters)
91 $this->connectionParameters = $connectionParameters;
92 return $this;
95 /**
96 * @return array
98 public function getConnectionParameters()
100 return $this->connectionParameters;
104 * @param resource $resource DB2 resource
105 * @return Connection
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;
113 return $this;
117 * Get current schema
119 * @return string
121 public function getCurrentSchema()
123 if (!$this->isConnected()) {
124 $this->connect();
127 $info = db2_server_info($this->resource);
128 return (isset($info->DB_NAME) ? $info->DB_NAME : '');
132 * Get resource
134 * @return mixed
136 public function getResource()
138 return $this->resource;
142 * Connect
144 * @return ConnectionInterface
146 public function connect()
148 if (is_resource($this->resource)) {
149 return $this;
152 // localize
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])) {
159 return $p[$name];
162 return null;
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',
181 __METHOD__
184 return $this;
188 * Is connected
190 * @return bool
192 public function isConnected()
194 return ($this->resource !== null);
198 * Disconnect
200 * @return ConnectionInterface
202 public function disconnect()
204 if ($this->resource) {
205 db2_close($this->resource);
206 $this->resource = null;
208 return $this;
212 * Begin transaction
214 * @return ConnectionInterface
216 public function beginTransaction()
218 // TODO: Implement beginTransaction() method.
222 * Commit
224 * @return ConnectionInterface
226 public function commit()
228 // TODO: Implement commit() method.
232 * Rollback
234 * @return ConnectionInterface
236 public function rollback()
238 // TODO: Implement rollback() method.
242 * Execute
244 * @param string $sql
245 * @return Result
247 public function execute($sql)
249 if (!$this->isConnected()) {
250 $this->connect();
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
278 * @return int
280 public function getLastGeneratedValue($name = null)
282 return db2_last_insert_id($this->resource);