Search in sources :

Example 31 with TableModel

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

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

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

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

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

Aggregations

TableModel (com.github.drinkjava2.jdialects.model.TableModel)36 Test (org.junit.Test)19 ColumnModel (com.github.drinkjava2.jdialects.model.ColumnModel)11 AutoIdGenerator (com.github.drinkjava2.jdialects.id.AutoIdGenerator)9 IdGenerator (com.github.drinkjava2.jdialects.id.IdGenerator)9 SequenceIdGenerator (com.github.drinkjava2.jdialects.id.SequenceIdGenerator)5 TableIdGenerator (com.github.drinkjava2.jdialects.id.TableIdGenerator)5 FKeyModel (com.github.drinkjava2.jdialects.model.FKeyModel)5 IndexModel (com.github.drinkjava2.jdialects.model.IndexModel)3 UniqueModel (com.github.drinkjava2.jdialects.model.UniqueModel)3 SQLException (java.sql.SQLException)3 ArrayList (java.util.ArrayList)3 Dialect (com.github.drinkjava2.jdialects.Dialect)2 Connection (java.sql.Connection)2 Map (java.util.Map)2 SortedUUIDGenerator (com.github.drinkjava2.jdialects.id.SortedUUIDGenerator)1 Table (com.github.drinkjava2.jdialects.model.Table)1 HikariDataSource (com.zaxxer.hikari.HikariDataSource)1 BeanInfo (java.beans.BeanInfo)1 PropertyDescriptor (java.beans.PropertyDescriptor)1