Example usage for org.hibernate.mapping Constraint getColumn

List of usage examples for org.hibernate.mapping Constraint getColumn

Introduction

In this page you can find the example usage for org.hibernate.mapping Constraint getColumn.

Prototype

public Column getColumn(int i) 

Source Link

Usage

From source file:com.github.gekoh.yagen.ddl.CreateDDL.java

License:Apache License

public String updateCreateConstraint(Dialect dialect, StringBuffer buf, String name, Table table,
        Constraint constraint) {
    NamingStrategy namingStrategy = getProfile().getNamingStrategy();
    String newName = namingStrategy.constraintName(constraint,
            getEntityClassName(namingStrategy.tableName(table.getName())));

    if (!name.equals(newName)) {
        String sqlCreate = buf.toString();
        Matcher matcher = CONSTRAINT_PATTERN.matcher(sqlCreate);
        if (matcher.find()) {
            buf = new StringBuffer();
            buf.append(sqlCreate.substring(0, matcher.start(1)));
            buf.append(newName);//from  w w  w  .j ava 2s.c om
            buf.append(sqlCreate.substring(matcher.end(1)));
        }
        name = newName;
    }

    String tableNameLC = getProfile().getNamingStrategy().tableName(table.getName()).toLowerCase();

    if (!renderTable(tableNameLC) || externalViews.contains(tableNameLC)) {
        return "-- skipped creation of constraint '" + name + "' for table '" + table.getName()
                + "' as the mapped entity was not chosen to be processed or is a view";
    }

    TableConfig tableConfig = tblNameToConfig.get(tableNameLC);

    String refTblNameLC = null;
    if (constraint instanceof ForeignKey) {
        if (tableConfig.getColumnNamesIsNoFK().contains(constraint.getColumn(0).getName().toLowerCase())) {
            return "-- skipped creation of foreign key constraint '" + name + "' for table '" + table.getName()
                    + "' according to annotation of type " + NoForeignKeyConstraint.class.getSimpleName();
        }
        refTblNameLC = getProfile().getNamingStrategy()
                .tableName(((ForeignKey) constraint).getReferencedTable().getName()).toLowerCase();
    }

    checkObjectName(dialect, name);
    String i18nFK = tableConfig.getI18nBaseEntityFkCol();

    if (i18nFK != null) {
        StringBuilder sql = new StringBuilder();
        tableNameLC = getI18NDetailTableName(tableNameLC);
        Matcher matcher = TBL_ALTER_PATTERN.matcher(buf.toString());
        if (matcher.find()) {
            sql.append(buf.substring(0, matcher.start(1))).append(tableNameLC)
                    .append(buf.substring(matcher.end(1)));
        }
        buf = new StringBuffer(sql.toString());
    }

    if (constraint instanceof ForeignKey) {
        StringBuilder colList = new StringBuilder();
        org.hibernate.mapping.Column singleColumn = null;

        TableConfig refTableConfig = tblNameToConfig.get(refTblNameLC);
        IntervalPartitioning refTblPart = refTableConfig != null
                ? refTableConfig.getTableAnnotationOfType(IntervalPartitioning.class)
                : null;

        for (org.hibernate.mapping.Column column : (Iterable<? extends org.hibernate.mapping.Column>) constraint
                .getColumns()) {
            if (colList.length() > 0) {
                colList.append(", ");
            }
            colList.append(column.getName().toLowerCase());
            singleColumn = singleColumn == null ? column : null;
        }

        if (externalViews.contains(refTblNameLC)) {
            buf = new StringBuffer("-- skipped creation of constraint '" + name + "' on table '" + tableNameLC
                    + "' since a view will be referenced");
        } else if (refTblPart != null && refTblPart.useLocalPK() && supportsPartitioning(dialect)) {
            buf = new StringBuffer();
            buf.append("-- skipped creation of foreign key constraint '").append(name).append("' on table '")
                    .append(tableNameLC).append("' to table '").append(refTblNameLC)
                    .append("' as the partitioned target table has a local PK (see @IntervalPartitioning on ")
                    .append(((ForeignKey) constraint).getReferencedEntityName()).append(")");
        } else {
            if (singleColumn != null) {
                if (tableConfig.getColumnNamesIsCascadeNullable()
                        .contains(singleColumn.getName().toLowerCase())) {
                    buf.append(" on delete set null");
                } else if (tableConfig.getColumnNamesIsCascadeDelete()
                        .contains(singleColumn.getName().toLowerCase()) && buf.indexOf("on delete") < 0) {
                    buf.append(" on delete cascade");
                }
            }

            Map<String, Deferrable> col2Deferrable = tableConfig.getColumnNameToDeferrable();
            Deferrable deferrable;
            if (supportsDeferrable(dialect) && col2Deferrable != null
                    && (deferrable = col2Deferrable.get(colList.toString())) != null) {
                buf.append(" deferrable");
                if (deferrable.initiallyDeferred()) {
                    buf.append(" initially deferred");
                }
            }

            if (getProfile().isDisableFKs()) {
                buf.insert(0,
                        "-- creating FK constraint initially disabled since we do not need it for profile '"
                                + getProfile() + "'\n");
                buf.append(" disable");
            }
        }

        getProfile().duplex(ObjectType.CONSTRAINT, name, buf.toString());

        if (constraint.getColumnSpan() == 1 && hasIndex(table, tableNameLC, singleColumn)) {
            LOG.debug("not creating foreign key index as there is already an index on table " + tableNameLC
                    + " and column " + colList.toString());
        } else {
            String fkIndexName = getProfile().getNamingStrategy().indexName(getEntityClassName(tableNameLC),
                    tableNameLC, DefaultNamingStrategy.concatColumnNames(colList.toString()));
            StringBuilder objDdl = new StringBuilder();
            objDdl.append("create index ").append(fkIndexName).append(" on ").append(tableNameLC).append(" (")
                    .append(colList.toString()).append(")");

            if (constraint.getColumnSpan() == 1) {
                tblColNameHasSingleColIndex.add(tableNameLC + "." + colList.toString());
            }

            buf.append(STATEMENT_SEPARATOR).append(objDdl);

            getProfile().duplex(ObjectType.INDEX, fkIndexName, objDdl.toString());
        }
    }

    return buf.toString();
}