List of usage examples for org.hibernate.mapping Table checkConstraints
List checkConstraints
To view the source code for org.hibernate.mapping Table checkConstraints.
Click Source Link
From source file:com.github.gekoh.yagen.ddl.CreateDDL.java
License:Apache License
private String addConstraintsAndNames(Dialect dialect, StringBuffer additionalObjects, String sqlCreate, String nameLC, Map<String, String> column2EnumConstraint) { List<String> pkColumns = getPkColumnNamesFrom(sqlCreate); TableConfig tableConfig = tblNameToConfig.get(nameLC); StringBuilder b = new StringBuilder(); StringBuilder enumConstraints = new StringBuilder(); Matcher matcher = COL_PATTERN.matcher(sqlCreate); int idx = 0;//from ww w. j av a 2 s. c o m while (matcher.find(idx)) { String defColName = matcher.group(COL_PATTERN_IDX_COLNAME); String colName = TableConfig.getIdentifierForReference(defColName); String constraintDef = column2EnumConstraint != null ? column2EnumConstraint.get(colName) : null; if (constraintDef != null) { String constraintName = getProfile().getNamingStrategy().constraintName(getEntityClassName(nameLC), nameLC, colName, Constants._CK); enumConstraints.append(", constraint ").append(constraintName); enumConstraints.append(" check (").append(defColName).append(" in (").append(constraintDef) .append("))"); } // name not null constraint idx = appendConstraint(b, sqlCreate, nameLC, colName, idx, matcher, COL_PATTERN_IDX_NOTNULL, Constants._NN); // name unique constraint idx = appendConstraint(b, sqlCreate, nameLC, colName, idx, matcher, COL_PATTERN_IDX_UNIQUE, Constants._UK); b.append(sqlCreate.substring(idx, matcher.end())); idx = matcher.end(); } b.append(sqlCreate.substring(idx)); sqlCreate = b.toString(); b = new StringBuilder(); matcher = UNIQUE_PATTERN.matcher(sqlCreate); idx = 0; while (matcher.find(idx)) { // name unique constraint idx = appendConstraint(b, sqlCreate, nameLC, DefaultNamingStrategy.concatColumnNames(matcher.group(3)), idx, matcher, 2, Constants._UK); b.append(sqlCreate.substring(idx, matcher.end())); idx = matcher.end(); } b.append(sqlCreate.substring(idx)); sqlCreate = b.toString(); matcher = TBL_PATTERN.matcher(sqlCreate); if (matcher.find() && matcher.group(TBL_PATTERN_IDX_PK_CLAUSE) != null) { b = new StringBuilder(); idx = matcher.start(TBL_PATTERN_IDX_PK_START); b.append(sqlCreate.substring(0, idx)); // name primary key constraint idx = appendConstraint(b, sqlCreate, nameLC, DefaultNamingStrategy.concatColumnNames(matcher.group(TBL_PATTERN_IDX_PK_COLLIST)), idx, matcher, TBL_PATTERN_IDX_PK_START, Constants._PK); b.append(sqlCreate.substring(idx)); sqlCreate = b.toString(); } else { LOG.info("no primary key found for table {}", nameLC); } matcher = TBL_PATTERN_WO_PK.matcher(sqlCreate); if (matcher.matches()) { b = new StringBuilder(sqlCreate.substring(0, matcher.start(TBL_PATTERN_WO_PK_IDX_AFTER_COL_DEF))); if (enumConstraints.length() > 0) { b.append(enumConstraints); } com.github.gekoh.yagen.api.Table tblAnnotation = tableConfig != null ? tableConfig.getTableAnnotationOfType(com.github.gekoh.yagen.api.Table.class) : null; if (tblAnnotation != null) { for (CheckConstraint checkConstraint : tblAnnotation.checkConstraints()) { String constraintName = getProfile().getNamingStrategy().constraintName(checkConstraint); if (StringUtils.isEmpty(constraintName)) { throw new IllegalArgumentException( "please specify a check constraint name in annotation CheckConstraint for table " + nameLC); } checkObjectName(dialect, constraintName); if (checkConstraint.initiallyDeferred() && isPostgreSql(dialect)) { String objectName = constraintName + "_FCT"; additionalObjects.append(STATEMENT_SEPARATOR) .append(getDeferredCheckConstraintFunction(dialect, objectName, constraintName, nameLC, String.format(checkConstraint.declaration(), "t."), pkColumns)) .append("\n/"); additionalObjects.append(STATEMENT_SEPARATOR).append("create constraint trigger ") .append(getProfile().getNamingStrategy().triggerName(constraintName + "_TRG")) .append("\n").append("after insert or update\n" + "on ").append(nameLC) .append(" initially deferred for each row\n" + "execute procedure ") .append(objectName).append("();"); } else { b.append(", constraint ").append(constraintName); b.append(" check (").append(String.format(checkConstraint.declaration(), "")).append(")"); if (supportsDeferrable(dialect) && checkConstraint.initiallyDeferred()) { b.append(" deferrable initially deferred"); } } } for (UniqueConstraint uniqueConstraint : tblAnnotation.uniqueConstraints()) { // custom declarations of unique keys need to be created with separate unique index DDL // when specifying column names we may use an inline unique constraint if (uniqueConstraint.columnNames().length < 1) { continue; } String constraintName = getProfile().getNamingStrategy().constraintName(uniqueConstraint); if (StringUtils.isEmpty(constraintName)) { throw new IllegalArgumentException( "please specify a unique constraint name in annotation UniqueConstraint on table " + nameLC); } checkObjectName(dialect, constraintName); if (StringUtils.isNotEmpty(uniqueConstraint.declaration()) && uniqueConstraint.columnNames().length > 0) { throw new IllegalArgumentException( "please specify either a declaration or a set of column names for UniqueConstraint on table " + nameLC); } StringBuilder declaration = new StringBuilder(); for (String columnName : uniqueConstraint.columnNames()) { if (declaration.length() > 0) { declaration.append(", "); } declaration.append(columnName); } b.append(", constraint ").append(constraintName); b.append(" unique (").append(declaration).append(")"); if (supportsDeferrable(dialect) && uniqueConstraint.initiallyDeferred()) { b.append(" deferrable initially deferred"); } if (uniqueConstraint.usingLocalIndex() && supportsPartitioning(dialect)) { b.append(" using index (create unique index ").append(constraintName).append(" on ") .append(nameLC); b.append(" (").append(declaration).append(") local)"); } } } b.append(sqlCreate.substring(matcher.start(TBL_PATTERN_WO_PK_IDX_AFTER_COL_DEF))); sqlCreate = b.toString(); } return sqlCreate; }