Search in sources :

Example 21 with Update

use of org.h2.command.dml.Update in project h2database by h2database.

the class Parser method parseCreateTrigger.

private CreateTrigger parseCreateTrigger(boolean force) {
    boolean ifNotExists = readIfNotExists();
    String triggerName = readIdentifierWithSchema(null);
    Schema schema = getSchema();
    boolean insteadOf, isBefore;
    if (readIf("INSTEAD")) {
        read("OF");
        isBefore = true;
        insteadOf = true;
    } else if (readIf("BEFORE")) {
        insteadOf = false;
        isBefore = true;
    } else {
        read("AFTER");
        insteadOf = false;
        isBefore = false;
    }
    int typeMask = 0;
    boolean onRollback = false;
    do {
        if (readIf("INSERT")) {
            typeMask |= Trigger.INSERT;
        } else if (readIf("UPDATE")) {
            typeMask |= Trigger.UPDATE;
        } else if (readIf("DELETE")) {
            typeMask |= Trigger.DELETE;
        } else if (readIf("SELECT")) {
            typeMask |= Trigger.SELECT;
        } else if (readIf("ROLLBACK")) {
            onRollback = true;
        } else {
            throw getSyntaxError();
        }
    } while (readIf(","));
    read("ON");
    String tableName = readIdentifierWithSchema();
    checkSchema(schema);
    CreateTrigger command = new CreateTrigger(session, getSchema());
    command.setForce(force);
    command.setTriggerName(triggerName);
    command.setIfNotExists(ifNotExists);
    command.setInsteadOf(insteadOf);
    command.setBefore(isBefore);
    command.setOnRollback(onRollback);
    command.setTypeMask(typeMask);
    command.setTableName(tableName);
    if (readIf("FOR")) {
        read("EACH");
        read("ROW");
        command.setRowBased(true);
    } else {
        command.setRowBased(false);
    }
    if (readIf("QUEUE")) {
        command.setQueueSize(readPositiveInt());
    }
    command.setNoWait(readIf("NOWAIT"));
    if (readIf("AS")) {
        command.setTriggerSource(readString());
    } else {
        read("CALL");
        command.setTriggerClassName(readUniqueIdentifier());
    }
    return command;
}
Also used : CreateTrigger(org.h2.command.ddl.CreateTrigger) DropSchema(org.h2.command.ddl.DropSchema) CreateSchema(org.h2.command.ddl.CreateSchema) Schema(org.h2.schema.Schema) ValueString(org.h2.value.ValueString) AlterTableRenameConstraint(org.h2.command.ddl.AlterTableRenameConstraint) AlterTableAddConstraint(org.h2.command.ddl.AlterTableAddConstraint) AlterTableDropConstraint(org.h2.command.ddl.AlterTableDropConstraint)

Example 22 with Update

use of org.h2.command.dml.Update in project h2database by h2database.

the class Parser method parseWith.

private Prepared parseWith() {
    List<TableView> viewsCreated = new ArrayList<>();
    readIf("RECURSIVE");
    // this WITH statement might not be a temporary view - allow optional keyword to
    // tell us that this keyword. This feature will not be documented - H2 internal use only.
    boolean isPersistent = readIf("PERSISTENT");
    // as in CREATE VIEW abc AS WITH my_cte - this auto detects that condition
    if (session.isParsingCreateView()) {
        isPersistent = true;
    }
    do {
        viewsCreated.add(parseSingleCommonTableExpression(isPersistent));
    } while (readIf(","));
    Prepared p = null;
    // reverse the order of constructed CTE views - as the destruction order
    // (since later created view may depend on previously created views -
    // we preserve that dependency order in the destruction sequence )
    // used in setCteCleanups
    Collections.reverse(viewsCreated);
    if (isToken("SELECT")) {
        Query query = parseSelectUnion();
        query.setPrepareAlways(true);
        query.setNeverLazy(true);
        p = query;
    } else if (readIf("INSERT")) {
        p = parseInsert();
        p.setPrepareAlways(true);
    } else if (readIf("UPDATE")) {
        p = parseUpdate();
        p.setPrepareAlways(true);
    } else if (readIf("MERGE")) {
        p = parseMerge();
        p.setPrepareAlways(true);
    } else if (readIf("DELETE")) {
        p = parseDelete();
        p.setPrepareAlways(true);
    } else if (readIf("CREATE")) {
        if (!isToken("TABLE")) {
            throw DbException.get(ErrorCode.SYNTAX_ERROR_1, WITH_STATEMENT_SUPPORTS_LIMITED_SUB_STATEMENTS);
        }
        p = parseCreate();
        p.setPrepareAlways(true);
    } else {
        throw DbException.get(ErrorCode.SYNTAX_ERROR_1, WITH_STATEMENT_SUPPORTS_LIMITED_SUB_STATEMENTS);
    }
    // dependencies) - but only if they are not persistent
    if (!isPersistent) {
        p.setCteCleanups(viewsCreated);
    }
    return p;
}
Also used : Query(org.h2.command.dml.Query) ArrayList(java.util.ArrayList) TableView(org.h2.table.TableView)

Example 23 with Update

