Db2
2 years ago
Mysqli
2 years ago
Oracle
2 years ago
Pdo
2 years ago
Sqlsrv
2 years ago
Db2.php
1 year ago
Exception.php
2 years ago
Interface.php
2 years ago
Mysqli.php
1 year ago
Oracle.php
1 year ago
Pdo.php
1 year ago
Sqlsrv.php
1 year ago
Mysqli.php
323 lines
| 1 | <?php |
| 2 | |
| 3 | namespace { |
| 4 | /** |
| 5 | * Zend Framework |
| 6 | * |
| 7 | * LICENSE |
| 8 | * |
| 9 | * This source file is subject to the new BSD license that is bundled |
| 10 | * with this package in the file LICENSE.txt. |
| 11 | * It is also available through the world-wide-web at this URL: |
| 12 | * http://framework.zend.com/license/new-bsd |
| 13 | * If you did not receive a copy of the license and are unable to |
| 14 | * obtain it through the world-wide-web, please send an email |
| 15 | * to license@zend.com so we can send you a copy immediately. |
| 16 | * |
| 17 | * @category Zend |
| 18 | * @package Zend_Db |
| 19 | * @subpackage Statement |
| 20 | * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com) |
| 21 | * @license http://framework.zend.com/license/new-bsd New BSD License |
| 22 | * @version $Id: Mysqli.php 23775 2011-03-01 17:25:24Z ralph $ |
| 23 | */ |
| 24 | /** |
| 25 | * @see Zend_Db_Statement |
| 26 | */ |
| 27 | // require_once 'Zend/Db/Statement.php'; |
| 28 | /** |
| 29 | * Extends for Mysqli |
| 30 | * |
| 31 | * @category Zend |
| 32 | * @package Zend_Db |
| 33 | * @subpackage Statement |
| 34 | * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com) |
| 35 | * @license http://framework.zend.com/license/new-bsd New BSD License |
| 36 | */ |
| 37 | class Zend_Db_Statement_Mysqli extends \Zend_Db_Statement |
| 38 | { |
| 39 | /** |
| 40 | * Column names. |
| 41 | * |
| 42 | * @var array |
| 43 | */ |
| 44 | protected $_keys; |
| 45 | /** |
| 46 | * Fetched result values. |
| 47 | * |
| 48 | * @var array |
| 49 | */ |
| 50 | protected $_values; |
| 51 | /** |
| 52 | * @var array |
| 53 | */ |
| 54 | protected $_meta = null; |
| 55 | /** |
| 56 | * @param string $sql |
| 57 | * @return void |
| 58 | * @throws Zend_Db_Statement_Mysqli_Exception |
| 59 | */ |
| 60 | public function _prepare($sql) |
| 61 | { |
| 62 | $mysqli = $this->_adapter->getConnection(); |
| 63 | $this->_stmt = $mysqli->prepare($sql); |
| 64 | if ($this->_stmt === \false || $mysqli->errno) { |
| 65 | /** |
| 66 | * @see Zend_Db_Statement_Mysqli_Exception |
| 67 | */ |
| 68 | // require_once 'Zend/Db/Statement/Mysqli/Exception.php'; |
| 69 | throw new \Zend_Db_Statement_Mysqli_Exception("Mysqli prepare error: " . $mysqli->error, $mysqli->errno); |
| 70 | } |
| 71 | } |
| 72 | /** |
| 73 | * Binds a parameter to the specified variable name. |
| 74 | * |
| 75 | * @param mixed $parameter Name the parameter, either integer or string. |
| 76 | * @param mixed $variable Reference to PHP variable containing the value. |
| 77 | * @param mixed $type OPTIONAL Datatype of SQL parameter. |
| 78 | * @param mixed $length OPTIONAL Length of SQL parameter. |
| 79 | * @param mixed $options OPTIONAL Other options. |
| 80 | * @return bool |
| 81 | * @throws Zend_Db_Statement_Mysqli_Exception |
| 82 | */ |
| 83 | protected function _bindParam($parameter, &$variable, $type = null, $length = null, $options = null) |
| 84 | { |
| 85 | return \true; |
| 86 | } |
| 87 | /** |
| 88 | * Closes the cursor and the statement. |
| 89 | * |
| 90 | * @return bool |
| 91 | */ |
| 92 | public function close() |
| 93 | { |
| 94 | if ($this->_stmt) { |
| 95 | $r = $this->_stmt->close(); |
| 96 | $this->_stmt = null; |
| 97 | return $r; |
| 98 | } |
| 99 | return \false; |
| 100 | } |
| 101 | /** |
| 102 | * Closes the cursor, allowing the statement to be executed again. |
| 103 | * |
| 104 | * @return bool |
| 105 | */ |
| 106 | public function closeCursor() |
| 107 | { |
| 108 | if ($stmt = $this->_stmt) { |
| 109 | $mysqli = $this->_adapter->getConnection(); |
| 110 | while ($mysqli->more_results()) { |
| 111 | $mysqli->next_result(); |
| 112 | } |
| 113 | $this->_stmt->free_result(); |
| 114 | return $this->_stmt->reset(); |
| 115 | } |
| 116 | return \false; |
| 117 | } |
| 118 | /** |
| 119 | * Returns the number of columns in the result set. |
| 120 | * Returns null if the statement has no result set metadata. |
| 121 | * |
| 122 | * @return int The number of columns. |
| 123 | */ |
| 124 | public function columnCount() |
| 125 | { |
| 126 | if (isset($this->_meta) && $this->_meta) { |
| 127 | return $this->_meta->field_count; |
| 128 | } |
| 129 | return 0; |
| 130 | } |
| 131 | /** |
| 132 | * Retrieves the error code, if any, associated with the last operation on |
| 133 | * the statement handle. |
| 134 | * |
| 135 | * @return string error code. |
| 136 | */ |
| 137 | public function errorCode() |
| 138 | { |
| 139 | if (!$this->_stmt) { |
| 140 | return \false; |
| 141 | } |
| 142 | return \substr($this->_stmt->sqlstate, 0, 5); |
| 143 | } |
| 144 | /** |
| 145 | * Retrieves an array of error information, if any, associated with the |
| 146 | * last operation on the statement handle. |
| 147 | * |
| 148 | * @return array |
| 149 | */ |
| 150 | public function errorInfo() |
| 151 | { |
| 152 | if (!$this->_stmt) { |
| 153 | return \false; |
| 154 | } |
| 155 | return array(\substr($this->_stmt->sqlstate, 0, 5), $this->_stmt->errno, $this->_stmt->error); |
| 156 | } |
| 157 | /** |
| 158 | * Executes a prepared statement. |
| 159 | * |
| 160 | * @param array $params OPTIONAL Values to bind to parameter placeholders. |
| 161 | * @return bool |
| 162 | * @throws Zend_Db_Statement_Mysqli_Exception |
| 163 | */ |
| 164 | public function _execute(?array $params = null) |
| 165 | { |
| 166 | if (!$this->_stmt) { |
| 167 | return \false; |
| 168 | } |
| 169 | // if no params were given as an argument to execute(), |
| 170 | // then default to the _bindParam array |
| 171 | if ($params === null) { |
| 172 | $params = $this->_bindParam; |
| 173 | } |
| 174 | // send $params as input parameters to the statement |
| 175 | if ($params) { |
| 176 | \array_unshift($params, \str_repeat('s', \count($params))); |
| 177 | $stmtParams = array(); |
| 178 | foreach ($params as $k => &$value) { |
| 179 | $stmtParams[$k] =& $value; |
| 180 | } |
| 181 | \call_user_func_array(array($this->_stmt, 'bind_param'), $stmtParams); |
| 182 | } |
| 183 | // execute the statement |
| 184 | $retval = $this->_stmt->execute(); |
| 185 | if ($retval === \false) { |
| 186 | /** |
| 187 | * @see Zend_Db_Statement_Mysqli_Exception |
| 188 | */ |
| 189 | // require_once 'Zend/Db/Statement/Mysqli/Exception.php'; |
| 190 | throw new \Zend_Db_Statement_Mysqli_Exception("Mysqli statement execute error : " . $this->_stmt->error, $this->_stmt->errno); |
| 191 | } |
| 192 | // retain metadata |
| 193 | if ($this->_meta === null) { |
| 194 | $this->_meta = $this->_stmt->result_metadata(); |
| 195 | if ($this->_stmt->errno) { |
| 196 | /** |
| 197 | * @see Zend_Db_Statement_Mysqli_Exception |
| 198 | */ |
| 199 | // require_once 'Zend/Db/Statement/Mysqli/Exception.php'; |
| 200 | throw new \Zend_Db_Statement_Mysqli_Exception("Mysqli statement metadata error: " . $this->_stmt->error, $this->_stmt->errno); |
| 201 | } |
| 202 | } |
| 203 | // statements that have no result set do not return metadata |
| 204 | if ($this->_meta !== \false) { |
| 205 | // get the column names that will result |
| 206 | $this->_keys = array(); |
| 207 | foreach ($this->_meta->fetch_fields() as $col) { |
| 208 | $this->_keys[] = $this->_adapter->foldCase($col->name); |
| 209 | } |
| 210 | // set up a binding space for result variables |
| 211 | $this->_values = \array_fill(0, \count($this->_keys), null); |
| 212 | // set up references to the result binding space. |
| 213 | // just passing $this->_values in the call_user_func_array() |
| 214 | // below won't work, you need references. |
| 215 | $refs = array(); |
| 216 | foreach ($this->_values as $i => &$f) { |
| 217 | $refs[$i] =& $f; |
| 218 | } |
| 219 | $this->_stmt->store_result(); |
| 220 | // bind to the result variables |
| 221 | \call_user_func_array(array($this->_stmt, 'bind_result'), $this->_values); |
| 222 | } |
| 223 | return $retval; |
| 224 | } |
| 225 | /** |
| 226 | * Fetches a row from the result set. |
| 227 | * |
| 228 | * @param int $style OPTIONAL Fetch mode for this fetch operation. |
| 229 | * @param int $cursor OPTIONAL Absolute, relative, or other. |
| 230 | * @param int $offset OPTIONAL Number for absolute or relative cursors. |
| 231 | * @return mixed Array, object, or scalar depending on fetch mode. |
| 232 | * @throws Zend_Db_Statement_Mysqli_Exception |
| 233 | */ |
| 234 | public function fetch($style = null, $cursor = null, $offset = null) |
| 235 | { |
| 236 | if (!$this->_stmt) { |
| 237 | return \false; |
| 238 | } |
| 239 | // fetch the next result |
| 240 | $retval = $this->_stmt->fetch(); |
| 241 | switch ($retval) { |
| 242 | case null: |
| 243 | // end of data |
| 244 | case \false: |
| 245 | // error occurred |
| 246 | $this->_stmt->reset(); |
| 247 | return \false; |
| 248 | default: |
| 249 | } |
| 250 | // make sure we have a fetch mode |
| 251 | if ($style === null) { |
| 252 | $style = $this->_fetchMode; |
| 253 | } |
| 254 | // dereference the result values, otherwise things like fetchAll() |
| 255 | // return the same values for every entry (because of the reference). |
| 256 | $values = array(); |
| 257 | foreach ($this->_values as $key => $val) { |
| 258 | $values[] = $val; |
| 259 | } |
| 260 | $row = \false; |
| 261 | switch ($style) { |
| 262 | case \Zend_Db::FETCH_NUM: |
| 263 | $row = $values; |
| 264 | break; |
| 265 | case \Zend_Db::FETCH_ASSOC: |
| 266 | $row = \array_combine($this->_keys, $values); |
| 267 | break; |
| 268 | case \Zend_Db::FETCH_BOTH: |
| 269 | $assoc = \array_combine($this->_keys, $values); |
| 270 | $row = \array_merge($values, $assoc); |
| 271 | break; |
| 272 | case \Zend_Db::FETCH_OBJ: |
| 273 | $row = (object) \array_combine($this->_keys, $values); |
| 274 | break; |
| 275 | case \Zend_Db::FETCH_BOUND: |
| 276 | $assoc = \array_combine($this->_keys, $values); |
| 277 | $row = \array_merge($values, $assoc); |
| 278 | return $this->_fetchBound($row); |
| 279 | break; |
| 280 | default: |
| 281 | /** |
| 282 | * @see Zend_Db_Statement_Mysqli_Exception |
| 283 | */ |
| 284 | // require_once 'Zend/Db/Statement/Mysqli/Exception.php'; |
| 285 | throw new \Zend_Db_Statement_Mysqli_Exception("Invalid fetch mode '{$style}' specified"); |
| 286 | break; |
| 287 | } |
| 288 | return $row; |
| 289 | } |
| 290 | /** |
| 291 | * Retrieves the next rowset (result set) for a SQL statement that has |
| 292 | * multiple result sets. An example is a stored procedure that returns |
| 293 | * the results of multiple queries. |
| 294 | * |
| 295 | * @return bool |
| 296 | * @throws Zend_Db_Statement_Mysqli_Exception |
| 297 | */ |
| 298 | public function nextRowset() |
| 299 | { |
| 300 | /** |
| 301 | * @see Zend_Db_Statement_Mysqli_Exception |
| 302 | */ |
| 303 | // require_once 'Zend/Db/Statement/Mysqli/Exception.php'; |
| 304 | throw new \Zend_Db_Statement_Mysqli_Exception(__FUNCTION__ . '() is not implemented'); |
| 305 | } |
| 306 | /** |
| 307 | * Returns the number of rows affected by the execution of the |
| 308 | * last INSERT, DELETE, or UPDATE statement executed by this |
| 309 | * statement object. |
| 310 | * |
| 311 | * @return int The number of rows affected. |
| 312 | */ |
| 313 | public function rowCount() |
| 314 | { |
| 315 | if (!$this->_adapter) { |
| 316 | return \false; |
| 317 | } |
| 318 | $mysqli = $this->_adapter->getConnection(); |
| 319 | return $mysqli->affected_rows; |
| 320 | } |
| 321 | } |
| 322 | } |
| 323 |