Skip to content

Commit f6f9cd6

Browse files
committed
Implement SQLiteBatch::StartCursor, ReadAtCursor, and CloseCursor
1 parent bf90e03 commit f6f9cd6

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

src/wallet/sqlite.cpp

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -369,16 +369,42 @@ bool SQLiteBatch::HasKey(CDataStream&& key)
369369

370370
bool SQLiteBatch::StartCursor()
371371
{
372-
return false;
372+
assert(!m_cursor_init);
373+
if (!m_database.m_db) return false;
374+
m_cursor_init = true;
375+
return true;
373376
}
374377

375378
bool SQLiteBatch::ReadAtCursor(CDataStream& key, CDataStream& value, bool& complete)
376379
{
377-
return false;
380+
complete = false;
381+
382+
if (!m_cursor_init) return false;
383+
384+
int res = sqlite3_step(m_cursor_stmt);
385+
if (res == SQLITE_DONE) {
386+
complete = true;
387+
return true;
388+
}
389+
if (res != SQLITE_ROW) {
390+
LogPrintf("SQLiteBatch::ReadAtCursor: Unable to execute cursor step: %s\n", sqlite3_errstr(res));
391+
return false;
392+
}
393+
394+
// Leftmost column in result is index 0
395+
const char* key_data = reinterpret_cast<const char*>(sqlite3_column_blob(m_cursor_stmt, 0));
396+
int key_data_size = sqlite3_column_bytes(m_cursor_stmt, 0);
397+
key.write(key_data, key_data_size);
398+
const char* value_data = reinterpret_cast<const char*>(sqlite3_column_blob(m_cursor_stmt, 1));
399+
int value_data_size = sqlite3_column_bytes(m_cursor_stmt, 1);
400+
value.write(value_data, value_data_size);
401+
return true;
378402
}
379403

380404
void SQLiteBatch::CloseCursor()
381405
{
406+
sqlite3_reset(m_cursor_stmt);
407+
m_cursor_init = false;
382408
}
383409

384410
bool SQLiteBatch::TxnBegin()

src/wallet/sqlite.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ class SQLiteBatch : public DatabaseBatch
1818
private:
1919
SQLiteDatabase& m_database;
2020

21+
bool m_cursor_init = false;
22+
2123
sqlite3_stmt* m_read_stmt{nullptr};
2224
sqlite3_stmt* m_insert_stmt{nullptr};
2325
sqlite3_stmt* m_overwrite_stmt{nullptr};

0 commit comments

Comments
 (0)