Pgsql.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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-2010 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_TestUtil_Pdo_Common
  24. */
  25. require_once 'Zend/Db/TestUtil/Pdo/Common.php';
  26. PHPUnit_Util_Filter::addFileToFilter(__FILE__);
  27. /**
  28. * @category Zend
  29. * @package Zend_Db
  30. * @subpackage UnitTests
  31. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  32. * @license http://framework.zend.com/license/new-bsd New BSD License
  33. */
  34. class Zend_Db_TestUtil_Pdo_Pgsql extends Zend_Db_TestUtil_Pdo_Common
  35. {
  36. public function setUp(Zend_Db_Adapter_Abstract $db)
  37. {
  38. $this->_db = $db;
  39. $this->createSequence('zfproducts_seq');
  40. parent::setUp($db);
  41. }
  42. public function getParams(array $constants = array())
  43. {
  44. $constants = array (
  45. 'host' => 'TESTS_ZEND_DB_ADAPTER_PDO_PGSQL_HOSTNAME',
  46. 'username' => 'TESTS_ZEND_DB_ADAPTER_PDO_PGSQL_USERNAME',
  47. 'password' => 'TESTS_ZEND_DB_ADAPTER_PDO_PGSQL_PASSWORD',
  48. 'dbname' => 'TESTS_ZEND_DB_ADAPTER_PDO_PGSQL_DATABASE'
  49. );
  50. return parent::getParams($constants);
  51. }
  52. public function getSchema()
  53. {
  54. return 'public';
  55. }
  56. /**
  57. * For PostgreSQL, override the Products table to use an
  58. * explicit sequence-based column.
  59. */
  60. protected function _getColumnsProducts()
  61. {
  62. return array(
  63. 'product_id' => 'INT NOT NULL PRIMARY KEY',
  64. 'product_name' => 'VARCHAR(100)'
  65. );
  66. }
  67. protected function _getDataProducts()
  68. {
  69. $data = parent::_getDataProducts();
  70. foreach ($data as &$row) {
  71. $row['product_id'] = new Zend_Db_Expr('NEXTVAL('.$this->_db->quote('zfproducts_seq').')');
  72. }
  73. return $data;
  74. }
  75. public function getSqlType($type)
  76. {
  77. if ($type == 'IDENTITY') {
  78. return 'SERIAL PRIMARY KEY';
  79. }
  80. if ($type == 'DATETIME') {
  81. return 'TIMESTAMP';
  82. }
  83. if ($type == 'CLOB') {
  84. return 'TEXT';
  85. }
  86. if ($type == 'BLOB') {
  87. return 'TEXT';
  88. }
  89. return $type;
  90. }
  91. protected function _getSqlCreateTable($tableName)
  92. {
  93. $tableList = $this->_db->fetchCol('SELECT relname AS table_name FROM pg_class '
  94. . $this->_db->quoteInto(' WHERE relkind = \'r\' AND relname = ?', $tableName)
  95. );
  96. if (in_array($tableName, $tableList)) {
  97. return null;
  98. }
  99. return 'CREATE TABLE ' . $this->_db->quoteIdentifier($tableName);
  100. }
  101. protected function _getSqlDropTable($tableName)
  102. {
  103. $tableList = $this->_db->fetchCol('SELECT relname AS table_name FROM pg_class '
  104. . $this->_db->quoteInto(' WHERE relkind = \'r\' AND relname = ?', $tableName)
  105. );
  106. if (in_array($tableName, $tableList)) {
  107. return 'DROP TABLE ' . $this->_db->quoteIdentifier($tableName) . ' CASCADE';
  108. }
  109. return null;
  110. }
  111. protected function _getSqlCreateSequence($sequenceName)
  112. {
  113. $seqList = $this->_db->fetchCol('SELECT relname AS sequence_name FROM pg_class '
  114. . $this->_db->quoteInto(' WHERE relkind = \'S\' AND relname = ?', $sequenceName)
  115. );
  116. if (in_array($sequenceName, $seqList)) {
  117. return null;
  118. }
  119. return 'CREATE SEQUENCE ' . $this->_db->quoteIdentifier($sequenceName);
  120. }
  121. protected function _getSqlDropSequence($sequenceName)
  122. {
  123. $seqList = $this->_db->fetchCol('SELECT relname AS sequence_name FROM pg_class '
  124. . $this->_db->quoteInto(' WHERE relkind = \'S\' AND relname = ?', $sequenceName)
  125. );
  126. if (in_array($sequenceName, $seqList)) {
  127. return 'DROP SEQUENCE ' . $this->_db->quoteIdentifier($sequenceName);
  128. }
  129. return null;
  130. }
  131. }