Search in sources :

Example 1 with UniqueModel

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

the class TableTest method uniqueModel.

private static Table uniqueModel() {
    // unique
    Table t = new Table("testTable");
    t.column("s1").STRING(20).unique().notNull();
    t.column("s2").STRING(20).unique();
    t.column("s3").STRING(20).unique("uname1").notNull();
    t.column("s4").STRING(20).unique("uname2");
    return t;
}
Also used : Table(com.github.drinkjava2.jdialects.model.Table)

Example 2 with UniqueModel

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

the class DDLCreateUtils method transferTableToObjectList.

/**
 * Transfer table to a mixed DDL String or TableGen Object list
 */
/**
 * @param dialect
 * @param t
 * @param objectResultList
 */
private static void transferTableToObjectList(Dialect dialect, TableModel t, List<Object> objectResultList) {
    DDLFeatures features = dialect.ddlFeatures;
    StringBuilder buf = new StringBuilder();
    boolean hasPkey = false;
    String pkeys = "";
    String tableName = t.getTableName();
    List<ColumnModel> columns = t.getColumns();
    // Reserved words check
    dialect.checkNotEmptyReservedWords(tableName, "Table name can not be empty");
    // check index names
    List<IndexModel> idexChks = t.getIndexConsts();
    if (idexChks != null && !idexChks.isEmpty())
        for (IndexModel index : idexChks) dialect.checkReservedWords(index.getName());
    // check unique names
    List<UniqueModel> ukChks = t.getUniqueConsts();
    if (ukChks != null && !ukChks.isEmpty())
        for (UniqueModel unique : ukChks) dialect.checkReservedWords(unique.getName());
    // check Fkey names
    List<FKeyModel> fkeyChks = t.getFkeyConstraints();
    if (fkeyChks != null && !fkeyChks.isEmpty())
        for (FKeyModel fkey : fkeyChks) dialect.checkReservedWords(fkey.getFkeyName());
    for (// check column names
    ColumnModel col : // check column names
    columns) dialect.checkNotEmptyReservedWords(col.getColumnName(), "Column name can not be empty");
    // idGenerator
    for (IdGenerator idGen : t.getIdGenerators()) objectResultList.add(idGen);
    // Foreign key
    for (FKeyModel fkey : t.getFkeyConstraints()) objectResultList.add(fkey);
    // check and cache prime keys
    for (ColumnModel col : columns) {
        if (col.getTransientable())
            continue;
        if (col.getPkey()) {
            hasPkey = true;
            if (StrUtils.isEmpty(pkeys))
                pkeys = col.getColumnName();
            else
                pkeys += "," + col.getColumnName();
        }
    }
    // create table
    buf.append(hasPkey ? dialect.ddlFeatures.createTableString : dialect.ddlFeatures.createMultisetTableString).append(" ").append(tableName).append(" ( ");
    for (ColumnModel c : columns) {
        if (c.getTransientable())
            continue;
        if (c.getColumnType() == null)
            DialectException.throwEX("Type not set on column \"" + c.getColumnName() + "\" at table \"" + tableName + "\"");
        // column definition
        buf.append(c.getColumnName()).append(" ");
        // Identity
        if (GenerationType.IDENTITY.equals(c.getIdGenerationType()) && !features.supportsIdentityColumns)
            DialectException.throwEX("Unsupported identity setting for dialect \"" + dialect + "\" on column \"" + c.getColumnName() + "\" at table \"" + tableName + "\"");
        // Column type definition
        if (GenerationType.IDENTITY.equals(c.getIdGenerationType())) {
            if (features.hasDataTypeInIdentityColumn)
                buf.append(dialect.translateToDDLType(c.getColumnType(), c.getLengths()));
            buf.append(' ');
            if (Type.BIGINT.equals(c.getColumnType()))
                buf.append(features.identityColumnStringBigINT);
            else
                buf.append(features.identityColumnString);
        } else {
            buf.append(dialect.translateToDDLType(c.getColumnType(), c.getLengths()));
            // Default
            String defaultValue = c.getDefaultValue();
            if (defaultValue != null) {
                buf.append(" default ").append(defaultValue);
            }
            // Not null
            if (!c.getNullable())
                buf.append(" not null");
            else
                buf.append(features.nullColumnString);
        }
        // Check
        if (!StrUtils.isEmpty(c.getCheck())) {
            if (features.supportsColumnCheck)
                buf.append(" check (").append(c.getCheck()).append(")");
            else
                logger.warn("Ignore unsupported check setting for dialect \"" + dialect + "\" on column \"" + c.getColumnName() + "\" at table \"" + tableName + "\" with value: " + c.getCheck());
        }
        // Comments
        if (c.getComment() != null) {
            if (StrUtils.isEmpty(features.columnComment) && !features.supportsCommentOn)
                logger.warn("Ignore unsupported comment setting for dialect \"" + dialect + "\" on column \"" + c.getColumnName() + "\" at table \"" + tableName + "\" with value: " + c.getComment());
            else
                buf.append(StrUtils.replace(features.columnComment, "_COMMENT", c.getComment()));
        }
        // tail String
        if (!StrUtils.isEmpty(c.getTail()))
            buf.append(c.getTail());
        buf.append(",");
    }
    // PKEY
    if (!StrUtils.isEmpty(pkeys)) {
        buf.append(" primary key (").append(pkeys).append("),");
    }
    // Table Check
    if (!StrUtils.isEmpty(t.getCheck())) {
        if (features.supportsTableCheck)
            buf.append(" check (").append(t.getCheck()).append("),");
        else
            logger.warn("Ignore unsupported table check setting for dialect \"" + dialect + "\" on table \"" + tableName + "\" with value: " + t.getCheck());
    }
    buf.setLength(buf.length() - 1);
    buf.append(")");
    // Engine for MariaDB & MySql only, for example "engine=innoDB"
    String tableTypeString = features.tableTypeString;
    if (!StrUtils.isEmpty(tableTypeString) && !DDLFeatures.NOT_SUPPORT.equals(tableTypeString)) {
        buf.append(tableTypeString);
        // EngineTail, for example:" DEFAULT CHARSET=utf8"
        if (!StrUtils.isEmpty(t.getEngineTail()))
            buf.append(t.getEngineTail());
    }
    objectResultList.add(buf.toString());
    // table comment on
    if (t.getComment() != null) {
        if (features.supportsCommentOn)
            objectResultList.add("comment on table " + t.getTableName() + " is '" + t.getComment() + "'");
        else
            logger.warn("Ignore unsupported table comment setting for dialect \"" + dialect + "\" on table \"" + tableName + "\" with value: " + t.getComment());
    }
    // column comment on
    for (ColumnModel c : columns) {
        if (features.supportsCommentOn && c.getComment() != null && StrUtils.isEmpty(features.columnComment))
            objectResultList.add("comment on column " + tableName + '.' + c.getColumnName() + " is '" + c.getComment() + "'");
    }
    // index
    buildIndexDLL(dialect, objectResultList, t);
    // unique
    buildUniqueDLL(dialect, objectResultList, t);
}
Also used : TableIdGenerator(com.github.drinkjava2.jdialects.id.TableIdGenerator) AutoIdGenerator(com.github.drinkjava2.jdialects.id.AutoIdGenerator) SequenceIdGenerator(com.github.drinkjava2.jdialects.id.SequenceIdGenerator) IdGenerator(com.github.drinkjava2.jdialects.id.IdGenerator) IndexModel(com.github.drinkjava2.jdialects.model.IndexModel) UniqueModel(com.github.drinkjava2.jdialects.model.UniqueModel) ColumnModel(com.github.drinkjava2.jdialects.model.ColumnModel) FKeyModel(com.github.drinkjava2.jdialects.model.FKeyModel)

