IDEMPIERE-4180 Add DDL and Data Type methods to AdempiereDatabase interface

This commit is contained in:
hengsin 2020-03-03 17:27:53 +08:00
parent 26e764c2a2
commit 4a436c5157
6 changed files with 480 additions and 283 deletions

View File

@ -25,6 +25,8 @@ import java.sql.Timestamp;
import javax.sql.DataSource;
import org.compiere.dbPort.Convert;
import org.compiere.model.MColumn;
import org.compiere.model.MTable;
import org.compiere.model.PO;
//import org.compiere.util.CPreparedStatement;
@ -301,14 +303,6 @@ public interface AdempiereDatabase
*/
public boolean isQueryTimeoutSupported();
/**
* Get Data Type
* @param DisplayType display type
* @return data type
*/
// public String getDataType (int displayType, int precision,
// boolean defaultValue)
/**
* Default sql use to test whether a connection is still valid
*/
@ -369,5 +363,103 @@ public interface AdempiereDatabase
public default boolean isNativeMode() {
return true;
}
/**
* @return numeric data type name
*/
public String getNumericDataType();
/**
*
* @return fixed lenght character data type name
*/
public String getCharacterDataType();
/**
*
* @return variable length character data type name
*/
public String getVarcharDataType();
/**
*
* @return binary large object data type name
*/
public String getBlobDataType();
/**
*
* @return character large object data type name
*/
public String getClobDataType();
/**
*
* @return time stamp data type name
*/
public String getTimestampDataType();
/**
* Get SQL Create
* @param table
* @return create table DDL
*/
public default String getSQLCreate(MTable table)
{
StringBuffer sb = new StringBuffer("CREATE TABLE ")
.append(table.getTableName()).append(" (");
//
StringBuffer constraints = new StringBuffer();
MColumn[] columns = table.getColumns(true);
boolean columnAdded = false;
for (int i = 0; i < columns.length; i++)
{
MColumn column = columns[i];
String colSQL = column.getSQLDDL();
if ( colSQL != null )
{
if (columnAdded)
sb.append(", ");
else
columnAdded = true;
sb.append(column.getSQLDDL());
}
else // virtual column
continue;
//
String constraint = column.getConstraint(table.getTableName());
if (constraint != null && constraint.length() > 0)
constraints.append(", ").append(constraint);
}
sb.append(constraints)
.append(")");
return sb.toString();
} // getSQLCreate
/**
*
* @param column
* @return ddl sql for column
*/
public String getSQLDDL(MColumn column);
/**
*
* @param table
* @param column
* @return add column sql
*/
public String getSQLAdd(MTable table, MColumn column);
/**
*
* @param table
* @param column
* @param setNullOption
* @return alter column sql
*/
public String getSQLModify (MTable table, MColumn column, boolean setNullOption);
} // AdempiereDatabase

View File

