Search in sources :

Example 26 with Dialect

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

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

the class DDLUtils method buildFKeyConstraintDDL.

private static void buildFKeyConstraintDDL(Dialect dialect, List<String> stringList, List<FKeyConstraint> fKeyConstraintList) {
    for (FKeyConstraint kfc : fKeyConstraintList) {
        dialect.checkNotEmptyReservedWords(kfc.getFkeyReferenceTable(), "FkeyReferenceTable can not be empty");
        for (String refColName : kfc.getFkeyReferenceColumns()) dialect.checkNotEmptyReservedWords(refColName, "FkeyReferenceColumn name can not be empty");
    }
    /*
		 * join table col1 refTable ref1 ref2 + table col2 refTable ref1 ref2
		 * into one
		 */
    List<TrueFKeyConstraint> trueList = new ArrayList<>();
    for (int i = 0; i < fKeyConstraintList.size(); i++) {
        FKeyConstraint fk = fKeyConstraintList.get(i);
        TrueFKeyConstraint temp = new TrueFKeyConstraint(fk.getTableName(), fk.getColumnName(), fk.getFkeyReferenceTable(), fk.getFkeyReferenceColumns());
        temp.columnNames.add(fk.getColumnName());
        if (i == 0) {
            trueList.add(temp);
        } else {
            TrueFKeyConstraint found = null;
            for (TrueFKeyConstraint old : trueList) {
                if (fk.getTableName().equals(old.getTableName()) && fk.getFkeyReferenceTable().equals(old.getFkeyReferenceTable()) && StrUtils.arraysEqual(fk.getFkeyReferenceColumns(), old.getFkeyReferenceColumns())) {
                    found = old;
                }
            }
            if (found == null)
                trueList.add(temp);
            else
                found.columnNames.add(fk.getColumnName());
        }
    }
    for (TrueFKeyConstraint t : trueList) {
        /*
			 * ADD CONSTRAINT _FKEYNAME FOREIGN KEY _FKEYNAME (_FK1, _FK2)
			 * REFERENCES _REFTABLE (_REF1, _REF2)
			 */
        String s = dialect.ddlFeatures.addForeignKeyConstraintString;
        s = StrUtils.replace(s, "_FK1, _FK2", StrUtils.listToString(t.columnNames));
        s = StrUtils.replace(s, "_REFTABLE", t.getFkeyReferenceTable());
        s = StrUtils.replace(s, "_FKEYNAME", "fk_" + StrUtils.replace(StrUtils.listToString(t.columnNames), ",", "_"));
        stringList.add("alter table " + s);
    }
}
Also used : ArrayList(java.util.ArrayList) FKeyConstraint(com.github.drinkjava2.jdialects.model.FKeyConstraint) FKeyConstraint(com.github.drinkjava2.jdialects.model.FKeyConstraint)

Example 28 with Dialect

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

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

the class DialectTest method testDDLTypeMapping8.

@Test
public void testDDLTypeMapping8() {
    Dialect d = Dialect.MySQL5InnoDBDialect;
    String ddl = //
    "create table " + d.checkReservedWords("test") + "(" + //
    d.BIGINT("f1") + ", " + //
    d.BINARY("f2", 5) + ", " + //
    d.BIT("f3") + ", " + //
    d.BLOB("f4") + ", " + //
    d.BOOLEAN("f5") + ", " + //
    d.CHAR("f6") + ", " + //
    d.CLOB("f7") + ", " + //
    d.DATE("f8") + // + ", " + d.DECIMAL("f9",3,5) //
    ", " + //
    d.DOUBLE("f10") + ", " + //
    d.FLOAT("f11") + ", " + //
    d.INTEGER("f12") + // + ", " + d.JAVA_OBJECT("f13") //
    ", " + //
    d.LONGNVARCHAR("f14", 10) + ", " + //
    d.LONGVARBINARY("f15") + ", " + //
    d.LONGVARCHAR("f16") + ", " + //
    d.NCHAR("f17", 5) + ", " + //
    d.NCLOB("f18") + ", " + //
    d.NUMERIC("f19", 6, 4) + ", " + //
    d.NVARCHAR("f20", 6) + // + ", " + d.OTHER("f21") //
    ", " + //
    d.REAL("f22") + ", " + //
    d.SMALLINT("f23") + ", " + //
    d.TIME("f24") + ", " + //
    d.TIMESTAMP("f25") + ", " + //
    d.TINYINT("f26") + ", " + //
    d.VARBINARY("f27") + ", " + //
    d.VARCHAR("f28", 10) + ", " + //
    d.VARCHAR("f28", 30) + ")" + d.engine();
    System.out.println(ddl);
}
Also used : DerbyDialect(com.github.drinkjava2.jdialects.Dialect.DerbyDialect) PostgresPlusDialect(com.github.drinkjava2.jdialects.Dialect.PostgresPlusDialect) DB2Dialect(com.github.drinkjava2.jdialects.Dialect.DB2Dialect) Dialect(com.github.drinkjava2.jdialects.Dialect) MySQLDialect(com.github.drinkjava2.jdialects.Dialect.MySQLDialect) PostgreSQL81Dialect(com.github.drinkjava2.jdialects.Dialect.PostgreSQL81Dialect) PostgreSQL9Dialect(com.github.drinkjava2.jdialects.Dialect.PostgreSQL9Dialect) Oracle10gDialect(com.github.drinkjava2.jdialects.Dialect.Oracle10gDialect) PostgreSQL82Dialect(com.github.drinkjava2.jdialects.Dialect.PostgreSQL82Dialect) SybaseASE15Dialect(com.github.drinkjava2.jdialects.Dialect.SybaseASE15Dialect) SybaseAnywhereDialect(com.github.drinkjava2.jdialects.Dialect.SybaseAnywhereDialect) DerbyTenSixDialect(com.github.drinkjava2.jdialects.Dialect.DerbyTenSixDialect) SQLServerDialect(com.github.drinkjava2.jdialects.Dialect.SQLServerDialect) HSQLDialect(com.github.drinkjava2.jdialects.Dialect.HSQLDialect) DerbyTenSevenDialect(com.github.drinkjava2.jdialects.Dialect.DerbyTenSevenDialect) H2Dialect(com.github.drinkjava2.jdialects.Dialect.H2Dialect) Oracle8iDialect(com.github.drinkjava2.jdialects.Dialect.Oracle8iDialect) Oracle9iDialect(com.github.drinkjava2.jdialects.Dialect.Oracle9iDialect) InformixDialect(com.github.drinkjava2.jdialects.Dialect.InformixDialect) SQLiteDialect(com.github.drinkjava2.jdialects.Dialect.SQLiteDialect) MySQL5Dialect(com.github.drinkjava2.jdialects.Dialect.MySQL5Dialect) DB2400Dialect(com.github.drinkjava2.jdialects.Dialect.DB2400Dialect) IngresDialect(com.github.drinkjava2.jdialects.Dialect.IngresDialect) DerbyTenFiveDialect(com.github.drinkjava2.jdialects.Dialect.DerbyTenFiveDialect) Test(org.junit.Test)

