DbAdapter.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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_Adapter_Abstract
  24. */
  25. require_once "Zend/Db/Adapter/Abstract.php";
  26. /**
  27. * @see Zend_Test_DbStatement
  28. */
  29. require_once "Zend/Test/DbStatement.php";
  30. /**
  31. * Testing Database Adapter which acts as a stack for SQL Results
  32. *
  33. * @category Zend
  34. * @package Zend_Test
  35. * @subpackage PHPUnit
  36. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  37. * @license http://framework.zend.com/license/new-bsd New BSD License
  38. */
  39. class Zend_Test_DbAdapter extends Zend_Db_Adapter_Abstract
  40. {
  41. /**
  42. * @var array
  43. */
  44. protected $_statementStack = array();
  45. /**
  46. * @var boolean
  47. */
  48. protected $_connected = false;
  49. /**
  50. * @var array
  51. */
  52. protected $_listTables = array();
  53. /**
  54. * @var array
  55. */
  56. protected $_lastInsertIdStack = array();
  57. /**
  58. * @var array
  59. */
  60. protected $_describeTables = array();
  61. /**
  62. * Empty constructor to make it parameterless.
  63. */
  64. public function __construct()
  65. {
  66. }
  67. /**
  68. * Append a new Statement to the SQL Result Stack.
  69. *
  70. * @param Zend_Test_DbStatement $stmt
  71. * @return Zend_Test_DbAdapter
  72. */
  73. public function appendStatementToStack(Zend_Test_DbStatement $stmt)
  74. {
  75. array_push($this->_statementStack, $stmt);
  76. return $this;
  77. }
  78. /**
  79. * Append a new Insert Id to the {@see lastInsertId}.
  80. *
  81. * @param int|string $id
  82. * @return Zend_Test_DbAdapter
  83. */
  84. public function appendLastInsertIdToStack($id)
  85. {
  86. array_push($this->_lastInsertIdStack, $id);
  87. return $this;
  88. }
  89. /**
  90. * Returns the symbol the adapter uses for delimited identifiers.
  91. *
  92. * @return string
  93. */
  94. public function getQuoteIdentifierSymbol()
  95. {
  96. return '';
  97. }
  98. /**
  99. * Set the result from {@see listTables()}.
  100. *
  101. * @param array $listTables
  102. */
  103. public function setListTables(array $listTables)
  104. {
  105. $this->_listTables = $listTables;
  106. }
  107. /**
  108. * Returns a list of the tables in the database.
  109. *
  110. * @return array
  111. */
  112. public function listTables()
  113. {
  114. return $this->_listTables;
  115. }
  116. /**
  117. *
  118. * @param string $table
  119. * @param array $tableInfo
  120. * @return Zend_Test_DbAdapter
  121. */
  122. public function setDescribeTable($table, $tableInfo)
  123. {
  124. $this->_describeTables[$table] = $tableInfo;
  125. return $this;
  126. }
  127. /**
  128. * Returns the column descriptions for a table.
  129. *
  130. * The return value is an associative array keyed by the column name,
  131. * as returned by the RDBMS.
  132. *
  133. * The value of each array element is an associative array
  134. * with the following keys:
  135. *
  136. * SCHEMA_NAME => string; name of database or schema
  137. * TABLE_NAME => string;
  138. * COLUMN_NAME => string; column name
  139. * COLUMN_POSITION => number; ordinal position of column in table
  140. * DATA_TYPE => string; SQL datatype name of column
  141. * DEFAULT => string; default expression of column, null if none
  142. * NULLABLE => boolean; true if column can have nulls
  143. * LENGTH => number; length of CHAR/VARCHAR
  144. * SCALE => number; scale of NUMERIC/DECIMAL
  145. * PRECISION => number; precision of NUMERIC/DECIMAL
  146. * UNSIGNED => boolean; unsigned property of an integer type
  147. * PRIMARY => boolean; true if column is part of the primary key
  148. * PRIMARY_POSITION => integer; position of column in primary key
  149. *
  150. * @param string $tableName
  151. * @param string $schemaName OPTIONAL
  152. * @return array
  153. */
  154. public function describeTable($tableName, $schemaName = null)
  155. {
  156. if(isset($this->_describeTables[$tableName])) {
  157. return $this->_describeTables[$tableName];
  158. } else {
  159. return array();
  160. }
  161. }
  162. /**
  163. * Creates a connection to the database.
  164. *
  165. * @return void
  166. */
  167. protected function _connect()
  168. {
  169. $this->_connected = true;
  170. }
  171. /**
  172. * Test if a connection is active
  173. *
  174. * @return boolean
  175. */
  176. public function isConnected()
  177. {
  178. return $this->_connected;
  179. }
  180. /**
  181. * Force the connection to close.
  182. *
  183. * @return void
  184. */
  185. public function closeConnection()
  186. {
  187. $this->_connected = false;
  188. }
  189. /**
  190. * Prepare a statement and return a PDOStatement-like object.
  191. *
  192. * @param string|Zend_Db_Select $sql SQL query
  193. * @return Zend_Db_Statment|PDOStatement
  194. */
  195. public function prepare($sql)
  196. {
  197. if(count($this->_statementStack)) {
  198. return array_pop($this->_statementStack);
  199. } else {
  200. return new Zend_Test_DbStatement();
  201. }
  202. }
  203. /**
  204. * Gets the last ID generated automatically by an IDENTITY/AUTOINCREMENT column.
  205. *
  206. * As a convention, on RDBMS brands that support sequences
  207. * (e.g. Oracle, PostgreSQL, DB2), this method forms the name of a sequence
  208. * from the arguments and returns the last id generated by that sequence.
  209. * On RDBMS brands that support IDENTITY/AUTOINCREMENT columns, this method
  210. * returns the last value generated for such a column, and the table name
  211. * argument is disregarded.
  212. *
  213. * @param string $tableName OPTIONAL Name of table.
  214. * @param string $primaryKey OPTIONAL Name of primary key column.
  215. * @return string
  216. */
  217. public function lastInsertId($tableName = null, $primaryKey = null)
  218. {
  219. if(count($this->_lastInsertIdStack)) {
  220. return array_pop($this->_lastInsertIdStack);
  221. } else {
  222. return false;
  223. }
  224. }
  225. /**
  226. * Begin a transaction.
  227. */
  228. protected function _beginTransaction()
  229. {
  230. return;
  231. }
  232. /**
  233. * Commit a transaction.
  234. */
  235. protected function _commit()
  236. {
  237. }
  238. /**
  239. * Roll-back a transaction.
  240. */
  241. protected function _rollBack()
  242. {
  243. }
  244. /**
  245. * Set the fetch mode.
  246. *
  247. * @param integer $mode
  248. * @return void
  249. * @throws Zend_Db_Adapter_Exception
  250. */
  251. public function setFetchMode($mode)
  252. {
  253. return;
  254. }
  255. /**
  256. * Adds an adapter-specific LIMIT clause to the SELECT statement.
  257. *
  258. * @param mixed $sql
  259. * @param integer $count
  260. * @param integer $offset
  261. * @return string
  262. */
  263. public function limit($sql, $count, $offset = 0)
  264. {
  265. return sprintf('%s LIMIT %d,%d', $sql, $offset, $count);
  266. }
  267. /**
  268. * Check if the adapter supports real SQL parameters.
  269. *
  270. * @param string $type 'positional' or 'named'
  271. * @return bool
  272. */
  273. public function supportsParameters($type)
  274. {
  275. return false;
  276. }
  277. /**
  278. * Retrieve server version in PHP style
  279. *
  280. * @return string
  281. */
  282. function getServerVersion()
  283. {
  284. return "1.0.0";
  285. }
  286. }