@ -32,6 +32,7 @@ import java.util.Properties;
import java.util.logging.Level;
import org.adempiere.exceptions.DBException;
import org.compiere.db.AdempiereDatabase;
import org.compiere.db.Database;
import org.compiere.util.CCache;
import org.compiere.util.CLogger;
@ -507,16 +508,11 @@ public class MColumn extends X_AD_Column
*/
public String getSQLAdd (MTable table)
{
StringBuilder sql = new StringBuilder ("ALTER TABLE ")
.append(table.getTableName())
.append(" ADD ").append(getSQLDDL());
String constraint = getConstraint(table.getTableName());
if (constraint != null && constraint.length() > 0) {
sql.append(DB.SQLSTATEMENT_SEPARATOR).append("ALTER TABLE ")
.append(table.getTableName())
.append(" ADD ").append(constraint);
}
return sql.toString();
AdempiereDatabase db = DB.getDatabase();
if (db.isNativeMode())
return db.getSQLAdd(table, this);
else
return Database.getDatabase(Database.DB_ORACLE).getSQLAdd(table, this);
} // getSQLAdd
/**
@ -528,44 +524,11 @@ public class MColumn extends X_AD_Column
if (isVirtualColumn())
return null;
StringBuilder sql = new StringBuilder ().append(getColumnName())
.append(" ").append(getSQLDataType());
// Default
String defaultValue = getDefaultValue();
if (defaultValue != null
&& defaultValue.length() > 0
&& defaultValue.indexOf('@') == -1 // no variables
&& ( ! (DisplayType.isID(getAD_Reference_ID()) && defaultValue.equals("-1") ) ) ) // not for ID's with default -1
{
if (DisplayType.isText(getAD_Reference_ID())
|| getAD_Reference_ID() == DisplayType.List
|| getAD_Reference_ID() == DisplayType.YesNo
// Two special columns: Defined as Table but DB Type is String
|| getColumnName().equals("EntityType") || getColumnName().equals("AD_Language")
|| (getAD_Reference_ID() == DisplayType.Button &&
!(getColumnName().endsWith("_ID"))))
{
if (!defaultValue.startsWith("'") && !defaultValue.endsWith("'"))
defaultValue = DB.TO_STRING(defaultValue);
}
sql.append(" DEFAULT ").append(defaultValue);
}
AdempiereDatabase db = DB.getDatabase();
if (db.isNativeMode())
return db.getSQLDDL(this);
else
{
if (! isMandatory())
sql.append(" DEFAULT NULL ");
defaultValue = null;
}
// Inline Constraint
if (getAD_Reference_ID() == DisplayType.YesNo)
sql.append(" CHECK (").append(getColumnName()).append(" IN ('Y','N'))");
// Null
if (isMandatory())
sql.append(" NOT NULL");
return sql.toString();
return Database.getDatabase(Database.DB_ORACLE).getSQLDDL(this);
} // getSQLDDL
/**
@ -576,66 +539,11 @@ public class MColumn extends X_AD_Column
*/
public String getSQLModify (MTable table, boolean setNullOption)
{
StringBuilder sql = new StringBuilder();
StringBuilder sqlBase = new StringBuilder ("ALTER TABLE ")
.append(table.getTableName())
.append(" MODIFY ").append(getColumnName());
// Default
StringBuilder sqlDefault = new StringBuilder(sqlBase)
.append(" ").append(getSQLDataType());
String defaultValue = getDefaultValue();
if (defaultValue != null
&& defaultValue.length() > 0
&& defaultValue.indexOf('@') == -1 // no variables
&& ( ! (DisplayType.isID(getAD_Reference_ID()) && defaultValue.equals("-1") ) ) ) // not for ID's with default -1
{
if (DisplayType.isText(getAD_Reference_ID())
|| getAD_Reference_ID() == DisplayType.List
|| getAD_Reference_ID() == DisplayType.YesNo
// Two special columns: Defined as Table but DB Type is String
|| getColumnName().equals("EntityType") || getColumnName().equals("AD_Language")
|| (getAD_Reference_ID() == DisplayType.Button &&
!(getColumnName().endsWith("_ID"))))
{
if (!defaultValue.startsWith("'") && !defaultValue.endsWith("'"))
defaultValue = DB.TO_STRING(defaultValue);
}
sqlDefault.append(" DEFAULT ").append(defaultValue);
}
AdempiereDatabase db = DB.getDatabase();
if (db.isNativeMode())
return db.getSQLModify(table, this, setNullOption);
else
{
if (! isMandatory())
sqlDefault.append(" DEFAULT NULL ");
defaultValue = null;
}
sql.append(sqlDefault);
// Constraint
// Null Values
if (isMandatory() && defaultValue != null && defaultValue.length() > 0)
{
StringBuilder sqlSet = new StringBuilder("UPDATE ")
.append(table.getTableName())
.append(" SET ").append(getColumnName())
.append("=").append(defaultValue)
.append(" WHERE ").append(getColumnName()).append(" IS NULL");
sql.append(DB.SQLSTATEMENT_SEPARATOR).append(sqlSet);
}
// Null
if (setNullOption)
{
StringBuilder sqlNull = new StringBuilder(sqlBase);
if (isMandatory())
sqlNull.append(" NOT NULL");
else
sqlNull.append(" NULL");
sql.append(DB.SQLSTATEMENT_SEPARATOR).append(sqlNull);
}
//
return sql.toString();
return Database.getDatabase(Database.DB_ORACLE).getSQLModify(table, this, setNullOption);
} // getSQLModify
/**

View File

@ -32,6 +32,7 @@ import java.util.logging.Level;
import org.adempiere.base.IModelFactory;
import org.adempiere.base.Service;
import org.adempiere.model.GenericPO;
import org.compiere.db.AdempiereDatabase;
import org.compiere.db.Database;
import org.compiere.util.CCache;
import org.compiere.util.CLogger;
@ -577,60 +578,11 @@ public class MTable extends X_AD_Table
*/
public String getSQLCreate()
{
StringBuffer sb = new StringBuffer("CREATE TABLE ")
.append(getTableName()).append(" (");
//
// boolean hasPK = false;
// boolean hasParents = false;
StringBuffer constraints = new StringBuffer();
getColumns(true);
boolean columnAdded = false;
for (int i = 0; i < m_columns.length; i++)
{
MColumn column = m_columns[i];
String colSQL = column.getSQLDDL();
if ( colSQL != null )
{
if (columnAdded)
sb.append(", ");
else
columnAdded = true;
sb.append(column.getSQLDDL());
}
else // virtual column
continue;
//
// if (column.isKey())
// hasPK = true;
// if (column.isParent())
// hasParents = true;
String constraint = column.getConstraint(getTableName());
if (constraint != null && constraint.length() > 0)
constraints.append(", ").append(constraint);
}
/* IDEMPIERE-1901 - deprecate code that create composite primary key
// Multi Column PK
if (!hasPK && hasParents)
{
StringBuffer cols = new StringBuffer();
for (int i = 0; i < m_columns.length; i++)
{
MColumn column = m_columns[i];
if (!column.isParent())
continue;
if (cols.length() > 0)
cols.append(", ");
cols.append(column.getColumnName());
}
sb.append(", CONSTRAINT ")
.append(getTableName()).append("_Key PRIMARY KEY (")
.append(cols).append(")");
}
*/
sb.append(constraints)
.append(")");
return sb.toString();
AdempiereDatabase db = DB.getDatabase();
if (db.isNativeMode())
return db.getSQLCreate(this);
else
return Database.getDatabase(Database.DB_ORACLE).getSQLCreate(this);
} // getSQLCreate
// globalqss

