Search in sources :

Example 6 with Sequence

use of com.github.drinkjava2.jdialects.model.Sequence in project jDialects by drinkjava2.

the class DDLUtils method buildSequenceDDL.

private static void buildSequenceDDL(Dialect dialect, List<String> stringList, List<Sequence> sequenceList) {
    DDLFeatures features = dialect.ddlFeatures;
    for (Sequence seq : sequenceList) {
        DialectException.assureNotEmpty(seq.getName(), "Sequence name can not be empty");
        DialectException.assureNotEmpty(seq.getSequenceName(), "sequenceName can not be empty of \"" + seq.getName() + "\"");
    }
    for (Sequence seq : sequenceList) {
        for (Sequence seq2 : sequenceList) {
            if (seq != seq2 && (seq2.getAllocationSize() != 0)) {
                if (seq.getName().equalsIgnoreCase(seq2.getName())) {
                    // set to 0 to skip repeated
                    seq.setAllocationSize(0);
                } else {
                    if (seq.getSequenceName().equalsIgnoreCase(seq2.getSequenceName()))
                        DialectException.throwEX("Dulplicated Sequence setting \"" + seq.getName() + "\" and \"" + seq2.getName() + "\" found.");
                }
            }
        }
    }
    Set<String> sequenceNameExisted = new HashSet<>();
    for (Sequence seq : sequenceList) {
        if (seq.getAllocationSize() != 0) {
            String sequenceName = seq.getSequenceName().toLowerCase();
            if (!sequenceNameExisted.contains(sequenceName)) {
                if (!(features.supportsPooledSequences || features.supportsSequences)) {
                    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);
                }
                sequenceNameExisted.add(sequenceName);
            }
        }
    }
}
Also used : Sequence(com.github.drinkjava2.jdialects.model.Sequence) HashSet(java.util.HashSet)

Example 7 with Sequence

use of com.github.drinkjava2.jdialects.model.Sequence in project jDialects by drinkjava2.

the class DDLUtils method transferTableToObjectList.

/**
	 * Transfer table to a mixed DDL String or TableGenerator Object list
	 */
