Search in sources :

Example 16 with Dialect

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

Example 17 with Dialect

use of com.github.drinkjava2.jdialects.Dialect in project jDialects by drinkjava2.

the class DDLCreateUtils method outputFKeyConstraintDDL.

private static void outputFKeyConstraintDDL(Dialect dialect, List<String> stringList, List<FKeyModel> trueList) {
    if (DDLFeatures.NOT_SUPPORT.equals(dialect.ddlFeatures.addForeignKeyConstraintString)) {
        logger.warn("Dialect \"" + dialect + "\" does not support foreign key setting, settings be ignored");
        return;
    }
    for (FKeyModel t : trueList) {
        if (!t.getDdl())
            // if ddl is false, skip
            continue;
        /*
			 * ADD CONSTRAINT _FKEYNAME FOREIGN KEY _FKEYNAME (_FK1, _FK2) REFERENCES
			 * _REFTABLE (_REF1, _REF2)
			 */
        String constName = t.getFkeyName();
        if (StrUtils.isEmpty(constName))
            constName = "fk_" + t.getTableName().toLowerCase() + "_" + StrUtils.replace(StrUtils.listToString(t.getColumnNames()), ",", "_");
        String[] refTableAndColumns = t.getRefTableAndColumns();
        DialectException.assureNotNull(refTableAndColumns);
        String fkeyTemplate;
        if (refTableAndColumns.length == 1)
            fkeyTemplate = dialect.ddlFeatures.addFKeyRefPkeyString;
        else
            fkeyTemplate = dialect.ddlFeatures.addForeignKeyConstraintString;
        fkeyTemplate = StrUtils.replace(fkeyTemplate, "_FK1, _FK2", StrUtils.listToString(t.getColumnNames()));
        fkeyTemplate = StrUtils.replace(fkeyTemplate, "_REF1, _REF2", StrUtils.arrayToStringButSkipFirst(t.getRefTableAndColumns()));
        fkeyTemplate = StrUtils.replace(fkeyTemplate, "_REFTABLE", t.getRefTableAndColumns()[0]);
        fkeyTemplate = StrUtils.replace(fkeyTemplate, "_FKEYNAME", constName);
        String tail = StrUtils.isEmpty(t.getFkeyTail()) ? "" : " " + t.getFkeyTail();
        stringList.add("alter table " + t.getTableName() + " " + fkeyTemplate + tail);
    }
}
Also used : FKeyModel(com.github.drinkjava2.jdialects.model.FKeyModel)

Example 18 with Dialect

use of com.github.drinkjava2.jdialects.Dialect in project jDialects by drinkjava2.

the class DDLCreateUtils method buildIndexDLL.

private static void buildIndexDLL(Dialect dialect, List<Object> objectResultList, TableModel t) {
    List<IndexModel> l = t.getIndexConsts();
    if (l == null || l.isEmpty())
        return;
    String template;
    if (Dialect.Teradata14Dialect.equals(dialect))
        template = "create $ifUnique index $indexName ($indexValues) on " + t.getTableName();
    else
        template = "create $ifUnique index $indexName on " + t.getTableName() + " ($indexValues)";
    for (IndexModel index : l) {
        String indexname = index.getName();
        if (StrUtils.isEmpty(indexname))
            indexname = "IX_" + t.getTableName() + "_" + StrUtils.arrayToString(index.getColumnList(), "_");
        String ifUnique = index.getUnique() ? "unique" : "";
        String result = StrUtils.replace(template, "$ifUnique", ifUnique);
        result = StrUtils.replace(result, "$indexName", indexname);
        result = StrUtils.replace(result, "$indexValues", StrUtils.arrayToString(index.getColumnList()));
        objectResultList.add(result);
    }
}
Also used : IndexModel(com.github.drinkjava2.jdialects.model.IndexModel)

Example 19 with Dialect

use of com.github.drinkjava2.jdialects.Dialect in project jDialects by drinkjava2.

the class DDLDropUtils method outputDropFKeyConstraintDDL.