Example 30 with Dialect

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

the class DialectTest method testDropTable.

@Test
public void testDropTable() {
    for (Dialect d : Dialect.values()) {
        String dropTable = d.dropTable("testTable");
        System.out.println(dropTable);
        Assert.assertTrue(StrUtils.containsIgnoreCase(dropTable, "drop"));
        Assert.assertTrue(StrUtils.containsIgnoreCase(dropTable, "testTable"));
    }
}
Also used : DerbyDialect(com.github.drinkjava2.jdialects.Dialect.DerbyDialect) PostgresPlusDialect(com.github.drinkjava2.jdialects.Dialect.PostgresPlusDialect) DB2Dialect(com.github.drinkjava2.jdialects.Dialect.DB2Dialect) Dialect(com.github.drinkjava2.jdialects.Dialect) MySQLDialect(com.github.drinkjava2.jdialects.Dialect.MySQLDialect) PostgreSQL81Dialect(com.github.drinkjava2.jdialects.Dialect.PostgreSQL81Dialect) PostgreSQL9Dialect(com.github.drinkjava2.jdialects.Dialect.PostgreSQL9Dialect) Oracle10gDialect(com.github.drinkjava2.jdialects.Dialect.Oracle10gDialect) PostgreSQL82Dialect(com.github.drinkjava2.jdialects.Dialect.PostgreSQL82Dialect) SybaseASE15Dialect(com.github.drinkjava2.jdialects.Dialect.SybaseASE15Dialect) SybaseAnywhereDialect(com.github.drinkjava2.jdialects.Dialect.SybaseAnywhereDialect) DerbyTenSixDialect(com.github.drinkjava2.jdialects.Dialect.DerbyTenSixDialect) SQLServerDialect(com.github.drinkjava2.jdialects.Dialect.SQLServerDialect) HSQLDialect(com.github.drinkjava2.jdialects.Dialect.HSQLDialect) DerbyTenSevenDialect(com.github.drinkjava2.jdialects.Dialect.DerbyTenSevenDialect) H2Dialect(com.github.drinkjava2.jdialects.Dialect.H2Dialect) Oracle8iDialect(com.github.drinkjava2.jdialects.Dialect.Oracle8iDialect) Oracle9iDialect(com.github.drinkjava2.jdialects.Dialect.Oracle9iDialect) InformixDialect(com.github.drinkjava2.jdialects.Dialect.InformixDialect) SQLiteDialect(com.github.drinkjava2.jdialects.Dialect.SQLiteDialect) MySQL5Dialect(com.github.drinkjava2.jdialects.Dialect.MySQL5Dialect) DB2400Dialect(com.github.drinkjava2.jdialects.Dialect.DB2400Dialect) IngresDialect(com.github.drinkjava2.jdialects.Dialect.IngresDialect) DerbyTenFiveDialect(com.github.drinkjava2.jdialects.Dialect.DerbyTenFiveDialect) Test(org.junit.Test)

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