Search in sources :

Example 21 with Constraint

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();
}
Also used : StatementBuilder(org.h2.util.StatementBuilder) IndexColumn(org.h2.table.IndexColumn)

Example 22 with Constraint

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);
    }
}
Also used : Constraint(org.h2.constraint.Constraint)

Example 23 with Constraint

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);
        }
    }
}
Also used : Constraint(org.h2.constraint.Constraint) Index(org.h2.index.Index) HashSet(java.util.HashSet)

Example 24 with Constraint

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);
        }
    }
}
Also used : Constraint(org.h2.constraint.Constraint) Right(org.h2.engine.Right) TriggerObject(org.h2.schema.TriggerObject) Sequence(org.h2.schema.Sequence)

Example 25 with Constraint

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);
}
Also used : ValueString(org.h2.value.ValueString) Parser(org.h2.command.Parser)

Aggregations

Constraint (org.h2.constraint.Constraint)15 Index (org.h2.index.Index)10 ValueString (org.h2.value.ValueString)9 Table (org.h2.table.Table)8 Column (org.h2.table.Column)6 IndexColumn (org.h2.table.IndexColumn)6 Connection (java.sql.Connection)5 Statement (java.sql.Statement)5 Database (org.h2.engine.Database)5 StatementBuilder (org.h2.util.StatementBuilder)5 ResultSet (java.sql.ResultSet)4 ArrayList (java.util.ArrayList)4 AlterTableAddConstraint (org.h2.command.ddl.AlterTableAddConstraint)4 DbObject (org.h2.engine.DbObject)4 Right (org.h2.engine.Right)4 Expression (org.h2.expression.Expression)4 ExpressionColumn (org.h2.expression.ExpressionColumn)4 Schema (org.h2.schema.Schema)4 Sequence (org.h2.schema.Sequence)4 PreparedStatement (java.sql.PreparedStatement)3