/** Write the unique constraints inline with the create table statement. */ protected void writeUniqueConstraints(DdlBuffer apply, CreateTable createTable) throws IOException { boolean inlineUniqueOneToOne = platformDdl.isInlineUniqueOneToOne(); List<Column> columns = createTable.getColumn(); for (Column column : columns) { if (hasValue(column.getUnique()) || (inlineUniqueOneToOne && hasValue(column.getUniqueOneToOne()))) { // normal mechanism for adding unique constraint inlineUniqueConstraintSingle(apply, column); } else if (!inlineUniqueOneToOne && hasValue(column.getUniqueOneToOne())) { // MsSqlServer specific mechanism for adding unique constraints (that allow nulls) externalUnique.add(column); } } }
/** Write the unique constraint inline with the create table statement. */ protected void inlineUniqueConstraintSingle(DdlBuffer buffer, Column column) throws IOException { String uqName = column.getUnique(); if (uqName == null) { uqName = column.getUniqueOneToOne(); } buffer.append(",").newLine(); buffer.append(" constraint ").append(uqName).append(" unique "); buffer.append("("); buffer.append(lowerName(column.getName())); buffer.append(")"); }
/** * Specific handling of OneToOne unique constraints for MsSqlServer. For all other DB platforms * these unique constraints are done inline as per normal. */ protected void writeUniqueOneToOneConstraints(DdlWrite write, CreateTable createTable) throws IOException { String tableName = createTable.getName(); for (Column col : externalUnique) { String uqName = col.getUniqueOneToOne(); String[] columnNames = {col.getName()}; write .apply() .append(platformDdl.alterTableAddUniqueConstraint(tableName, uqName, columnNames)) .endOfStatement(); write.rollbackForeignKeys().append(platformDdl.dropIndex(uqName, tableName)).endOfStatement(); } }