From 9a929f1dc647a5a37d3933706dd1595f6a5c8c20 Mon Sep 17 00:00:00 2001 From: Heng Sin Low Date: Mon, 2 Jul 2007 03:32:51 +0000 Subject: [PATCH] * Fixed add column with default value failed - reported by Tim during testing of 2pack import of Libero. --- .../compiere/dbPort/Convert_PostgreSQL.java | 61 +++++++++++++++---- .../dbPort/Convert_PostgreSQLTest.java | 6 ++ 2 files changed, 55 insertions(+), 12 deletions(-) diff --git a/base/src/org/compiere/dbPort/Convert_PostgreSQL.java b/base/src/org/compiere/dbPort/Convert_PostgreSQL.java index 6d428a670a..89925327da 100644 --- a/base/src/org/compiere/dbPort/Convert_PostgreSQL.java +++ b/base/src/org/compiere/dbPort/Convert_PostgreSQL.java @@ -941,11 +941,56 @@ public class Convert_PostgreSQL extends Convert_SQL92 { // + 1)); String rest = sqlStatement.substring(end_col + 1); - if (action.equals(" ADD ")) - DDL = sqlStatement + if (action.equals(" ADD ")) { + if (rest.toUpperCase().indexOf(" DEFAULT ") != -1) { + String beforeDefault = rest.substring(0, rest.toUpperCase().indexOf(" DEFAULT ")); + 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(' ')); + } else { + rest = ""; + } + if (defaultvalue.equalsIgnoreCase("NULL")) { + DDL = sqlStatement.substring(0, begin_col + - action.length()) + + " ADD COLUMN " + + column + + " " + beforeDefault.trim() + + " DEFAULT " + + defaultvalue.trim() + " " + rest.trim(); + } else { + // Check if default value is already quoted, no need to double quote + if(defaultvalue.startsWith("'") && defaultvalue.endsWith("'")) + { + DDL = sqlStatement.substring(0, begin_col + - action.length()) + + " ADD COLUMN " + + column + + " " + beforeDefault.trim() + + " DEFAULT " + + defaultvalue.trim() + " " + rest.trim(); + } + else + { + DDL = sqlStatement.substring(0, begin_col + - action.length()) + + " ADD COLUMN " + + column + + " " + beforeDefault.trim() + + " DEFAULT '" + + defaultvalue.trim() + "' " + rest.trim(); + } + } + } else { + DDL = sqlStatement .substring(0, begin_col - action.length()) - + action + "COLUMN " + column + " " + rest; - else if (action.equals(" MODIFY ")) + + action + "COLUMN " + column + " " + rest.trim(); + } + } else if (action.equals(" MODIFY ")) { rest = rest.trim(); if (rest.toUpperCase().startsWith("NOT ") || rest.toUpperCase().startsWith("NULL ")) @@ -958,14 +1003,6 @@ public class Convert_PostgreSQL extends Convert_SQL92 { type = rest.substring(0, typeEnd).trim(); rest = rest.substring(typeEnd); } - /* - DDL = sqlStatement - .substring(0, begin_col - action.length()) - + " ALTER COLUMN " - + column - + " TYPE " - + type - + "; ";*/ if (rest.toUpperCase().indexOf(" DEFAULT ") != -1) { begin_default = rest.toUpperCase().indexOf( diff --git a/base/src/org/compiere/dbPort/Convert_PostgreSQLTest.java b/base/src/org/compiere/dbPort/Convert_PostgreSQLTest.java index 1c86efc02a..32352201bd 100644 --- a/base/src/org/compiere/dbPort/Convert_PostgreSQLTest.java +++ b/base/src/org/compiere/dbPort/Convert_PostgreSQLTest.java @@ -72,6 +72,12 @@ public final class Convert_PostgreSQLTest extends TestCase{ sqe = "insert into t_alter_column values('lpi_publication','AD_Client_ID','NUMERIC(10)',null,'NULL')"; r = convert.convert(sql); assertEquals(sqe, r[0]); + + //add column with default fail in libero 2pack import + sql = "ALTER TABLE XX ADD A VARCHAR2(10) DEFAULT --"; + sqe = "ALTER TABLE XX ADD COLUMN A VARCHAR(10) DEFAULT '--'"; + r = convert.convert(sql); + assertEquals(sqe, r[0].trim()); } // Convert.recoverQuotedStrings() error on strings with "<-->" - teo_sarca [ 1705768 ]