Search in sources :

Example 26 with TableModel

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

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

the class DDLTest method testCompondPkey.

@Test
public void testCompondPkey() {
    // Compound PKEY
    TableModel t = new TableModel("testTable");
    t.column("i4").INTEGER().pkey().notNull().defaultValue("1");
    t.column("l5").LONG().pkey();
    t.column("s6").SHORT();
    printAllDialectsDDLs(t);
    testOnCurrentRealDatabase(t);
}
Also used : TableModel(com.github.drinkjava2.jdialects.model.TableModel) Test(org.junit.Test)

Example 28 with TableModel

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

the class DDLTest method tableCheckModel.

private static TableModel tableCheckModel() {
    // table check
    TableModel t = new TableModel("testTable");
    t.check("s2>10");
    t.column("s1").STRING(20).notNull();
    t.column("s2").STRING(20);
    return t;
}
Also used : TableModel(com.github.drinkjava2.jdialects.model.TableModel)

Example 29 with TableModel

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

the class Db2ModelsTest method testDb2Model.

@Test
public void testDb2Model() {
    TableModel t = new TableModel("testTable");
    t.column("id").LONG().pkey();
    t.column("b1").BOOLEAN();
    t.column("d2").DOUBLE();
    t.column("f3").FLOAT(5);
    t.column("i4").INTEGER();
    t.column("l5").LONG();
    t.column("s6").SHORT();
    t.column("b7").BIGDECIMAL(10, 2);
    t.column("s8").STRING(20);
    t.column("d9").DATE();
    t.column("t10").TIME();
    t.column("t11").TIMESTAMP();
    t.column("v12").VARCHAR(300);
    String[] ddls = guessedDialect.toDropDDL(t);
    quietExecuteDDLs(ddls);
    ddls = guessedDialect.toCreateDDL(t);
    executeDDLs(ddls);
    Connection con = null;
    TableModel[] tableModels = null;
    try {
        con = ds.getConnection();
        tableModels = TableModelUtils.db2Models(con, guessedDialect);
        for (TableModel tableModel : tableModels) {
            List<ColumnModel> columns = tableModel.getColumns();
            System.out.println(tableModel.getTableName());
            for (ColumnModel columnModel : columns) {
                System.out.print(columnModel.getColumnName() + ",");
                System.out.print(columnModel.getColumnType() + ",");
                System.out.print(columnModel.getLength() + ",");
                System.out.print(columnModel.getPrecision() + ",");
                System.out.print(columnModel.getScale() + "\r");
            }
            System.out.println();
        }
    } catch (SQLException e) {
        e.printStackTrace();
    } finally {
        try {
            if (con != null)
                con.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) ColumnModel(com.github.drinkjava2.jdialects.model.ColumnModel) TableModel(com.github.drinkjava2.jdialects.model.TableModel) Test(org.junit.Test)

Example 30 with TableModel

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

the class DDLCreateUtils method toCreateDDL.

/**
 * Transfer tables to DDL by given dialect and without format it, if want get a
 * formatted DDL, use DDLFormatter.format(DDLs) method to format it
 */
public static String[] toCreateDDL(Dialect dialect, TableModel... tables) {
    // Store mixed DDL String, TableGen Object, SequenceGen Object ...
    List<Object> objectResultList = new ArrayList<Object>();
    for (TableModel table : tables) transferTableToObjectList(dialect, table, objectResultList);
    boolean hasAutoIdGenerator = false;
    for (TableModel table : tables) {
        for (ColumnModel column : table.getColumns()) if (GenerationType.AUTO.equals(column.getIdGenerationType())) {
            hasAutoIdGenerator = true;
            break;
        }
        for (IdGenerator idGens : table.getIdGenerators()) if (hasAutoIdGenerator || idGens.dependOnAutoIdGenerator()) {
            hasAutoIdGenerator = true;
            break;
        }
    }
    List<String> stringResultList = new ArrayList<String>();
    List<TableIdGenerator> tbGeneratorList = new ArrayList<TableIdGenerator>();
    List<SequenceIdGenerator> sequenceList = new ArrayList<SequenceIdGenerator>();
    List<FKeyModel> fKeyConstraintList = new ArrayList<FKeyModel>();
    for (Object strOrObj : objectResultList) {
        if (!StrUtils.isEmpty(strOrObj)) {
            if (strOrObj instanceof String)
                stringResultList.add((String) strOrObj);
            else if (strOrObj instanceof TableIdGenerator)
                tbGeneratorList.add((TableIdGenerator) strOrObj);
            else if (strOrObj instanceof SequenceIdGenerator)
                sequenceList.add((SequenceIdGenerator) strOrObj);
            else if (strOrObj instanceof FKeyModel)
                fKeyConstraintList.add((FKeyModel) strOrObj);
        }
    }
    if (hasAutoIdGenerator) {
        IdGenerator realIdGen = AutoIdGenerator.INSTANCE.getSequenceOrTableIdGenerator(dialect);
        if (realIdGen instanceof TableIdGenerator)
            tbGeneratorList.add((TableIdGenerator) realIdGen);
        else if (realIdGen instanceof SequenceIdGenerator)
            sequenceList.add((SequenceIdGenerator) realIdGen);
        else
            throw new DialectException("Unknow exception happen for realIdGen, please report this bug");
    }
    buildSequenceDDL(dialect, stringResultList, sequenceList);
    buildTableGeneratorDDL(dialect, stringResultList, tbGeneratorList);
    outputFKeyConstraintDDL(dialect, stringResultList, fKeyConstraintList);
    String[] result = stringResultList.toArray(new String[stringResultList.size()]);
    if (Dialect.getGlobalAllowShowSql())
        Dialect.logger.info("Create DDL:\r" + StrUtils.arrayToString(result, "\r"));
    return result;
}
Also used : ArrayList(java.util.ArrayList) 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) TableIdGenerator(com.github.drinkjava2.jdialects.id.TableIdGenerator) SequenceIdGenerator(com.github.drinkjava2.jdialects.id.SequenceIdGenerator) ColumnModel(com.github.drinkjava2.jdialects.model.ColumnModel) TableModel(com.github.drinkjava2.jdialects.model.TableModel) FKeyModel(com.github.drinkjava2.jdialects.model.FKeyModel)

Aggregations

TableModel (com.github.drinkjava2.jdialects.model.TableModel)36 Test (org.junit.Test)19 ColumnModel (com.github.drinkjava2.jdialects.model.ColumnModel)11 AutoIdGenerator (com.github.drinkjava2.jdialects.id.AutoIdGenerator)9 IdGenerator (com.github.drinkjava2.jdialects.id.IdGenerator)9 SequenceIdGenerator (com.github.drinkjava2.jdialects.id.SequenceIdGenerator)5 TableIdGenerator (com.github.drinkjava2.jdialects.id.TableIdGenerator)5 FKeyModel (com.github.drinkjava2.jdialects.model.FKeyModel)5 IndexModel (com.github.drinkjava2.jdialects.model.IndexModel)3 UniqueModel (com.github.drinkjava2.jdialects.model.UniqueModel)3 SQLException (java.sql.SQLException)3 ArrayList (java.util.ArrayList)3 Dialect (com.github.drinkjava2.jdialects.Dialect)2 Connection (java.sql.Connection)2 Map (java.util.Map)2 SortedUUIDGenerator (com.github.drinkjava2.jdialects.id.SortedUUIDGenerator)1 Table (com.github.drinkjava2.jdialects.model.Table)1 HikariDataSource (com.zaxxer.hikari.HikariDataSource)1 BeanInfo (java.beans.BeanInfo)1 PropertyDescriptor (java.beans.PropertyDescriptor)1