Pgsql.php 4.3 KB

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