Example 3 with UniqueModel

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

the class DDLTest method uniqueModel.

private static TableModel uniqueModel() {
    // unique constraint
    TableModel t = new TableModel("testTable");
    t.column("s1").STRING(20).singleUnique();
    t.column("s2").STRING(20).notNull().singleUnique();
    t.column("s3").STRING(20).singleUnique("uname1");
    t.column("s4").STRING(20).notNull().singleUnique("uname2");
    t.column("s5").STRING(20).singleUnique("A");
    t.column("s6").STRING(20).singleUnique("A");
    t.column("s7").STRING(20).notNull().singleUnique("B");
    t.column("s8").STRING(20).notNull().singleUnique("B");
    t.column("s9").STRING(20).singleUnique("C");
    t.column("s10").STRING(20).singleUnique("D");
    t.column("s11").STRING(20).notNull().singleUnique("E");
    t.column("s12").STRING(20).notNull().singleUnique("F");
    t.unique().columns("S9", "S10");
    t.unique("uk1").columns("s11", "s12");
    t.unique().columns("s5");
    return t;
}
Also used : TableModel(com.github.drinkjava2.jdialects.model.TableModel)

Example 4 with UniqueModel

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

the class DDLDropUtils method transferTableToObjectList.

/**
 * Transfer table to a mixed DDL String or TableGen Object list
 */
