Search in sources :

Example 26 with Constraint

use of org.h2.constraint.Constraint in project h2database by h2database.

the class Parser method parseComment.

private Prepared parseComment() {
    int type = 0;
    read("ON");
    boolean column = false;
    if (readIf("TABLE") || readIf("VIEW")) {
        type = DbObject.TABLE_OR_VIEW;
    } else if (readIf("COLUMN")) {
        column = true;
        type = DbObject.TABLE_OR_VIEW;
    } else if (readIf("CONSTANT")) {
        type = DbObject.CONSTANT;
    } else if (readIf("CONSTRAINT")) {
        type = DbObject.CONSTRAINT;
    } else if (readIf("ALIAS")) {
        type = DbObject.FUNCTION_ALIAS;
    } else if (readIf("INDEX")) {
        type = DbObject.INDEX;
    } else if (readIf("ROLE")) {
        type = DbObject.ROLE;
    } else if (readIf("SCHEMA")) {
        type = DbObject.SCHEMA;
    } else if (readIf("SEQUENCE")) {
        type = DbObject.SEQUENCE;
    } else if (readIf("TRIGGER")) {
        type = DbObject.TRIGGER;
    } else if (readIf("USER")) {
        type = DbObject.USER;
    } else if (readIf("DOMAIN")) {
        type = DbObject.USER_DATATYPE;
    } else {
        throw getSyntaxError();
    }
    SetComment command = new SetComment(session);
    String objectName;
    if (column) {
        // can't use readIdentifierWithSchema() because
        // it would not read schema.table.column correctly
        // if the db name is equal to the schema name
        ArrayList<String> list = New.arrayList();
        do {
            list.add(readUniqueIdentifier());
        } while (readIf("."));
        schemaName = session.getCurrentSchemaName();
        if (list.size() == 4) {
            if (!equalsToken(database.getShortName(), list.remove(0))) {
                throw DbException.getSyntaxError(sqlCommand, parseIndex, "database name");
            }
        }
        if (list.size() == 3) {
            schemaName = list.remove(0);
        }
        if (list.size() != 2) {
            throw DbException.getSyntaxError(sqlCommand, parseIndex, "table.column");
        }
        objectName = list.get(0);
        command.setColumn(true);
        command.setColumnName(list.get(1));
    } else {
        objectName = readIdentifierWithSchema();
    }
    command.setSchemaName(schemaName);
    command.setObjectName(objectName);
    command.setObjectType(type);
    read("IS");
    command.setCommentExpression(readExpression());
    return command;
}
Also used : SetComment(org.h2.command.ddl.SetComment) ValueString(org.h2.value.ValueString) AlterTableRenameConstraint(org.h2.command.ddl.AlterTableRenameConstraint) AlterTableAddConstraint(org.h2.command.ddl.AlterTableAddConstraint) AlterTableDropConstraint(org.h2.command.ddl.AlterTableDropConstraint)

Example 27 with Constraint

use of org.h2.constraint.Constraint in project h2database by h2database.

the class AlterTableRenameConstraint method update.

@Override
public int update() {
    session.commit(true);
    Constraint constraint = getSchema().findConstraint(session, constraintName);
    if (constraint == null) {
        throw DbException.get(ErrorCode.CONSTRAINT_NOT_FOUND_1, constraintName);
    }
    if (getSchema().findConstraint(session, newConstraintName) != null || newConstraintName.equals(constraintName)) {
        throw DbException.get(ErrorCode.CONSTRAINT_ALREADY_EXISTS_1, newConstraintName);
    }
    session.getUser().checkRight(constraint.getTable(), Right.ALL);
    session.getUser().checkRight(constraint.getRefTable(), Right.ALL);
    session.getDatabase().renameSchemaObject(session, constraint, newConstraintName);
    return 0;
}
Also used : Constraint(org.h2.constraint.Constraint)

Example 28 with Constraint

use of org.h2.constraint.Constraint in project h2database by h2database.

the class AlterTableAddConstraint method createIndex.

