Search in sources :

Example 36 with Update

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

the class Update method update.

@Override
public int update() {
    targetTableFilter.startQuery(session);
    targetTableFilter.reset();
    RowList rows = new RowList(session);
    try {
        Table table = targetTableFilter.getTable();
        session.getUser().checkRight(table, Right.UPDATE);
        table.fire(session, Trigger.UPDATE, true);
        table.lock(session, true, false);
        int columnCount = table.getColumns().length;
        // get the old rows, compute the new rows
        setCurrentRowNumber(0);
        int count = 0;
        Column[] columns = table.getColumns();
        int limitRows = -1;
        if (limitExpr != null) {
            Value v = limitExpr.getValue(session);
            if (v != ValueNull.INSTANCE) {
                limitRows = v.getInt();
            }
        }
        while (targetTableFilter.next()) {
            setCurrentRowNumber(count + 1);
            if (limitRows >= 0 && count >= limitRows) {
                break;
            }
            if (condition == null || condition.getBooleanValue(session)) {
                Row oldRow = targetTableFilter.get();
                Row newRow = table.getTemplateRow();
                boolean setOnUpdate = false;
                for (int i = 0; i < columnCount; i++) {
                    Expression newExpr = expressionMap.get(columns[i]);
                    Column column = table.getColumn(i);
                    Value newValue;
                    if (newExpr == null) {
                        if (column.getOnUpdateExpression() != null) {
                            setOnUpdate = true;
                        }
                        newValue = oldRow.getValue(i);
                    } else if (newExpr == ValueExpression.getDefault()) {
                        newValue = table.getDefaultValue(session, column);
                    } else {
                        newValue = column.convert(newExpr.getValue(session));
                    }
                    newRow.setValue(i, newValue);
                }
                if (setOnUpdate || updateToCurrentValuesReturnsZero) {
                    setOnUpdate = false;
                    for (int i = 0; i < columnCount; i++) {
                        // Use equals here to detect changes from numeric 0 to 0.0 and similar
                        if (!Objects.equals(oldRow.getValue(i), newRow.getValue(i))) {
                            setOnUpdate = true;
                            break;
                        }
                    }
                    if (setOnUpdate) {
                        for (int i = 0; i < columnCount; i++) {
                            if (expressionMap.get(columns[i]) == null) {
                                Column column = table.getColumn(i);
                                if (column.getOnUpdateExpression() != null) {
                                    newRow.setValue(i, table.getOnUpdateValue(session, column));
                                }
                            }
                        }
                    } else if (updateToCurrentValuesReturnsZero) {
                        count--;
                    }
                }
                table.validateConvertUpdateSequence(session, newRow);
                boolean done = false;
                if (table.fireRow()) {
                    done = table.fireBeforeRow(session, oldRow, newRow);
                }
                if (!done) {
                    rows.add(oldRow);
                    rows.add(newRow);
                }
                count++;
            }
        }
        // TODO self referencing referential integrity constraints
        // don't work if update is multi-row and 'inversed' the condition!
        // probably need multi-row triggers with 'deleted' and 'inserted'
        // at the same time. anyway good for sql compatibility
        // TODO update in-place (but if the key changes,
        // we need to update all indexes) before row triggers
        // the cached row is already updated - we need the old values
        table.updateRows(this, session, rows);
        if (table.fireRow()) {
            rows.invalidateCache();
            for (rows.reset(); rows.hasNext(); ) {
                Row o = rows.next();
                Row n = rows.next();
                table.fireAfterRow(session, o, n, false);
            }
        }
        table.fire(session, Trigger.UPDATE, false);
        return count;
    } finally {
        rows.close();
    }
}
Also used : RowList(org.h2.result.RowList) Table(org.h2.table.Table) Column(org.h2.table.Column) ValueExpression(org.h2.expression.ValueExpression) Expression(org.h2.expression.Expression) Value(org.h2.value.Value) Row(org.h2.result.Row)