private static void transferTableToObjectList(Dialect dialect, TableModel t, List<Object> objectResultList) {
    StringBuilder buf = new StringBuilder();
    String tableName = t.getTableName();
    List<ColumnModel> columns = t.getColumns();
    // Reserved words check
    dialect.checkNotEmptyReservedWords(tableName, "Table name can not be empty");
    // check index names
    List<IndexModel> l = t.getIndexConsts();
    if (l != null && !l.isEmpty())
        for (IndexModel index : l) dialect.checkReservedWords(index.getName());
    // check unique names
    List<UniqueModel> l2 = t.getUniqueConsts();
    if (l2 != null && !l2.isEmpty())
        for (UniqueModel unique : l2) dialect.checkReservedWords(unique.getName());
    // check Fkey names
    List<FKeyModel> fkeyChks = t.getFkeyConstraints();
    if (fkeyChks != null && !fkeyChks.isEmpty())
        for (FKeyModel fkey : fkeyChks) dialect.checkReservedWords(fkey.getFkeyName());
    for (ColumnModel col : columns) dialect.checkNotEmptyReservedWords(col.getColumnName(), "Column name can not be empty");
    // idGenerator
    for (IdGenerator idGen : t.getIdGenerators()) objectResultList.add(idGen);
    // Foreign key
    for (FKeyModel fkey : t.getFkeyConstraints()) objectResultList.add(fkey);
    // drop table
    buf.append(dialect.dropTableDDL(tableName));
    objectResultList.add(buf.toString());
}
Also used : ColumnModel(com.github.drinkjava2.jdialects.model.ColumnModel) TableIdGenerator(com.github.drinkjava2.jdialects.id.TableIdGenerator) AutoIdGenerator(com.github.drinkjava2.jdialects.id.AutoIdGenerator) SequenceIdGenerator(com.github.drinkjava2.jdialects.id.SequenceIdGenerator) IdGenerator(com.github.drinkjava2.jdialects.id.IdGenerator) IndexModel(com.github.drinkjava2.jdialects.model.IndexModel) UniqueModel(com.github.drinkjava2.jdialects.model.UniqueModel) FKeyModel(com.github.drinkjava2.jdialects.model.FKeyModel)

Example 5 with UniqueModel

use of com.github.drinkjava2.jdialects.model.UniqueModel 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)3 UniqueModel (com.github.drinkjava2.jdialects.model.UniqueModel)3 AutoIdGenerator (com.github.drinkjava2.jdialects.id.AutoIdGenerator)2 IdGenerator (com.github.drinkjava2.jdialects.id.IdGenerator)2 SequenceIdGenerator (com.github.drinkjava2.jdialects.id.SequenceIdGenerator)2 TableIdGenerator (com.github.drinkjava2.jdialects.id.TableIdGenerator)2 FKeyModel (com.github.drinkjava2.jdialects.model.FKeyModel)2 IndexModel (com.github.drinkjava2.jdialects.model.IndexModel)2 Table (com.github.drinkjava2.jdialects.model.Table)1 TableModel (com.github.drinkjava2.jdialects.model.TableModel)1