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\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
22 protected static $statementIndex = 0;
27 protected $statementName = '';
32 protected $driver = null;
35 * @var Profiler\ProfilerInterface
37 protected $profiler = null;
42 protected $pgsql = null;
47 protected $resource = null;
55 * @var ParameterContainer
57 protected $parameterContainer;
60 * @param Pgsql $driver
63 public function setDriver(Pgsql
$driver)
65 $this->driver
= $driver;
70 * @param Profiler\ProfilerInterface $profiler
73 public function setProfiler(Profiler\ProfilerInterface
$profiler)
75 $this->profiler
= $profiler;
80 * @return null|Profiler\ProfilerInterface
82 public function getProfiler()
84 return $this->profiler
;
90 * @param resource $pgsql
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"',
100 get_resource_type($pgsql)
103 $this->pgsql
= $pgsql;
111 public function getResource()
113 // TODO: Implement getResource() method.
122 public function setSql($sql)
133 public function getSql()
139 * Set parameter container
141 * @param ParameterContainer $parameterContainer
144 public function setParameterContainer(ParameterContainer
$parameterContainer)
146 $this->parameterContainer
= $parameterContainer;
151 * Get parameter container
153 * @return ParameterContainer
155 public function getParameterContainer()
157 return $this->parameterContainer
;
165 public function prepare($sql = null)
167 $sql = ($sql) ?
: $this->sql
;
170 $sql = preg_replace_callback(
171 '#\$\##', function ($foo) use (&$pCount) {
172 return '$' . $pCount++
;
178 $this->statementName
= 'statement' . ++
static::$statementIndex;
179 $this->resource = pg_prepare($this->pgsql
, $this->statementName
, $sql);
187 public function isPrepared()
189 return isset($this->resource);
195 * @param ParameterContainer|null $parameters
196 * @throws Exception\InvalidQueryException
199 public function execute($parameters = null)
201 if (!$this->isPrepared()) {
205 /** START Standard ParameterContainer Merging Block */
206 if (!$this->parameterContainer
instanceof ParameterContainer
) {
207 if ($parameters instanceof ParameterContainer
) {
208 $this->parameterContainer
= $parameters;
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);