00001 <?php
00027 require_once 'Zend/Db/Statement.php';
00028
00029
00039 class Zend_Db_Statement_Mysqli extends Zend_Db_Statement
00040 {
00041
00047 protected $_keys;
00048
00054 protected $_values;
00055
00059 protected $_meta = null;
00060
00066 public function _prepare($sql)
00067 {
00068 $mysqli = $this->_adapter->getConnection();
00069
00070 $this->_stmt = $mysqli->prepare($sql);
00071
00072 if ($this->_stmt === false || $mysqli->errno) {
00076 require_once 'Zend/Db/Statement/Mysqli/Exception.php';
00077 throw new Zend_Db_Statement_Mysqli_Exception("Mysqli prepare error: " . $mysqli->error, $mysqli->errno);
00078 }
00079 }
00080
00092 protected function _bindParam($parameter, &$variable, $type = null, $length = null, $options = null)
00093 {
00094 return true;
00095 }
00096
00102 public function close()
00103 {
00104 if ($this->_stmt) {
00105 $r = $this->_stmt->close();
00106 $this->_stmt = null;
00107 return $r;
00108 }
00109 return false;
00110 }
00111
00117 public function closeCursor()
00118 {
00119 if ($stmt = $this->_stmt) {
00120 $mysqli = $this->_adapter->getConnection();
00121 while ($mysqli->more_results()) {
00122 $mysqli->next_result();
00123 }
00124 $this->_stmt->free_result();
00125 return $this->_stmt->reset();
00126 }
00127 return false;
00128 }
00129
00136 public function columnCount()
00137 {
00138 if (isset($this->_meta) && $this->_meta) {
00139 return $this->_meta->field_count;
00140 }
00141 return 0;
00142 }
00143
00150 public function errorCode()
00151 {
00152 if (!$this->_stmt) {
00153 return false;
00154 }
00155 return substr($this->_stmt->sqlstate, 0, 5);
00156 }
00157
00164 public function errorInfo()
00165 {
00166 if (!$this->_stmt) {
00167 return false;
00168 }
00169 return array(
00170 substr($this->_stmt->sqlstate, 0, 5),
00171 $this->_stmt->errno,
00172 $this->_stmt->error,
00173 );
00174 }
00175
00183 public function _execute(array $params = null)
00184 {
00185 if (!$this->_stmt) {
00186 return false;
00187 }
00188
00189
00190
00191 if ($params === null) {
00192 $params = $this->_bindParam;
00193 }
00194
00195 if ($params) {
00196 array_unshift($params, str_repeat('s', count($params)));
00197 $stmtParams = array();
00198 foreach ($params as $k => &$value) {
00199 $stmtParams[$k] = &$value;
00200 }
00201 call_user_func_array(
00202 array($this->_stmt, 'bind_param'),
00203 $stmtParams
00204 );
00205 }
00206
00207
00208 $retval = $this->_stmt->execute();
00209 if ($retval === false) {
00213 require_once 'Zend/Db/Statement/Mysqli/Exception.php';
00214 throw new Zend_Db_Statement_Mysqli_Exception("Mysqli statement execute error : " . $this->_stmt->error, $this->_stmt->errno);
00215 }
00216
00217
00218
00219 if ($this->_meta === null) {
00220 $this->_meta = $this->_stmt->result_metadata();
00221 if ($this->_stmt->errno) {
00225 require_once 'Zend/Db/Statement/Mysqli/Exception.php';
00226 throw new Zend_Db_Statement_Mysqli_Exception("Mysqli statement metadata error: " . $this->_stmt->error, $this->_stmt->errno);
00227 }
00228 }
00229
00230
00231 if ($this->_meta !== false) {
00232
00233
00234 $this->_keys = array();
00235 foreach ($this->_meta->fetch_fields() as $col) {
00236 $this->_keys[] = $this->_adapter->foldCase($col->name);
00237 }
00238
00239
00240 $this->_values = array_fill(0, count($this->_keys), null);
00241
00242
00243
00244
00245 $refs = array();
00246 foreach ($this->_values as $i => &$f) {
00247 $refs[$i] = &$f;
00248 }
00249
00250 $this->_stmt->store_result();
00251
00252 call_user_func_array(
00253 array($this->_stmt, 'bind_result'),
00254 $this->_values
00255 );
00256 }
00257 return $retval;
00258 }
00259
00260
00270 public function fetch($style = null, $cursor = null, $offset = null)
00271 {
00272 if (!$this->_stmt) {
00273 return false;
00274 }
00275
00276 $retval = $this->_stmt->fetch();
00277 switch ($retval) {
00278 case null:
00279 case false:
00280 $this->_stmt->reset();
00281 return false;
00282 default:
00283
00284 }
00285
00286
00287 if ($style === null) {
00288 $style = $this->_fetchMode;
00289 }
00290
00291
00292
00293 $values = array();
00294 foreach ($this->_values as $key => $val) {
00295 $values[] = $val;
00296 }
00297
00298 $row = false;
00299 switch ($style) {
00300 case Zend_Db::FETCH_NUM:
00301 $row = $values;
00302 break;
00303 case Zend_Db::FETCH_ASSOC:
00304 $row = array_combine($this->_keys, $values);
00305 break;
00306 case Zend_Db::FETCH_BOTH:
00307 $assoc = array_combine($this->_keys, $values);
00308 $row = array_merge($values, $assoc);
00309 break;
00310 case Zend_Db::FETCH_OBJ:
00311 $row = (object) array_combine($this->_keys, $values);
00312 break;
00313 case Zend_Db::FETCH_BOUND:
00314 $assoc = array_combine($this->_keys, $values);
00315 $row = array_merge($values, $assoc);
00316 return $this->_fetchBound($row);
00317 break;
00318 default:
00322 require_once 'Zend/Db/Statement/Mysqli/Exception.php';
00323 throw new Zend_Db_Statement_Mysqli_Exception("Invalid fetch mode '$style' specified");
00324 break;
00325 }
00326 return $row;
00327 }
00328
00337 public function nextRowset()
00338 {
00342 require_once 'Zend/Db/Statement/Mysqli/Exception.php';
00343 throw new Zend_Db_Statement_Mysqli_Exception(__FUNCTION__.'() is not implemented');
00344 }
00345
00353 public function rowCount()
00354 {
00355 if (!$this->_adapter) {
00356 return false;
00357 }
00358 $mysqli = $this->_adapter->getConnection();
00359 return $mysqli->affected_rows;
00360 }
00361
00362 }