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 / Pgsql / Connection.php
blob96906793761a085b1d4070fce7ca252ba03e9a91
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\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
18 /**
19 * @var Pgsql
21 protected $driver = null;
23 /**
24 * @var Profiler\ProfilerInterface
26 protected $profiler = null;
28 /**
29 * Connection parameters
31 * @var array
33 protected $connectionParameters = array();
35 /**
36 * @var resource
38 protected $resource = null;
40 /**
41 * In transaction
43 * @var bool
45 protected $inTransaction = false;
47 /**
48 * Constructor
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);
61 /**
62 * Set connection parameters
64 * @param array $connectionParameters
65 * @return Connection
67 public function setConnectionParameters(array $connectionParameters)
69 $this->connectionParameters = $connectionParameters;
70 return $this;
73 /**
74 * Set driver
76 * @param Pgsql $driver
77 * @return Connection
79 public function setDriver(Pgsql $driver)
81 $this->driver = $driver;
82 return $this;
85 /**
86 * @param Profiler\ProfilerInterface $profiler
87 * @return Connection
89 public function setProfiler(Profiler\ProfilerInterface $profiler)
91 $this->profiler = $profiler;
92 return $this;
95 /**
96 * @return null|Profiler\ProfilerInterface
98 public function getProfiler()
100 return $this->profiler;
104 * Set resource
106 * @param resource $resource
107 * @return Connection
109 public function setResource($resource)
111 $this->resource = $resource;
112 return;
116 * Get current schema
118 * @return null|string
120 public function getCurrentSchema()
122 if (!$this->isConnected()) {
123 $this->connect();
126 $result = pg_query($this->resource, 'SELECT CURRENT_SCHEMA AS "currentschema"');
127 if ($result == false) {
128 return null;
130 return pg_fetch_result($result, 0, 'currentschema');
134 * Get resource
136 * @return resource
138 public function getResource()
140 if (!$this->isConnected()) {
141 $this->connect();
143 return $this->resource;
147 * Connect to the database
149 * @return Connection
150 * @throws Exception\RuntimeException on failure
152 public function connect()
154 if (is_resource($this->resource)) {
155 return $this;
158 // localize
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])) {
165 return $p[$name];
168 return null;
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',
193 __METHOD__
197 return $this;
201 * @return bool
203 public function isConnected()
205 return (is_resource($this->resource));
209 * @return void
211 public function disconnect()
213 pg_close($this->resource);
217 * @return void
219 public function beginTransaction()
221 if ($this->inTransaction) {
222 throw new Exception\RuntimeException('Nested transactions are not supported');
225 if (!$this->isConnected()) {
226 $this->connect();
229 pg_query($this->resource, 'BEGIN');
230 $this->inTransaction = true;
234 * @return void
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;
247 * @return void
249 public function rollback()
251 if (!$this->inTransaction) {
252 return;
255 pg_query($this->resource, 'ROLLBACK');
256 $this->inTransaction = false;
260 * @param string $sql
261 * @throws Exception\InvalidQueryException
262 * @return resource|\Zend\Db\ResultSet\ResultSetInterface
264 public function execute($sql)
266 if (!$this->isConnected()) {
267 $this->connect();
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
293 * @return string
295 public function getLastGeneratedValue($name = null)
297 if ($name == null) {
298 return null;
300 $result = pg_query($this->resource, 'SELECT CURRVAL(\'' . str_replace('\'', '\\\'', $name) . '\') as "currval"');
301 return pg_fetch_result($result, 0, 'currval');