Example 37 with Update

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

the class Set method update.

@Override
public int update() {
    Database database = session.getDatabase();
    String name = SetTypes.getTypeName(type);
    switch(type) {
        case SetTypes.ALLOW_LITERALS:
            {
                session.getUser().checkAdmin();
                int value = getIntValue();
                if (value < 0 || value > 2) {
                    throw DbException.getInvalidValueException("ALLOW_LITERALS", getIntValue());
                }
                database.setAllowLiterals(value);
                addOrUpdateSetting(name, null, value);
                break;
            }
        case SetTypes.CACHE_SIZE:
            if (getIntValue() < 0) {
                throw DbException.getInvalidValueException("CACHE_SIZE", getIntValue());
            }
            session.getUser().checkAdmin();
            database.setCacheSize(getIntValue());
            addOrUpdateSetting(name, null, getIntValue());
            break;
        case SetTypes.CLUSTER:
            {
                if (Constants.CLUSTERING_ENABLED.equals(stringValue)) {
                    // ignore, as the cluster setting is checked later
                    break;
                }
                String value = StringUtils.quoteStringSQL(stringValue);
                if (!value.equals(database.getCluster())) {
                    if (!value.equals(Constants.CLUSTERING_DISABLED)) {
                        // anybody can disable the cluster
                        // (if he can't access a cluster node)
                        session.getUser().checkAdmin();
                    }
                    database.setCluster(value);
                    // use the system session so that the current transaction
                    // (if any) is not committed
                    Session sysSession = database.getSystemSession();
                    synchronized (sysSession) {
                        synchronized (database) {
                            addOrUpdateSetting(sysSession, name, value, 0);
                            sysSession.commit(true);
                        }
                    }
                }
                break;
            }
        case SetTypes.COLLATION:
            {
                session.getUser().checkAdmin();
                final boolean binaryUnsigned = database.getCompareMode().isBinaryUnsigned();
                CompareMode compareMode;
                StringBuilder buff = new StringBuilder(stringValue);
                if (stringValue.equals(CompareMode.OFF)) {
                    compareMode = CompareMode.getInstance(null, 0, binaryUnsigned);
                } else {
                    int strength = getIntValue();
                    buff.append(" STRENGTH ");
                    if (strength == Collator.IDENTICAL) {
                        buff.append("IDENTICAL");
                    } else if (strength == Collator.PRIMARY) {
                        buff.append("PRIMARY");
                    } else if (strength == Collator.SECONDARY) {
                        buff.append("SECONDARY");
                    } else if (strength == Collator.TERTIARY) {
                        buff.append("TERTIARY");
                    }
                    compareMode = CompareMode.getInstance(stringValue, strength, binaryUnsigned);
                }
                CompareMode old = database.getCompareMode();
                if (old.equals(compareMode)) {
                    break;
                }
                Table table = database.getFirstUserTable();
                if (table != null) {
                    throw DbException.get(ErrorCode.COLLATION_CHANGE_WITH_DATA_TABLE_1, table.getSQL());
                }
                addOrUpdateSetting(name, buff.toString(), 0);
                database.setCompareMode(compareMode);
                break;
            }
        case SetTypes.BINARY_COLLATION:
            {
                session.getUser().checkAdmin();
                Table table = database.getFirstUserTable();
                if (table != null) {
                    throw DbException.get(ErrorCode.COLLATION_CHANGE_WITH_DATA_TABLE_1, table.getSQL());
                }
                CompareMode currentMode = database.getCompareMode();
                CompareMode newMode;
                if (stringValue.equals(CompareMode.SIGNED)) {
                    newMode = CompareMode.getInstance(currentMode.getName(), currentMode.getStrength(), false);
                } else if (stringValue.equals(CompareMode.UNSIGNED)) {
                    newMode = CompareMode.getInstance(currentMode.getName(), currentMode.getStrength(), true);
                } else {
                    throw DbException.getInvalidValueException("BINARY_COLLATION", stringValue);
                }
                addOrUpdateSetting(name, stringValue, 0);
                database.setCompareMode(newMode);
                break;
            }
        case SetTypes.COMPRESS_LOB:
            {
                session.getUser().checkAdmin();
                int algo = CompressTool.getCompressAlgorithm(stringValue);
                database.setLobCompressionAlgorithm(algo == Compressor.NO ? null : stringValue);
                addOrUpdateSetting(name, stringValue, 0);
                break;
            }
        case SetTypes.CREATE_BUILD:
            {
                session.getUser().checkAdmin();
                if (database.isStarting()) {
                    // just ignore the command if not starting
                    // this avoids problems when running recovery scripts
                    int value = getIntValue();
                    addOrUpdateSetting(name, null, value);
                }
                break;
            }
        case SetTypes.DATABASE_EVENT_LISTENER:
            {
                session.getUser().checkAdmin();
                database.setEventListenerClass(stringValue);
                break;
            }
        case SetTypes.DB_CLOSE_DELAY:
            {
                int x = getIntValue();
                if (x == -1) {
                // -1 is a special value for in-memory databases,
                // which means "keep the DB alive and use the same
                // DB for all connections"
                } else if (x < 0) {
                    throw DbException.getInvalidValueException("DB_CLOSE_DELAY", x);
                }
                session.getUser().checkAdmin();
                database.setCloseDelay(getIntValue());
                addOrUpdateSetting(name, null, getIntValue());
                break;
            }
        case SetTypes.DEFAULT_LOCK_TIMEOUT:
            if (getIntValue() < 0) {
                throw DbException.getInvalidValueException("DEFAULT_LOCK_TIMEOUT", getIntValue());
            }
            session.getUser().checkAdmin();
            addOrUpdateSetting(name, null, getIntValue());
            break;
        case SetTypes.DEFAULT_TABLE_TYPE:
            session.getUser().checkAdmin();
            database.setDefaultTableType(getIntValue());
            addOrUpdateSetting(name, null, getIntValue());
            break;
        case SetTypes.EXCLUSIVE:
            {
                session.getUser().checkAdmin();
                int value = getIntValue();
                switch(value) {
                    case 0:
                        database.setExclusiveSession(null, false);
                        break;
                    case 1:
                        database.setExclusiveSession(session, false);
                        break;
                    case 2:
                        database.setExclusiveSession(session, true);
                        break;
                    default:
                        throw DbException.getInvalidValueException("EXCLUSIVE", value);
                }
                break;
            }
        case SetTypes.JAVA_OBJECT_SERIALIZER:
            {
                session.getUser().checkAdmin();
                Table table = database.getFirstUserTable();
                if (table != null) {
                    throw DbException.get(ErrorCode.JAVA_OBJECT_SERIALIZER_CHANGE_WITH_DATA_TABLE, table.getSQL());
                }
                database.setJavaObjectSerializerName(stringValue);
                addOrUpdateSetting(name, stringValue, 0);
                break;
            }
        case SetTypes.IGNORECASE:
            session.getUser().checkAdmin();
            database.setIgnoreCase(getIntValue() == 1);
            addOrUpdateSetting(name, null, getIntValue());
            break;
        case SetTypes.LOCK_MODE:
            session.getUser().checkAdmin();
            database.setLockMode(getIntValue());
            addOrUpdateSetting(name, null, getIntValue());
            break;
        case SetTypes.LOCK_TIMEOUT:
            if (getIntValue() < 0) {
                throw DbException.getInvalidValueException("LOCK_TIMEOUT", getIntValue());
            }
            session.setLockTimeout(getIntValue());
            break;
        case SetTypes.LOG:
            {
                int value = getIntValue();
                if (database.isPersistent() && value != database.getLogMode()) {
                    session.getUser().checkAdmin();
                    database.setLogMode(value);
                }
                break;
            }
        case SetTypes.MAX_LENGTH_INPLACE_LOB:
            {
                if (getIntValue() < 0) {
                    throw DbException.getInvalidValueException("MAX_LENGTH_INPLACE_LOB", getIntValue());
                }
                session.getUser().checkAdmin();
                database.setMaxLengthInplaceLob(getIntValue());
                addOrUpdateSetting(name, null, getIntValue());
                break;
            }
        case SetTypes.MAX_LOG_SIZE:
            if (getIntValue() < 0) {
                throw DbException.getInvalidValueException("MAX_LOG_SIZE", getIntValue());
            }
            session.getUser().checkAdmin();
            database.setMaxLogSize((long) getIntValue() * 1024 * 1024);
            addOrUpdateSetting(name, null, getIntValue());
            break;
        case SetTypes.MAX_MEMORY_ROWS:
            {
                if (getIntValue() < 0) {
                    throw DbException.getInvalidValueException("MAX_MEMORY_ROWS", getIntValue());
                }
                session.getUser().checkAdmin();
                database.setMaxMemoryRows(getIntValue());
                addOrUpdateSetting(name, null, getIntValue());
                break;
            }
        case SetTypes.MAX_MEMORY_UNDO:
            {
                if (getIntValue() < 0) {
                    throw DbException.getInvalidValueException("MAX_MEMORY_UNDO", getIntValue());
                }
                session.getUser().checkAdmin();
                database.setMaxMemoryUndo(getIntValue());
                addOrUpdateSetting(name, null, getIntValue());
                break;
            }
        case SetTypes.MAX_OPERATION_MEMORY:
            {
                if (getIntValue() < 0) {
                    throw DbException.getInvalidValueException("MAX_OPERATION_MEMORY", getIntValue());
                }
                session.getUser().checkAdmin();
                int value = getIntValue();
                database.setMaxOperationMemory(value);
                break;
            }
        case SetTypes.MODE:
            Mode mode = Mode.getInstance(stringValue);
            if (mode == null) {
                throw DbException.get(ErrorCode.UNKNOWN_MODE_1, stringValue);
            }
            if (database.getMode() != mode) {
                session.getUser().checkAdmin();
                database.setMode(mode);
                session.getColumnNamerConfiguration().configure(mode.getEnum());
            }
            break;
        case SetTypes.MULTI_THREADED:
            {
                session.getUser().checkAdmin();
                database.setMultiThreaded(getIntValue() == 1);
                break;
            }
        case SetTypes.MVCC:
            {
                if (database.isMultiVersion() != (getIntValue() == 1)) {
                    throw DbException.get(ErrorCode.CANNOT_CHANGE_SETTING_WHEN_OPEN_1, "MVCC");
                }
                break;
            }
        case SetTypes.OPTIMIZE_REUSE_RESULTS:
            {
                session.getUser().checkAdmin();
                database.setOptimizeReuseResults(getIntValue() != 0);
                break;
            }
        case SetTypes.QUERY_TIMEOUT:
            {
                if (getIntValue() < 0) {
                    throw DbException.getInvalidValueException("QUERY_TIMEOUT", getIntValue());
                }
                int value = getIntValue();
                session.setQueryTimeout(value);
                break;
            }
        case SetTypes.REDO_LOG_BINARY:
            {
                int value = getIntValue();
                session.setRedoLogBinary(value == 1);
                break;
            }
        case SetTypes.REFERENTIAL_INTEGRITY:
            {
                session.getUser().checkAdmin();
                int value = getIntValue();
                if (value < 0 || value > 1) {
                    throw DbException.getInvalidValueException("REFERENTIAL_INTEGRITY", getIntValue());
                }
                database.setReferentialIntegrity(value == 1);
                break;
            }
        case SetTypes.QUERY_STATISTICS:
            {
                session.getUser().checkAdmin();
                int value = getIntValue();
                if (value < 0 || value > 1) {
                    throw DbException.getInvalidValueException("QUERY_STATISTICS", getIntValue());
                }
                database.setQueryStatistics(value == 1);
                break;
            }
        case SetTypes.QUERY_STATISTICS_MAX_ENTRIES:
            {
                session.getUser().checkAdmin();
                int value = getIntValue();
                if (value < 1) {
                    throw DbException.getInvalidValueException("QUERY_STATISTICS_MAX_ENTRIES", getIntValue());
                }
                database.setQueryStatisticsMaxEntries(value);
                break;
            }
        case SetTypes.SCHEMA:
            {
                Schema schema = database.getSchema(stringValue);
                session.setCurrentSchema(schema);
                break;
            }
        case SetTypes.SCHEMA_SEARCH_PATH:
            {
                session.setSchemaSearchPath(stringValueList);
                break;
            }
        case SetTypes.TRACE_LEVEL_FILE:
            session.getUser().checkAdmin();
            if (getCurrentObjectId() == 0) {
                // don't set the property when opening the database
                // this is for compatibility with older versions, because
                // this setting was persistent
                database.getTraceSystem().setLevelFile(getIntValue());
            }
            break;
        case SetTypes.TRACE_LEVEL_SYSTEM_OUT:
            session.getUser().checkAdmin();
            if (getCurrentObjectId() == 0) {
                // don't set the property when opening the database
                // this is for compatibility with older versions, because
                // this setting was persistent
                database.getTraceSystem().setLevelSystemOut(getIntValue());
            }
            break;
        case SetTypes.TRACE_MAX_FILE_SIZE:
            {
                if (getIntValue() < 0) {
                    throw DbException.getInvalidValueException("TRACE_MAX_FILE_SIZE", getIntValue());
                }
                session.getUser().checkAdmin();
                int size = getIntValue() * 1024 * 1024;
                database.getTraceSystem().setMaxFileSize(size);
                addOrUpdateSetting(name, null, getIntValue());
                break;
            }
        case SetTypes.THROTTLE:
            {
                if (getIntValue() < 0) {
                    throw DbException.getInvalidValueException("THROTTLE", getIntValue());
                }
                session.setThrottle(getIntValue());
                break;
            }
        case SetTypes.UNDO_LOG:
            {
                int value = getIntValue();
                if (value < 0 || value > 1) {
                    throw DbException.getInvalidValueException("UNDO_LOG", getIntValue());
                }
                session.setUndoLogEnabled(value == 1);
                break;
            }
        case SetTypes.VARIABLE:
            {
                Expression expr = expression.optimize(session);
                session.setVariable(stringValue, expr.getValue(session));
                break;
            }
        case SetTypes.WRITE_DELAY:
            {
                if (getIntValue() < 0) {
                    throw DbException.getInvalidValueException("WRITE_DELAY", getIntValue());
                }
                session.getUser().checkAdmin();
                database.setWriteDelay(getIntValue());
                addOrUpdateSetting(name, null, getIntValue());
                break;
            }
        case SetTypes.RETENTION_TIME:
            {
                if (getIntValue() < 0) {
                    throw DbException.getInvalidValueException("RETENTION_TIME", getIntValue());
                }
                session.getUser().checkAdmin();
                database.setRetentionTime(getIntValue());
                addOrUpdateSetting(name, null, getIntValue());
                break;
            }
        case SetTypes.ROW_FACTORY:
            {
                session.getUser().checkAdmin();
                String rowFactoryName = expression.getColumnName();
                Class<RowFactory> rowFactoryClass = JdbcUtils.loadUserClass(rowFactoryName);
                RowFactory rowFactory;
                try {
                    rowFactory = rowFactoryClass.newInstance();
                } catch (Exception e) {
                    throw DbException.convert(e);
                }
                database.setRowFactory(rowFactory);
                break;
            }
        case SetTypes.BATCH_JOINS:
            {
                int value = getIntValue();
                if (value != 0 && value != 1) {
                    throw DbException.getInvalidValueException("BATCH_JOINS", getIntValue());
                }
                session.setJoinBatchEnabled(value == 1);
                break;
            }
        case SetTypes.FORCE_JOIN_ORDER:
            {
                int value = getIntValue();
                if (value != 0 && value != 1) {
                    throw DbException.getInvalidValueException("FORCE_JOIN_ORDER", value);
                }
                session.setForceJoinOrder(value == 1);
                break;
            }
        case SetTypes.LAZY_QUERY_EXECUTION:
            {
                int value = getIntValue();
                if (value != 0 && value != 1) {
                    throw DbException.getInvalidValueException("LAZY_QUERY_EXECUTION", value);
                }
                session.setLazyQueryExecution(value == 1);
                break;
            }
        case SetTypes.BUILTIN_ALIAS_OVERRIDE:
            {
                session.getUser().checkAdmin();
                int value = getIntValue();
                if (value != 0 && value != 1) {
                    throw DbException.getInvalidValueException("BUILTIN_ALIAS_OVERRIDE", value);
                }
                database.setAllowBuiltinAliasOverride(value == 1);
                break;
            }
        case SetTypes.COLUMN_NAME_RULES:
            {
                session.getUser().checkAdmin();
                session.getColumnNamerConfiguration().configure(expression.getColumnName());
                break;
            }
        default:
            DbException.throwInternalError("type=" + type);
    }
    // the meta data information has changed
    database.getNextModificationDataId();
    // query caches might be affected as well, for example
    // when changing the compatibility mode
    database.getNextModificationMetaId();
    return 0;
}
Also used : RowFactory(org.h2.result.RowFactory) Table(org.h2.table.Table) ValueExpression(org.h2.expression.ValueExpression) Expression(org.h2.expression.Expression) CompareMode(org.h2.value.CompareMode) Mode(org.h2.engine.Mode) Schema(org.h2.schema.Schema) Database(org.h2.engine.Database) CompareMode(org.h2.value.CompareMode) DbException(org.h2.message.DbException) Session(org.h2.engine.Session)