private static void transferTableToObjectList(Dialect dialect, Table t, List<Object> objectResultList) {
    DDLFeatures features = dialect.ddlFeatures;
    StringBuilder buf = new StringBuilder();
    boolean hasPkey = false;
    String pkeys = "";
    String tableName = t.getTableName();
    Map<String, Column> columns = t.getColumns();
    // Reserved words check
    dialect.checkNotEmptyReservedWords(tableName, "Table name can not be empty");
    for (Column col : columns.values()) {
        dialect.checkNotEmptyReservedWords(col.getColumnName(), "Column name can not be empty");
        dialect.checkReservedWords(col.getPkeyName());
        dialect.checkReservedWords(col.getUniqueConstraintName());
    }
    for (Column col : columns.values()) {
        // autoGenerator, only support sequence or table for "Auto" type
        if (col.getAutoGenerator()) {
            if (features.supportsSequences || features.supportsPooledSequences) {
                objectResultList.add(new Sequence(GlobalIdGenerator.JDIALECTS_IDGEN_TABLE, GlobalIdGenerator.JDIALECTS_IDGEN_TABLE, 1, 1));
            } else {
                // GlobalIdGenerator
                objectResultList.add(new GlobalIdGenerator());
            }
        }
        // foreign keys
        if (!StrUtils.isEmpty(col.getFkeyReferenceTable()))
            objectResultList.add(new FKeyConstraint(tableName, col.getColumnName(), col.getFkeyReferenceTable(), col.getFkeyReferenceColumns()));
    }
    // sequence
    for (Sequence seq : t.getSequences().values()) objectResultList.add(seq);
    // tableGenerator
    for (TableGenerator tg : t.getTableGenerators().values()) objectResultList.add(tg);
    // check and cache prime keys
    for (Column col : columns.values()) {
        if (col.getPkey()) {
            hasPkey = true;
            if (StrUtils.isEmpty(pkeys))
                pkeys = col.getColumnName();
            else
                pkeys += "," + col.getColumnName();
        }
    }
    // create table
    buf.append(hasPkey ? dialect.ddlFeatures.createTableString : dialect.ddlFeatures.createMultisetTableString).append(" ").append(tableName).append(" (");
    for (Column c : columns.values()) {
        if (c.getColumnType() == null)
            DialectException.throwEX("Type not set on column \"" + c.getColumnName() + "\" at table \"" + tableName + "\"");
        // column definition
        buf.append(c.getColumnName()).append(" ");
        // Identity or autoGenerator+supportIdentity
        if (c.getIdentity() && !features.supportsIdentityColumns)
            DialectException.throwEX("Unsupported identity setting for dialect \"" + dialect + "\" on column \"" + c.getColumnName() + "\" at table \"" + tableName + "\"");
        if (c.getIdentity()) {
            if (features.hasDataTypeInIdentityColumn)
                buf.append(dialect.translateToDDLType(c.getColumnType(), c.getLengths()));
            buf.append(' ');
            if (Type.BIGINT.equals(c.getColumnType()))
                buf.append(features.identityColumnStringBigINT);
            else
                buf.append(features.identityColumnString);
        } else {
            buf.append(dialect.translateToDDLType(c.getColumnType(), c.getLengths()));
            // Default
            String defaultValue = c.getDefaultValue();
            if (defaultValue != null) {
                buf.append(" default ").append(defaultValue);
            }
            // Not null
            if (c.getNotNull())
                buf.append(" not null");
            else
                buf.append(features.nullColumnString);
        }
        // Check
        if (!StrUtils.isEmpty(c.getCheck())) {
            if (features.supportsColumnCheck)
                buf.append(" check (").append(c.getCheck()).append(")");
            else
                logger.warn("Ignore unsupported check setting for dialect \"" + dialect + "\" on column \"" + c.getColumnName() + "\" at table \"" + tableName + "\" with value: " + c.getCheck());
        }
        // Comments
        if (c.getComment() != null) {
            if (StrUtils.isEmpty(features.columnComment) && !features.supportsCommentOn)
                logger.warn("Ignore unsupported comment setting for dialect \"" + dialect + "\" on column \"" + c.getColumnName() + "\" at table \"" + tableName + "\" with value: " + c.getComment());
            else
                buf.append(StrUtils.replace(features.columnComment, "_COMMENT", c.getComment()));
        }
        buf.append(",");
    }
    // PKEY
    if (!StrUtils.isEmpty(pkeys)) {
        buf.append(" primary key (").append(pkeys).append("),");
    }
    // Table Check
    if (!StrUtils.isEmpty(t.getCheck())) {
        if (features.supportsTableCheck)
            buf.append(" check (").append(t.getCheck()).append("),");
        else
            logger.warn("Ignore unsupported table check setting for dialect \"" + dialect + "\" on table \"" + tableName + "\" with value: " + t.getCheck());
    }
    buf.setLength(buf.length() - 1);
    buf.append(")");
    // type or engine for MariaDB & MySql
    buf.append(dialect.engine());
    objectResultList.add(buf.toString());
    // unique constraint
    for (Column column : columns.values()) addUniqueConstraintDDL(objectResultList, dialect, tableName, column);
    // table comment on
    if (t.getComment() != null) {
        if (features.supportsCommentOn)
            objectResultList.add("comment on table " + t.getTableName() + " is '" + t.getComment() + "'");
        else
            logger.warn("Ignore unsupported table comment setting for dialect \"" + dialect + "\" on table \"" + tableName + "\" with value: " + t.getComment());
    }
    // column comment on
    for (Column c : columns.values()) {
        if (features.supportsCommentOn && c.getComment() != null && StrUtils.isEmpty(features.columnComment))
            objectResultList.add("comment on column " + tableName + '.' + c.getColumnName() + " is '" + c.getComment() + "'");
    }
}
Also used : Column(com.github.drinkjava2.jdialects.model.Column) FKeyConstraint(com.github.drinkjava2.jdialects.model.FKeyConstraint) Sequence(com.github.drinkjava2.jdialects.model.Sequence) TableGenerator(com.github.drinkjava2.jdialects.model.TableGenerator) GlobalIdGenerator(com.github.drinkjava2.jdialects.model.GlobalIdGenerator)

Example 8 with Sequence

use of com.github.drinkjava2.jdialects.model.Sequence in project jDialects by drinkjava2.

the class TableTest method SequenceModel.

private static Table SequenceModel() {
    // Sequence
    Table t = new Table("testTable");
    t.addSequence("seq1", "seq_1", 1, 1);
    t.addSequence("seq2", "seq_2", 1, 1);
    t.column("i1").INTEGER().pkey().sequence("seq1");
    t.column("i2").INTEGER().pkey().sequence("seq2");
    return t;
}
Also used : Table(com.github.drinkjava2.jdialects.model.Table)

Aggregations

Sequence (com.github.drinkjava2.jdialects.model.Sequence)3 HashSet (java.util.HashSet)3 SequenceIdGenerator (com.github.drinkjava2.jdialects.id.SequenceIdGenerator)2 FKeyConstraint (com.github.drinkjava2.jdialects.model.FKeyConstraint)2 GlobalIdGenerator (com.github.drinkjava2.jdialects.model.GlobalIdGenerator)2 Table (com.github.drinkjava2.jdialects.model.Table)2 TableGenerator (com.github.drinkjava2.jdialects.model.TableGenerator)2 ArrayList (java.util.ArrayList)2 DialectException (com.github.drinkjava2.jdialects.DialectException)1 SortedUUIDGenerator (com.github.drinkjava2.jdialects.id.SortedUUIDGenerator)1 Column (com.github.drinkjava2.jdialects.model.Column)1 ColumnModel (com.github.drinkjava2.jdialects.model.ColumnModel)1 TableModel (com.github.drinkjava2.jdialects.model.TableModel)1 DatabaseMetaData (java.sql.DatabaseMetaData)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1