DbAdapter.php 8.2 KB

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