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\Pgsql
;
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;
29 * Connection parameters
33 protected $connectionParameters = array();
38 protected $resource = null;
45 protected $inTransaction = false;
50 * @param resource|array|null $connectionInfo
52 public function __construct($connectionInfo = null)
54 if (is_array($connectionInfo)) {
55 $this->setConnectionParameters($connectionInfo);
56 } elseif (is_resource($connectionInfo)) {
57 $this->setResource($connectionInfo);
62 * Set connection parameters
64 * @param array $connectionParameters
67 public function setConnectionParameters(array $connectionParameters)
69 $this->connectionParameters
= $connectionParameters;
76 * @param Pgsql $driver
79 public function setDriver(Pgsql
$driver)
81 $this->driver
= $driver;
86 * @param Profiler\ProfilerInterface $profiler
89 public function setProfiler(Profiler\ProfilerInterface
$profiler)
91 $this->profiler
= $profiler;
96 * @return null|Profiler\ProfilerInterface
98 public function getProfiler()
100 return $this->profiler
;
106 * @param resource $resource
109 public function setResource($resource)
111 $this->resource = $resource;
118 * @return null|string
120 public function getCurrentSchema()
122 if (!$this->isConnected()) {
126 $result = pg_query($this->resource, 'SELECT CURRENT_SCHEMA AS "currentschema"');
127 if ($result == false) {
130 return pg_fetch_result($result, 0, 'currentschema');
138 public function getResource()
140 if (!$this->isConnected()) {
143 return $this->resource;
147 * Connect to the database
150 * @throws Exception\RuntimeException on failure
152 public function connect()
154 if (is_resource($this->resource)) {
159 $p = $this->connectionParameters
;
161 // given a list of key names, test for existence in $p
162 $findParameterValue = function (array $names) use ($p) {
163 foreach ($names as $name) {
164 if (isset($p[$name])) {
171 $connection = array();
172 $connection['host'] = $findParameterValue(array('hostname', 'host'));
173 $connection['user'] = $findParameterValue(array('username', 'user'));
174 $connection['password'] = $findParameterValue(array('password', 'passwd', 'pw'));
175 $connection['dbname'] = $findParameterValue(array('database', 'dbname', 'db', 'schema'));
176 $connection['port'] = (isset($p['port'])) ?
(int) $p['port'] : null;
177 $connection['socket'] = (isset($p['socket'])) ?
$p['socket'] : null;
179 $connection = array_filter($connection); // remove nulls
180 $connection = http_build_query($connection, null, ' '); // @link http://php.net/pg_connect
182 set_error_handler(function ($number, $string) {
183 throw new Exception\
RuntimeException(
184 __METHOD__
. ': Unable to connect to database', null, new Exception\
ErrorException($string, $number)
187 $this->resource = pg_connect($connection);
188 restore_error_handler();
190 if ($this->resource === false) {
191 throw new Exception\
RuntimeException(sprintf(
192 '%s: Unable to connect to database',
203 public function isConnected()
205 return (is_resource($this->resource));
211 public function disconnect()
213 pg_close($this->resource);
219 public function beginTransaction()
221 if ($this->inTransaction
) {
222 throw new Exception\
RuntimeException('Nested transactions are not supported');
225 if (!$this->isConnected()) {
229 pg_query($this->resource, 'BEGIN');
230 $this->inTransaction
= true;
236 public function commit()
238 if (!$this->inTransaction
) {
239 return; // We ignore attempts to commit non-existing transaction
242 pg_query($this->resource, 'COMMIT');
243 $this->inTransaction
= false;
249 public function rollback()
251 if (!$this->inTransaction
) {
255 pg_query($this->resource, 'ROLLBACK');
256 $this->inTransaction
= false;
261 * @throws Exception\InvalidQueryException
262 * @return resource|\Zend\Db\ResultSet\ResultSetInterface
264 public function execute($sql)
266 if (!$this->isConnected()) {
270 if ($this->profiler
) {
271 $this->profiler
->profilerStart($sql);
274 $resultResource = pg_query($this->resource, $sql);
276 if ($this->profiler
) {
277 $this->profiler
->profilerFinish($sql);
280 //var_dump(pg_result_status($resultResource));
282 // if the returnValue is something other than a pg result resource, bypass wrapping it
283 if ($resultResource === false) {
284 throw new Exception\
InvalidQueryException(pg_errormessage());
287 $resultPrototype = $this->driver
->createResult(($resultResource === true) ?
$this->resource : $resultResource);
288 return $resultPrototype;
292 * @param null $name Ignored
295 public function getLastGeneratedValue($name = null)
300 $result = pg_query($this->resource, 'SELECT CURRVAL(\'' . str_replace('\'', '\\\'', $name) . '\') as "currval"');
301 return pg_fetch_result($result, 0, 'currval');