Example 38 with Update

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

the class AlterSequence method update.

@Override
public int update() {
    Database db = session.getDatabase();
    if (sequence == null) {
        sequence = getSchema().findSequence(sequenceName);
        if (sequence == null) {
            if (!ifExists) {
                throw DbException.get(ErrorCode.SEQUENCE_NOT_FOUND_1, sequenceName);
            }
            return 0;
        }
    }
    if (table != null) {
        session.getUser().checkRight(table, Right.ALL);
    }
    if (cycle != null) {
        sequence.setCycle(cycle);
    }
    if (cacheSize != null) {
        long size = cacheSize.optimize(session).getValue(session).getLong();
        sequence.setCacheSize(size);
    }
    if (start != null || minValue != null || maxValue != null || increment != null) {
        Long startValue = getLong(start);
        Long min = getLong(minValue);
        Long max = getLong(maxValue);
        Long inc = getLong(increment);
        sequence.modify(startValue, min, max, inc);
    }
    db.updateMeta(session, sequence);
    return 0;
}
Also used : Database(org.h2.engine.Database)

Example 39 with Update

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

the class Delete method update.

@Override
public int update() {
    targetTableFilter.startQuery(session);
    targetTableFilter.reset();
    Table table = targetTableFilter.getTable();
    session.getUser().checkRight(table, Right.DELETE);
    table.fire(session, Trigger.DELETE, true);
    table.lock(session, true, false);
    RowList rows = new RowList(session);
    int limitRows = -1;
    if (limitExpr != null) {
        Value v = limitExpr.getValue(session);
        if (v != ValueNull.INSTANCE) {
            limitRows = v.getInt();
        }
    }
    try {
        setCurrentRowNumber(0);
        int count = 0;
        while (limitRows != 0 && targetTableFilter.next()) {
            setCurrentRowNumber(rows.size() + 1);
            if (condition == null || condition.getBooleanValue(session)) {
                Row row = targetTableFilter.get();
                boolean done = false;
                if (table.fireRow()) {
                    done = table.fireBeforeRow(session, row, null);
                }
                if (!done) {
                    rows.add(row);
                }
                count++;
                if (limitRows >= 0 && count >= limitRows) {
                    break;
                }
            }
        }
        int rowScanCount = 0;
        for (rows.reset(); rows.hasNext(); ) {
            if ((++rowScanCount & 127) == 0) {
                checkCanceled();
            }
            Row row = rows.next();
            table.removeRow(session, row);
            session.log(table, UndoLogRecord.DELETE, row);
        }
        if (table.fireRow()) {
            for (rows.reset(); rows.hasNext(); ) {
                Row row = rows.next();
                table.fireAfterRow(session, row, null, false);
            }
        }
        table.fire(session, Trigger.DELETE, false);
        return count;
    } finally {
        rows.close();
    }
}
Also used : RowList(org.h2.result.RowList) Table(org.h2.table.Table) Value(org.h2.value.Value) Row(org.h2.result.Row)

