Search in sources :

Example 51 with Update

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

the class DropSynonym method update.

@Override
public int update() {
    session.commit(true);
    session.getUser().checkAdmin();
    TableSynonym synonym = getSchema().getSynonym(synonymName);
    if (synonym == null) {
        if (!ifExists) {
            throw DbException.get(ErrorCode.TABLE_OR_VIEW_NOT_FOUND_1, synonymName);
        }
    } else {
        session.getDatabase().removeSchemaObject(session, synonym);
    }
    return 0;
}
Also used : TableSynonym(org.h2.table.TableSynonym)

Example 52 with Update

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

the class Session method unlockReadLocks.

/**
 * Unlock all read locks. This is done if the transaction isolation mode is
 * READ_COMMITTED.
 */
public void unlockReadLocks() {
    if (database.isMultiVersion()) {
        // MVCC: keep shared locks (insert / update / delete)
        return;
    }
    // locks is modified in the loop
    for (int i = 0; i < locks.size(); i++) {
        Table t = locks.get(i);
        if (!t.isLockedExclusively()) {
            synchronized (database) {
                t.unlock(this);
                locks.remove(i);
            }
            i--;
        }
    }
}
Also used : MVTable(org.h2.mvstore.db.MVTable) Table(org.h2.table.Table) Constraint(org.h2.constraint.Constraint)

Example 53 with Update

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

the class PageBtreeNode method remove.

@Override
SearchRow remove(SearchRow row) {
    int at = find(row, false, false, true);
    // merge is not implemented to allow concurrent usage
    // TODO maybe implement merge
    PageBtree page = index.getPage(childPageIds[at]);
    SearchRow last = page.remove(row);
    index.getPageStore().logUndo(this, data);
    updateRowCount(-1);
    written = false;
    changeCount = index.getPageStore().getChangeCount();
    if (last == null) {
        // the last row didn't change - nothing to do
        return null;
    } else if (last == row) {
        // this child is now empty
        index.getPageStore().free(page.getPos());
        if (entryCount < 1) {
            // no more children - this page is empty as well
            return row;
        }
        if (at == entryCount) {
            // removing the last child
            last = getRow(at - 1);
        } else {
            last = null;
        }
        removeChild(at);
        index.getPageStore().update(this);
        return last;
    }
    // the last row is in the last child
    if (at == entryCount) {
        return last;
    }
    int child = childPageIds[at];
    removeChild(at);
    // TODO this can mean only the position is now stored
    // should split at the next possible moment
    addChild(at, child, last);
    // remove and add swapped two children, fix that
    int temp = childPageIds[at];
    childPageIds[at] = childPageIds[at + 1];
    childPageIds[at + 1] = temp;
    index.getPageStore().update(this);
    return null;
}
Also used : SearchRow(org.h2.result.SearchRow)

Example 54 with Update

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

the class LinkedIndex method update.

/**
 * Update a row using a UPDATE statement. This method is to be called if the
 * emit updates option is enabled.
 *
 * @param oldRow the old data
 * @param newRow the new data
 */
public void update(Row oldRow, Row newRow) {
    ArrayList<Value> params = New.arrayList();
    StatementBuilder buff = new StatementBuilder("UPDATE ");
    buff.append(targetTableName).append(" SET ");
    for (int i = 0; i < newRow.getColumnCount(); i++) {
        buff.appendExceptFirst(", ");
        buff.append(table.getColumn(i).getSQL()).append('=');
        Value v = newRow.getValue(i);
        if (v == null) {
            buff.append("DEFAULT");
        } else {
            buff.append('?');
            params.add(v);
        }
    }
    buff.append(" WHERE ");
    buff.resetCount();
    for (int i = 0; i < oldRow.getColumnCount(); i++) {
        Column col = table.getColumn(i);
        buff.appendExceptFirst(" AND ");
        buff.append(col.getSQL());
        Value v = oldRow.getValue(i);
        if (isNull(v)) {
            buff.append(" IS NULL");
        } else {
            buff.append('=');
            params.add(v);
            addParameter(buff, col);
        }
    }
    String sql = buff.toString();
    try {
        link.execute(sql, params, true);
    } catch (Exception e) {
        throw TableLink.wrapException(sql, e);
    }
}
Also used : Column(org.h2.table.Column) IndexColumn(org.h2.table.IndexColumn) StatementBuilder(org.h2.util.StatementBuilder) Value(org.h2.value.Value) DbException(org.h2.message.DbException)

Example 55 with Update

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

the class FileLock method lockFile.

private void lockFile() {
    method = FILE;
    properties = new SortedProperties();
    properties.setProperty("method", String.valueOf(method));
    setUniqueId();
    FileUtils.createDirectories(FileUtils.getParent(fileName));
    if (!FileUtils.createFile(fileName)) {
        waitUntilOld();
        String m2 = load().getProperty("method", FILE);
        if (!m2.equals(FILE)) {
            throw getExceptionFatal("Unsupported lock method " + m2, null);
        }
        save();
        sleep(2 * sleep);
        if (!load().equals(properties)) {
            throw getExceptionAlreadyInUse("Locked by another process: " + fileName);
        }
        FileUtils.delete(fileName);
        if (!FileUtils.createFile(fileName)) {
            throw getExceptionFatal("Another process was faster", null);
        }
    }
    save();
    sleep(SLEEP_GAP);
    if (!load().equals(properties)) {
        fileName = null;
        throw getExceptionFatal("Concurrent update", null);
    }
    locked = true;
    watchdog = new Thread(this, "H2 File Lock Watchdog " + fileName);
    Driver.setThreadContextClassLoader(watchdog);
    watchdog.setDaemon(true);
    watchdog.setPriority(Thread.MAX_PRIORITY - 1);
    watchdog.start();
}
Also used : SortedProperties(org.h2.util.SortedProperties)

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