Search in sources :

Example 1 with IdGenerator

use of com.github.drinkjava2.jdialects.id.IdGenerator in project jDialects by drinkjava2.

the class DDLTest method sampleTest.

@Test
public void sampleTest() {
    // An example used to put on README.md
    TableModel t1 = new TableModel("customers");
    t1.column("name").STRING(20).pkey();
    t1.column("email").STRING(20).pkey().entityField("email").updatable(true).insertable(false);
    t1.column("address").VARCHAR(50).defaultValue("'Beijing'").comment("address comment");
    t1.column("phoneNumber").VARCHAR(50).singleIndex("IDX2");
    t1.column("age").INTEGER().notNull().check("'>0'");
    t1.index("idx3").columns("address", "phoneNumber").unique();
    TableModel t2 = new TableModel("orders").comment("order comment");
    t2.column("id").LONG().autoId().pkey();
    t2.column("name").STRING(20);
    t2.column("email").STRING(20);
    t2.column("name2").STRING(20).pkey().tail(" default 'Sam'");
    t2.column("email2").STRING(20);
    t2.fkey().columns("name2", "email2").refs("customers", "name", "email");
    t2.fkey("fk1").columns("name", "email").refs("customers", "name", "email");
    t2.unique("uk1").columns("name2", "email2");
    TableModel t3 = new TableModel("sampletable");
    t3.column("id").LONG().identityId().pkey();
    t3.tableGenerator("table_gen1", "tb1", "pkcol2", "valcol", "pkval", 1, 10);
    t3.column("id1").INTEGER().idGenerator("table_gen1");
    t3.sequenceGenerator("seq1", "seq_1", 1, 1);
    t3.column("id2").INTEGER().idGenerator("seq1");
    t3.engineTail(" DEFAULT CHARSET=utf8");
    String[] dropAndCreateDDL = Dialect.H2Dialect.toDropAndCreateDDL(t1, t2, t3);
    for (String ddl : dropAndCreateDDL) System.out.println(ddl);
    testOnCurrentRealDatabase(t1, t2, t3);
}
Also used : TableModel(com.github.drinkjava2.jdialects.model.TableModel) Test(org.junit.Test)

Example 2 with IdGenerator

use of com.github.drinkjava2.jdialects.id.IdGenerator in project jDialects by drinkjava2.

the class DDLTest method SequenceModel.

private static TableModel SequenceModel() {
    // SequenceGen
    TableModel t = new TableModel("testTable");
    t.sequenceGenerator("seq1", "seq_1", 1, 1);
    t.sequenceGenerator("seq2", "seq_2", 1, 2);
    t.column("i1").INTEGER().pkey().idGenerator("seq1");
    t.column("i2").INTEGER().pkey().idGenerator("seq2");
    return t;
}
Also used : TableModel(com.github.drinkjava2.jdialects.model.TableModel)

Example 3 with IdGenerator

use of com.github.drinkjava2.jdialects.id.IdGenerator 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 4 with IdGenerator

use of com.github.drinkjava2.jdialects.id.IdGenerator in project jDialects by drinkjava2.

the class DDLTest method tableGeneratorModel2.

private static TableModel tableGeneratorModel2() {
    // tableGenerator
    TableModel t = new TableModel("testTableGeneratorModel2");
    t.tableGenerator("tbgen1", "tb1", "pkcol", "valcol", "pkval", 1, 10);
    t.tableGenerator("tbgen2", "tb1", "pkcol2", "valcol", "pkval", 1, 10);
    t.tableGenerator("tbgen3", "tb1", "pkcol3", "valcol", "pkval", 1, 10);
    t.tableGenerator("tbgen4", "tb1", "pkcol3", "valcol", "pkval2", 1, 10);
    t.tableGenerator("tbgen5", "tb1", "pkcol4", "valcol", "pkval3", 1, 10);
    t.tableGenerator("tbgen6", "tb1", "pkcol4", "valcol", "pkval4", 1, 10);
    t.column("i1").INTEGER().pkey().idGenerator("tbgen1");
    t.column("i2").INTEGER().pkey().idGenerator("tbgen2");
    return t;
}
Also used : TableModel(com.github.drinkjava2.jdialects.model.TableModel)

Example 5 with IdGenerator

use of com.github.drinkjava2.jdialects.id.IdGenerator in project jDialects by drinkjava2.

the class DDLTest method tableGeneratorModel.

private static TableModel tableGeneratorModel() {
    // tableGenerator
    TableModel t = new TableModel("testTable");
    t.tableGenerator("tbgen1", "tb1", "pkcol", "valcol", "pkval", 1, 10);
    t.tableGenerator("tbgen2", "tb1", "pkcol2", "valcol", "pkval", 1, 10);
    t.column("i1").INTEGER().pkey().idGenerator("tbgen1");
    t.column("i2").INTEGER().pkey().idGenerator("tbgen2");
    return t;
}
Also used : TableModel(com.github.drinkjava2.jdialects.model.TableModel)

Aggregations

TableModel (com.github.drinkjava2.jdialects.model.TableModel)11 AutoIdGenerator (com.github.drinkjava2.jdialects.id.AutoIdGenerator)9 IdGenerator (com.github.drinkjava2.jdialects.id.IdGenerator)9 Test (org.junit.Test)6 SequenceIdGenerator (com.github.drinkjava2.jdialects.id.SequenceIdGenerator)4 TableIdGenerator (com.github.drinkjava2.jdialects.id.TableIdGenerator)4 ColumnModel (com.github.drinkjava2.jdialects.model.ColumnModel)4 FKeyModel (com.github.drinkjava2.jdialects.model.FKeyModel)4 IndexModel (com.github.drinkjava2.jdialects.model.IndexModel)2 UniqueModel (com.github.drinkjava2.jdialects.model.UniqueModel)2 ArrayList (java.util.ArrayList)2 SortedUUIDGenerator (com.github.drinkjava2.jdialects.id.SortedUUIDGenerator)1