use of org.h2.command.dml.Update in project h2database by h2database.

the class Parser method parseInsert.

private Insert parseInsert() {
    Insert command = new Insert(session);
    currentPrepared = command;
    if (database.getMode().onDuplicateKeyUpdate && readIf("IGNORE")) {
        command.setIgnore(true);
    }
    read("INTO");
    Table table = readTableOrView();
    command.setTable(table);
    Insert returnedCommand = parseInsertGivenTable(command, table);
    if (returnedCommand != null) {
        return returnedCommand;
    }
    if (database.getMode().onDuplicateKeyUpdate) {
        if (readIf("ON")) {
            read("DUPLICATE");
            read("KEY");
            read("UPDATE");
            do {
                String columnName = readColumnIdentifier();
                if (readIf(".")) {
                    String schemaOrTableName = columnName;
                    String tableOrColumnName = readColumnIdentifier();
                    if (readIf(".")) {
                        if (!table.getSchema().getName().equals(schemaOrTableName)) {
                            throw DbException.get(ErrorCode.SCHEMA_NAME_MUST_MATCH);
                        }
                        columnName = readColumnIdentifier();
                    } else {
                        columnName = tableOrColumnName;
                        tableOrColumnName = schemaOrTableName;
                    }
                    if (!table.getName().equals(tableOrColumnName)) {
                        throw DbException.get(ErrorCode.TABLE_OR_VIEW_NOT_FOUND_1, tableOrColumnName);
                    }
                }
                Column column = table.getColumn(columnName);
                read("=");
                Expression expression;
                if (readIf("DEFAULT")) {
                    expression = ValueExpression.getDefault();
                } else {
                    expression = readExpression();
                }
                command.addAssignmentForDuplicate(column, expression);
            } while (readIf(","));
        }
    }
    if (database.getMode().isolationLevelInSelectOrInsertStatement) {
        parseIsolationClause();
    }
    return command;
}
Also used : RangeTable(org.h2.table.RangeTable) TruncateTable(org.h2.command.ddl.TruncateTable) CreateTable(org.h2.command.ddl.CreateTable) FunctionTable(org.h2.table.FunctionTable) CreateLinkedTable(org.h2.command.ddl.CreateLinkedTable) Table(org.h2.table.Table) DropTable(org.h2.command.ddl.DropTable) AlterTableRenameColumn(org.h2.command.ddl.AlterTableRenameColumn) AlterTableAlterColumn(org.h2.command.ddl.AlterTableAlterColumn) Column(org.h2.table.Column) ExpressionColumn(org.h2.expression.ExpressionColumn) IndexColumn(org.h2.table.IndexColumn) Expression(org.h2.expression.Expression) ValueExpression(org.h2.expression.ValueExpression) ValueString(org.h2.value.ValueString) Insert(org.h2.command.dml.Insert)

Example 24 with Update

use of org.h2.command.dml.Update in project h2database by h2database.

the class Command method executeUpdate.

@Override
public ResultWithGeneratedKeys executeUpdate(Object generatedKeysRequest) {
    long start = 0;
    Database database = session.getDatabase();
    Object sync = database.isMultiThreaded() ? (Object) session : (Object) database;
    session.waitIfExclusiveModeEnabled();
    boolean callStop = true;
    boolean writing = !isReadOnly();
    if (writing) {
        while (!database.beforeWriting()) {
        // wait
        }
    }
    synchronized (sync) {
        Session.Savepoint rollback = session.setSavepoint();
        session.setCurrentCommand(this, generatedKeysRequest);
        try {
            while (true) {
                database.checkPowerOff();
                try {
                    int updateCount = update();
                    if (!Boolean.FALSE.equals(generatedKeysRequest)) {
                        return new ResultWithGeneratedKeys.WithKeys(updateCount, session.getGeneratedKeys().getKeys(session));
                    }
                    return ResultWithGeneratedKeys.of(updateCount);
                } catch (DbException e) {
                    start = filterConcurrentUpdate(e, start);
                } catch (OutOfMemoryError e) {
                    callStop = false;
                    database.shutdownImmediately();
                    throw DbException.convert(e);
                } catch (Throwable e) {
                    throw DbException.convert(e);
                }
            }
        } catch (DbException e) {
            e = e.addSQL(sql);
            SQLException s = e.getSQLException();
            database.exceptionThrown(s, sql);
            if (s.getErrorCode() == ErrorCode.OUT_OF_MEMORY) {
                callStop = false;
                database.shutdownImmediately();
                throw e;
            }
            database.checkPowerOff();
            if (s.getErrorCode() == ErrorCode.DEADLOCK_1) {
                session.rollback();
            } else {
                session.rollbackTo(rollback, false);
            }
            throw e;
        } finally {
            try {
                if (callStop) {
                    stop();
                }
            } finally {
                if (writing) {
                    database.afterWriting();
                }
            }
        }
    }
}
Also used : SQLException(java.sql.SQLException) Database(org.h2.engine.Database) Session(org.h2.engine.Session) DbException(org.h2.message.DbException)

Example 25 with Update

use of org.h2.command.dml.Update in project h2database by h2database.

the class AlterTableDropConstraint method update.

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

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