DbAdapter.php 7.9 KB

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