Search in sources :

Example 21 with Dialect

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

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

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

the class DDLDropUtils method buildDropSequenceDDL.

private static void buildDropSequenceDDL(Dialect dialect, List<String> stringResultList, List<SequenceIdGenerator> sequenceList) {
    Set<SequenceIdGenerator> notRepeatedSequences = new HashSet<SequenceIdGenerator>();
    for (SequenceIdGenerator seq : sequenceList) DDLCreateUtils.checkAndInsertToNotRepeatSeq(notRepeatedSequences, seq);
    DDLFeatures features = dialect.ddlFeatures;
    for (SequenceIdGenerator seq : notRepeatedSequences) {
        if (!features.supportBasicOrPooledSequence()) {
            DialectException.throwEX("Dialect \"" + dialect + "\" does not support sequence setting on sequence \"" + seq.getName() + "\"");
        }
        if (!DDLFeatures.NOT_SUPPORT.equals(features.dropSequenceStrings) && !StrUtils.isEmpty(features.dropSequenceStrings)) {
            stringResultList.add(0, StrUtils.replace(features.dropSequenceStrings, "_SEQNAME", seq.getSequenceName()));
        } else
            DialectException.throwEX("Dialect \"" + dialect + "\" does not support drop sequence ddl, on sequence \"" + seq.getName() + "\"");
    }
}
Also used : SequenceIdGenerator(com.github.drinkjava2.jdialects.id.SequenceIdGenerator) HashSet(java.util.HashSet)

Example 24 with Dialect

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

the class DDLCreateUtils method buildTableGeneratorDDL.

private static void buildTableGeneratorDDL(Dialect dialect, List<String> stringList, List<TableIdGenerator> tbGeneratorList) {
    Set<TableIdGenerator> notRepeatedTab = new HashSet<TableIdGenerator>();
    for (TableIdGenerator tab : tbGeneratorList) checkAndInsertToNotRepeatTable(notRepeatedTab, tab);
    Set<String> tableExisted = new HashSet<String>();
    Set<String> columnExisted = new HashSet<String>();
    for (TableIdGenerator tg : tbGeneratorList) if (tg.getAllocationSize() != 0) {
        String tableName = tg.getTable().toLowerCase();
        String tableAndPKColumn = tg.getTable().toLowerCase() + "..XXOO.." + tg.getPkColumnName();
        String tableAndValColumn = tg.getTable().toLowerCase() + "..XXOO.." + tg.getValueColumnName();
        if (!tableExisted.contains(tableName)) {
            String s = dialect.ddlFeatures.createTableString + " " + tableName + " (";
            s += tg.getPkColumnName() + " " + dialect.translateToDDLType(Type.VARCHAR, 100) + ",";
            s += tg.getValueColumnName() + " " + dialect.translateToDDLType(Type.BIGINT) + " )";
            stringList.add(s);
            tableExisted.add(tableName);
            columnExisted.add(tableAndPKColumn);
            columnExisted.add(tableAndValColumn);
        } else {
            if (!columnExisted.contains(tableAndPKColumn)) {
                stringList.add("alter table " + tableName + " " + dialect.ddlFeatures.addColumnString + " " + tg.getPkColumnName() + " " + dialect.translateToDDLType(Type.VARCHAR, 100) + " " + dialect.ddlFeatures.addColumnSuffixString);
                columnExisted.add(tableAndPKColumn);
            }
            if (!columnExisted.contains(tableAndValColumn)) {
                stringList.add("alter table " + tableName + " " + dialect.ddlFeatures.addColumnString + " " + tg.getValueColumnName() + " " + dialect.translateToDDLType(Type.VARCHAR, 100) + " " + dialect.ddlFeatures.addColumnSuffixString);
                columnExisted.add(tableAndValColumn);
            }
        }
    }
}
Also used : TableIdGenerator(com.github.drinkjava2.jdialects.id.TableIdGenerator) HashSet(java.util.HashSet)

Example 25 with Dialect

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

the class DDLCreateUtils method buildUniqueDLL.

private static void buildUniqueDLL(Dialect dialect, List<Object> objectResultList, TableModel t) {
    List<UniqueModel> l = t.getUniqueConsts();
    if (l == null || l.isEmpty())
        return;
    String dialectName = "" + dialect;
    for (UniqueModel unique : l) {
        boolean nullable = false;
        String[] columns = unique.getColumnList();
        for (String colNames : columns) {
            ColumnModel vc = t.getColumn(colNames.toLowerCase());
            if (vc != null && vc.getNullable())
                nullable = true;
        }
        String uniqueName = unique.getName();
        if (StrUtils.isEmpty(uniqueName))
            uniqueName = "UK_" + t.getTableName() + "_" + StrUtils.arrayToString(unique.getColumnList(), "_");
        String template = "alter table $TABLE add constraint $UKNAME unique ($COLUMNS)";
        if ((// DB2 and
        StrUtils.startsWithIgnoreCase(dialectName, "DB2") || // DERBY
        StrUtils.startsWithIgnoreCase(dialectName, "DERBY")) && nullable)
            template = "create unique index $UKNAME on $TABLE ($COLUMNS)";
        else if (StrUtils.startsWithIgnoreCase(dialectName, "Informix"))
            template = "alter table $TABLE add constraint unique ($COLUMNS) constraint $UKNAME";
        String result = StrUtils.replace(template, "$TABLE", t.getTableName());
        result = StrUtils.replace(result, "$UKNAME", uniqueName);
        result = StrUtils.replace(result, "$COLUMNS", StrUtils.arrayToString(unique.getColumnList()));
        objectResultList.add(result);
    }
}
Also used : ColumnModel(com.github.drinkjava2.jdialects.model.ColumnModel) UniqueModel(com.github.drinkjava2.jdialects.model.UniqueModel)

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