private static void outputDropFKeyConstraintDDL(Dialect dialect, List<String> stringResultList, List<FKeyModel> trueList) {
    if (DDLFeatures.NOT_SUPPORT.equals(dialect.ddlFeatures.addForeignKeyConstraintString))
        return;
    for (FKeyModel t : trueList) {
        if (!t.getDdl())
            // if ddl is false, skip
            continue;
        String dropStr = dialect.ddlFeatures.dropForeignKeyString;
        String constName = t.getFkeyName();
        if (StrUtils.isEmpty(constName))
            constName = "fk_" + t.getTableName().toLowerCase() + "_" + StrUtils.replace(StrUtils.listToString(t.getColumnNames()), ",", "_");
        if (DDLFeatures.NOT_SUPPORT.equals(dropStr))
            DialectException.throwEX("Dialect \"" + dialect + "\" does not support drop foreign key, for setting: \"" + "fk_" + constName + "\"");
        stringResultList.add(0, "alter table " + t.getTableName() + " " + dropStr + " " + constName);
    }
}
Also used : FKeyModel(com.github.drinkjava2.jdialects.model.FKeyModel)

Example 20 with Dialect

use of com.github.drinkjava2.jdialects.Dialect in project jDialects by drinkjava2.

the class DDLDropUtils method buildDropTableGeneratorDDL.

private static void buildDropTableGeneratorDDL(Dialect dialect, List<String> stringResultList, List<TableIdGenerator> tbGeneratorList) {
    for (TableIdGenerator tg : tbGeneratorList) {
        // @formatter:off
        DialectException.assureNotEmpty(tg.getName(), "TableGen name can not be empty");
        DialectException.assureNotEmpty(tg.getTable(), "TableGen tableName can not be empty of \"" + tg.getName() + "\"");
        DialectException.assureNotEmpty(tg.getPkColumnName(), "TableGen pkColumnName can not be empty of \"" + tg.getName() + "\"");
        DialectException.assureNotEmpty(tg.getPkColumnValue(), "TableGen pkColumnValue can not be empty of \"" + tg.getName() + "\"");
        DialectException.assureNotEmpty(tg.getValueColumnName(), "TableGen valueColumnName can not be empty of \"" + tg.getName() + "\"");
    // @formatter:on
    }
    Set<String> tableExisted = new HashSet<String>();
    for (TableIdGenerator tg : tbGeneratorList) {
        String tableName = tg.getTable().toLowerCase();
        if (!tableExisted.contains(tableName)) {
            stringResultList.add(0, dialect.dropTableDDL(tableName));
            tableExisted.add(tableName);
        }
    }
}
Also used : TableIdGenerator(com.github.drinkjava2.jdialects.id.TableIdGenerator) HashSet(java.util.HashSet)

Aggregations

Dialect (com.github.drinkjava2.jdialects.Dialect)12 Test (org.junit.Test)10 DB2400Dialect (com.github.drinkjava2.jdialects.Dialect.DB2400Dialect)9 DB2Dialect (com.github.drinkjava2.jdialects.Dialect.DB2Dialect)9 DerbyDialect (com.github.drinkjava2.jdialects.Dialect.DerbyDialect)9 DerbyTenFiveDialect (com.github.drinkjava2.jdialects.Dialect.DerbyTenFiveDialect)9 DerbyTenSevenDialect (com.github.drinkjava2.jdialects.Dialect.DerbyTenSevenDialect)9 DerbyTenSixDialect (com.github.drinkjava2.jdialects.Dialect.DerbyTenSixDialect)9 H2Dialect (com.github.drinkjava2.jdialects.Dialect.H2Dialect)9 HSQLDialect (com.github.drinkjava2.jdialects.Dialect.HSQLDialect)9 InformixDialect (com.github.drinkjava2.jdialects.Dialect.InformixDialect)9 IngresDialect (com.github.drinkjava2.jdialects.Dialect.IngresDialect)9 Oracle10gDialect (com.github.drinkjava2.jdialects.Dialect.Oracle10gDialect)9 Oracle8iDialect (com.github.drinkjava2.jdialects.Dialect.Oracle8iDialect)9 Oracle9iDialect (com.github.drinkjava2.jdialects.Dialect.Oracle9iDialect)9 PostgreSQL81Dialect (com.github.drinkjava2.jdialects.Dialect.PostgreSQL81Dialect)9 PostgreSQL82Dialect (com.github.drinkjava2.jdialects.Dialect.PostgreSQL82Dialect)9 PostgreSQL9Dialect (com.github.drinkjava2.jdialects.Dialect.PostgreSQL9Dialect)9 PostgresPlusDialect (com.github.drinkjava2.jdialects.Dialect.PostgresPlusDialect)9 SQLServerDialect (com.github.drinkjava2.jdialects.Dialect.SQLServerDialect)9