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\Mysqli
;
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 $mysqli = null;
28 protected $driver = null;
31 * @var Profiler\ProfilerInterface
33 protected $profiler = null;
43 * @var ParameterContainer
45 protected $parameterContainer = null;
50 protected $resource = null;
57 protected $isPrepared = false;
62 protected $bufferResults = false;
65 * @param bool $bufferResults
67 public function __construct($bufferResults = false)
69 $this->bufferResults
= (bool) $bufferResults;
75 * @param Mysqli $driver
78 public function setDriver(Mysqli
$driver)
80 $this->driver
= $driver;
85 * @param Profiler\ProfilerInterface $profiler
88 public function setProfiler(Profiler\ProfilerInterface
$profiler)
90 $this->profiler
= $profiler;
95 * @return null|Profiler\ProfilerInterface
97 public function getProfiler()
99 return $this->profiler
;
105 * @param \mysqli $mysqli
108 public function initialize(\mysqli
$mysqli)
110 $this->mysqli
= $mysqli;
120 public function setSql($sql)
127 * Set Parameter container
129 * @param ParameterContainer $parameterContainer
132 public function setParameterContainer(ParameterContainer
$parameterContainer)
134 $this->parameterContainer
= $parameterContainer;
143 public function getResource()
145 return $this->resource;
151 * @param \mysqli_stmt $mysqliStatement
154 public function setResource(\mysqli_stmt
$mysqliStatement)
156 $this->resource = $mysqliStatement;
157 $this->isPrepared
= true;
166 public function getSql()
172 * Get parameter count
174 * @return ParameterContainer
176 public function getParameterContainer()
178 return $this->parameterContainer
;
186 public function isPrepared()
188 return $this->isPrepared
;
195 * @throws Exception\InvalidQueryException
196 * @throws Exception\RuntimeException
199 public function prepare($sql = null)
201 if ($this->isPrepared
) {
202 throw new Exception\
RuntimeException('This statement has already been prepared');
205 $sql = ($sql) ?
: $this->sql
;
207 $this->resource = $this->mysqli
->prepare($sql);
208 if (!$this->resource instanceof \mysqli_stmt
) {
209 throw new Exception\
InvalidQueryException(
210 'Statement couldn\'t be produced with sql: ' . $sql,
212 new Exception\
ErrorException($this->mysqli
->error
, $this->mysqli
->errno
)
216 $this->isPrepared
= true;
223 * @param ParameterContainer|array $parameters
224 * @throws Exception\RuntimeException
227 public function execute($parameters = null)
229 if (!$this->isPrepared
) {
233 /** START Standard ParameterContainer Merging Block */
234 if (!$this->parameterContainer
instanceof ParameterContainer
) {
235 if ($parameters instanceof ParameterContainer
) {
236 $this->parameterContainer
= $parameters;
239 $this->parameterContainer
= new ParameterContainer();
243 if (is_array($parameters)) {
244 $this->parameterContainer
->setFromArray($parameters);
247 if ($this->parameterContainer
->count() > 0) {
248 $this->bindParametersFromContainer();
250 /** END Standard ParameterContainer Merging Block */
252 if ($this->profiler
) {
253 $this->profiler
->profilerStart($this);
256 $return = $this->resource->execute();
258 if ($this->profiler
) {
259 $this->profiler
->profilerFinish();
262 if ($return === false) {
263 throw new Exception\
RuntimeException($this->resource->error
);
266 if ($this->bufferResults
=== true) {
267 $this->resource->store_result();
268 $this->isPrepared
= false;
274 $result = $this->driver
->createResult($this->resource, $buffered);
279 * Bind parameters from container
283 protected function bindParametersFromContainer()
285 $parameters = $this->parameterContainer
->getNamedArray();
289 foreach ($parameters as $name => &$value) {
290 if ($this->parameterContainer
->offsetHasErrata($name)) {
291 switch ($this->parameterContainer
->offsetGetErrata($name)) {
292 case ParameterContainer
::TYPE_DOUBLE
:
295 case ParameterContainer
::TYPE_NULL
:
296 $value = null; // as per @see http://www.php.net/manual/en/mysqli-stmt.bind-param.php#96148
297 case ParameterContainer
::TYPE_INTEGER
:
300 case ParameterContainer
::TYPE_STRING
:
312 array_unshift($args, $type);
313 call_user_func_array(array($this->resource, 'bind_param'), $args);