private Index createIndex(Table t, IndexColumn[] cols, boolean unique) {
    int indexId = getObjectId();
    IndexType indexType;
    if (unique) {
        // for unique constraints
        indexType = IndexType.createUnique(t.isPersistIndexes(), false);
    } else {
        // constraints
        indexType = IndexType.createNonUnique(t.isPersistIndexes());
    }
    indexType.setBelongsToConstraint(true);
    String prefix = constraintName == null ? "CONSTRAINT" : constraintName;
    String indexName = t.getSchema().getUniqueIndexName(session, t, prefix + "_INDEX_");
    try {
        Index index = t.addIndex(session, indexName, indexId, cols, indexType, true, null);
        createdIndexes.add(index);
        return index;
    } finally {
        getSchema().freeUniqueName(indexName);
    }
}
Also used : Index(org.h2.index.Index) IndexType(org.h2.index.IndexType) Constraint(org.h2.constraint.Constraint)

Example 29 with Constraint

use of org.h2.constraint.Constraint in project h2database by h2database.

the class DropIndex method update.

@Override
public int update() {
    session.commit(true);
    Database db = session.getDatabase();
    Index index = getSchema().findIndex(session, indexName);
    if (index == null) {
        if (!ifExists) {
            throw DbException.get(ErrorCode.INDEX_NOT_FOUND_1, indexName);
        }
    } else {
        Table table = index.getTable();
        session.getUser().checkRight(index.getTable(), Right.ALL);
        Constraint pkConstraint = null;
        ArrayList<Constraint> constraints = table.getConstraints();
        for (int i = 0; constraints != null && i < constraints.size(); i++) {
            Constraint cons = constraints.get(i);
            if (cons.usesIndex(index)) {
                // can drop primary key index (for compatibility)
                if (Constraint.Type.PRIMARY_KEY == cons.getConstraintType()) {
                    pkConstraint = cons;
                } else {
                    throw DbException.get(ErrorCode.INDEX_BELONGS_TO_CONSTRAINT_2, indexName, cons.getName());
                }
            }
        }
        index.getTable().setModified();
        if (pkConstraint != null) {
            db.removeSchemaObject(session, pkConstraint);
        } else {
            db.removeSchemaObject(session, index);
        }
    }
    return 0;
}
Also used : Table(org.h2.table.Table) Constraint(org.h2.constraint.Constraint) Database(org.h2.engine.Database) Index(org.h2.index.Index) Constraint(org.h2.constraint.Constraint)

Example 30 with Constraint

use of org.h2.constraint.Constraint in project h2database by h2database.

the class Schema method removeChildrenAndResources.

@Override
public void removeChildrenAndResources(Session session) {
    while (triggers != null && triggers.size() > 0) {
        TriggerObject obj = (TriggerObject) triggers.values().toArray()[0];
        database.removeSchemaObject(session, obj);
    }
    while (constraints != null && constraints.size() > 0) {
        Constraint obj = (Constraint) constraints.values().toArray()[0];
        database.removeSchemaObject(session, obj);
    }
    // There can be dependencies between tables e.g. using computed columns,
    // so we might need to loop over them multiple times.
    boolean runLoopAgain = false;
    do {
        runLoopAgain = false;
        if (tablesAndViews != null) {
            // Loop over a copy because the map is modified underneath us.
            for (Table obj : new ArrayList<>(tablesAndViews.values())) {
                // in one go underneath us.
                if (obj.getName() != null) {
                    if (database.getDependentTable(obj, obj) == null) {
                        database.removeSchemaObject(session, obj);
                    } else {
                        runLoopAgain = true;
                    }
                }
            }
        }
    } while (runLoopAgain);
    while (indexes != null && indexes.size() > 0) {
        Index obj = (Index) indexes.values().toArray()[0];
        database.removeSchemaObject(session, obj);
    }
    while (sequences != null && sequences.size() > 0) {
        Sequence obj = (Sequence) sequences.values().toArray()[0];
        database.removeSchemaObject(session, obj);
    }
    while (constants != null && constants.size() > 0) {
        Constant obj = (Constant) constants.values().toArray()[0];
        database.removeSchemaObject(session, obj);
    }
    while (functions != null && functions.size() > 0) {
        FunctionAlias obj = (FunctionAlias) functions.values().toArray()[0];
        database.removeSchemaObject(session, obj);
    }
    for (Right right : database.getAllRights()) {
        if (right.getGrantedObject() == this) {
            database.removeDatabaseObject(session, right);
        }
    }
    database.removeMeta(session, getId());
    owner = null;
    invalidate();
}
Also used : RegularTable(org.h2.table.RegularTable) Table(org.h2.table.Table) Constraint(org.h2.constraint.Constraint) FunctionAlias(org.h2.engine.FunctionAlias) ArrayList(java.util.ArrayList) Right(org.h2.engine.Right) Index(org.h2.index.Index)

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