use of org.h2.command.dml.Update in project h2database by h2database.
the class DropRole method update.
@Override
public int update() {
session.getUser().checkAdmin();
session.commit(true);
Database db = session.getDatabase();
if (roleName.equals(Constants.PUBLIC_ROLE_NAME)) {
throw DbException.get(ErrorCode.ROLE_CAN_NOT_BE_DROPPED_1, roleName);
}
Role role = db.findRole(roleName);
if (role == null) {
if (!ifExists) {
throw DbException.get(ErrorCode.ROLE_NOT_FOUND_1, roleName);
}
} else {
db.removeDatabaseObject(session, role);
}
return 0;
}
use of org.h2.command.dml.Update in project h2database by h2database.
the class SetComment method update.
@Override
public int update() {
session.commit(true);
Database db = session.getDatabase();
session.getUser().checkAdmin();
DbObject object = null;
int errorCode = ErrorCode.GENERAL_ERROR_1;
if (schemaName == null) {
schemaName = session.getCurrentSchemaName();
}
switch(objectType) {
case DbObject.CONSTANT:
object = db.getSchema(schemaName).getConstant(objectName);
break;
case DbObject.CONSTRAINT:
object = db.getSchema(schemaName).getConstraint(objectName);
break;
case DbObject.FUNCTION_ALIAS:
object = db.getSchema(schemaName).findFunction(objectName);
errorCode = ErrorCode.FUNCTION_ALIAS_NOT_FOUND_1;
break;
case DbObject.INDEX:
object = db.getSchema(schemaName).getIndex(objectName);
break;
case DbObject.ROLE:
schemaName = null;
object = db.findRole(objectName);
errorCode = ErrorCode.ROLE_NOT_FOUND_1;
break;
case DbObject.SCHEMA:
schemaName = null;
object = db.findSchema(objectName);
errorCode = ErrorCode.SCHEMA_NOT_FOUND_1;
break;
case DbObject.SEQUENCE:
object = db.getSchema(schemaName).getSequence(objectName);
break;
case DbObject.TABLE_OR_VIEW:
object = db.getSchema(schemaName).getTableOrView(session, objectName);
break;
case DbObject.TRIGGER:
object = db.getSchema(schemaName).findTrigger(objectName);
errorCode = ErrorCode.TRIGGER_NOT_FOUND_1;
break;
case DbObject.USER:
schemaName = null;
object = db.getUser(objectName);
break;
case DbObject.USER_DATATYPE:
schemaName = null;
object = db.findUserDataType(objectName);
errorCode = ErrorCode.USER_DATA_TYPE_ALREADY_EXISTS_1;
break;
default:
}
if (object == null) {
throw DbException.get(errorCode, objectName);
}
String text = expr.optimize(session).getValue(session).getString();
if (column) {
Table table = (Table) object;
table.getColumn(columnName).setComment(text);
} else {
object.setComment(text);
}
if (column || objectType == DbObject.TABLE_OR_VIEW || objectType == DbObject.USER || objectType == DbObject.INDEX || objectType == DbObject.CONSTRAINT) {
db.updateMeta(session, object);
} else {
Comment comment = db.findComment(object);
if (comment == null) {
if (text == null) {
// reset a non-existing comment - nothing to do
} else {
int id = getObjectId();
comment = new Comment(db, id, object);
comment.setCommentText(text);
db.addDatabaseObject(session, comment);
}
} else {
if (text == null) {
db.removeDatabaseObject(session, comment);
} else {
comment.setCommentText(text);
db.updateMeta(session, comment);
}
}
}
return 0;
}
use of org.h2.command.dml.Update in project h2database by h2database.
the class CreateConstant method update.
@Override
public int update() {
session.commit(true);
session.getUser().checkAdmin();
Database db = session.getDatabase();
if (getSchema().findConstant(constantName) != null) {
if (ifNotExists) {
return 0;
}
throw DbException.get(ErrorCode.CONSTANT_ALREADY_EXISTS_1, constantName);
}
int id = getObjectId();
Constant constant = new Constant(getSchema(), id, constantName);
expression = expression.optimize(session);
Value value = expression.getValue(session);
constant.setValue(value);
db.addSchemaObject(session, constant);
return 0;
}
use of org.h2.command.dml.Update in project h2database by h2database.
the class CreateSequence method update.
@Override
public int update() {
session.commit(true);
Database db = session.getDatabase();
if (getSchema().findSequence(sequenceName) != null) {
if (ifNotExists) {
return 0;
}
throw DbException.get(ErrorCode.SEQUENCE_ALREADY_EXISTS_1, sequenceName);
}
int id = getObjectId();
Long startValue = getLong(start);
Long inc = getLong(increment);
Long cache = getLong(cacheSize);
Long min = getLong(minValue);
Long max = getLong(maxValue);
Sequence sequence = new Sequence(getSchema(), id, sequenceName, startValue, inc, cache, min, max, cycle, belongsToTable);
db.addSchemaObject(session, sequence);
return 0;
}
use of org.h2.command.dml.Update in project h2database by h2database.
the class CreateTable method update.
@Override
public int update() {
if (!transactional) {
session.commit(true);
}
Database db = session.getDatabase();
if (!db.isPersistent()) {
data.persistIndexes = false;
}
boolean isSessionTemporary = data.temporary && !data.globalTemporary;
if (!isSessionTemporary) {
db.lockMeta(session);
}
if (getSchema().resolveTableOrView(session, data.tableName) != null) {
if (ifNotExists) {
return 0;
}
throw DbException.get(ErrorCode.TABLE_OR_VIEW_ALREADY_EXISTS_1, data.tableName);
}
if (asQuery != null) {
asQuery.prepare();
if (data.columns.isEmpty()) {
generateColumnsFromQuery();
} else if (data.columns.size() != asQuery.getColumnCount()) {
throw DbException.get(ErrorCode.COLUMN_COUNT_DOES_NOT_MATCH);
}
}
changePrimaryKeysToNotNull(data.columns);
data.id = getObjectId();
data.create = create;
data.session = session;
Table table = getSchema().createTable(data);
ArrayList<Sequence> sequences = generateSequences(data.columns, data.temporary);
table.setComment(comment);
if (isSessionTemporary) {
if (onCommitDrop) {
table.setOnCommitDrop(true);
}
if (onCommitTruncate) {
table.setOnCommitTruncate(true);
}
session.addLocalTempTable(table);
} else {
db.lockMeta(session);
db.addSchemaObject(session, table);
}
try {
for (Column c : data.columns) {
c.prepareExpression(session);
}
for (Sequence sequence : sequences) {
table.addSequence(sequence);
}
createConstraints();
if (asQuery != null) {
boolean old = session.isUndoLogEnabled();
try {
session.setUndoLogEnabled(false);
session.startStatementWithinTransaction();
Insert insert = new Insert(session);
insert.setSortedInsertMode(sortedInsertMode);
insert.setQuery(asQuery);
insert.setTable(table);
insert.setInsertFromSelect(true);
insert.prepare();
insert.update();
} finally {
session.setUndoLogEnabled(old);
}
}
HashSet<DbObject> set = new HashSet<>();
set.clear();
table.addDependencies(set);
for (DbObject obj : set) {
if (obj == table) {
continue;
}
if (obj.getType() == DbObject.TABLE_OR_VIEW) {
if (obj instanceof Table) {
Table t = (Table) obj;
if (t.getId() > table.getId()) {
throw DbException.get(ErrorCode.FEATURE_NOT_SUPPORTED_1, "Table depends on another table " + "with a higher ID: " + t + ", this is currently not supported, " + "as it would prevent the database from " + "being re-opened");
}
}
}
}
} catch (DbException e) {
db.checkPowerOff();
db.removeSchemaObject(session, table);
if (!transactional) {
session.commit(true);
}
throw e;
}
return 0;
}
Aggregations