Search in sources :

Example 11 with Column

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

the class DDLDropUtils method toDropDDL.

/**
 * Transfer tables to drop DDL and without format it
 */
public static String[] toDropDDL(Dialect dialect, TableModel... tables) {
    // resultList store mixed drop DDL + drop Ojbects
    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");
    }
    buildDropSequenceDDL(dialect, stringResultList, sequenceList);
    buildDropTableGeneratorDDL(dialect, stringResultList, tbGeneratorList);
    outputDropFKeyConstraintDDL(dialect, stringResultList, fKeyConstraintList);
    String[] result = stringResultList.toArray(new String[stringResultList.size()]);
    if (Dialect.getGlobalAllowShowSql())
        Dialect.logger.info("Drop 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)

Example 12 with Column

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

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

the class DebugUtils method getTableModelDebugInfo.

public static String getTableModelDebugInfo(TableModel model) {
    StringBuilder sb = new StringBuilder();
    sb.append("\rtableName=" + model.getTableName()).append("\r");
    sb.append("getEntityClass=" + model.getEntityClass()).append("\r");
    sb.append("getAlias=" + model.getAlias()).append("\r");
    sb.append(getFkeyDebugInfo(model));
    List<ColumnModel> columns = model.getColumns();
    for (ColumnModel column : columns) sb.append(getColumnModelDebugInfo(column)).append("\r");
    return sb.toString();
}
Also used : ColumnModel(com.github.drinkjava2.jdialects.model.ColumnModel)

Example 14 with Column

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

the class IdgeneratorTest method testSequenceIdGenerator.

@Test
public void testSequenceIdGenerator() {
    if (!guessedDialect.getDdlFeatures().supportBasicOrPooledSequence())
        return;
    TableModel table1 = new TableModel("testTableIdGenerator");
    table1.sequenceGenerator("seq1", "seq1", 1, 10);
    table1.column("id").STRING(30).pkey().idGenerator("seq1");
    table1.column("id2").STRING(30).pkey().sequenceGenerator("seq2", "seq2", 1, 20);
    TableModel table2 = new TableModel("testTableIdGenerator2");
    table2.sequenceGenerator("seq3", "seq3", 1, 10);
    table2.column("id").STRING(30).pkey().idGenerator("seq3");
    table2.column("id2").STRING(30).pkey().sequenceGenerator("seq2", "seq2", 1, 20);
    reBuildDB(table1, table2);
    IdGenerator gen1 = table1.getIdGenerator("seq1");
    IdGenerator gen2 = table1.getIdGenerator("seq2");
    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("seq3");
    IdGenerator gen4 = table2.getIdGenerator("seq2");
    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 15 with Column

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

Aggregations

TableModel (com.github.drinkjava2.jdialects.model.TableModel)11 Test (org.junit.Test)9 ColumnModel (com.github.drinkjava2.jdialects.model.ColumnModel)7 AutoIdGenerator (com.github.drinkjava2.jdialects.id.AutoIdGenerator)6 IdGenerator (com.github.drinkjava2.jdialects.id.IdGenerator)6 SequenceIdGenerator (com.github.drinkjava2.jdialects.id.SequenceIdGenerator)4 TableIdGenerator (com.github.drinkjava2.jdialects.id.TableIdGenerator)4 FKeyModel (com.github.drinkjava2.jdialects.model.FKeyModel)4 Table (com.github.drinkjava2.jdialects.model.Table)3 Column (com.github.drinkjava2.jdialects.model.Column)2 IndexModel (com.github.drinkjava2.jdialects.model.IndexModel)2 UniqueModel (com.github.drinkjava2.jdialects.model.UniqueModel)2 ArrayList (java.util.ArrayList)2 FKeyConstraint (com.github.drinkjava2.jdialects.model.FKeyConstraint)1 GlobalIdGenerator (com.github.drinkjava2.jdialects.model.GlobalIdGenerator)1 Sequence (com.github.drinkjava2.jdialects.model.Sequence)1 TableGenerator (com.github.drinkjava2.jdialects.model.TableGenerator)1 BeanInfo (java.beans.BeanInfo)1 PropertyDescriptor (java.beans.PropertyDescriptor)1 Annotation (java.lang.annotation.Annotation)1