DbStatement.php 11 KB

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