Search in sources :

Example 1 with TableIdGenerator

use of com.github.drinkjava2.jdialects.id.TableIdGenerator 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 2 with TableIdGenerator

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

Example 3 with TableIdGenerator

use of com.github.drinkjava2.jdialects.id.TableIdGenerator 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 4 with TableIdGenerator

use of com.github.drinkjava2.jdialects.id.TableIdGenerator in project jDialects by drinkjava2.

the class DDLCreateUtils method checkAndInsertToNotRepeatTable.

/**
 * if name not found, add <br/>
 * If name same, but other fields different, throw exception </br>
 * If name same, and other field same, ignore </br>
 */
protected static void checkAndInsertToNotRepeatTable(Set<TableIdGenerator> notRepeatedSeq, TableIdGenerator tab) {
    DialectException.assureNotEmpty(tab.getName(), "TableGen name can not be empty");
    DialectException.assureNotEmpty(tab.getTable(), "TableGen tableName can not be empty of \"" + tab.getName() + "\"");
    DialectException.assureNotEmpty(tab.getPkColumnName(), "TableGen pkColumnName can not be empty of \"" + tab.getName() + "\"");
    DialectException.assureNotEmpty(tab.getPkColumnValue(), "TableGen pkColumnValue can not be empty of \"" + tab.getName() + "\"");
    DialectException.assureNotEmpty(tab.getValueColumnName(), "TableGen valueColumnName can not be empty of \"" + tab.getName() + "\"");
    boolean canAdd = true;
    for (TableIdGenerator not : notRepeatedSeq) {
        if (tab.getName().equals(not.getName())) {
            canAdd = false;
            if (!(tab.getTable().equals(not.getTable()) && tab.getPkColumnName().equals(not.getPkColumnName()) && tab.getPkColumnValue().equals(not.getPkColumnValue()) && tab.getValueColumnName().equals(not.getValueColumnName()) && tab.getInitialValue().equals(not.getInitialValue()) && tab.getAllocationSize().equals(not.getAllocationSize())))
                throw new DialectException("In one or more tableModel, duplicated TableIdGenerator name '" + tab.getName() + "' but different value of table/pKColumnName/pkColumnValue/valueColumnName/initialValue/allocationSize setting");
        }
    }
    if (canAdd)
        notRepeatedSeq.add(tab);
}
Also used : TableIdGenerator(com.github.drinkjava2.jdialects.id.TableIdGenerator)

Example 5 with TableIdGenerator

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

Aggregations

TableIdGenerator (com.github.drinkjava2.jdialects.id.TableIdGenerator)5 AutoIdGenerator (com.github.drinkjava2.jdialects.id.AutoIdGenerator)2 IdGenerator (com.github.drinkjava2.jdialects.id.IdGenerator)2 SequenceIdGenerator (com.github.drinkjava2.jdialects.id.SequenceIdGenerator)2 ColumnModel (com.github.drinkjava2.jdialects.model.ColumnModel)2 FKeyModel (com.github.drinkjava2.jdialects.model.FKeyModel)2 TableModel (com.github.drinkjava2.jdialects.model.TableModel)2 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2