View File

@ -68,6 +68,8 @@ import java.util.logging.Level;
import org.adempiere.base.IDisplayTypeFactory;
import org.adempiere.base.Service;
import org.compiere.db.AdempiereDatabase;
import org.compiere.db.Database;
import org.compiere.model.MLanguage;
/**
@ -602,6 +604,15 @@ public final class DisplayType
return Object.class;
} // getClass
private static final AdempiereDatabase getDatabase()
{
AdempiereDatabase db = DB.getDatabase();
if (db.isNativeMode())
return db;
else
return Database.getDatabase(Database.DB_ORACLE);
}
/**
* Get SQL DataType
* @param displayType AD_Reference_ID
@ -613,51 +624,51 @@ public final class DisplayType
{
if (columnName.equals("EntityType")
|| columnName.equals ("AD_Language"))
return "VARCHAR2(" + fieldLength + ")";
return getDatabase().getVarcharDataType() + "(" + fieldLength + ")";
// ID
if (DisplayType.isID(displayType))
{
if (displayType == DisplayType.Image // FIXTHIS
&& columnName.equals("BinaryData"))
return "BLOB";
return getDatabase().getBlobDataType();
// ID, CreatedBy/UpdatedBy, Acct
else if (columnName.endsWith("_ID")
|| columnName.endsWith("tedBy")
|| columnName.endsWith("_Acct") )
return "NUMBER(10)";
return getDatabase().getNumericDataType()+"(10)";
else if (fieldLength < 4)
return "CHAR(" + fieldLength + ")";
return getDatabase().getCharacterDataType()+"(" + fieldLength + ")";
else // EntityType, AD_Language fallback
return "VARCHAR2(" + fieldLength + ")";
return getDatabase().getVarcharDataType()+"(" + fieldLength + ")";
}
//
if (displayType == DisplayType.Integer)
return "NUMBER(10)";
return getDatabase().getNumericDataType()+"(10)";
if (DisplayType.isDate(displayType))
return "DATE";
return getDatabase().getTimestampDataType();
if (DisplayType.isNumeric(displayType))
return "NUMBER";
return getDatabase().getNumericDataType();
if (displayType == DisplayType.Binary)
return "BLOB";
return getDatabase().getBlobDataType();
if (displayType == DisplayType.TextLong
|| (displayType == DisplayType.Text && fieldLength >= 4000))
return "CLOB";
return getDatabase().getClobDataType();
if (displayType == DisplayType.YesNo)
return "CHAR(1)";
return getDatabase().getCharacterDataType()+"(1)";
if (displayType == DisplayType.List || displayType == DisplayType.Payment) {
if (fieldLength == 1)
return "CHAR(" + fieldLength + ")";
return getDatabase().getCharacterDataType()+"(" + fieldLength + ")";
else
return "VARCHAR2(" + fieldLength + ")";
return getDatabase().getVarcharDataType()+"(" + fieldLength + ")";
}
if (displayType == DisplayType.Color)
return "VARCHAR2(" + fieldLength + ")";
return getDatabase().getVarcharDataType()+"(" + fieldLength + ")";
if (displayType == DisplayType.Button)
{
if (columnName.endsWith("_ID"))
return "NUMBER(10)";
return getDatabase().getNumericDataType()+"(10)";
else
return "CHAR(" + fieldLength + ")";
return getDatabase().getCharacterDataType()+"(" + fieldLength + ")";
}
List<IDisplayTypeFactory> factoryList = Service.locator().list(IDisplayTypeFactory.class).getServices();
@ -671,9 +682,9 @@ public final class DisplayType
s_log.severe("Unhandled Data Type = " + displayType);
if (columnName.endsWith("_ID"))
return "NUMBER(10)";
return getDatabase().getNumericDataType()+"(10)";
return "VARCHAR2(" + fieldLength + ")";
return getDatabase().getVarcharDataType()+"(" + fieldLength + ")";
} // getSQLDataType
/**

View File

@ -46,6 +46,8 @@ import org.adempiere.exceptions.DBException;
import org.compiere.Adempiere;
import org.compiere.dbPort.Convert;
import org.compiere.dbPort.Convert_Oracle;
import org.compiere.model.MColumn;
import org.compiere.model.MTable;
import org.compiere.model.PO;
import org.compiere.util.CLogger;
import org.compiere.util.DB;
@ -909,106 +911,12 @@ public class DB_Oracle implements AdempiereDatabase
* @param precision precision
* @param defaultValue if true adds default value
* @return data type
* @deprecated
*/
public String getDataType (String columnName, int displayType, int precision,
boolean defaultValue)
{
String retValue = null;
//handle special case, bug [ 1618423 ]
if (columnName != null)
{
if (displayType == DisplayType.Button
&& columnName.endsWith("_ID"))
{
retValue = "NUMBER(10)";
}
}
if (retValue == null)
{
switch (displayType)
{
// IDs
case DisplayType.Account:
case DisplayType.Assignment:
case DisplayType.ID:
case DisplayType.Location:
case DisplayType.Locator:
case DisplayType.PAttribute:
case DisplayType.Search:
case DisplayType.Table:
case DisplayType.TableDir:
case DisplayType.Image:
case DisplayType.Chart:
retValue = "NUMBER(10)";
break;
// Dynamic Precision
case DisplayType.Amount:
retValue = "NUMBER";
if (defaultValue)
retValue += " DEFAULT 0";
break;
case DisplayType.Binary:
retValue = "BLOB";
break;
case DisplayType.Button:
retValue = "CHAR(1)";
break;
// Number Dynamic Precision
case DisplayType.CostPrice:
retValue = "NUMBER";
if (defaultValue)
retValue += " DEFAULT 0";
break;
// Date
case DisplayType.Date:
case DisplayType.DateTime:
case DisplayType.Time:
retValue = "DATE";
if (defaultValue)
retValue += " DEFAULT getDate()";
break;
// Number(10)
case DisplayType.Integer:
retValue = "NUMBER(10)";
break;
case DisplayType.List:
retValue = "CHAR(" + precision + ")";
break;
// NVARCHAR
case DisplayType.Color:
case DisplayType.Memo:
case DisplayType.String:
case DisplayType.Text:
retValue = "NVARCHAR(" + precision + ")";
break;
case DisplayType.TextLong:
retValue = "CLOB";
break;
// Dyn Prec
case DisplayType.Quantity:
retValue = "NUMBER";
break;
case DisplayType.YesNo:
retValue = "CHAR(1)";
break;
default:
log.severe("Unknown: " + displayType);
break;
}
}
return retValue;
return DisplayType.getSQLDataType(displayType, columnName, precision);
} // getDataType
@ -1446,5 +1354,168 @@ public class DB_Oracle implements AdempiereDatabase
.append("') IS NOT EMPTY");
return builder.toString();
}
}
@Override
public String getNumericDataType() {
return "NUMBER";
}
@Override
public String getCharacterDataType() {
return "CHAR";
}
@Override
public String getVarcharDataType() {
return "VARCHAR2";
}
@Override
public String getBlobDataType() {
return "BLOB";
}
@Override
public String getClobDataType() {
return "CLOB";
}
@Override
public String getTimestampDataType() {
return "DATE";
}
@Override
public String getSQLDDL(MColumn column) {
StringBuilder sql = new StringBuilder ().append(column.getColumnName())
.append(" ").append(column.getSQLDataType());
// Default
String defaultValue = column.getDefaultValue();
if (defaultValue != null
&& defaultValue.length() > 0
&& defaultValue.indexOf('@') == -1 // no variables
&& ( ! (DisplayType.isID(column.getAD_Reference_ID()) && defaultValue.equals("-1") ) ) ) // not for ID's with default -1
{
if (DisplayType.isText(column.getAD_Reference_ID())
|| column.getAD_Reference_ID() == DisplayType.List
|| column.getAD_Reference_ID() == DisplayType.YesNo
// Two special columns: Defined as Table but DB Type is String
|| column.getColumnName().equals("EntityType") || column.getColumnName().equals("AD_Language")
|| (column.getAD_Reference_ID() == DisplayType.Button &&
!(column.getColumnName().endsWith("_ID"))))
{
if (!defaultValue.startsWith("'") && !defaultValue.endsWith("'"))
defaultValue = DB.TO_STRING(defaultValue);
}
sql.append(" DEFAULT ").append(defaultValue);
}
else
{
if (! column.isMandatory())
sql.append(" DEFAULT NULL ");
defaultValue = null;
}
// Inline Constraint
if (column.getAD_Reference_ID() == DisplayType.YesNo)
sql.append(" CHECK (").append(column.getColumnName()).append(" IN ('Y','N'))");
// Null
if (column.isMandatory())
sql.append(" NOT NULL");
return sql.toString();
}
/**
* Get SQL Add command
* @param table table
* @return sql
*/
@Override
public String getSQLAdd (MTable table, MColumn column)
{
StringBuilder sql = new StringBuilder ("ALTER TABLE ")
.append(table.getTableName())
.append(" ADD ").append(column.getSQLDDL());
String constraint = column.getConstraint(table.getTableName());
if (constraint != null && constraint.length() > 0) {
sql.append(DB.SQLSTATEMENT_SEPARATOR).append("ALTER TABLE ")
.append(table.getTableName())
.append(" ADD ").append(constraint);
}
return sql.toString();
} // getSQLAdd
/**
* Get SQL Modify command
* @param table table
* @param setNullOption generate null / not null statement
* @return sql separated by ;
*/
public String getSQLModify (MTable table, MColumn column, boolean setNullOption)
{
StringBuilder sql = new StringBuilder();
StringBuilder sqlBase = new StringBuilder ("ALTER TABLE ")
.append(table.getTableName())
.append(" MODIFY ").append(column.getColumnName());
// Default
StringBuilder sqlDefault = new StringBuilder(sqlBase)
.append(" ").append(column.getSQLDataType());
String defaultValue = column.getDefaultValue();
if (defaultValue != null
&& defaultValue.length() > 0
&& defaultValue.indexOf('@') == -1 // no variables
&& ( ! (DisplayType.isID(column.getAD_Reference_ID()) && defaultValue.equals("-1") ) ) ) // not for ID's with default -1
{
if (DisplayType.isText(column.getAD_Reference_ID())
|| column.getAD_Reference_ID() == DisplayType.List
|| column.getAD_Reference_ID() == DisplayType.YesNo
// Two special columns: Defined as Table but DB Type is String
|| column.getColumnName().equals("EntityType") || column.getColumnName().equals("AD_Language")
|| (column.getAD_Reference_ID() == DisplayType.Button &&
!(column.getColumnName().endsWith("_ID"))))
{
if (!defaultValue.startsWith("'") && !defaultValue.endsWith("'"))
defaultValue = DB.TO_STRING(defaultValue);
}
sqlDefault.append(" DEFAULT ").append(defaultValue);
}
else
{
if (! column.isMandatory())
sqlDefault.append(" DEFAULT NULL ");
defaultValue = null;
}
sql.append(sqlDefault);
// Constraint
// Null Values
if (column.isMandatory() && defaultValue != null && defaultValue.length() > 0)
{
StringBuilder sqlSet = new StringBuilder("UPDATE ")
.append(table.getTableName())
.append(" SET ").append(column.getColumnName())
.append("=").append(defaultValue)
.append(" WHERE ").append(column.getColumnName()).append(" IS NULL");
sql.append(DB.SQLSTATEMENT_SEPARATOR).append(sqlSet);
}
// Null
if (setNullOption)
{
StringBuilder sqlNull = new StringBuilder(sqlBase);
if (column.isMandatory())
sqlNull.append(" NOT NULL");
else
sqlNull.append(" NULL");
sql.append(DB.SQLSTATEMENT_SEPARATOR).append(sqlNull);
}
//
return sql.toString();
} // getSQLModify
} // DB_Oracle

