use of org.h2.command.dml.Update in project h2database by h2database.
the class DropAggregate method update.
@Override
public int update() {
session.getUser().checkAdmin();
session.commit(true);
Database db = session.getDatabase();
UserAggregate aggregate = db.findAggregate(name);
if (aggregate == null) {
if (!ifExists) {
throw DbException.get(ErrorCode.AGGREGATE_NOT_FOUND_1, name);
}
} else {
db.removeDatabaseObject(session, aggregate);
}
return 0;
}
use of org.h2.command.dml.Update in project h2database by h2database.
the class ConstraintReferential method checkRowRefTable.
private void checkRowRefTable(Session session, Row oldRow, Row newRow) {
if (oldRow == null) {
// this is an insert
return;
}
if (newRow != null && isEqual(oldRow, newRow)) {
// on an update, if both old and new are the same, don't do anything
return;
}
if (newRow == null) {
// this is a delete
if (deleteAction == ConstraintActionType.RESTRICT) {
checkRow(session, oldRow);
} else {
int i = deleteAction == ConstraintActionType.CASCADE ? 0 : columns.length;
Prepared deleteCommand = getDelete(session);
setWhere(deleteCommand, i, oldRow);
updateWithSkipCheck(deleteCommand);
}
} else {
// this is an update
if (updateAction == ConstraintActionType.RESTRICT) {
checkRow(session, oldRow);
} else {
Prepared updateCommand = getUpdate(session);
if (updateAction == ConstraintActionType.CASCADE) {
ArrayList<Parameter> params = updateCommand.getParameters();
for (int i = 0, len = columns.length; i < len; i++) {
Parameter param = params.get(i);
Column refCol = refColumns[i].column;
param.setValue(newRow.getValue(refCol.getColumnId()));
}
}
setWhere(updateCommand, columns.length, oldRow);
updateWithSkipCheck(updateCommand);
}
}
}
use of org.h2.command.dml.Update in project h2database by h2database.
the class ConstraintReferential method getCreateSQLForCopy.
/**
* Create the SQL statement of this object so a copy of the table can be
* made.
*
* @param forTable the table to create the object for
* @param forRefTable the referenced table
* @param quotedName the name of this object (quoted if necessary)
* @param internalIndex add the index name to the statement
* @return the SQL statement
*/
public String getCreateSQLForCopy(Table forTable, Table forRefTable, String quotedName, boolean internalIndex) {
StatementBuilder buff = new StatementBuilder("ALTER TABLE ");
String mainTable = forTable.getSQL();
buff.append(mainTable).append(" ADD CONSTRAINT ");
if (forTable.isHidden()) {
buff.append("IF NOT EXISTS ");
}
buff.append(quotedName);
if (comment != null) {
buff.append(" COMMENT ").append(StringUtils.quoteStringSQL(comment));
}
IndexColumn[] cols = columns;
IndexColumn[] refCols = refColumns;
buff.append(" FOREIGN KEY(");
for (IndexColumn c : cols) {
buff.appendExceptFirst(", ");
buff.append(c.getSQL());
}
buff.append(')');
if (internalIndex && indexOwner && forTable == this.table) {
buff.append(" INDEX ").append(index.getSQL());
}
buff.append(" REFERENCES ");
String quotedRefTable;
if (this.table == this.refTable) {
// self-referencing constraints: need to use new table
quotedRefTable = forTable.getSQL();
} else {
quotedRefTable = forRefTable.getSQL();
}
buff.append(quotedRefTable).append('(');
buff.resetCount();
for (IndexColumn r : refCols) {
buff.appendExceptFirst(", ");
buff.append(r.getSQL());
}
buff.append(')');
if (internalIndex && refIndexOwner && forTable == this.table) {
buff.append(" INDEX ").append(refIndex.getSQL());
}
if (deleteAction != ConstraintActionType.RESTRICT) {
buff.append(" ON DELETE ").append(deleteAction.getSqlName());
}
if (updateAction != ConstraintActionType.RESTRICT) {
buff.append(" ON UPDATE ").append(updateAction.getSqlName());
}
return buff.append(" NOCHECK").toString();
}
use of org.h2.command.dml.Update in project h2database by h2database.
the class CreateUser method update.
@Override
public int update() {
session.getUser().checkAdmin();
session.commit(true);
Database db = session.getDatabase();
if (db.findRole(userName) != null) {
throw DbException.get(ErrorCode.ROLE_ALREADY_EXISTS_1, userName);
}
if (db.findUser(userName) != null) {
if (ifNotExists) {
return 0;
}
throw DbException.get(ErrorCode.USER_ALREADY_EXISTS_1, userName);
}
int id = getObjectId();
User user = new User(db, id, userName, false);
user.setAdmin(admin);
user.setComment(comment);
if (hash != null && salt != null) {
setSaltAndHash(user, session, salt, hash);
} else if (password != null) {
setPassword(user, session, password);
} else {
throw DbException.throwInternalError();
}
db.addDatabaseObject(session, user);
return 0;
}
use of org.h2.command.dml.Update in project h2database by h2database.
the class DropSchema method update.
@Override
public int update() {
session.getUser().checkSchemaAdmin();
session.commit(true);
Database db = session.getDatabase();
Schema schema = db.findSchema(schemaName);
if (schema == null) {
if (!ifExists) {
throw DbException.get(ErrorCode.SCHEMA_NOT_FOUND_1, schemaName);
}
} else {
if (!schema.canDrop()) {
throw DbException.get(ErrorCode.SCHEMA_CAN_NOT_BE_DROPPED_1, schemaName);
}
if (dropAction == ConstraintActionType.RESTRICT && !schema.isEmpty()) {
StatementBuilder buff = new StatementBuilder();
for (SchemaObject object : schema.getAll()) {
buff.appendExceptFirst(", ");
buff.append(object.getName());
}
if (buff.length() > 0) {
throw DbException.get(ErrorCode.CANNOT_DROP_2, schemaName, buff.toString());
}
}
db.removeDatabaseObject(session, schema);
}
return 0;
}
Aggregations