DbAdapter.php 7.5 KB

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