Search in sources :

Example 11 with ColumnModel

use of com.github.drinkjava2.jdialects.model.ColumnModel in project jDialects by drinkjava2.

the class DDLCreateUtils method buildUniqueDLL.

private static void buildUniqueDLL(Dialect dialect, List<Object> objectResultList, TableModel t) {
    List<UniqueModel> l = t.getUniqueConsts();
    if (l == null || l.isEmpty())
        return;
    String dialectName = "" + dialect;
    for (UniqueModel unique : l) {
        boolean nullable = false;
        String[] columns = unique.getColumnList();
        for (String colNames : columns) {
            ColumnModel vc = t.getColumn(colNames.toLowerCase());
            if (vc != null && vc.getNullable())
                nullable = true;
        }
        String uniqueName = unique.getName();
        if (StrUtils.isEmpty(uniqueName))
            uniqueName = "UK_" + t.getTableName() + "_" + StrUtils.arrayToString(unique.getColumnList(), "_");
        String template = "alter table $TABLE add constraint $UKNAME unique ($COLUMNS)";
        if ((// DB2 and
        StrUtils.startsWithIgnoreCase(dialectName, "DB2") || // DERBY
        StrUtils.startsWithIgnoreCase(dialectName, "DERBY")) && nullable)
            template = "create unique index $UKNAME on $TABLE ($COLUMNS)";
        else if (StrUtils.startsWithIgnoreCase(dialectName, "Informix"))
            template = "alter table $TABLE add constraint unique ($COLUMNS) constraint $UKNAME";
        String result = StrUtils.replace(template, "$TABLE", t.getTableName());
        result = StrUtils.replace(result, "$UKNAME", uniqueName);
        result = StrUtils.replace(result, "$COLUMNS", StrUtils.arrayToString(unique.getColumnList()));
        objectResultList.add(result);
    }
}
Also used : ColumnModel(com.github.drinkjava2.jdialects.model.ColumnModel) UniqueModel(com.github.drinkjava2.jdialects.model.UniqueModel)

Aggregations

ColumnModel (com.github.drinkjava2.jdialects.model.ColumnModel)11 TableModel (com.github.drinkjava2.jdialects.model.TableModel)7 AutoIdGenerator (com.github.drinkjava2.jdialects.id.AutoIdGenerator)4 IdGenerator (com.github.drinkjava2.jdialects.id.IdGenerator)4 SequenceIdGenerator (com.github.drinkjava2.jdialects.id.SequenceIdGenerator)4 TableIdGenerator (com.github.drinkjava2.jdialects.id.TableIdGenerator)4 FKeyModel (com.github.drinkjava2.jdialects.model.FKeyModel)4 UniqueModel (com.github.drinkjava2.jdialects.model.UniqueModel)3 ArrayList (java.util.ArrayList)3 Test (org.junit.Test)3 IndexModel (com.github.drinkjava2.jdialects.model.IndexModel)2 SQLException (java.sql.SQLException)2 BeanInfo (java.beans.BeanInfo)1 PropertyDescriptor (java.beans.PropertyDescriptor)1 Annotation (java.lang.annotation.Annotation)1 Field (java.lang.reflect.Field)1 Connection (java.sql.Connection)1 DatabaseMetaData (java.sql.DatabaseMetaData)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1