Search in sources :

Example 46 with Update

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;
}
Also used : UserAggregate(org.h2.engine.UserAggregate) Database(org.h2.engine.Database)

Example 47 with Update

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);
        }
    }
}
Also used : Column(org.h2.table.Column) IndexColumn(org.h2.table.IndexColumn) Prepared(org.h2.command.Prepared) Parameter(org.h2.expression.Parameter)

Example 48 with Update

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

Example 49 with Update

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;
}
Also used : User(org.h2.engine.User) Database(org.h2.engine.Database)

Example 50 with Update

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;
}
Also used : SchemaObject(org.h2.schema.SchemaObject) Schema(org.h2.schema.Schema) StatementBuilder(org.h2.util.StatementBuilder) Database(org.h2.engine.Database)

Aggregations

SQLException (java.sql.SQLException)44 DbException (org.h2.message.DbException)40 Database (org.h2.engine.Database)39 Connection (java.sql.Connection)37 PreparedStatement (java.sql.PreparedStatement)35 Value (org.h2.value.Value)34 ResultSet (java.sql.ResultSet)32 Statement (java.sql.Statement)31 Column (org.h2.table.Column)30 Table (org.h2.table.Table)23 JdbcConnection (org.h2.jdbc.JdbcConnection)22 Expression (org.h2.expression.Expression)19 StatementBuilder (org.h2.util.StatementBuilder)14 ValueExpression (org.h2.expression.ValueExpression)13 ValueString (org.h2.value.ValueString)13 ArrayList (java.util.ArrayList)10 Constraint (org.h2.constraint.Constraint)10 Index (org.h2.index.Index)10 IndexColumn (org.h2.table.IndexColumn)10 Task (org.h2.util.Task)10