PageRenderTime 126ms CodeModel.GetById 39ms RepoModel.GetById 3ms app.codeStats 0ms

/library/Zend/Test/DbStatement.php

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