Example 40 with Update

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

the class Merge method merge.

/**
 * Merge the given row.
 *
 * @param row the row
 */
protected void merge(Row row) {
    ArrayList<Parameter> k = update.getParameters();
    for (int i = 0; i < columns.length; i++) {
        Column col = columns[i];
        Value v = row.getValue(col.getColumnId());
        Parameter p = k.get(i);
        p.setValue(v);
    }
    for (int i = 0; i < keys.length; i++) {
        Column col = keys[i];
        Value v = row.getValue(col.getColumnId());
        if (v == null) {
            throw DbException.get(ErrorCode.COLUMN_CONTAINS_NULL_VALUES_1, col.getSQL());
        }
        Parameter p = k.get(columns.length + i);
        p.setValue(v);
    }
    // try and update
    int count = update.update();
    // if update fails try an insert
    if (count == 0) {
        try {
            targetTable.validateConvertUpdateSequence(session, row);
            boolean done = targetTable.fireBeforeRow(session, null, row);
            if (!done) {
                targetTable.lock(session, true, false);
                targetTable.addRow(session, row);
                session.getGeneratedKeys().confirmRow(row);
                session.log(targetTable, UndoLogRecord.INSERT, row);
                targetTable.fireAfterRow(session, null, row, false);
            }
        } catch (DbException e) {
            if (e.getErrorCode() == ErrorCode.DUPLICATE_KEY_1) {
                // possibly a concurrent merge or insert
                Index index = (Index) e.getSource();
                if (index != null) {
                    // verify the index columns match the key
                    Column[] indexColumns = index.getColumns();
                    boolean indexMatchesKeys = true;
                    if (indexColumns.length <= keys.length) {
                        for (int i = 0; i < indexColumns.length; i++) {
                            if (indexColumns[i] != keys[i]) {
                                indexMatchesKeys = false;
                                break;
                            }
                        }
                    }
                    if (indexMatchesKeys) {
                        throw DbException.get(ErrorCode.CONCURRENT_UPDATE_1, targetTable.getName());
                    }
                }
            }
            throw e;
        }
    } else if (count != 1) {
        throw DbException.get(ErrorCode.DUPLICATE_KEY_1, targetTable.getSQL());
    }
}
Also used : Column(org.h2.table.Column) SequenceValue(org.h2.expression.SequenceValue) Value(org.h2.value.Value) Parameter(org.h2.expression.Parameter) Index(org.h2.index.Index) DbException(org.h2.message.DbException)

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