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\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
23 protected $pdo = null;
26 * @var Profiler\ProfilerInterface
28 protected $profiler = null;
33 protected $driver = null;
45 protected $isQuery = null;
49 * @var ParameterContainer
51 protected $parameterContainer = null;
56 protected $parametersBound = false;
61 protected $resource = null;
67 protected $isPrepared = false;
75 public function setDriver(Pdo
$driver)
77 $this->driver
= $driver;
82 * @param Profiler\ProfilerInterface $profiler
85 public function setProfiler(Profiler\ProfilerInterface
$profiler)
87 $this->profiler
= $profiler;
92 * @return null|Profiler\ProfilerInterface
94 public function getProfiler()
96 return $this->profiler
;
102 * @param \PDO $connectionResource
105 public function initialize(\PDO
$connectionResource)
107 $this->pdo
= $connectionResource;
114 * @param \PDOStatement $pdoStatement
117 public function setResource(\PDOStatement
$pdoStatement)
119 $this->resource = $pdoStatement;
128 public function getResource()
130 return $this->resource;
139 public function setSql($sql)
150 public function getSql()
156 * @param ParameterContainer $parameterContainer
159 public function setParameterContainer(ParameterContainer
$parameterContainer)
161 $this->parameterContainer
= $parameterContainer;
166 * @return ParameterContainer
168 public function getParameterContainer()
170 return $this->parameterContainer
;
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');
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;
200 public function isPrepared()
202 return $this->isPrepared
;
206 * @param mixed $parameters
207 * @throws Exception\InvalidQueryException
210 public function execute($parameters = null)
212 if (!$this->isPrepared
) {
216 /** START Standard ParameterContainer Merging Block */
217 if (!$this->parameterContainer
instanceof ParameterContainer
) {
218 if ($parameters instanceof ParameterContainer
) {
219 $this->parameterContainer
= $parameters;
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);
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);
257 * Bind parameters from container
259 protected function bindParametersFromContainer()
261 if ($this->parametersBound
) {
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
;
273 case ParameterContainer
::TYPE_NULL
:
274 $type = \PDO
::PARAM_NULL
;
276 case ParameterContainer
::TYPE_LOB
:
277 $type = \PDO
::PARAM_LOB
;
279 case (is_bool($value)):
280 $type = \PDO
::PARAM_BOOL
;
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
;