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 / Mysqli / Statement.php
bloba3184f6c2520294da687add2f8e7022d74b3b820
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\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
20 /**
21 * @var \mysqli
23 protected $mysqli = null;
25 /**
26 * @var Mysqli
28 protected $driver = null;
30 /**
31 * @var Profiler\ProfilerInterface
33 protected $profiler = null;
35 /**
36 * @var string
38 protected $sql = '';
40 /**
41 * Parameter container
43 * @var ParameterContainer
45 protected $parameterContainer = null;
47 /**
48 * @var \mysqli_stmt
50 protected $resource = null;
52 /**
53 * Is prepared
55 * @var bool
57 protected $isPrepared = false;
59 /**
60 * @var bool
62 protected $bufferResults = false;
64 /**
65 * @param bool $bufferResults
67 public function __construct($bufferResults = false)
69 $this->bufferResults = (bool) $bufferResults;
72 /**
73 * Set driver
75 * @param Mysqli $driver
76 * @return Statement
78 public function setDriver(Mysqli $driver)
80 $this->driver = $driver;
81 return $this;
84 /**
85 * @param Profiler\ProfilerInterface $profiler
86 * @return Statement
88 public function setProfiler(Profiler\ProfilerInterface $profiler)
90 $this->profiler = $profiler;
91 return $this;
94 /**
95 * @return null|Profiler\ProfilerInterface
97 public function getProfiler()
99 return $this->profiler;
103 * Initialize
105 * @param \mysqli $mysqli
106 * @return Statement
108 public function initialize(\mysqli $mysqli)
110 $this->mysqli = $mysqli;
111 return $this;
115 * Set sql
117 * @param string $sql
118 * @return Statement
120 public function setSql($sql)
122 $this->sql = $sql;
123 return $this;
127 * Set Parameter container
129 * @param ParameterContainer $parameterContainer
130 * @return Statement
132 public function setParameterContainer(ParameterContainer $parameterContainer)
134 $this->parameterContainer = $parameterContainer;
135 return $this;
139 * Get resource
141 * @return mixed
143 public function getResource()
145 return $this->resource;
149 * Set resource
151 * @param \mysqli_stmt $mysqliStatement
152 * @return Statement
154 public function setResource(\mysqli_stmt $mysqliStatement)
156 $this->resource = $mysqliStatement;
157 $this->isPrepared = true;
158 return $this;
162 * Get sql
164 * @return string
166 public function getSql()
168 return $this->sql;
172 * Get parameter count
174 * @return ParameterContainer
176 public function getParameterContainer()
178 return $this->parameterContainer;
182 * Is prepared
184 * @return bool
186 public function isPrepared()
188 return $this->isPrepared;
192 * Prepare
194 * @param string $sql
195 * @throws Exception\InvalidQueryException
196 * @throws Exception\RuntimeException
197 * @return Statement
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,
211 null,
212 new Exception\ErrorException($this->mysqli->error, $this->mysqli->errno)
216 $this->isPrepared = true;
217 return $this;
221 * Execute
223 * @param ParameterContainer|array $parameters
224 * @throws Exception\RuntimeException
225 * @return mixed
227 public function execute($parameters = null)
229 if (!$this->isPrepared) {
230 $this->prepare();
233 /** START Standard ParameterContainer Merging Block */
234 if (!$this->parameterContainer instanceof ParameterContainer) {
235 if ($parameters instanceof ParameterContainer) {
236 $this->parameterContainer = $parameters;
237 $parameters = null;
238 } else {
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;
269 $buffered = true;
270 } else {
271 $buffered = false;
274 $result = $this->driver->createResult($this->resource, $buffered);
275 return $result;
279 * Bind parameters from container
281 * @return void
283 protected function bindParametersFromContainer()
285 $parameters = $this->parameterContainer->getNamedArray();
286 $type = '';
287 $args = array();
289 foreach ($parameters as $name => &$value) {
290 if ($this->parameterContainer->offsetHasErrata($name)) {
291 switch ($this->parameterContainer->offsetGetErrata($name)) {
292 case ParameterContainer::TYPE_DOUBLE:
293 $type .= 'd';
294 break;
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:
298 $type .= 'i';
299 break;
300 case ParameterContainer::TYPE_STRING:
301 default:
302 $type .= 's';
303 break;
305 } else {
306 $type .= 's';
308 $args[] = &$value;
311 if ($args) {
312 array_unshift($args, $type);
313 call_user_func_array(array($this->resource, 'bind_param'), $args);