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 / Pdo / Statement.php
blobae22ed54b71861909541cf5011f88134e502620c
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\Pdo;
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
20 /**
21 * @var \PDO
23 protected $pdo = null;
25 /**
26 * @var Profiler\ProfilerInterface
28 protected $profiler = null;
30 /**
31 * @var Pdo
33 protected $driver = null;
35 /**
37 * @var string
39 protected $sql = '';
41 /**
43 * @var bool
45 protected $isQuery = null;
47 /**
49 * @var ParameterContainer
51 protected $parameterContainer = null;
53 /**
54 * @var bool
56 protected $parametersBound = false;
58 /**
59 * @var \PDOStatement
61 protected $resource = null;
63 /**
65 * @var bool
67 protected $isPrepared = false;
69 /**
70 * Set driver
72 * @param Pdo $driver
73 * @return Statement
75 public function setDriver(Pdo $driver)
77 $this->driver = $driver;
78 return $this;
81 /**
82 * @param Profiler\ProfilerInterface $profiler
83 * @return Statement
85 public function setProfiler(Profiler\ProfilerInterface $profiler)
87 $this->profiler = $profiler;
88 return $this;
91 /**
92 * @return null|Profiler\ProfilerInterface
94 public function getProfiler()
96 return $this->profiler;
99 /**
100 * Initialize
102 * @param \PDO $connectionResource
103 * @return Statement
105 public function initialize(\PDO $connectionResource)
107 $this->pdo = $connectionResource;
108 return $this;
112 * Set resource
114 * @param \PDOStatement $pdoStatement
115 * @return Statement
117 public function setResource(\PDOStatement $pdoStatement)
119 $this->resource = $pdoStatement;
120 return $this;
124 * Get resource
126 * @return mixed
128 public function getResource()
130 return $this->resource;
134 * Set sql
136 * @param string $sql
137 * @return Statement
139 public function setSql($sql)
141 $this->sql = $sql;
142 return $this;
146 * Get sql
148 * @return string
150 public function getSql()
152 return $this->sql;
156 * @param ParameterContainer $parameterContainer
157 * @return Statement
159 public function setParameterContainer(ParameterContainer $parameterContainer)
161 $this->parameterContainer = $parameterContainer;
162 return $this;
166 * @return ParameterContainer
168 public function getParameterContainer()
170 return $this->parameterContainer;
174 * @param string $sql
175 * @throws Exception\RuntimeException
177 public function prepare($sql = null)
179 if ($this->isPrepared) {
180 throw new Exception\RuntimeException('This statement has been prepared already');
183 if ($sql == null) {
184 $sql = $this->sql;
187 $this->resource = $this->pdo->prepare($sql);
189 if ($this->resource === false) {
190 $error = $this->pdo->errorInfo();
191 throw new Exception\RuntimeException($error[2]);
194 $this->isPrepared = true;
198 * @return bool
200 public function isPrepared()
202 return $this->isPrepared;
206 * @param mixed $parameters
207 * @throws Exception\InvalidQueryException
208 * @return Result
210 public function execute($parameters = null)
212 if (!$this->isPrepared) {
213 $this->prepare();
216 /** START Standard ParameterContainer Merging Block */
217 if (!$this->parameterContainer instanceof ParameterContainer) {
218 if ($parameters instanceof ParameterContainer) {
219 $this->parameterContainer = $parameters;
220 $parameters = null;
221 } else {
222 $this->parameterContainer = new ParameterContainer();
226 if (is_array($parameters)) {
227 $this->parameterContainer->setFromArray($parameters);
230 if ($this->parameterContainer->count() > 0) {
231 $this->bindParametersFromContainer();
233 /** END Standard ParameterContainer Merging Block */
235 if ($this->profiler) {
236 $this->profiler->profilerStart($this);
239 try {
240 $this->resource->execute();
241 } catch (\PDOException $e) {
242 if ($this->profiler) {
243 $this->profiler->profilerFinish();
245 throw new Exception\InvalidQueryException('Statement could not be executed', null, $e);
248 if ($this->profiler) {
249 $this->profiler->profilerFinish();
252 $result = $this->driver->createResult($this->resource, $this);
253 return $result;
257 * Bind parameters from container
259 protected function bindParametersFromContainer()
261 if ($this->parametersBound) {
262 return;
265 $parameters = $this->parameterContainer->getNamedArray();
266 foreach ($parameters as $name => &$value) {
267 $type = \PDO::PARAM_STR;
268 if ($this->parameterContainer->offsetHasErrata($name)) {
269 switch ($this->parameterContainer->offsetGetErrata($name)) {
270 case ParameterContainer::TYPE_INTEGER:
271 $type = \PDO::PARAM_INT;
272 break;
273 case ParameterContainer::TYPE_NULL:
274 $type = \PDO::PARAM_NULL;
275 break;
276 case ParameterContainer::TYPE_LOB:
277 $type = \PDO::PARAM_LOB;
278 break;
279 case (is_bool($value)):
280 $type = \PDO::PARAM_BOOL;
281 break;
285 // parameter is named or positional, value is reference
286 $parameter = is_int($name) ? ($name + 1) : $name;
287 $this->resource->bindParam($parameter, $value, $type);
293 * Perform a deep clone
294 * @return Statement A cloned statement
296 public function __clone()
298 $this->isPrepared = false;
299 $this->parametersBound = false;
300 $this->resource = null;
301 if ($this->parameterContainer) {
302 $this->parameterContainer = clone $this->parameterContainer;