/src/ru/adv/db/adapter/Postgres83.java

http://mozart.googlecode.com/ · Java · 115 lines · 54 code · 31 blank · 30 comment · 4 complexity · 998d3883a29badc4128873dcf1a0e9ed MD5 · raw file

  1. /* =================================================================
  2. Copyright (C) 2009 ADV/web-engineering All rights reserved.
  3. This file is part of Mozart.
  4. Mozart is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. Mozart is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with Foobar. If not, see <http://www.gnu.org/licenses/>.
  14. Mozart
  15. http://www.mozartcms.ru
  16. ================================================================= */
  17. package ru.adv.db.adapter;
  18. import java.sql.SQLException;
  19. import java.util.ArrayList;
  20. import java.util.Collections;
  21. import java.util.List;
  22. import ru.adv.db.DBConnection;
  23. import ru.adv.db.config.ObjectAttr;
  24. public class Postgres83 extends Postgres80 {
  25. @Override
  26. public boolean isSupportConstraints() {
  27. return true;
  28. }
  29. @Override
  30. public List<String> getSetValueSequenceSql(String name, long value, int increment) {
  31. return Collections.singletonList(
  32. "ALTER SEQUENCE "+qi(name) + " INCREMENT BY " + increment + " RESTART WITH " + value
  33. );
  34. }
  35. @Override
  36. public String getCreateDatabaseSql(String name) {
  37. return "CREATE DATABASE " + qi(name) +" ENCODING='utf-8'";
  38. }
  39. @Override
  40. public List<String> getSystemStoredProcedureSql(String schema) {
  41. List<String> sqlList = new ArrayList<String>();
  42. for (String sql : super.getSystemStoredProcedureSql(schema)) {
  43. if ("text(bool)".equalsIgnoreCase( getStoredProcedureName(sql)) ) {
  44. // it's impossible change text(bool) function in this version
  45. // previous realisation casts boolean into '1'/'0'
  46. continue; //SKIP creation
  47. }
  48. if ("bool(text)".equalsIgnoreCase( getStoredProcedureName(sql)) ) {
  49. // there is perfect bool(text) funcation in this version
  50. continue; //SKIP creation
  51. }
  52. sqlList.add(sql);
  53. }
  54. return sqlList;
  55. }
  56. @Override
  57. protected void ceateNullabilityConstraint(DBConnection conn, ObjectAttr a) {
  58. conn.execute(String.format(
  59. "ALTER TABLE %1$s ALTER COLUMN %2$s %3$s NOT NULL",
  60. qi(a.getConfigObject().getRealName()), a.getSQLName(), ( a.isNullable() ? "DROP" : "SET" )
  61. ));
  62. }
  63. @Override
  64. public void dropConstraints(DBConnection conn, String schema, String tableName) throws SQLException {
  65. // select all constrain names for tabel
  66. List<Object[]> rows = conn.executeQuery(
  67. "SELECT pc.conname FROM pg_constraint pc, pg_class cl WHERE pc.conrelid = cl.oid AND pc.conname LIKE 'mz_%' AND cl.relname=?",
  68. new Object[]{registeredName(tableName)}
  69. );
  70. // drop all constrains
  71. for (Object[] row : rows) {
  72. conn.executeUpdate( String.format( "ALTER TABLE %1$s DROP CONSTRAINT %2$s", qi(tableName), qi(row[0].toString()) ));
  73. }
  74. }
  75. // @Override
  76. // public boolean isSupportForeignConstrain() {
  77. // return true;
  78. // }
  79. }