Search in sources :

Example 16 with Delete

use of org.h2.command.dml.Delete 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 17 with Delete

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

the class LobStorageMap method init.

@Override
public void init() {
    if (init) {
        return;
    }
    init = true;
    Store s = database.getMvStore();
    MVStore mvStore;
    if (s == null) {
        // in-memory database
        mvStore = MVStore.open(null);
    } else {
        mvStore = s.getStore();
    }
    lobMap = mvStore.openMap("lobMap");
    refMap = mvStore.openMap("lobRef");
    dataMap = mvStore.openMap("lobData");
    streamStore = new StreamStore(dataMap);
    // garbage collection of the last blocks
    if (database.isReadOnly()) {
        return;
    }
    if (dataMap.isEmpty()) {
        return;
    }
    // search for the last block
    // (in theory, only the latest lob can have unreferenced blocks,
    // but the latest lob could be a copy of another one, and
    // we don't know that, so we iterate over all lobs)
    long lastUsedKey = -1;
    for (Entry<Long, Object[]> e : lobMap.entrySet()) {
        long lobId = e.getKey();
        Object[] v = e.getValue();
        byte[] id = (byte[]) v[0];
        long max = streamStore.getMaxBlockKey(id);
        // a lob may not have a referenced blocks if data is kept inline
        if (max != -1 && max > lastUsedKey) {
            lastUsedKey = max;
            if (TRACE) {
                trace("lob " + lobId + " lastUsedKey=" + lastUsedKey);
            }
        }
    }
    if (TRACE) {
        trace("lastUsedKey=" + lastUsedKey);
    }
    // delete all blocks that are newer
    while (true) {
        Long last = dataMap.lastKey();
        if (last == null || last <= lastUsedKey) {
            break;
        }
        if (TRACE) {
            trace("gc " + last);
        }
        dataMap.remove(last);
    }
    // don't re-use block ids, except at the very end
    Long last = dataMap.lastKey();
    if (last != null) {
        streamStore.setNextKey(last + 1);
    }
}
Also used : MVStore(org.h2.mvstore.MVStore) StreamStore(org.h2.mvstore.StreamStore) Store(org.h2.mvstore.db.MVTableEngine.Store) MVStore(org.h2.mvstore.MVStore) StreamStore(org.h2.mvstore.StreamStore)

Example 18 with Delete

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

the class PageStore method redoDelete.

/**
 * Redo a delete in a table.
 *
 * @param tableId the object id of the table
 * @param key the key of the row to delete
 */
void redoDelete(int tableId, long key) {
    Index index = metaObjects.get(tableId);
    PageDataIndex scan = (PageDataIndex) index;
    Row row = scan.getRowWithKey(key);
    if (row == null || row.getKey() != key) {
        trace.error(null, "Entry not found: " + key + " found instead: " + row + " - ignoring");
        return;
    }
    redo(tableId, row, false);
}
Also used : PageDataIndex(org.h2.index.PageDataIndex) Index(org.h2.index.Index) PageIndex(org.h2.index.PageIndex) PageDelegateIndex(org.h2.index.PageDelegateIndex) MultiVersionIndex(org.h2.index.MultiVersionIndex) PageBtreeIndex(org.h2.index.PageBtreeIndex) PageDataIndex(org.h2.index.PageDataIndex) Row(org.h2.result.Row)

Example 19 with Delete

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

the class UpdatableRow method deleteRow.

/**
 * Delete the given row in the database.
 *
 * @param current the row
 * @throws SQLException if this row has already been deleted
 */
public void deleteRow(Value[] current) throws SQLException {
    StatementBuilder buff = new StatementBuilder("DELETE FROM ");
    appendTableName(buff);
    appendKeyCondition(buff);
    PreparedStatement prep = conn.prepareStatement(buff.toString());
    setKey(prep, 1, current);
    int count = prep.executeUpdate();
    if (count != 1) {
        // the row has already been deleted
        throw DbException.get(ErrorCode.NO_DATA_AVAILABLE);
    }
}
Also used : StatementBuilder(org.h2.util.StatementBuilder) PreparedStatement(java.sql.PreparedStatement)

Example 20 with Delete

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

the class Test method main.

public static void main(String... args) throws Exception {
    H2Database db = H2Utils.openOrCreateDatabase("helloWorld.db", MODE_PRIVATE, null);
    log("opened ps=" + db.getPageSize());
    try {
        // db.execSQL("DROP TABLE IF EXISTS test");
        // log("dropped");
        db.execSQL("CREATE TABLE if not exists test(ID INTEGER PRIMARY KEY, NAME VARCHAR)");
        log("created");
        for (int j = 0; j < 10; j++) {
            Cursor c = db.rawQuery("select * from test", new String[0]);
            int count = c.getCount();
            for (int i = 0; i < count; i++) {
                c.move(1);
                c.getInt(0);
                c.getString(1);
            }
            c.close();
        }
        // log("select " + count);
        db.execSQL("delete from test");
        log("delete");
        db.beginTransaction();
        for (int i = 0; i < 1000; i++) {
            db.execSQL("INSERT INTO TEST VALUES(?, 'Hello')", new Object[] { i });
        }
        db.setTransactionSuccessful();
        db.endTransaction();
        log("inserted");
        for (int i = 0; i < 10; i++) {
            Cursor c = db.rawQuery("select * from test where id=?", new String[] { "" + i });
            int count = c.getCount();
            if (count > 0) {
                c.move(1);
                c.getInt(0);
                c.getString(1);
            }
            c.close();
        }
        log("select");
    } finally {
        db.close();
        log("closed");
    }
}
Also used : Cursor(android.database.Cursor) H2Database(org.h2.android.H2Database)

Aggregations

Connection (java.sql.Connection)40 PreparedStatement (java.sql.PreparedStatement)39 Statement (java.sql.Statement)38 ResultSet (java.sql.ResultSet)36 JdbcConnection (org.h2.jdbc.JdbcConnection)25 SQLException (java.sql.SQLException)17 SimpleResultSet (org.h2.tools.SimpleResultSet)14 Savepoint (java.sql.Savepoint)13 StatementBuilder (org.h2.util.StatementBuilder)9 DbException (org.h2.message.DbException)8 Column (org.h2.table.Column)8 ValueString (org.h2.value.ValueString)7 Random (java.util.Random)6 Expression (org.h2.expression.Expression)5 ValueExpression (org.h2.expression.ValueExpression)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 ArrayList (java.util.ArrayList)4 GridH2Table (org.apache.ignite.internal.processors.query.h2.opt.GridH2Table)4 AlterTableAddConstraint (org.h2.command.ddl.AlterTableAddConstraint)4 AlterTableDropConstraint (org.h2.command.ddl.AlterTableDropConstraint)4