2
0

Common.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  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_Expr
  24. */
  25. require_once 'Zend/Db/Expr.php';
  26. /**
  27. * @category Zend
  28. * @package Zend_Db
  29. * @subpackage UnitTests
  30. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  31. * @license http://framework.zend.com/license/new-bsd New BSD License
  32. */
  33. abstract class Zend_Db_TestUtil_Common
  34. {
  35. /**
  36. * @var Zend_Db_Adapter_Abstract
  37. */
  38. protected $_db = null;
  39. /**
  40. * @var array
  41. */
  42. protected $_tables = array();
  43. /**
  44. * @var array
  45. */
  46. protected $_sequences = array();
  47. protected function _getSqlCreateTable($tableName)
  48. {
  49. return 'CREATE TABLE ' . $this->getAdapter()->quoteIdentifier($tableName, true);
  50. }
  51. protected function _getSqlCreateTableType()
  52. {
  53. return null;
  54. }
  55. protected function _getSqlDropTable($tableName)
  56. {
  57. return 'DROP TABLE ' . $this->getAdapter()->quoteIdentifier($tableName, true);
  58. }
  59. public function getSqlType($type)
  60. {
  61. return $type;
  62. }
  63. public function createTable($tableId, array $columns = array())
  64. {
  65. if (!$columns) {
  66. $columns = $this->{'_getColumns'.$tableId}();
  67. }
  68. $tableName = $this->getTableName($tableId);
  69. $this->dropTable($tableName);
  70. if (isset($this->_tables[$tableName])) {
  71. return;
  72. }
  73. $sql = $this->_getSqlCreateTable($tableName);
  74. if (!$sql) {
  75. return;
  76. }
  77. $sql .= " (\n\t";
  78. $pKey = null;
  79. $pKeys = array();
  80. if (isset($columns['PRIMARY KEY'])) {
  81. $pKey = $columns['PRIMARY KEY'];
  82. unset($columns['PRIMARY KEY']);
  83. foreach (explode(',', $pKey) as $pKeyCol) {
  84. $pKeys[] = $this->getAdapter()->quoteIdentifier($pKeyCol, true);
  85. }
  86. $pKey = implode(', ', $pKeys);
  87. }
  88. foreach ($columns as $columnName => $type) {
  89. $col[] = $this->getAdapter()->quoteIdentifier($columnName, true) . ' ' . $this->getSqlType($type);
  90. }
  91. if ($pKey) {
  92. $col[] = "PRIMARY KEY ($pKey)";
  93. }
  94. $sql .= implode(",\n\t", $col);
  95. $sql .= "\n)" . $this->_getSqlCreateTableType();
  96. $result = $this->_tryRawQuery($sql);
  97. if ($result === false) {
  98. throw new Zend_Db_Exception("Statement failed:\n$sql\nError: " . $this->getAdapter()->getConnection()->error);
  99. }
  100. $this->_tables[$tableName] = true;
  101. }
  102. public function dropTable($tableName = null)
  103. {
  104. if (!$tableName) {
  105. foreach ($this->_tableName as $tab) {
  106. $this->dropTable($tab);
  107. }
  108. return;
  109. }
  110. $sql = $this->_getSqlDropTable($tableName);
  111. if (!$sql) {
  112. return;
  113. }
  114. $result = $this->_tryRawQuery($sql);
  115. if ($result === false) {
  116. throw new Zend_Db_Exception("DROP TABLE statement failed:\n$sql\nError: " . $this->getAdapter()->getConnection()->error);
  117. }
  118. unset($this->_tables[$tableName]);
  119. }
  120. protected function _getSqlCreateSequence($sequenceName)
  121. {
  122. return null;
  123. }
  124. protected function _getSqlDropSequence($sequenceName)
  125. {
  126. return null;
  127. }
  128. public function createSequence($sequenceName)
  129. {
  130. $this->dropSequence($sequenceName);
  131. if (isset($this->_sequences[$sequenceName])) {
  132. return;
  133. }
  134. $sql = $this->_getSqlCreateSequence($sequenceName);
  135. if (!$sql) {
  136. return;
  137. }
  138. $result = $this->_tryRawQuery($sql);
  139. if ($result === false) {
  140. throw new Zend_Db_Exception("CREATE SEQUENCE statement failed:\n$sql\nError: " . $this->getAdapter()->getConnection()->error);
  141. }
  142. $this->_sequences[$sequenceName] = true;
  143. }
  144. public function dropSequence($sequenceName = null)
  145. {
  146. if (!$sequenceName) {
  147. foreach (array_keys($this->_sequences) as $seq) {
  148. $this->dropSequence($seq);
  149. }
  150. return;
  151. }
  152. $sql = $this->_getSqlDropSequence($sequenceName);
  153. if (!$sql) {
  154. return;
  155. }
  156. $result = $this->_tryRawQuery($sql);
  157. if ($result === false) {
  158. throw new Zend_Db_Exception("DROP SEQUENCE statement failed:\n$sql\nError: " . $this->getAdapter()->getConnection()->error);
  159. }
  160. unset($this->_sequences[$sequenceName]);
  161. }
  162. public function getParams(array $constants = array())
  163. {
  164. $params = array();
  165. foreach ($constants as $key => $constant) {
  166. if (defined($constant)) {
  167. $params[$key] = constant($constant);
  168. }
  169. }
  170. return $params;
  171. }
  172. public function getSchema()
  173. {
  174. $param = $this->getParams();
  175. if (isset($param['dbname']) && strpos($param['dbname'], ':') === false) {
  176. return $param['dbname'];
  177. }
  178. return null;
  179. }
  180. protected $_tableName = array(
  181. 'Accounts' => 'zfaccounts',
  182. 'Products' => 'zfproducts',
  183. 'Bugs' => 'zfbugs',
  184. 'BugsProducts' => 'zfbugs_products',
  185. 'noquote' => 'zfnoquote',
  186. 'noprimarykey' => 'zfnoprimarykey',
  187. 'Documents' => 'zfdocuments',
  188. 'Price' => 'zfprice',
  189. 'AltBugsProducts' => 'zfalt_bugs_products'
  190. );
  191. public function getTableName($tableId)
  192. {
  193. if (!isset($this->_tableName)) {
  194. throw new Exception("Invalid table id '$tableId'");
  195. }
  196. if (array_key_exists($tableId, $this->_tableName)) {
  197. return $this->_tableName[$tableId];
  198. } else {
  199. return $tableId;
  200. }
  201. }
  202. protected function _getColumnsBugs()
  203. {
  204. return array(
  205. 'bug_id' => 'IDENTITY',
  206. 'bug_description' => 'VARCHAR(100)',
  207. 'bug_status' => 'VARCHAR(20)',
  208. 'created_on' => 'DATETIME',
  209. 'updated_on' => 'DATETIME',
  210. 'reported_by' => 'VARCHAR(100)',
  211. 'assigned_to' => 'VARCHAR(100)',
  212. 'verified_by' => 'VARCHAR(100)'
  213. );
  214. }
  215. protected function _getColumnsAccounts()
  216. {
  217. return array(
  218. 'account_name' => 'VARCHAR(100) NOT NULL',
  219. 'PRIMARY KEY' => 'account_name'
  220. );
  221. }
  222. protected function _getColumnsProducts()
  223. {
  224. return array(
  225. 'product_id' => 'IDENTITY',
  226. 'product_name' => 'VARCHAR(100)'
  227. );
  228. }
  229. protected function _getColumnsBugsProducts()
  230. {
  231. return array(
  232. 'bug_id' => 'INTEGER NOT NULL',
  233. 'product_id' => 'INTEGER NOT NULL',
  234. 'PRIMARY KEY' => 'bug_id,product_id'
  235. );
  236. }
  237. protected function _getColumnsDocuments()
  238. {
  239. return array(
  240. 'doc_id' => 'INTEGER NOT NULL',
  241. 'doc_clob' => 'CLOB',
  242. 'doc_blob' => 'BLOB',
  243. 'PRIMARY KEY' => 'doc_id'
  244. );
  245. }
  246. protected function _getColumnsPrice()
  247. {
  248. return array(
  249. 'product_id' => 'INTEGER NOT NULL',
  250. 'price_name' => 'VARCHAR(100)',
  251. 'price_total' => 'DECIMAL(10,2) NOT NULL',
  252. 'PRIMARY KEY' => 'product_id'
  253. );
  254. }
  255. protected function _getDataAccounts()
  256. {
  257. return array(
  258. array('account_name' => 'mmouse'),
  259. array('account_name' => 'dduck'),
  260. array('account_name' => 'goofy'),
  261. );
  262. }
  263. protected function _getDataBugs()
  264. {
  265. return array(
  266. array(
  267. 'bug_description' => 'System needs electricity to run',
  268. 'bug_status' => 'NEW',
  269. 'created_on' => '2007-04-01',
  270. 'updated_on' => '2007-04-01',
  271. 'reported_by' => 'goofy',
  272. 'assigned_to' => 'mmouse',
  273. 'verified_by' => 'dduck'
  274. ),
  275. array(
  276. 'bug_description' => 'Implement Do What I Mean function',
  277. 'bug_status' => 'VERIFIED',
  278. 'created_on' => '2007-04-02',
  279. 'updated_on' => '2007-04-02',
  280. 'reported_by' => 'goofy',
  281. 'assigned_to' => 'mmouse',
  282. 'verified_by' => 'dduck'
  283. ),
  284. array(
  285. 'bug_description' => 'Where are my keys?',
  286. 'bug_status' => 'FIXED',
  287. 'created_on' => '2007-04-03',
  288. 'updated_on' => '2007-04-03',
  289. 'reported_by' => 'dduck',
  290. 'assigned_to' => 'mmouse',
  291. 'verified_by' => 'dduck'
  292. ),
  293. array(
  294. 'bug_description' => 'Bug no product',
  295. 'bug_status' => 'INCOMPLETE',
  296. 'created_on' => '2007-04-04',
  297. 'updated_on' => '2007-04-04',
  298. 'reported_by' => 'mmouse',
  299. 'assigned_to' => 'goofy',
  300. 'verified_by' => 'dduck'
  301. )
  302. );
  303. }
  304. protected function _getDataProducts()
  305. {
  306. return array(
  307. array('product_name' => 'Windows'),
  308. array('product_name' => 'Linux'),
  309. array('product_name' => 'OS X'),
  310. );
  311. }
  312. protected function _getDataBugsProducts()
  313. {
  314. return array(
  315. array(
  316. 'bug_id' => 1,
  317. 'product_id' => 1
  318. ),
  319. array(
  320. 'bug_id' => 1,
  321. 'product_id' => 2
  322. ),
  323. array(
  324. 'bug_id' => 1,
  325. 'product_id' => 3
  326. ),
  327. array(
  328. 'bug_id' => 2,
  329. 'product_id' => 3
  330. ),
  331. array(
  332. 'bug_id' => 3,
  333. 'product_id' => 2
  334. ),
  335. array(
  336. 'bug_id' => 3,
  337. 'product_id' => 3
  338. ),
  339. );
  340. }
  341. protected function _getDataDocuments()
  342. {
  343. return array (
  344. array(
  345. 'doc_id' => 1,
  346. 'doc_clob' => 'this is the clob that never ends...'.
  347. 'this is the clob that never ends...'.
  348. 'this is the clob that never ends...',
  349. 'doc_blob' => 'this is the blob that never ends...'.
  350. 'this is the blob that never ends...'.
  351. 'this is the blob that never ends...'
  352. )
  353. );
  354. }
  355. protected function _getDataPrice()
  356. {
  357. return array(
  358. array(
  359. 'product_id' => 1,
  360. 'price_name' => 'Price 1',
  361. 'price_total' => 200.45
  362. )
  363. );
  364. }
  365. public function populateTable($tableId)
  366. {
  367. $tableName = $this->getTableName($tableId);
  368. $data = $this->{'_getData'.$tableId}();
  369. foreach ($data as $row) {
  370. $sql = 'INSERT INTO ' . $this->getAdapter()->quoteIdentifier($tableName, true);
  371. $cols = array();
  372. $vals = array();
  373. foreach ($row as $col => $val) {
  374. $cols[] = $this->getAdapter()->quoteIdentifier($col, true);
  375. if ($val instanceof Zend_Db_Expr) {
  376. $vals[] = $val->__toString();
  377. } else {
  378. $vals[] = $this->getAdapter()->quote($val);
  379. }
  380. }
  381. $sql .= ' (' . implode(', ', $cols) . ')';
  382. $sql .= ' VALUES (' . implode(', ', $vals) . ')';
  383. $result = $this->_tryRawQuery($sql);
  384. if ($result === false) {
  385. throw new Zend_Db_Exception("Statement failed:\n$sql\nError: " . $this->getAdapter()->getConnection()->error);
  386. }
  387. }
  388. }
  389. protected function _getSqlCreateView($viewName)
  390. {
  391. return 'CREATE VIEW ' . $this->getAdapter()->quoteIdentifier($viewName, true);
  392. }
  393. protected function _getSqlDropView($viewName)
  394. {
  395. return 'DROP VIEW ' . $this->getAdapter()->quoteIdentifier($viewName, true);
  396. }
  397. public function createView()
  398. {
  399. $sql = $this->_getSqlCreateView('temp_view')
  400. . ' AS SELECT * FROM '
  401. . $this->getAdapter()->quoteIdentifier('zfbugs', true);
  402. $result = $this->_tryRawQuery($sql);
  403. if ($result === false) {
  404. throw new Zend_Db_Exception("Statement failed:\n$sql\nError: " . $this->getAdapter()->getConnection()->error);
  405. }
  406. }
  407. public function dropView()
  408. {
  409. $sql = $this->_getSqlDropView('temp_view');
  410. if (!$sql) {
  411. return;
  412. }
  413. $result = $this->_tryRawQuery($sql);
  414. if ($result === false) {
  415. throw new Zend_Db_Exception("Statement failed:\n$sql\nError: " . $this->getAdapter()->getConnection()->error);
  416. }
  417. }
  418. public function setUp(Zend_Db_Adapter_Abstract $db)
  419. {
  420. $this->setAdapter($db);
  421. $this->createTable('Accounts');
  422. $this->populateTable('Accounts');
  423. $this->createTable('Products');
  424. $this->populateTable('Products');
  425. $this->createTable('Bugs');
  426. $this->populateTable('Bugs');
  427. $this->createTable('BugsProducts');
  428. $this->populateTable('BugsProducts');
  429. $this->createTable('Documents');
  430. $this->populateTable('Documents');
  431. $this->createTable('Price');
  432. $this->populateTable('Price');
  433. $this->createView();
  434. }
  435. public function setAdapter(Zend_Db_Adapter_Abstract $db)
  436. {
  437. $this->_db = $db;
  438. }
  439. protected function getAdapter()
  440. {
  441. if($this->_db == null) {
  442. require_once "Zend/Db/Exception.php";
  443. throw new Zend_Db_Exception("No adapter was set in TestUtils.");
  444. }
  445. return $this->_db;
  446. }
  447. public function tearDown()
  448. {
  449. $this->dropView();
  450. $this->dropTable();
  451. $this->dropSequence();
  452. $this->getAdapter()->closeConnection();
  453. }
  454. protected function _tryRawQuery($sql)
  455. {
  456. if($this->_db == null) {
  457. require_once "Zend/Db/Exception.php";
  458. throw new Zend_Db_Exception("No database adapter set.");
  459. }
  460. $this->_rawQuery($sql);
  461. }
  462. protected abstract function _rawQuery($sql);
  463. }