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;
}
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;
}
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);
}
}
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;
}
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();
}
Aggregations