Search in sources :

Example 6 with TableGenerator

use of com.github.drinkjava2.jdialects.model.TableGenerator 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 7 with TableGenerator

use of com.github.drinkjava2.jdialects.model.TableGenerator 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)

Example 8 with TableGenerator

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

the class IdgeneratorTest method testTableIdGenerator.

@Test
public void testTableIdGenerator() {
    TableModel table1 = new TableModel("testTableIdGenerator");
    table1.tableGenerator("tab1", "tb1", "pkCol", "valueColname", "pkColVal", 1, 10);
    table1.column("id").STRING(30).pkey().idGenerator("tab1");
    table1.column("id2").STRING(30).pkey().tableGenerator("tab2", "tb1", "pkCol", "valueColname", "pkColVal", 1, 10);
    TableModel table2 = new TableModel("testTableIdGenerator2");
    table2.tableGenerator("tab3", "tb1", "pkCol", "valueColname", "pkColVal", 1, 10);
    table2.column("id").STRING(30).pkey().idGenerator("tab3");
    table2.column("id2").STRING(30).pkey().tableGenerator("tab2", "tb1", "pkCol", "valueColname", "pkColVal", 1, 10);
    reBuildDB(table1, table2);
    IdGenerator gen1 = table1.getIdGenerator("tab1");
    IdGenerator gen2 = table1.getIdGenerator("tab2");
    for (int i = 0; i < 3; i++) {
        System.out.println(gen1.getNextID(dbPro, guessedDialect, null));
        System.out.println(gen2.getNextID(dbPro, guessedDialect, null));
    }
    IdGenerator gen3 = table2.getIdGenerator("tab3");
    IdGenerator gen4 = table2.getIdGenerator("tab2");
    for (int i = 0; i < 3; i++) {
        System.out.println(gen3.getNextID(dbPro, guessedDialect, null));
        System.out.println(gen4.getNextID(dbPro, guessedDialect, null));
    }
}
Also used : AutoIdGenerator(com.github.drinkjava2.jdialects.id.AutoIdGenerator) IdGenerator(com.github.drinkjava2.jdialects.id.IdGenerator) TableModel(com.github.drinkjava2.jdialects.model.TableModel) Test(org.junit.Test)

Example 9 with TableGenerator

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

the class DDLUtils method transferTableToObjectList.

/**
	 * Transfer table to a mixed DDL String or TableGenerator Object list
	 */
private static void transferTableToObjectList(Dialect dialect, Table t, List<Object> objectResultList) {
    DDLFeatures features = dialect.ddlFeatures;
    StringBuilder buf = new StringBuilder();
    boolean hasPkey = false;
    String pkeys = "";
    String tableName = t.getTableName();
    Map<String, Column> columns = t.getColumns();
    // Reserved words check
    dialect.checkNotEmptyReservedWords(tableName, "Table name can not be empty");
    for (Column col : columns.values()) {
        dialect.checkNotEmptyReservedWords(col.getColumnName(), "Column name can not be empty");
        dialect.checkReservedWords(col.getPkeyName());
        dialect.checkReservedWords(col.getUniqueConstraintName());
    }
    for (Column col : columns.values()) {
        // autoGenerator, only support sequence or table for "Auto" type
        if (col.getAutoGenerator()) {
            if (features.supportsSequences || features.supportsPooledSequences) {
                objectResultList.add(new Sequence(GlobalIdGenerator.JDIALECTS_IDGEN_TABLE, GlobalIdGenerator.JDIALECTS_IDGEN_TABLE, 1, 1));
            } else {
                // GlobalIdGenerator
                objectResultList.add(new GlobalIdGenerator());
            }
        }
        // foreign keys
        if (!StrUtils.isEmpty(col.getFkeyReferenceTable()))
            objectResultList.add(new FKeyConstraint(tableName, col.getColumnName(), col.getFkeyReferenceTable(), col.getFkeyReferenceColumns()));
    }
    // sequence
    for (Sequence seq : t.getSequences().values()) objectResultList.add(seq);
    // tableGenerator
    for (TableGenerator tg : t.getTableGenerators().values()) objectResultList.add(tg);
    // check and cache prime keys
    for (Column col : columns.values()) {
        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 (Column c : columns.values()) {
        if (c.getColumnType() == null)
            DialectException.throwEX("Type not set on column \"" + c.getColumnName() + "\" at table \"" + tableName + "\"");
        // column definition
        buf.append(c.getColumnName()).append(" ");
        // Identity or autoGenerator+supportIdentity
        if (c.getIdentity() && !features.supportsIdentityColumns)
            DialectException.throwEX("Unsupported identity setting for dialect \"" + dialect + "\" on column \"" + c.getColumnName() + "\" at table \"" + tableName + "\"");
        if (c.getIdentity()) {
            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.getNotNull())
                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()));
        }
        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(")");
    // type or engine for MariaDB & MySql
    buf.append(dialect.engine());
    objectResultList.add(buf.toString());
    // unique constraint
    for (Column column : columns.values()) addUniqueConstraintDDL(objectResultList, dialect, tableName, column);
    // 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 (Column c : columns.values()) {
        if (features.supportsCommentOn && c.getComment() != null && StrUtils.isEmpty(features.columnComment))
            objectResultList.add("comment on column " + tableName + '.' + c.getColumnName() + " is '" + c.getComment() + "'");
    }
}
Also used : Column(com.github.drinkjava2.jdialects.model.Column) FKeyConstraint(com.github.drinkjava2.jdialects.model.FKeyConstraint) Sequence(com.github.drinkjava2.jdialects.model.Sequence) TableGenerator(com.github.drinkjava2.jdialects.model.TableGenerator) GlobalIdGenerator(com.github.drinkjava2.jdialects.model.GlobalIdGenerator)

Example 10 with TableGenerator

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

the class TableTest method tableGeneratorModel.

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

Aggregations

TableModel (com.github.drinkjava2.jdialects.model.TableModel)5 Table (com.github.drinkjava2.jdialects.model.Table)3 TableGenerator (com.github.drinkjava2.jdialects.model.TableGenerator)3 FKeyConstraint (com.github.drinkjava2.jdialects.model.FKeyConstraint)2 GlobalIdGenerator (com.github.drinkjava2.jdialects.model.GlobalIdGenerator)2 Sequence (com.github.drinkjava2.jdialects.model.Sequence)2 Test (org.junit.Test)2 AutoIdGenerator (com.github.drinkjava2.jdialects.id.AutoIdGenerator)1 IdGenerator (com.github.drinkjava2.jdialects.id.IdGenerator)1 Column (com.github.drinkjava2.jdialects.model.Column)1 ColumnModel (com.github.drinkjava2.jdialects.model.ColumnModel)1 BeanInfo (java.beans.BeanInfo)1 PropertyDescriptor (java.beans.PropertyDescriptor)1 Annotation (java.lang.annotation.Annotation)1 Field (java.lang.reflect.Field)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1