PageRenderTime 56ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/Zend/Db/Statement/Mysqli.php

https://github.com/lanmediaservice/lms-tplib
PHP | 369 lines | 171 code | 35 blank | 163 comment | 21 complexity | cf3c7b53b6357a99ffdddc80d15f3214 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-2009 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 16927 2009-07-21 17:49:39Z ralph $
  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-2009 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. $mysqli = $this->_adapter->getConnection();
  114. while ($mysqli->more_results()) {
  115. $mysqli->next_result();
  116. }
  117. $this->_stmt->free_result();
  118. return $this->_stmt->reset();
  119. }
  120. return false;
  121. }
  122. /**
  123. * Returns the number of columns in the result set.
  124. * Returns null if the statement has no result set metadata.
  125. *
  126. * @return int The number of columns.
  127. */
  128. public function columnCount()
  129. {
  130. if (isset($this->_meta) && $this->_meta) {
  131. return $this->_meta->field_count;
  132. }
  133. return 0;
  134. }
  135. /**
  136. * Retrieves the error code, if any, associated with the last operation on
  137. * the statement handle.
  138. *
  139. * @return string error code.
  140. */
  141. public function errorCode()
  142. {
  143. if (!$this->_stmt) {
  144. return false;
  145. }
  146. return substr($this->_stmt->sqlstate, 0, 5);
  147. }
  148. /**
  149. * Retrieves an array of error information, if any, associated with the
  150. * last operation on the statement handle.
  151. *
  152. * @return array
  153. */
  154. public function errorInfo()
  155. {
  156. if (!$this->_stmt) {
  157. return false;
  158. }
  159. return array(
  160. substr($this->_stmt->sqlstate, 0, 5),
  161. $this->_stmt->errno,
  162. $this->_stmt->error,
  163. );
  164. }
  165. /**
  166. * Executes a prepared statement.
  167. *
  168. * @param array $params OPTIONAL Values to bind to parameter placeholders.
  169. * @return bool
  170. * @throws Zend_Db_Statement_Mysqli_Exception
  171. */
  172. public function _execute(array $params = null)
  173. {
  174. if (!$this->_stmt) {
  175. return false;
  176. }
  177. // if no params were given as an argument to execute(),
  178. // then default to the _bindParam array
  179. if ($params === null) {
  180. $params = $this->_bindParam;
  181. }
  182. // send $params as input parameters to the statement
  183. if ($params) {
  184. array_unshift($params, str_repeat('s', count($params)));
  185. $stmtParams = array();
  186. foreach ($params as $k => &$value) {
  187. $stmtParams[$k] = &$value;
  188. }
  189. call_user_func_array(
  190. array($this->_stmt, 'bind_param'),
  191. $stmtParams
  192. );
  193. }
  194. // execute the statement
  195. $retval = $this->_stmt->execute();
  196. if ($retval === false) {
  197. /**
  198. * @see Zend_Db_Statement_Mysqli_Exception
  199. */
  200. //*** require_once 'Zend/Db/Statement/Mysqli/Exception.php';
  201. throw new Zend_Db_Statement_Mysqli_Exception("Mysqli statement execute error : " . $this->_stmt->error);
  202. }
  203. // retain metadata
  204. if ($this->_meta === null) {
  205. $this->_meta = $this->_stmt->result_metadata();
  206. if ($this->_stmt->errno) {
  207. /**
  208. * @see Zend_Db_Statement_Mysqli_Exception
  209. */
  210. //*** require_once 'Zend/Db/Statement/Mysqli/Exception.php';
  211. throw new Zend_Db_Statement_Mysqli_Exception("Mysqli statement metadata error: " . $this->_stmt->error);
  212. }
  213. }
  214. // statements that have no result set do not return metadata
  215. if ($this->_meta !== false) {
  216. // get the column names that will result
  217. $this->_keys = array();
  218. foreach ($this->_meta->fetch_fields() as $col) {
  219. $this->_keys[] = $this->_adapter->foldCase($col->name);
  220. }
  221. // set up a binding space for result variables
  222. $this->_values = array_fill(0, count($this->_keys), null);
  223. // set up references to the result binding space.
  224. // just passing $this->_values in the call_user_func_array()
  225. // below won't work, you need references.
  226. $refs = array();
  227. foreach ($this->_values as $i => &$f) {
  228. $refs[$i] = &$f;
  229. }
  230. $this->_stmt->store_result();
  231. // bind to the result variables
  232. call_user_func_array(
  233. array($this->_stmt, 'bind_result'),
  234. $this->_values
  235. );
  236. }
  237. return $retval;
  238. }
  239. /**
  240. * Fetches a row from the result set.
  241. *
  242. * @param int $style OPTIONAL Fetch mode for this fetch operation.
  243. * @param int $cursor OPTIONAL Absolute, relative, or other.
  244. * @param int $offset OPTIONAL Number for absolute or relative cursors.
  245. * @return mixed Array, object, or scalar depending on fetch mode.
  246. * @throws Zend_Db_Statement_Mysqli_Exception
  247. */
  248. public function fetch($style = null, $cursor = null, $offset = null)
  249. {
  250. if (!$this->_stmt) {
  251. return false;
  252. }
  253. // fetch the next result
  254. $retval = $this->_stmt->fetch();
  255. switch ($retval) {
  256. case null: // end of data
  257. case false: // error occurred
  258. $this->_stmt->reset();
  259. return $retval;
  260. default:
  261. // fallthrough
  262. }
  263. // make sure we have a fetch mode
  264. if ($style === null) {
  265. $style = $this->_fetchMode;
  266. }
  267. // dereference the result values, otherwise things like fetchAll()
  268. // return the same values for every entry (because of the reference).
  269. $values = array();
  270. foreach ($this->_values as $key => $val) {
  271. $values[] = $val;
  272. }
  273. $row = false;
  274. switch ($style) {
  275. case Zend_Db::FETCH_NUM:
  276. $row = $values;
  277. break;
  278. case Zend_Db::FETCH_ASSOC:
  279. $row = array_combine($this->_keys, $values);
  280. break;
  281. case Zend_Db::FETCH_BOTH:
  282. $assoc = array_combine($this->_keys, $values);
  283. $row = array_merge($values, $assoc);
  284. break;
  285. case Zend_Db::FETCH_OBJ:
  286. $row = (object) array_combine($this->_keys, $values);
  287. break;
  288. case Zend_Db::FETCH_BOUND:
  289. $assoc = array_combine($this->_keys, $values);
  290. $row = array_merge($values, $assoc);
  291. return $this->_fetchBound($row);
  292. break;
  293. default:
  294. /**
  295. * @see Zend_Db_Statement_Mysqli_Exception
  296. */
  297. //*** require_once 'Zend/Db/Statement/Mysqli/Exception.php';
  298. throw new Zend_Db_Statement_Mysqli_Exception("Invalid fetch mode '$style' specified");
  299. break;
  300. }
  301. return $row;
  302. }
  303. /**
  304. * Retrieves the next rowset (result set) for a SQL statement that has
  305. * multiple result sets. An example is a stored procedure that returns
  306. * the results of multiple queries.
  307. *
  308. * @return bool
  309. * @throws Zend_Db_Statement_Mysqli_Exception
  310. */
  311. public function nextRowset()
  312. {
  313. /**
  314. * @see Zend_Db_Statement_Mysqli_Exception
  315. */
  316. //*** require_once 'Zend/Db/Statement/Mysqli/Exception.php';
  317. throw new Zend_Db_Statement_Mysqli_Exception(__FUNCTION__.'() is not implemented');
  318. }
  319. /**
  320. * Returns the number of rows affected by the execution of the
  321. * last INSERT, DELETE, or UPDATE statement executed by this
  322. * statement object.
  323. *
  324. * @return int The number of rows affected.
  325. */
  326. public function rowCount()
  327. {
  328. if (!$this->_adapter) {
  329. return false;
  330. }
  331. $mysqli = $this->_adapter->getConnection();
  332. return $mysqli->affected_rows;
  333. }
  334. }