From f68c37b45faeebdc78c0db727b478f9684535296 Mon Sep 17 00:00:00 2001 From: Heng Sin Low Date: Fri, 9 Mar 2007 17:18:37 +0000 Subject: [PATCH] * [ 1668720 ] Convert failing in alter table --- .../compiere/dbPort/Convert_PostgreSQL.java | 87 ++++++++++--------- .../dbPort/Convert_PostgreSQLTest.java | 12 +++ 2 files changed, 60 insertions(+), 39 deletions(-) diff --git a/dbPort/src/org/compiere/dbPort/Convert_PostgreSQL.java b/dbPort/src/org/compiere/dbPort/Convert_PostgreSQL.java index 1072aaba2a..89b3aba29a 100644 --- a/dbPort/src/org/compiere/dbPort/Convert_PostgreSQL.java +++ b/dbPort/src/org/compiere/dbPort/Convert_PostgreSQL.java @@ -1636,9 +1636,13 @@ public class Convert_PostgreSQL extends Convert_SQL92 { begin_col = sqlStatement.toUpperCase().indexOf(" MODIFY ") + action.length(); } else if (sqlStatement.toUpperCase().indexOf(" ADD ") > 0) { - action = " ADD "; - begin_col = sqlStatement.toUpperCase().indexOf(" ADD ") - + action.length(); + if (sqlStatement.toUpperCase().indexOf(" ADD CONSTRAINT ") < 0 && + sqlStatement.toUpperCase().indexOf(" ADD FOREIGN KEY " ) < 0 ) + { + action = " ADD "; + begin_col = sqlStatement.toUpperCase().indexOf(" ADD ") + + action.length(); + } } // System.out.println( "MODIFY :" + @@ -1667,15 +1671,20 @@ public class Convert_PostgreSQL extends Convert_SQL92 { // begin_col + "en column:" + end_col ); // System.out.println(" type " + sqlStatement.substring(end_col // + 1)); - type = sqlStatement.substring(end_col + 1) + " "; - // System.out.println(" type 1 :" + type); - type = type.substring(0, type.indexOf(' ')); - // System.out.println(" type:" + type); + String rest = sqlStatement.substring(end_col + 1); + if (action.equals(" ADD ")) DDL = sqlStatement .substring(0, begin_col - action.length()) - + action + "COLUMN " + column + " " + type + "; "; + + action + "COLUMN " + column + " " + rest; else if (action.equals(" MODIFY ")) + { + int typeEnd = rest.indexOf(' '); + // System.out.println(" type 1 :" + type); + type = rest.substring(0, typeEnd); + rest = rest.substring(typeEnd); + // System.out.println(" type:" + type); + DDL = sqlStatement .substring(0, begin_col - action.length()) + " ALTER COLUMN " @@ -1684,40 +1693,40 @@ public class Convert_PostgreSQL extends Convert_SQL92 { + type + "; "; - if (sqlStatement.toUpperCase().indexOf(" DEFAULT ") != -1) { - begin_default = sqlStatement.toUpperCase().indexOf( - " DEFAULT ") + 9; - defaultvalue = sqlStatement.substring(begin_default); - int nextspace = defaultvalue.indexOf(' '); - String rest = null; - if (nextspace > -1) { - rest = defaultvalue.substring(nextspace); - defaultvalue = defaultvalue.substring(0, defaultvalue.indexOf(' ')); + if (rest.toUpperCase().indexOf(" DEFAULT ") != -1) { + begin_default = rest.toUpperCase().indexOf( + " DEFAULT ") + 9; + defaultvalue = rest.substring(begin_default); + int nextspace = defaultvalue.indexOf(' '); + if (nextspace > -1) { + rest = defaultvalue.substring(nextspace); + defaultvalue = defaultvalue.substring(0, defaultvalue.indexOf(' ')); + } + + if (defaultvalue.equalsIgnoreCase("NULL")) { + DDL += sqlStatement.substring(0, begin_col + - action.length()) + + " ALTER COLUMN " + + column + + " SET DEFAULT " + + defaultvalue + "; "; + } else { + DDL += sqlStatement.substring(0, begin_col + - action.length()) + + " ALTER COLUMN " + + column + + " SET DEFAULT '" + + defaultvalue + "'; "; + } + if (rest != null && rest.indexOf(" NOT NULL ") == 0) + DDL += sqlStatement.substring(0, begin_col) + + " ALTER COLUMN " + column + " SET " + rest + + ";"; + // return DDL; } - if (defaultvalue.equalsIgnoreCase("NULL")) { - DDL += sqlStatement.substring(0, begin_col - - action.length()) - + " ALTER COLUMN " - + column - + " SET DEFAULT " - + defaultvalue + "; "; - } else { - DDL += sqlStatement.substring(0, begin_col - - action.length()) - + " ALTER COLUMN " - + column - + " SET DEFAULT '" - + defaultvalue + "'; "; - } - if (rest != null && rest.indexOf(" NOT NULL ") == 0) - DDL += sqlStatement.substring(0, begin_col) - + " ALTER COLUMN " + column + " SET " + rest - + ";"; - // return DDL; + // System.out.println("DDL" + DDL); } - - // System.out.println("DDL" + DDL); return DDL; } } diff --git a/dbPort/src/org/compiere/dbPort/Convert_PostgreSQLTest.java b/dbPort/src/org/compiere/dbPort/Convert_PostgreSQLTest.java index 7851c86fd6..dfbac6b08d 100644 --- a/dbPort/src/org/compiere/dbPort/Convert_PostgreSQLTest.java +++ b/dbPort/src/org/compiere/dbPort/Convert_PostgreSQLTest.java @@ -28,6 +28,18 @@ public final class Convert_PostgreSQLTest { String sqe; String[] r; + //[ 1668720 ] Convert failing in alter table + sql = "ALTER TABLE GT_TaxBase ADD CONSTRAINT GT_TaxBase_Key PRIMARY KEY (GT_TaxBase_ID)"; + sqe = "ALTER TABLE GT_TaxBase ADD CONSTRAINT GT_TaxBase_Key PRIMARY KEY (GT_TaxBase_ID)"; + r = convert.convert(sql); + verify(sql, r, sqe); + + //[ 1668720 ] Convert failing in alter table + sql = "ALTER TABLE GT_TaxBase ADD GT_TaxBase_ID NUMBER(10) NOT NULL"; + sqe = "ALTER TABLE GT_TaxBase ADD COLUMN GT_TaxBase_ID NUMERIC(10) NOT NULL"; + r = convert.convert(sql); + verify(sql, r, sqe); + //[ 1662983 ] Convert cutting backslash from string sql = "SELECT 'C:\\Documentos\\Test' FROM DUAL"; sqe = "SELECT E'C:\\\\Documentos\\\\Test'";