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

/application/libraries/Zend/Test/DbStatement.php

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