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 / Pdo / Connection.php
blob2ece67590095a0265f005586eb72e5af466b69c5
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\Pdo;
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 /**
19 * @var Pdo
21 protected $driver = null;
23 /**
24 * @var Profiler\ProfilerInterface
26 protected $profiler = null;
28 /**
29 * @var string
31 protected $driverName = null;
33 /**
34 * @var array
36 protected $connectionParameters = array();
38 /**
39 * @var \PDO
41 protected $resource = null;
43 /**
44 * @var bool
46 protected $inTransaction = false;
48 /**
49 * Constructor
51 * @param array|\PDO|null $connectionParameters
52 * @throws Exception\InvalidArgumentException
54 public function __construct($connectionParameters = null)
56 if (is_array($connectionParameters)) {
57 $this->setConnectionParameters($connectionParameters);
58 } elseif ($connectionParameters instanceof \PDO) {
59 $this->setResource($connectionParameters);
60 } elseif (null !== $connectionParameters) {
61 throw new Exception\InvalidArgumentException('$connection must be an array of parameters, a PDO object or null');
65 /**
66 * Set driver
68 * @param Pdo $driver
69 * @return Connection
71 public function setDriver(Pdo $driver)
73 $this->driver = $driver;
74 return $this;
77 /**
78 * @param Profiler\ProfilerInterface $profiler
79 * @return Connection
81 public function setProfiler(Profiler\ProfilerInterface $profiler)
83 $this->profiler = $profiler;
84 return $this;
87 /**
88 * @return null|Profiler\ProfilerInterface
90 public function getProfiler()
92 return $this->profiler;
95 /**
96 * Get driver name
98 * @return null|string
100 public function getDriverName()
102 return $this->driverName;
106 * Set connection parameters
108 * @param array $connectionParameters
109 * @return void
111 public function setConnectionParameters(array $connectionParameters)
113 $this->connectionParameters = $connectionParameters;
114 if (isset($connectionParameters['dsn'])) {
115 $this->driverName = substr($connectionParameters['dsn'], 0,
116 strpos($connectionParameters['dsn'], ':')
118 } elseif (isset($connectionParameters['pdodriver'])) {
119 $this->driverName = strtolower($connectionParameters['pdodriver']);
120 } elseif (isset($connectionParameters['driver'])) {
121 $this->driverName = strtolower(substr(
122 str_replace(array('-', '_', ' '), '', $connectionParameters['driver']),
129 * Get connection parameters
131 * @return array
133 public function getConnectionParameters()
135 return $this->connectionParameters;
139 * Get current schema
141 * @return string
143 public function getCurrentSchema()
145 if (!$this->isConnected()) {
146 $this->connect();
149 switch ($this->driverName) {
150 case 'mysql':
151 $sql = 'SELECT DATABASE()';
152 break;
153 case 'sqlite':
154 return 'main';
155 case 'pgsql':
156 default:
157 $sql = 'SELECT CURRENT_SCHEMA';
158 break;
161 /** @var $result \PDOStatement */
162 $result = $this->resource->query($sql);
163 if ($result instanceof \PDOStatement) {
164 return $result->fetchColumn();
166 return false;
170 * Set resource
172 * @param \PDO $resource
173 * @return Connection
175 public function setResource(\PDO $resource)
177 $this->resource = $resource;
178 $this->driverName = strtolower($this->resource->getAttribute(\PDO::ATTR_DRIVER_NAME));
179 return $this;
183 * Get resource
185 * @return \PDO
187 public function getResource()
189 if (!$this->isConnected()) {
190 $this->connect();
192 return $this->resource;
196 * Connect
198 * @return Connection
199 * @throws Exception\InvalidConnectionParametersException
200 * @throws Exception\RuntimeException
202 public function connect()
204 if ($this->resource) {
205 return $this;
208 $dsn = $username = $password = $hostname = $database = null;
209 $options = array();
210 foreach ($this->connectionParameters as $key => $value) {
211 switch (strtolower($key)) {
212 case 'dsn':
213 $dsn = $value;
214 break;
215 case 'driver':
216 $value = strtolower($value);
217 if (strpos($value, 'pdo') === 0) {
218 $pdoDriver = strtolower(substr(str_replace(array('-', '_', ' '), '', $value), 3));
220 break;
221 case 'pdodriver':
222 $pdoDriver = (string) $value;
223 break;
224 case 'user':
225 case 'username':
226 $username = (string) $value;
227 break;
228 case 'pass':
229 case 'password':
230 $password = (string) $value;
231 break;
232 case 'host':
233 case 'hostname':
234 $hostname = (string) $value;
235 break;
236 case 'port':
237 $port = (int) $value;
238 break;
239 case 'database':
240 case 'dbname':
241 $database = (string) $value;
242 break;
243 case 'driver_options':
244 case 'options':
245 $value = (array) $value;
246 $options = array_diff_key($options, $value) + $value;
247 break;
248 default:
249 $options[$key] = $value;
250 break;
254 if (!isset($dsn) && isset($pdoDriver)) {
255 $dsn = array();
256 switch ($pdoDriver) {
257 case 'sqlite':
258 $dsn[] = $database;
259 break;
260 default:
261 if (isset($database)) {
262 $dsn[] = "dbname={$database}";
264 if (isset($hostname)) {
265 $dsn[] = "host={$hostname}";
267 if (isset($port)) {
268 $dsn[] = "port={$port}";
270 break;
272 $dsn = $pdoDriver . ':' . implode(';', $dsn);
273 } elseif (!isset($dsn)) {
274 throw new Exception\InvalidConnectionParametersException(
275 'A dsn was not provided or could not be constructed from your parameters',
276 $this->connectionParameters
280 try {
281 $this->resource = new \PDO($dsn, $username, $password, $options);
282 $this->resource->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
283 $this->driverName = strtolower($this->resource->getAttribute(\PDO::ATTR_DRIVER_NAME));
284 } catch (\PDOException $e) {
285 $code = $e->getCode();
286 if (!is_long($code)) {
287 $code = null;
289 throw new Exception\RuntimeException('Connect Error: ' . $e->getMessage(), $code, $e);
292 return $this;
296 * Is connected
298 * @return bool
300 public function isConnected()
302 return ($this->resource instanceof \PDO);
306 * Disconnect
308 * @return Connection
310 public function disconnect()
312 if ($this->isConnected()) {
313 $this->resource = null;
315 return $this;
319 * Begin transaction
321 * @return Connection
323 public function beginTransaction()
325 if (!$this->isConnected()) {
326 $this->connect();
328 $this->resource->beginTransaction();
329 $this->inTransaction = true;
330 return $this;
334 * Commit
336 * @return Connection
338 public function commit()
340 if (!$this->isConnected()) {
341 $this->connect();
344 $this->resource->commit();
345 $this->inTransaction = false;
346 return $this;
350 * Rollback
352 * @return Connection
353 * @throws Exception\RuntimeException
355 public function rollback()
357 if (!$this->isConnected()) {
358 throw new Exception\RuntimeException('Must be connected before you can rollback');
361 if (!$this->inTransaction) {
362 throw new Exception\RuntimeException('Must call beginTransaction() before you can rollback');
365 $this->resource->rollBack();
366 return $this;
370 * Execute
372 * @param $sql
373 * @return Result
374 * @throws Exception\InvalidQueryException
376 public function execute($sql)
378 if (!$this->isConnected()) {
379 $this->connect();
382 if ($this->profiler) {
383 $this->profiler->profilerStart($sql);
386 $resultResource = $this->resource->query($sql);
388 if ($this->profiler) {
389 $this->profiler->profilerFinish($sql);
392 if ($resultResource === false) {
393 $errorInfo = $this->resource->errorInfo();
394 throw new Exception\InvalidQueryException($errorInfo[2]);
397 $result = $this->driver->createResult($resultResource, $sql);
398 return $result;
403 * Prepare
405 * @param string $sql
406 * @return Statement
408 public function prepare($sql)
410 if (!$this->isConnected()) {
411 $this->connect();
414 $statement = $this->driver->createStatement($sql);
415 return $statement;
419 * Get last generated id
421 * @param string $name
422 * @return int|null|false
424 public function getLastGeneratedValue($name = null)
426 if ($name === null && $this->driverName == 'pgsql') {
427 return null;
430 try {
431 return $this->resource->lastInsertId($name);
432 } catch (\Exception $e) {
433 // do nothing
435 return false;