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\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
21 protected $driver = null;
24 * @var Profiler\ProfilerInterface
26 protected $profiler = null;
31 protected $driverName = null;
36 protected $connectionParameters = array();
41 protected $resource = null;
46 protected $inTransaction = false;
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');
71 public function setDriver(Pdo
$driver)
73 $this->driver
= $driver;
78 * @param Profiler\ProfilerInterface $profiler
81 public function setProfiler(Profiler\ProfilerInterface
$profiler)
83 $this->profiler
= $profiler;
88 * @return null|Profiler\ProfilerInterface
90 public function getProfiler()
92 return $this->profiler
;
100 public function getDriverName()
102 return $this->driverName
;
106 * Set connection parameters
108 * @param array $connectionParameters
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
133 public function getConnectionParameters()
135 return $this->connectionParameters
;
143 public function getCurrentSchema()
145 if (!$this->isConnected()) {
149 switch ($this->driverName
) {
151 $sql = 'SELECT DATABASE()';
157 $sql = 'SELECT CURRENT_SCHEMA';
161 /** @var $result \PDOStatement */
162 $result = $this->resource->query($sql);
163 if ($result instanceof \PDOStatement
) {
164 return $result->fetchColumn();
172 * @param \PDO $resource
175 public function setResource(\PDO
$resource)
177 $this->resource = $resource;
178 $this->driverName
= strtolower($this->resource->getAttribute(\PDO
::ATTR_DRIVER_NAME
));
187 public function getResource()
189 if (!$this->isConnected()) {
192 return $this->resource;
199 * @throws Exception\InvalidConnectionParametersException
200 * @throws Exception\RuntimeException
202 public function connect()
204 if ($this->resource) {
208 $dsn = $username = $password = $hostname = $database = null;
210 foreach ($this->connectionParameters
as $key => $value) {
211 switch (strtolower($key)) {
216 $value = strtolower($value);
217 if (strpos($value, 'pdo') === 0) {
218 $pdoDriver = strtolower(substr(str_replace(array('-', '_', ' '), '', $value), 3));
222 $pdoDriver = (string) $value;
226 $username = (string) $value;
230 $password = (string) $value;
234 $hostname = (string) $value;
237 $port = (int) $value;
241 $database = (string) $value;
243 case 'driver_options':
245 $value = (array) $value;
246 $options = array_diff_key($options, $value) +
$value;
249 $options[$key] = $value;
254 if (!isset($dsn) && isset($pdoDriver)) {
256 switch ($pdoDriver) {
261 if (isset($database)) {
262 $dsn[] = "dbname={$database}";
264 if (isset($hostname)) {
265 $dsn[] = "host={$hostname}";
268 $dsn[] = "port={$port}";
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
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)) {
289 throw new Exception\
RuntimeException('Connect Error: ' . $e->getMessage(), $code, $e);
300 public function isConnected()
302 return ($this->resource instanceof \PDO
);
310 public function disconnect()
312 if ($this->isConnected()) {
313 $this->resource = null;
323 public function beginTransaction()
325 if (!$this->isConnected()) {
328 $this->resource->beginTransaction();
329 $this->inTransaction
= true;
338 public function commit()
340 if (!$this->isConnected()) {
344 $this->resource->commit();
345 $this->inTransaction
= false;
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();
374 * @throws Exception\InvalidQueryException
376 public function execute($sql)
378 if (!$this->isConnected()) {
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);
408 public function prepare($sql)
410 if (!$this->isConnected()) {
414 $statement = $this->driver
->createStatement($sql);
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') {
431 return $this->resource->lastInsertId($name);
432 } catch (\Exception
$e) {