PageRenderTime 48ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/common/lib/Zend/Db/Statement/Mysqli.php

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