Search in sources :

Example 1 with FKeyConstraint

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

the class DDLUtils method toCreateDDLwithoutFormat.

/**
	 * Transfer table to DDL by given dialect and without format it
	 */
public static String[] toCreateDDLwithoutFormat(Dialect dialect, Table... tables) {
    // resultList store mixed DDL String + TableGenerator + Sequence
    List<Object> objectResultList = new ArrayList<>();
    for (Table table : tables) transferTableToObjectList(dialect, table, objectResultList);
    List<String> stringResultList = new ArrayList<>();
    List<TableGenerator> tbGeneratorList = new ArrayList<>();
    List<Sequence> sequenceList = new ArrayList<>();
    List<GlobalIdGenerator> globalIdGeneratorList = new ArrayList<>();
    List<FKeyConstraint> fKeyConstraintList = new ArrayList<>();
    for (Object ddl : objectResultList) {
        if (!StrUtils.isEmpty(ddl)) {
            if (ddl instanceof String)
                stringResultList.add((String) ddl);
            else if (ddl instanceof TableGenerator)
                tbGeneratorList.add((TableGenerator) ddl);
            else if (ddl instanceof Sequence)
                sequenceList.add((Sequence) ddl);
            else if (ddl instanceof GlobalIdGenerator)
                globalIdGeneratorList.add((GlobalIdGenerator) ddl);
            else if (ddl instanceof FKeyConstraint)
                fKeyConstraintList.add((FKeyConstraint) ddl);
        }
    }
    buildSequenceDDL(dialect, stringResultList, sequenceList);
    buildTableGeneratorDDL(dialect, stringResultList, tbGeneratorList);
    buildGolbalIDGeneratorDDL(dialect, stringResultList, globalIdGeneratorList);
    buildFKeyConstraintDDL(dialect, stringResultList, fKeyConstraintList);
    return stringResultList.toArray(new String[stringResultList.size()]);
}
Also used : Table(com.github.drinkjava2.jdialects.model.Table) ArrayList(java.util.ArrayList) TableGenerator(com.github.drinkjava2.jdialects.model.TableGenerator) Sequence(com.github.drinkjava2.jdialects.model.Sequence) GlobalIdGenerator(com.github.drinkjava2.jdialects.model.GlobalIdGenerator) FKeyConstraint(com.github.drinkjava2.jdialects.model.FKeyConstraint)

Example 2 with FKeyConstraint

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

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

Aggregations

FKeyConstraint (com.github.drinkjava2.jdialects.model.FKeyConstraint)3 GlobalIdGenerator (com.github.drinkjava2.jdialects.model.GlobalIdGenerator)2 Sequence (com.github.drinkjava2.jdialects.model.Sequence)2 TableGenerator (com.github.drinkjava2.jdialects.model.TableGenerator)2 ArrayList (java.util.ArrayList)2 Column (com.github.drinkjava2.jdialects.model.Column)1 Table (com.github.drinkjava2.jdialects.model.Table)1