Search in sources :

Example 1 with SequenceIdGenerator

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

the class DDLCreateUtils method buildSequenceDDL.

private static void buildSequenceDDL(Dialect dialect, List<String> stringList, List<SequenceIdGenerator> sequenceList) {
    Set<SequenceIdGenerator> notRepeatedSequences = new HashSet<SequenceIdGenerator>();
    for (SequenceIdGenerator seq : sequenceList) 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 (features.supportsPooledSequences) {
            // create sequence _SEQ start with 11 increment by 33
            String pooledSequence = StrUtils.replace(features.createPooledSequenceStrings, "_SEQ", seq.getSequenceName());
            pooledSequence = StrUtils.replace(pooledSequence, "11", "" + seq.getInitialValue());
            pooledSequence = StrUtils.replace(pooledSequence, "33", "" + seq.getAllocationSize());
            stringList.add(pooledSequence);
        } else {
            if (seq.getInitialValue() >= 2 || seq.getAllocationSize() >= 2)
                DialectException.throwEX("Dialect \"" + dialect + "\" does not support initialValue and allocationSize setting on sequence \"" + seq.getName() + "\", try set initialValue and allocationSize to 1 to fix");
            // "create sequence _SEQ"
            String simepleSeq = StrUtils.replace(features.createSequenceStrings, "_SEQ", seq.getSequenceName());
            stringList.add(simepleSeq);
        }
    }
}
Also used : SequenceIdGenerator(com.github.drinkjava2.jdialects.id.SequenceIdGenerator) HashSet(java.util.HashSet)

Example 2 with SequenceIdGenerator

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

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

the class DDLCreateUtils method checkAndInsertToNotRepeatSeq.

/**
 * 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 checkAndInsertToNotRepeatSeq(Set<SequenceIdGenerator> notRepeatedSeq, SequenceIdGenerator seq) {
    DialectException.assureNotEmpty(seq.getName(), "SequenceGen name can not be empty");
    DialectException.assureNotEmpty(seq.getSequenceName(), "sequenceName can not be empty of \"" + seq.getName() + "\"");
    boolean canAdd = true;
    for (SequenceIdGenerator not : notRepeatedSeq) {
        if (seq.getName().equals(not.getName())) {
            canAdd = false;
            if (!(seq.getSequenceName().equals(not.getSequenceName()) && seq.getInitialValue().equals(not.getInitialValue()) && seq.getAllocationSize().equals(not.getAllocationSize())))
                throw new DialectException("In one or more tableModel, duplicated SequenceIdGenerator name '" + seq.getName() + "' but different value of sequenceName/initialValue/allocationSize setting");
        } else {
            if (seq.getSequenceName().equals(not.getSequenceName()))
                throw new DialectException("In one or more tableModel, duplicated SequenceName '" + seq.getSequenceName() + "' found for '" + seq.getName() + "' and '" + not.getName() + "'");
        }
    }
    if (canAdd)
        notRepeatedSeq.add(seq);
}
Also used : SequenceIdGenerator(com.github.drinkjava2.jdialects.id.SequenceIdGenerator)

Example 4 with SequenceIdGenerator

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

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

Aggregations

SequenceIdGenerator (com.github.drinkjava2.jdialects.id.SequenceIdGenerator)5 AutoIdGenerator (com.github.drinkjava2.jdialects.id.AutoIdGenerator)2 IdGenerator (com.github.drinkjava2.jdialects.id.IdGenerator)2 TableIdGenerator (com.github.drinkjava2.jdialects.id.TableIdGenerator)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