package org.hibernate.metamodel.relational;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.AssertionFailure;
import org.hibernate.dialect.Dialect;
public abstract class AbstractConstraint implements Constraint {
private final TableSpecification table;
private final String name;
private List<Column> columns = new ArrayList<Column>();
protected AbstractConstraint(TableSpecification table, String name) {
this.table = table;
this.name = name;
}
public TableSpecification getTable() {
return table;
}
public String getName() {
return name;
}
public Iterable<Column> getColumns() {
return columns;
}
protected int getColumnSpan() {
return columns.size();
}
protected List<Column> internalColumnAccess() {
return columns;
}
public void addColumn(Column column) {
internalAddColumn( column );
}
protected void internalAddColumn(Column column) {
if ( column.getTable() != getTable() ) {
throw new AssertionFailure(
String.format(
"Unable to add column to constraint; tables [%s, %s] did not match",
column.getTable().toLoggableString(),
getTable().toLoggableString()
)
);
}
columns.add( column );
}
protected boolean isCreationVetoed(Dialect dialect) {
return false;
}
protected abstract String sqlConstraintStringInAlterTable(Dialect dialect);
public String[] sqlDropStrings(Dialect dialect) {
if ( isCreationVetoed( dialect ) ) {
return null;
}
else {
return new String[] {
new StringBuilder()
.append( "alter table " )
.append( getTable().getQualifiedName( dialect ) )
.append( " drop constraint " )
.append( dialect.quote( getName() ) )
.toString()
};
}
}
public String[] sqlCreateStrings(Dialect dialect) {
if ( isCreationVetoed( dialect ) ) {
return null;
}
else {
return new String[] {
new StringBuilder( "alter table " )
.append( getTable().getQualifiedName( dialect ) )
.append( sqlConstraintStringInAlterTable( dialect ) )
.toString()
};
}
}
}