2
0

Common.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  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-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_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-2015 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. 'CascadeRecursive' => 'zfalt_cascade_recursive'
  191. );
  192. public function getTableName($tableId)
  193. {
  194. if (!isset($this->_tableName)) {
  195. throw new Exception("Invalid table id '$tableId'");
  196. }
  197. if (array_key_exists($tableId, $this->_tableName)) {
  198. return $this->_tableName[$tableId];
  199. } else {
  200. return $tableId;
  201. }
  202. }
  203. protected function _getColumnsBugs()
  204. {
  205. return array(
  206. 'bug_id' => 'IDENTITY',
  207. 'bug_description' => 'VARCHAR(100)',
  208. 'bug_status' => 'VARCHAR(20)',
  209. 'created_on' => 'DATETIME',
  210. 'updated_on' => 'DATETIME',
  211. 'reported_by' => 'VARCHAR(100)',
  212. 'assigned_to' => 'VARCHAR(100)',
  213. 'verified_by' => 'VARCHAR(100)'
  214. );
  215. }
  216. protected function _getColumnsAccounts()
  217. {
  218. return array(
  219. 'account_name' => 'VARCHAR(100) NOT NULL',
  220. 'PRIMARY KEY' => 'account_name'
  221. );
  222. }
  223. protected function _getColumnsProducts()
  224. {
  225. return array(
  226. 'product_id' => 'IDENTITY',
  227. 'product_name' => 'VARCHAR(100)'
  228. );
  229. }
  230. protected function _getColumnsBugsProducts()
  231. {
  232. return array(
  233. 'bug_id' => 'INTEGER NOT NULL',
  234. 'product_id' => 'INTEGER NOT NULL',
  235. 'PRIMARY KEY' => 'bug_id,product_id'
  236. );
  237. }
  238. protected function _getColumnsDocuments()
  239. {
  240. return array(
  241. 'doc_id' => 'INTEGER NOT NULL',
  242. 'doc_clob' => 'CLOB',
  243. 'doc_blob' => 'BLOB',
  244. 'PRIMARY KEY' => 'doc_id'
  245. );
  246. }
  247. protected function _getColumnsPrice()
  248. {
  249. return array(
  250. 'product_id' => 'INTEGER NOT NULL',
  251. 'price_name' => 'VARCHAR(100)',
  252. 'price_total' => 'DECIMAL(10,2) NOT NULL',
  253. 'PRIMARY KEY' => 'product_id'
  254. );
  255. }
  256. protected function _getColumnsCascadeRecursive()
  257. {
  258. return array(
  259. 'item_id' => 'INTEGER NOT NULL',
  260. 'item_parent' => 'INTEGER NULL',
  261. 'item_data' => 'VARCHAR(100)',
  262. 'PRIMARY KEY' => 'item_id'
  263. );
  264. }
  265. protected function _getDataAccounts()
  266. {
  267. return array(
  268. array('account_name' => 'mmouse'),
  269. array('account_name' => 'dduck'),
  270. array('account_name' => 'goofy'),
  271. );
  272. }
  273. protected function _getDataBugs()
  274. {
  275. return array(
  276. array(
  277. 'bug_description' => 'System needs electricity to run',
  278. 'bug_status' => 'NEW',
  279. 'created_on' => '2007-04-01',
  280. 'updated_on' => '2007-04-01',
  281. 'reported_by' => 'goofy',
  282. 'assigned_to' => 'mmouse',
  283. 'verified_by' => 'dduck'
  284. ),
  285. array(
  286. 'bug_description' => 'Implement Do What I Mean function',
  287. 'bug_status' => 'VERIFIED',
  288. 'created_on' => '2007-04-02',
  289. 'updated_on' => '2007-04-02',
  290. 'reported_by' => 'goofy',
  291. 'assigned_to' => 'mmouse',
  292. 'verified_by' => 'dduck'
  293. ),
  294. array(
  295. 'bug_description' => 'Where are my keys?',
  296. 'bug_status' => 'FIXED',
  297. 'created_on' => '2007-04-03',
  298. 'updated_on' => '2007-04-03',
  299. 'reported_by' => 'dduck',
  300. 'assigned_to' => 'mmouse',
  301. 'verified_by' => 'dduck'
  302. ),
  303. array(
  304. 'bug_description' => 'Bug no product',
  305. 'bug_status' => 'INCOMPLETE',
  306. 'created_on' => '2007-04-04',
  307. 'updated_on' => '2007-04-04',
  308. 'reported_by' => 'mmouse',
  309. 'assigned_to' => 'goofy',
  310. 'verified_by' => 'dduck'
  311. )
  312. );
  313. }
  314. protected function _getDataProducts()
  315. {
  316. return array(
  317. array('product_name' => 'Windows'),
  318. array('product_name' => 'Linux'),
  319. array('product_name' => 'OS X'),
  320. );
  321. }
  322. protected function _getDataBugsProducts()
  323. {
  324. return array(
  325. array(
  326. 'bug_id' => 1,
  327. 'product_id' => 1
  328. ),
  329. array(
  330. 'bug_id' => 1,
  331. 'product_id' => 2
  332. ),
  333. array(
  334. 'bug_id' => 1,
  335. 'product_id' => 3
  336. ),
  337. array(
  338. 'bug_id' => 2,
  339. 'product_id' => 3
  340. ),
  341. array(
  342. 'bug_id' => 3,
  343. 'product_id' => 2
  344. ),
  345. array(
  346. 'bug_id' => 3,
  347. 'product_id' => 3
  348. ),
  349. );
  350. }
  351. protected function _getDataDocuments()
  352. {
  353. return array (
  354. array(
  355. 'doc_id' => 1,
  356. 'doc_clob' => 'this is the clob that never ends...'.
  357. 'this is the clob that never ends...'.
  358. 'this is the clob that never ends...',
  359. 'doc_blob' => 'this is the blob that never ends...'.
  360. 'this is the blob that never ends...'.
  361. 'this is the blob that never ends...'
  362. )
  363. );
  364. }
  365. protected function _getDataPrice()
  366. {
  367. return array(
  368. array(
  369. 'product_id' => 1,
  370. 'price_name' => 'Price 1',
  371. 'price_total' => 200.45
  372. )
  373. );
  374. }
  375. protected function _getDataCascadeRecursive()
  376. {
  377. return array(
  378. array('item_id' => '1', 'item_parent' => new Zend_Db_Expr('NULL'), 'item_data' => '1'),
  379. array('item_id' => '2', 'item_parent' => '1', 'item_data' => '1.2'),
  380. array('item_id' => '3', 'item_parent' => '1', 'item_data' => '1.3'),
  381. array('item_id' => '4', 'item_parent' => '3', 'item_data' => '1.3.4'),
  382. array('item_id' => '5', 'item_parent' => '3', 'item_data' => '1.3.5'),
  383. array('item_id' => '6', 'item_parent' => new Zend_Db_Expr('NULL'), 'item_data' => '6')
  384. );
  385. }
  386. public function populateTable($tableId)
  387. {
  388. $tableName = $this->getTableName($tableId);
  389. $data = $this->{'_getData'.$tableId}();
  390. foreach ($data as $row) {
  391. $sql = 'INSERT INTO ' . $this->getAdapter()->quoteIdentifier($tableName, true);
  392. $cols = array();
  393. $vals = array();
  394. foreach ($row as $col => $val) {
  395. $cols[] = $this->getAdapter()->quoteIdentifier($col, true);
  396. if ($val instanceof Zend_Db_Expr) {
  397. $vals[] = $val->__toString();
  398. } else {
  399. $vals[] = $this->getAdapter()->quote($val);
  400. }
  401. }
  402. $sql .= ' (' . implode(', ', $cols) . ')';
  403. $sql .= ' VALUES (' . implode(', ', $vals) . ')';
  404. $result = $this->_tryRawQuery($sql);
  405. if ($result === false) {
  406. throw new Zend_Db_Exception("Statement failed:\n$sql\nError: " . $this->getAdapter()->getConnection()->error);
  407. }
  408. }
  409. }
  410. protected function _getSqlCreateView($viewName)
  411. {
  412. return 'CREATE VIEW ' . $this->getAdapter()->quoteIdentifier($viewName, true);
  413. }
  414. protected function _getSqlDropView($viewName)
  415. {
  416. return 'DROP VIEW ' . $this->getAdapter()->quoteIdentifier($viewName, true);
  417. }
  418. public function createView()
  419. {
  420. $sql = $this->_getSqlCreateView('temp_view')
  421. . ' AS SELECT * FROM '
  422. . $this->getAdapter()->quoteIdentifier('zfbugs', true);
  423. $result = $this->_tryRawQuery($sql);
  424. if ($result === false) {
  425. throw new Zend_Db_Exception("Statement failed:\n$sql\nError: " . $this->getAdapter()->getConnection()->error);
  426. }
  427. }
  428. public function dropView()
  429. {
  430. $sql = $this->_getSqlDropView('temp_view');
  431. if (!$sql) {
  432. return;
  433. }
  434. $result = $this->_tryRawQuery($sql);
  435. if ($result === false) {
  436. throw new Zend_Db_Exception("Statement failed:\n$sql\nError: " . $this->getAdapter()->getConnection()->error);
  437. }
  438. }
  439. public function setUp(Zend_Db_Adapter_Abstract $db)
  440. {
  441. $this->setAdapter($db);
  442. $this->createTable('Accounts');
  443. $this->populateTable('Accounts');
  444. $this->createTable('Products');
  445. $this->populateTable('Products');
  446. $this->createTable('Bugs');
  447. $this->populateTable('Bugs');
  448. $this->createTable('BugsProducts');
  449. $this->populateTable('BugsProducts');
  450. $this->createTable('Documents');
  451. $this->populateTable('Documents');
  452. $this->createTable('Price');
  453. $this->populateTable('Price');
  454. $this->createTable('CascadeRecursive');
  455. $this->populateTable('CascadeRecursive');
  456. $this->createView();
  457. }
  458. public function setAdapter(Zend_Db_Adapter_Abstract $db)
  459. {
  460. $this->_db = $db;
  461. }
  462. protected function getAdapter()
  463. {
  464. if($this->_db == null) {
  465. require_once "Zend/Db/Exception.php";
  466. throw new Zend_Db_Exception("No adapter was set in TestUtils.");
  467. }
  468. return $this->_db;
  469. }
  470. public function tearDown()
  471. {
  472. $this->dropView();
  473. $this->dropTable();
  474. $this->dropSequence();
  475. $this->getAdapter()->closeConnection();
  476. }
  477. protected function _tryRawQuery($sql)
  478. {
  479. if($this->_db == null) {
  480. require_once "Zend/Db/Exception.php";
  481. throw new Zend_Db_Exception("No database adapter set.");
  482. }
  483. $this->_rawQuery($sql);
  484. }
  485. protected abstract function _rawQuery($sql);
  486. }