PageRenderTime 44ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/standard/tags/release-1.5.1/library/Zend/Db/Statement/Mysqli.php

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