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 / Statement.php
blob9b2eb501632831b9b16ecdb1f531328d73c4acac
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\StatementInterface;
13 use Zend\Db\Adapter\Exception;
14 use Zend\Db\Adapter\ParameterContainer;
15 use Zend\Db\Adapter\Profiler;
17 class Statement implements StatementInterface, Profiler\ProfilerAwareInterface
19 /**
20 * @var int
22 protected static $statementIndex = 0;
24 /**
25 * @var string
27 protected $statementName = '';
29 /**
30 * @var Pgsql
32 protected $driver = null;
34 /**
35 * @var Profiler\ProfilerInterface
37 protected $profiler = null;
39 /**
40 * @var resource
42 protected $pgsql = null;
44 /**
45 * @var resource
47 protected $resource = null;
49 /**
50 * @var string
52 protected $sql;
54 /**
55 * @var ParameterContainer
57 protected $parameterContainer;
59 /**
60 * @param Pgsql $driver
61 * @return Statement
63 public function setDriver(Pgsql $driver)
65 $this->driver = $driver;
66 return $this;
69 /**
70 * @param Profiler\ProfilerInterface $profiler
71 * @return Statement
73 public function setProfiler(Profiler\ProfilerInterface $profiler)
75 $this->profiler = $profiler;
76 return $this;
79 /**
80 * @return null|Profiler\ProfilerInterface
82 public function getProfiler()
84 return $this->profiler;
87 /**
88 * Initialize
90 * @param resource $pgsql
91 * @return void
92 * @throws Exception\RuntimeException for invalid or missing postgresql connection
94 public function initialize($pgsql)
96 if (!is_resource($pgsql) || get_resource_type($pgsql) !== 'pgsql link') {
97 throw new Exception\RuntimeException(sprintf(
98 '%s: Invalid or missing postgresql connection; received "%s"',
99 __METHOD__,
100 get_resource_type($pgsql)
103 $this->pgsql = $pgsql;
107 * Get resource
109 * @return resource
111 public function getResource()
113 // TODO: Implement getResource() method.
117 * Set sql
119 * @param string $sql
120 * @return Statement
122 public function setSql($sql)
124 $this->sql = $sql;
125 return $this;
129 * Get sql
131 * @return string
133 public function getSql()
135 return $this->sql;
139 * Set parameter container
141 * @param ParameterContainer $parameterContainer
142 * @return Statement
144 public function setParameterContainer(ParameterContainer $parameterContainer)
146 $this->parameterContainer = $parameterContainer;
147 return $this;
151 * Get parameter container
153 * @return ParameterContainer
155 public function getParameterContainer()
157 return $this->parameterContainer;
161 * Prepare
163 * @param string $sql
165 public function prepare($sql = null)
167 $sql = ($sql) ?: $this->sql;
169 $pCount = 1;
170 $sql = preg_replace_callback(
171 '#\$\##', function ($foo) use (&$pCount) {
172 return '$' . $pCount++;
174 $sql
177 $this->sql = $sql;
178 $this->statementName = 'statement' . ++static::$statementIndex;
179 $this->resource = pg_prepare($this->pgsql, $this->statementName, $sql);
183 * Is prepared
185 * @return bool
187 public function isPrepared()
189 return isset($this->resource);
193 * Execute
195 * @param ParameterContainer|null $parameters
196 * @throws Exception\InvalidQueryException
197 * @return Result
199 public function execute($parameters = null)
201 if (!$this->isPrepared()) {
202 $this->prepare();
205 /** START Standard ParameterContainer Merging Block */
206 if (!$this->parameterContainer instanceof ParameterContainer) {
207 if ($parameters instanceof ParameterContainer) {
208 $this->parameterContainer = $parameters;
209 $parameters = null;
210 } else {
211 $this->parameterContainer = new ParameterContainer();
215 if (is_array($parameters)) {
216 $this->parameterContainer->setFromArray($parameters);
219 if ($this->parameterContainer->count() > 0) {
220 $parameters = $this->parameterContainer->getPositionalArray();
222 /** END Standard ParameterContainer Merging Block */
224 if ($this->profiler) {
225 $this->profiler->profilerStart($this);
228 $resultResource = pg_execute($this->pgsql, $this->statementName, (array) $parameters);
230 if ($this->profiler) {
231 $this->profiler->profilerFinish();
234 if ($resultResource === false) {
235 throw new Exception\InvalidQueryException(pg_last_error());
238 $result = $this->driver->createResult($resultResource);
239 return $result;