DbStatement.php 10 KB

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