View File

@ -49,6 +49,8 @@ import org.adempiere.db.postgresql.PostgreSQLBundleActivator;
import org.adempiere.exceptions.DBException;
import org.compiere.dbPort.Convert;
import org.compiere.dbPort.Convert_PostgreSQL;
import org.compiere.model.MColumn;
import org.compiere.model.MTable;
import org.compiere.model.PO;
import org.compiere.util.CCache;
import org.compiere.util.CLogger;
@ -1247,4 +1249,165 @@ public class DB_PostgreSQL implements AdempiereDatabase
return statement.replace(DB_PostgreSQL.NATIVE_MARKER, "");
}
@Override
public String getNumericDataType() {
return "NUMERIC";
}
@Override
public String getCharacterDataType() {
return "CHAR";
}
@Override
public String getVarcharDataType() {
return "VARCHAR";
}
@Override
public String getBlobDataType() {
return "BYTEA";
}
@Override
public String getClobDataType() {
return "TEXT";
}
@Override
public String getTimestampDataType() {
return "TIMESTAMP";
}
@Override
public String getSQLDDL(MColumn column) {
StringBuilder sql = new StringBuilder ().append(column.getColumnName())
.append(" ").append(column.getSQLDataType());
// Null
if (column.isMandatory())
sql.append(" NOT NULL");
// Inline Constraint
if (column.getAD_Reference_ID() == DisplayType.YesNo)
sql.append(" CHECK (").append(column.getColumnName()).append(" IN ('Y','N'))");
// Default
String defaultValue = column.getDefaultValue();
if (defaultValue != null
&& defaultValue.length() > 0
&& defaultValue.indexOf('@') == -1 // no variables
&& ( ! (DisplayType.isID(column.getAD_Reference_ID()) && defaultValue.equals("-1") ) ) ) // not for ID's with default -1
{
if (DisplayType.isText(column.getAD_Reference_ID())
|| column.getAD_Reference_ID() == DisplayType.List
|| column.getAD_Reference_ID() == DisplayType.YesNo
// Two special columns: Defined as Table but DB Type is String
|| column.getColumnName().equals("EntityType") || column.getColumnName().equals("AD_Language")
|| (column.getAD_Reference_ID() == DisplayType.Button &&
!(column.getColumnName().endsWith("_ID"))))
{
if (!defaultValue.startsWith("'") && !defaultValue.endsWith("'"))
defaultValue = DB.TO_STRING(defaultValue);
}
if (defaultValue.equalsIgnoreCase("sysdate"))
defaultValue = "getDate()";
sql.append(" DEFAULT ").append(defaultValue);
}
else
{
if (! column.isMandatory())
sql.append(" DEFAULT NULL ");
defaultValue = null;
}
return sql.toString();
}
/**
* Get SQL Add command
* @param table table
* @return sql
*/
@Override
public String getSQLAdd (MTable table, MColumn column)
{
StringBuilder sql = new StringBuilder ("ALTER TABLE ")
.append(table.getTableName())
.append(" ADD COLUMN ").append(column.getSQLDDL());
String constraint = column.getConstraint(table.getTableName());
if (constraint != null && constraint.length() > 0) {
sql.append(DB.SQLSTATEMENT_SEPARATOR).append("ALTER TABLE ")
.append(table.getTableName())
.append(" ADD ").append(constraint);
}
return sql.toString();
} // getSQLAdd
/**
* Get SQL Modify command
* @param table table
* @param setNullOption generate null / not null statement
* @return sql separated by ;
*/
public String getSQLModify (MTable table, MColumn column, boolean setNullOption)
{
StringBuilder sql = new StringBuilder ("INSERT INTO t_alter_column values('")
.append(table.getTableName())
.append("','").append(quoteColumnName(column.getColumnName()))
.append("','")
.append(column.getSQLDataType())
.append("',");
// Null
if (setNullOption)
{
if (column.isMandatory())
sql.append("'NOT NULL',");
else
sql.append("'NULL',");
}
else
{
sql.append("null,");
}
// Default
String defaultValue = column.getDefaultValue();
if (defaultValue != null
&& defaultValue.length() > 0
&& defaultValue.indexOf('@') == -1 // no variables
&& ( ! (DisplayType.isID(column.getAD_Reference_ID()) && defaultValue.equals("-1") ) ) ) // not for ID's with default -1
{
if (defaultValue.equalsIgnoreCase("sysdate"))
defaultValue = "getDate()";
if (!defaultValue.startsWith("'") && !defaultValue.endsWith("'"))
defaultValue = "'" + defaultValue + "'";
sql.append(defaultValue);
}
else
{
sql.append("null");
}
sql.append(")");
// Null Values
if (column.isMandatory() && defaultValue != null && defaultValue.length() > 0)
{
StringBuilder sqlSet = new StringBuilder("UPDATE ")
.append(table.getTableName())
.append(" SET ").append(quoteColumnName(column.getColumnName()))
.append("=").append(defaultValue)
.append(" WHERE ").append(quoteColumnName(column.getColumnName())).append(" IS NULL");
sql.append(DB.SQLSTATEMENT_SEPARATOR).append(sqlSet);
}
//
return sql.toString();
} // getSQLModify
} // DB_PostgreSQL