Static.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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_Db
  17. * @subpackage UnitTests
  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$
  21. */
  22. /**
  23. * @see Zend_Db_Adapter_Abstract
  24. */
  25. require_once 'Zend/Db/Adapter/Abstract.php';
  26. /**
  27. * @see Zend_Db_Statement_Static
  28. */
  29. require_once 'Zend/Db/Statement/Static.php';
  30. /**
  31. * Class for connecting to SQL databases and performing common operations.
  32. *
  33. * @category Zend
  34. * @package Zend_Db
  35. * @subpackage UnitTests
  36. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  37. * @license http://framework.zend.com/license/new-bsd New BSD License
  38. */
  39. class Zend_Db_Adapter_Static extends Zend_Db_Adapter_Abstract
  40. {
  41. public $config = null;
  42. /**
  43. * The number of seconds to sleep upon query execution
  44. *
  45. * @var integer
  46. */
  47. protected $_onQuerySleep = 0;
  48. /**
  49. * Sets the number of seconds to sleep upon query execution
  50. *
  51. * @param integer $seconds
  52. * @return Zend_Db_Adapter_Static Provides a fluent interface
  53. */
  54. public function setOnQuerySleep($seconds = 0)
  55. {
  56. $this->_onQuerySleep = (integer) $seconds;
  57. return $this;
  58. }
  59. /**
  60. * Returns the number of seconds to sleep upon query execution
  61. *
  62. * @return integer
  63. */
  64. public function getOnQuerySleep()
  65. {
  66. return $this->_onQuerySleep;
  67. }
  68. /**
  69. * Check for config options that are mandatory.
  70. * Throw exceptions if any are missing.
  71. *
  72. * @param array $config
  73. * @throws Zend_Db_Adapter_Exception
  74. */
  75. protected function _checkRequiredOptions(array $config)
  76. {
  77. // we need at least a dbname
  78. if (! array_key_exists('dbname', $config)) {
  79. require_once 'Zend/Db/Adapter/Exception.php';
  80. throw new Zend_Db_Adapter_Exception("Configuration must have a key for 'dbname' that names the database instance");
  81. }
  82. $this->config = $config;
  83. }
  84. /**
  85. * Prepares and executes a SQL statement with bound data.
  86. *
  87. * @param string|Zend_Db_Select $sql The SQL statement with placeholders.
  88. * @param mixed $bind An array of data to bind to the placeholders.
  89. * @return Zend_Db_Statement (may also be PDOStatement in the case of PDO)
  90. */
  91. public function query($sql, $bind = array())
  92. {
  93. // connect to the database if needed
  94. $this->_connect();
  95. // is the $sql a Zend_Db_Select object?
  96. if ($sql instanceof Zend_Db_Select) {
  97. $sql = $sql->__toString();
  98. }
  99. // make sure $bind to an array;
  100. // don't use (array) typecasting because
  101. // because $bind may be a Zend_Db_Expr object
  102. if (!is_array($bind)) {
  103. $bind = array($bind);
  104. }
  105. // prepare and execute the statement with profiling
  106. $stmt = $this->prepare($sql);
  107. $q = $this->_profiler->queryStart($sql);
  108. if ($this->_onQuerySleep > 0) {
  109. sleep($this->_onQuerySleep);
  110. }
  111. $stmt->execute($bind);
  112. $this->_profiler->queryEnd($q);
  113. // return the results embedded in the prepared statement object
  114. $stmt->setFetchMode($this->_fetchMode);
  115. return $stmt;
  116. }
  117. /**
  118. * Returns a list of the tables in the database.
  119. *
  120. * @return array
  121. */
  122. public function listTables()
  123. {
  124. return array('dummy');
  125. }
  126. /**
  127. * Returns the column descriptions for a table.
  128. *
  129. * The return value is an associative array keyed by the column name,
  130. * as returned by the RDBMS.
  131. *
  132. * The value of each array element is an associative array
  133. * with the following keys:
  134. *
  135. * SCHEMA_NAME => string; name of database or schema
  136. * TABLE_NAME => string;
  137. * COLUMN_NAME => string; column name
  138. * COLUMN_POSITION => number; ordinal position of column in table
  139. * DATA_TYPE => string; SQL datatype name of column
  140. * DEFAULT => string; default expression of column, null if none
  141. * NULLABLE => boolean; true if column can have nulls
  142. * LENGTH => number; length of CHAR/VARCHAR
  143. * SCALE => number; scale of NUMERIC/DECIMAL
  144. * PRECISION => number; precision of NUMERIC/DECIMAL
  145. * UNSIGNED => boolean; unsigned property of an integer type
  146. * PRIMARY => boolean; true if column is part of the primary key
  147. * PRIMARY_POSITION => integer; position of column in primary key
  148. *
  149. * @param string $tableName
  150. * @param string $schemaName OPTIONAL
  151. * @return array
  152. */
  153. public function describeTable($tableName, $schemaName = null)
  154. {
  155. return array(
  156. 'SCHEMA_NAME' => $schemaName,
  157. 'TABLE_NAME' => $tableName,
  158. 'COLUMN_NAME' => null,
  159. 'COLUMN_POSITION' => null,
  160. 'DATA_TYPE' => null,
  161. 'DEFAULT' => null,
  162. 'NULLABLE' => null,
  163. 'LENGTH' => null,
  164. 'SCALE' => null,
  165. 'PRECISION' => null,
  166. 'UNSIGNED' => null,
  167. 'PRIMARY' => null,
  168. 'PRIMARY_POSITION' => null,
  169. );
  170. }
  171. /**
  172. * Creates a connection to the database.
  173. *
  174. * @return void
  175. */
  176. protected function _connect()
  177. {
  178. $this->_connection = $this;
  179. return;
  180. }
  181. /**
  182. * Test if a connection is active
  183. *
  184. * @return boolean
  185. */
  186. public function isConnected()
  187. {
  188. return ((bool) (!is_null($this->_connection)));
  189. }
  190. /**
  191. * Force the connection to close.
  192. *
  193. * @return void
  194. */
  195. public function closeConnection()
  196. {
  197. $this->_connection = null;
  198. }
  199. /**
  200. * Prepare a statement and return a PDOStatement-like object.
  201. *
  202. * @param string|Zend_Db_Select $sql SQL query
  203. * @return Zend_Db_Statment_Static
  204. */
  205. public function prepare($sql)
  206. {
  207. return new Zend_Db_Statement_Static();
  208. }
  209. /**
  210. * Gets the last ID generated automatically by an IDENTITY/AUTOINCREMENT column.
  211. *
  212. * As a convention, on RDBMS brands that support sequences
  213. * (e.g. Oracle, PostgreSQL, DB2), this method forms the name of a sequence
  214. * from the arguments and returns the last id generated by that sequence.
  215. * On RDBMS brands that support IDENTITY/AUTOINCREMENT columns, this method
  216. * returns the last value generated for such a column, and the table name
  217. * argument is disregarded.
  218. *
  219. * @param string $tableName OPTIONAL Name of table.
  220. * @param string $primaryKey OPTIONAL Name of primary key column.
  221. * @return integer
  222. */
  223. public function lastInsertId($tableName = null, $primaryKey = 'id')
  224. {
  225. return null;
  226. }
  227. /**
  228. * Begin a transaction.
  229. */
  230. protected function _beginTransaction()
  231. {
  232. return true;
  233. }
  234. /**
  235. * Commit a transaction.
  236. */
  237. protected function _commit()
  238. {
  239. return true;
  240. }
  241. /**
  242. * Roll-back a transaction.
  243. */
  244. protected function _rollBack()
  245. {
  246. return true;
  247. }
  248. /**
  249. * Set the fetch mode.
  250. *
  251. * @param integer $mode
  252. */
  253. public function setFetchMode($mode)
  254. {
  255. $this->_fetchMode = $mode;
  256. return $this;
  257. }
  258. /**
  259. * Adds an adapter-specific LIMIT clause to the SELECT statement.
  260. *
  261. * @param mixed $sql
  262. * @param integer $count
  263. * @param integer $offset
  264. * @return string
  265. */
  266. public function limit($sql, $count, $offset = 0)
  267. {
  268. return $sql . " LIMIT $count OFFSET $offset";
  269. }
  270. /**
  271. * Check if the adapter supports real SQL parameters.
  272. *
  273. * @param string $type
  274. * @return bool
  275. */
  276. public function supportsParameters($type)
  277. {
  278. return true;
  279. }
  280. /**
  281. * Retrieve server version in PHP style
  282. *
  283. * @return string
  284. */
  285. public function getServerVersion() {
  286. return "5.6.7.8";
  287. }
  288. }