3 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
15 * This software consists of voluntary contributions made by many individuals
16 * and is licensed under the MIT license. For more information, see
17 * <http://www.doctrine-project.org>.
20 namespace Doctrine\DBAL\Driver\Mysqli;
22 use Doctrine\DBAL\Driver\Statement;
26 * @author Kim Hemsø Rasmussen <kimhemsoe@gmail.com>
28 class MysqliStatement implements \IteratorAggregate, Statement
30 protected static $_paramTypeMap = array(
31 PDO::PARAM_STR => 's',
32 PDO::PARAM_BOOL => 'i',
33 PDO::PARAM_NULL => 's',
34 PDO::PARAM_INT => 'i',
35 PDO::PARAM_LOB => 's' // TODO Support LOB bigger then max package size.
42 * @var null|false|array
44 protected $_columnNames;
49 protected $_rowBindedValues;
54 protected $_bindedValues;
57 * Contains ref values for bindValue()
61 protected $_values = array();
63 protected $_defaultFetchMode = PDO::FETCH_BOTH;
65 public function __construct(\mysqli $conn, $prepareString)
68 $this->_stmt = $conn->prepare($prepareString);
69 if (false === $this->_stmt) {
70 throw new MysqliException($this->_conn->error, $this->_conn->errno);
73 $paramCount = $this->_stmt->param_count;
74 if (0 < $paramCount) {
76 // Need to init the string else php think we are trying to access it as a array.
77 $bindedValues = array(0 => str_repeat('s', $paramCount));
79 for ($i = 1; $i < $paramCount; $i++) {
80 $bindedValues[] =& $null;
82 $this->_bindedValues = $bindedValues;
89 public function bindParam($column, &$variable, $type = null, $length = null)
94 if (isset(self::$_paramTypeMap[$type])) {
95 $type = self::$_paramTypeMap[$type];
97 throw new MysqliException("Unkown type: '{$type}'");
101 $this->_bindedValues[$column] =& $variable;
102 $this->_bindedValues[0][$column - 1] = $type;
109 public function bindValue($param, $value, $type = null)
111 if (null === $type) {
114 if (isset(self::$_paramTypeMap[$type])) {
115 $type = self::$_paramTypeMap[$type];
117 throw new MysqliException("Unknown type: '{$type}'");
121 $this->_values[$param] = $value;
122 $this->_bindedValues[$param] =& $this->_values[$param];
123 $this->_bindedValues[0][$param - 1] = $type;
130 public function execute($params = null)
132 if (null !== $this->_bindedValues) {
133 if (null !== $params) {
134 if ( ! $this->_bindValues($params)) {
135 throw new MysqliException($this->_stmt->error, $this->_stmt->errno);
138 if (!call_user_func_array(array($this->_stmt, 'bind_param'), $this->_bindedValues)) {
139 throw new MysqliException($this->_stmt->error, $this->_stmt->errno);
144 if ( ! $this->_stmt->execute()) {
145 throw new MysqliException($this->_stmt->error, $this->_stmt->errno);
148 if (null === $this->_columnNames) {
149 $meta = $this->_stmt->result_metadata();
150 if (false !== $meta) {
151 $columnNames = array();
152 foreach ($meta->fetch_fields() as $col) {
153 $columnNames[] = $col->name;
157 $this->_columnNames = $columnNames;
158 $this->_rowBindedValues = array_fill(0, count($columnNames), NULL);
161 foreach ($this->_rowBindedValues as $key => &$value) {
162 $refs[$key] =& $value;
165 if (!call_user_func_array(array($this->_stmt, 'bind_result'), $refs)) {
166 throw new MysqliException($this->_stmt->error, $this->_stmt->errno);
169 $this->_columnNames = false;
174 if (false !== $this->_columnNames) {
175 $this->_stmt->store_result();
181 * Bind a array of values to bound parameters
183 * @param array $values
186 private function _bindValues($values)
189 $types = str_repeat('s', count($values));
192 foreach ($values as &$v) {
195 return call_user_func_array(array($this->_stmt, 'bind_param'), $params);
199 * @return boolean|array
201 private function _fetch()
203 $ret = $this->_stmt->fetch();
207 foreach ($this->_rowBindedValues as $v) {
208 // Mysqli converts them to a scalar type it can fit in.
209 $values[] = null === $v ? null : (string)$v;
219 public function fetch($fetchMode = null)
221 $values = $this->_fetch();
222 if (null === $values) {
226 if (false === $values) {
227 throw new MysqliException($this->_stmt->error, $this->_stmt->errno);
230 $fetchMode = $fetchMode ?: $this->_defaultFetchMode;
232 switch ($fetchMode) {
236 case PDO::FETCH_ASSOC:
237 return array_combine($this->_columnNames, $values);
239 case PDO::FETCH_BOTH:
240 $ret = array_combine($this->_columnNames, $values);
245 throw new MysqliException("Unknown fetch type '{$fetchMode}'");
252 public function fetchAll($fetchMode = null)
254 $fetchMode = $fetchMode ?: $this->_defaultFetchMode;
257 if (PDO::FETCH_COLUMN == $fetchMode) {
258 while (($row = $this->fetchColumn()) !== false) {
262 while (($row = $this->fetch($fetchMode)) !== null) {
273 public function fetchColumn($columnIndex = 0)
275 $row = $this->fetch(PDO::FETCH_NUM);
279 return $row[$columnIndex];
285 public function errorCode()
287 return $this->_stmt->errno;
293 public function errorInfo()
295 return $this->_stmt->error;
301 public function closeCursor()
303 $this->_stmt->free_result();
310 public function rowCount()
312 if (false === $this->_columnNames) {
313 return $this->_stmt->affected_rows;
315 return $this->_stmt->num_rows;
321 public function columnCount()
323 return $this->_stmt->field_count;
329 public function setFetchMode($fetchMode, $arg2 = null, $arg3 = null)
331 $this->_defaultFetchMode = $fetchMode;
337 public function getIterator()
339 $data = $this->fetchAll();
340 return new \ArrayIterator($data);