use of org.h2.constraint.Constraint in project h2database by h2database.
the class ConstraintUnique method getCreateSQLForCopy.
private String getCreateSQLForCopy(Table forTable, String quotedName, boolean internalIndex) {
StatementBuilder buff = new StatementBuilder("ALTER TABLE ");
buff.append(forTable.getSQL()).append(" ADD CONSTRAINT ");
if (forTable.isHidden()) {
buff.append("IF NOT EXISTS ");
}
buff.append(quotedName);
if (comment != null) {
buff.append(" COMMENT ").append(StringUtils.quoteStringSQL(comment));
}
buff.append(' ').append(getConstraintType().getSqlName()).append('(');
for (IndexColumn c : columns) {
buff.appendExceptFirst(", ");
buff.append(Parser.quoteIdentifier(c.column.getName()));
}
buff.append(')');
if (internalIndex && indexOwner && forTable == this.table) {
buff.append(" INDEX ").append(index.getSQL());
}
return buff.toString();
}
use of org.h2.constraint.Constraint in project h2database by h2database.
the class Table method removeIndexOrTransferOwnership.
/**
* If the index is still required by a constraint, transfer the ownership to
* it. Otherwise, the index is removed.
*
* @param session the session
* @param index the index that is no longer required
*/
public void removeIndexOrTransferOwnership(Session session, Index index) {
boolean stillNeeded = false;
if (constraints != null) {
for (Constraint cons : constraints) {
if (cons.usesIndex(index)) {
cons.setIndexOwner(index);
database.updateMeta(session, cons);
stillNeeded = true;
}
}
}
if (!stillNeeded) {
database.removeSchemaObject(session, index);
}
}
use of org.h2.constraint.Constraint in project h2database by h2database.
the class Table method dropMultipleColumnsConstraintsAndIndexes.
/**
* Check that these columns are not referenced by a multi-column constraint
* or multi-column index. If it is, an exception is thrown. Single-column
* references and indexes are dropped.
*
* @param session the session
* @param columnsToDrop the columns to drop
* @throws DbException if the column is referenced by multi-column
* constraints or indexes
*/
public void dropMultipleColumnsConstraintsAndIndexes(Session session, ArrayList<Column> columnsToDrop) {
HashSet<Constraint> constraintsToDrop = new HashSet<>();
if (constraints != null) {
for (Column col : columnsToDrop) {
for (Constraint constraint : constraints) {
HashSet<Column> columns = constraint.getReferencedColumns(this);
if (!columns.contains(col)) {
continue;
}
if (columns.size() == 1) {
constraintsToDrop.add(constraint);
} else {
throw DbException.get(ErrorCode.COLUMN_IS_REFERENCED_1, constraint.getSQL());
}
}
}
}
HashSet<Index> indexesToDrop = new HashSet<>();
ArrayList<Index> indexes = getIndexes();
if (indexes != null) {
for (Column col : columnsToDrop) {
for (Index index : indexes) {
if (index.getCreateSQL() == null) {
continue;
}
if (index.getColumnIndex(col) < 0) {
continue;
}
if (index.getColumns().length == 1) {
indexesToDrop.add(index);
} else {
throw DbException.get(ErrorCode.COLUMN_IS_REFERENCED_1, index.getSQL());
}
}
}
}
for (Constraint c : constraintsToDrop) {
session.getDatabase().removeSchemaObject(session, c);
}
for (Index i : indexesToDrop) {
// constraint
if (getIndexes().contains(i)) {
session.getDatabase().removeSchemaObject(session, i);
}
}
}
use of org.h2.constraint.Constraint in project h2database by h2database.
the class Table method removeChildrenAndResources.
@Override
public void removeChildrenAndResources(Session session) {
while (!dependentViews.isEmpty()) {
TableView view = dependentViews.get(0);
dependentViews.remove(0);
database.removeSchemaObject(session, view);
}
while (synonyms != null && !synonyms.isEmpty()) {
TableSynonym synonym = synonyms.remove(0);
database.removeSchemaObject(session, synonym);
}
while (triggers != null && !triggers.isEmpty()) {
TriggerObject trigger = triggers.remove(0);
database.removeSchemaObject(session, trigger);
}
while (constraints != null && !constraints.isEmpty()) {
Constraint constraint = constraints.remove(0);
database.removeSchemaObject(session, constraint);
}
for (Right right : database.getAllRights()) {
if (right.getGrantedObject() == this) {
database.removeDatabaseObject(session, right);
}
}
database.removeMeta(session, getId());
// before removing the table object)
while (sequences != null && !sequences.isEmpty()) {
Sequence sequence = sequences.remove(0);
// this is possible when calling ALTER TABLE ALTER COLUMN
if (database.getDependentTable(sequence, this) == null) {
database.removeSchemaObject(session, sequence);
}
}
}
use of org.h2.constraint.Constraint in project h2database by h2database.
the class Column method getCheckConstraint.
/**
* Get the check constraint expression for this column if set.
*
* @param session the session
* @param asColumnName the column name to use
* @return the constraint expression
*/
public Expression getCheckConstraint(Session session, String asColumnName) {
if (checkConstraint == null) {
return null;
}
Parser parser = new Parser(session);
String sql;
synchronized (this) {
String oldName = name;
name = asColumnName;
sql = checkConstraint.getSQL();
name = oldName;
}
return parser.parseExpression(sql);
}
Aggregations