Search in sources :

Example 1 with GlobalIdGenerator

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

use of com.github.drinkjava2.jdialects.model.GlobalIdGenerator 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)2 GlobalIdGenerator (com.github.drinkjava2.jdialects.model.GlobalIdGenerator)2 Sequence (com.github.drinkjava2.jdialects.model.Sequence)2 TableGenerator (com.github.drinkjava2.jdialects.model.TableGenerator)2 Column (com.github.drinkjava2.jdialects.model.Column)1 Table (com.github.drinkjava2.jdialects.model.Table)1 ArrayList (java.util.ArrayList)1