/library/Zend/Test/DbStatement.php

https://github.com/sidealice/zf2 · PHP · 405 lines · 150 code · 35 blank · 220 comment · 9 complexity · 214a1b343047ed9d669d222c2bfe0d3a 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_Test
  17. * @subpackage PHPUnit
  18. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. /**
  22. * @namespace
  23. */
  24. namespace Zend\Test;
  25. use Zend\Db\Statement;
  26. /**
  27. * Testing Database Statement that acts as a stack to SQL resultsets.
  28. *
  29. * @uses \Zend\Db\Statement\Exception
  30. * @uses \Zend\Db\Statement
  31. * @category Zend
  32. * @package Zend_Test
  33. * @subpackage PHPUnit
  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 DbStatement implements Statement
  38. {
  39. /**
  40. * @var array
  41. */
  42. protected $_fetchStack = array();
  43. /**
  44. * @var int
  45. */
  46. protected $_columnCount = 0;
  47. /**
  48. * @var int
  49. */
  50. protected $_rowCount = 0;
  51. /**
  52. * @var \Zend\Db\Profiler\Query
  53. */
  54. protected $_queryProfile = null;
  55. /**
  56. * Create a Select statement which returns the given array of rows.
  57. *
  58. * @param array $rows
  59. * @return \Zend\Test\DbStatement
  60. */
  61. static public function createSelectStatement(array $rows=array())
  62. {
  63. $stmt = new self();
  64. foreach($rows AS $row) {
  65. $stmt->append($row);
  66. }
  67. return $stmt;
  68. }
  69. /**
  70. * Create an Insert Statement
  71. *
  72. * @param int $affectedRows
  73. * @return \Zend\Test\DbStatement
  74. */
  75. static public function createInsertStatement($affectedRows=0)
  76. {
  77. return self::_createRowCountStatement($affectedRows);
  78. }
  79. /**
  80. * Create an Delete Statement
  81. *
  82. * @param int $affectedRows
  83. * @return \Zend\Test\DbStatement
  84. */
  85. static public function createDeleteStatement($affectedRows=0)
  86. {
  87. return self::_createRowCountStatement($affectedRows);
  88. }
  89. /**
  90. * Create an Update Statement
  91. *
  92. * @param int $affectedRows
  93. * @return \Zend\Test\DbStatement
  94. */
  95. static public function createUpdateStatement($affectedRows=0)
  96. {
  97. return self::_createRowCountStatement($affectedRows);
  98. }
  99. /**
  100. * Create a Row Count Statement
  101. *
  102. * @param int $affectedRows
  103. * @return \Zend\Test\DbStatement
  104. */
  105. static protected function _createRowCountStatement($affectedRows)
  106. {
  107. $stmt = new self();
  108. $stmt->setRowCount($affectedRows);
  109. return $stmt;
  110. }
  111. /**
  112. * @param \Zend\Db\Profiler\Query $qp
  113. */
  114. public function setQueryProfile(\Zend\Db\Profiler\Query $qp)
  115. {
  116. $this->_queryProfile = $qp;
  117. }
  118. /**
  119. * @param int $rowCount
  120. */
  121. public function setRowCount($rowCount)
  122. {
  123. $this->_rowCount = $rowCount;
  124. }
  125. /**
  126. * Append a new row to the fetch stack.
  127. *
  128. * @param array $row
  129. */
  130. public function append($row)
  131. {
  132. $this->_columnCount = count($row);
  133. $this->_fetchStack[] = $row;
  134. }
  135. /**
  136. * Bind a column of the statement result set to a PHP variable.
  137. *
  138. * @param string $column Name the column in the result set, either by
  139. * position or by name.
  140. * @param mixed $param Reference to the PHP variable containing the value.
  141. * @param mixed $type OPTIONAL
  142. * @return bool
  143. * @throws \Zend\Db\Statement\Exception
  144. */
  145. public function bindColumn($column, &$param, $type = null)
  146. {
  147. return true;
  148. }
  149. /**
  150. * Binds a parameter to the specified variable name.
  151. *
  152. * @param mixed $parameter Name the parameter, either integer or string.
  153. * @param mixed $variable Reference to PHP variable containing the value.
  154. * @param mixed $type OPTIONAL Datatype of SQL parameter.
  155. * @param mixed $length OPTIONAL Length of SQL parameter.
  156. * @param mixed $options OPTIONAL Other options.
  157. * @return bool
  158. * @throws \Zend\Db\Statement\Exception
  159. */
  160. public function bindParam($parameter, &$variable, $type = null, $length = null, $options = null)
  161. {
  162. if($this->_queryProfile !== null) {
  163. $this->_queryProfile->bindParam($parameter, $variable);
  164. }
  165. return true;
  166. }
  167. /**
  168. * Binds a value to a parameter.
  169. *
  170. * @param mixed $parameter Name the parameter, either integer or string.
  171. * @param mixed $value Scalar value to bind to the parameter.
  172. * @param mixed $type OPTIONAL Datatype of the parameter.
  173. * @return bool
  174. * @throws \Zend\Db\Statement\Exception
  175. */
  176. public function bindValue($parameter, $value, $type = null)
  177. {
  178. return true;
  179. }
  180. /**
  181. * Closes the cursor, allowing the statement to be executed again.
  182. *
  183. * @return bool
  184. * @throws \Zend\Db\Statement\Exception
  185. */
  186. public function closeCursor()
  187. {
  188. return true;
  189. }
  190. /**
  191. * Returns the number of columns in the result set.
  192. * Returns null if the statement has no result set metadata.
  193. *
  194. * @return int The number of columns.
  195. * @throws \Zend\Db\Statement\Exception
  196. */
  197. public function columnCount()
  198. {
  199. return $this->_columnCount;
  200. }
  201. /**
  202. * Retrieves the error code, if any, associated with the last operation on
  203. * the statement handle.
  204. *
  205. * @return string error code.
  206. * @throws \Zend\Db\Statement\Exception
  207. */
  208. public function errorCode()
  209. {
  210. return false;
  211. }
  212. /**
  213. * Retrieves an array of error information, if any, associated with the
  214. * last operation on the statement handle.
  215. *
  216. * @return array
  217. * @throws \Zend\Db\Statement\Exception
  218. */
  219. public function errorInfo()
  220. {
  221. return false;
  222. }
  223. /**
  224. * Executes a prepared statement.
  225. *
  226. * @param array $params OPTIONAL Values to bind to parameter placeholders.
  227. * @return bool
  228. * @throws \Zend\Db\Statement\Exception
  229. */
  230. public function execute(array $params = array())
  231. {
  232. if($this->_queryProfile !== null) {
  233. $this->_queryProfile->bindParams($params);
  234. $this->_queryProfile->end();
  235. }
  236. return true;
  237. }
  238. /**
  239. * Fetches a row from the result set.
  240. *
  241. * @param int $style OPTIONAL Fetch mode for this fetch operation.
  242. * @param int $cursor OPTIONAL Absolute, relative, or other.
  243. * @param int $offset OPTIONAL Number for absolute or relative cursors.
  244. * @return mixed Array, object, or scalar depending on fetch mode.
  245. * @throws \Zend\Db\Statement\Exception
  246. */
  247. public function fetch($style = null, $cursor = null, $offset = null)
  248. {
  249. if(count($this->_fetchStack)) {
  250. $row = array_shift($this->_fetchStack);
  251. return $row;
  252. } else {
  253. return false;
  254. }
  255. }
  256. /**
  257. * Returns an array containing all of the result set rows.
  258. *
  259. * @param int $style OPTIONAL Fetch mode.
  260. * @param int $col OPTIONAL Column number, if fetch mode is by column.
  261. * @return array Collection of rows, each in a format by the fetch mode.
  262. * @throws \Zend\Db\Statement\Exception
  263. */
  264. public function fetchAll($style = null, $col = null)
  265. {
  266. $rows = $this->_fetchStack;
  267. $this->_fetchStack = array();
  268. return $rows;
  269. }
  270. /**
  271. * Returns a single column from the next row of a result set.
  272. *
  273. * @param int $col OPTIONAL Position of the column to fetch.
  274. * @return string
  275. * @throws \Zend\Db\Statement\Exception
  276. */
  277. public function fetchColumn($col = 0)
  278. {
  279. $row = $this->fetch();
  280. if($row == false) {
  281. return false;
  282. } else {
  283. if(count($row) < $col) {
  284. throw new Statement\Exception(
  285. "Column Position '".$col."' is out of bounds."
  286. );
  287. }
  288. $keys = array_keys($row);
  289. return $row[$keys[$col]];
  290. }
  291. }
  292. /**
  293. * Fetches the next row and returns it as an object.
  294. *
  295. * @param string $class OPTIONAL Name of the class to create.
  296. * @param array $config OPTIONAL Constructor arguments for the class.
  297. * @return mixed One object instance of the specified class.
  298. * @throws \Zend\Db\Statement\Exception
  299. */
  300. public function fetchObject($class = 'stdClass', array $config = array())
  301. {
  302. if(!class_exists($class)) {
  303. throw new Statement\Exception("Class '".$class."' does not exist!");
  304. }
  305. $object = new $class();
  306. $row = $this->fetch();
  307. foreach($row AS $k => $v) {
  308. $object->$k = $v;
  309. }
  310. return $object;
  311. }
  312. /**
  313. * Retrieve a statement attribute.
  314. *
  315. * @param string $key Attribute name.
  316. * @return mixed Attribute value.
  317. * @throws \Zend\Db\Statement\Exception
  318. */
  319. public function getAttribute($key)
  320. {
  321. return false;
  322. }
  323. /**
  324. * Retrieves the next rowset (result set) for a SQL statement that has
  325. * multiple result sets. An example is a stored procedure that returns
  326. * the results of multiple queries.
  327. *
  328. * @return bool
  329. * @throws \Zend\Db\Statement\Exception
  330. */
  331. public function nextRowset()
  332. {
  333. return false;
  334. }
  335. /**
  336. * Returns the number of rows affected by the execution of the
  337. * last INSERT, DELETE, or UPDATE statement executed by this
  338. * statement object.
  339. *
  340. * @return int The number of rows affected.
  341. * @throws \Zend\Db\Statement\Exception
  342. */
  343. public function rowCount()
  344. {
  345. return $this->_rowCount;
  346. }
  347. /**
  348. * Set a statement attribute.
  349. *
  350. * @param string $key Attribute name.
  351. * @param mixed $val Attribute value.
  352. * @return bool
  353. * @throws \Zend\Db\Statement\Exception
  354. */
  355. public function setAttribute($key, $val)
  356. {
  357. return true;
  358. }
  359. /**
  360. * Set the default fetch mode for this statement.
  361. *
  362. * @param int $mode The fetch mode.
  363. * @return bool
  364. * @throws \Zend\Db\Statement\Exception
  365. */
  366. public function setFetchMode($mode)
  367. {
  368. return true;
  369. }
  370. }