Changeset View
Changeset View
Standalone View
Standalone View
src/wallet/sqlite.cpp
Show First 20 Lines • Show All 60 Lines • ▼ Show 20 Lines | try { | ||||
Open(); | Open(); | ||||
} catch (const std::runtime_error &) { | } catch (const std::runtime_error &) { | ||||
// If open fails, cleanup this object and rethrow the exception | // If open fails, cleanup this object and rethrow the exception | ||||
Cleanup(); | Cleanup(); | ||||
throw; | throw; | ||||
} | } | ||||
} | } | ||||
void SQLiteBatch::SetupSQLStatements() { | |||||
int res; | |||||
if (!m_read_stmt) { | |||||
if ((res = sqlite3_prepare_v2( | |||||
m_database.m_db, "SELECT value FROM main WHERE key = ?", -1, | |||||
&m_read_stmt, nullptr)) != SQLITE_OK) { | |||||
throw std::runtime_error(strprintf( | |||||
"SQLiteDatabase: Failed to setup SQL statements: %s\n", | |||||
sqlite3_errstr(res))); | |||||
} | |||||
} | |||||
if (!m_insert_stmt) { | |||||
if ((res = sqlite3_prepare_v2(m_database.m_db, | |||||
"INSERT INTO main VALUES(?, ?)", -1, | |||||
&m_insert_stmt, nullptr)) != SQLITE_OK) { | |||||
throw std::runtime_error(strprintf( | |||||
"SQLiteDatabase: Failed to setup SQL statements: %s\n", | |||||
sqlite3_errstr(res))); | |||||
} | |||||
} | |||||
if (!m_overwrite_stmt) { | |||||
if ((res = sqlite3_prepare_v2( | |||||
m_database.m_db, "INSERT or REPLACE into main values(?, ?)", | |||||
-1, &m_overwrite_stmt, nullptr)) != SQLITE_OK) { | |||||
throw std::runtime_error(strprintf( | |||||
"SQLiteDatabase: Failed to setup SQL statements: %s\n", | |||||
sqlite3_errstr(res))); | |||||
} | |||||
} | |||||
if (!m_delete_stmt) { | |||||
if ((res = sqlite3_prepare_v2(m_database.m_db, | |||||
"DELETE FROM main WHERE key = ?", -1, | |||||
&m_delete_stmt, nullptr)) != SQLITE_OK) { | |||||
throw std::runtime_error(strprintf( | |||||
"SQLiteDatabase: Failed to setup SQL statements: %s\n", | |||||
sqlite3_errstr(res))); | |||||
} | |||||
} | |||||
if (!m_cursor_stmt) { | |||||
if ((res = sqlite3_prepare_v2(m_database.m_db, | |||||
"SELECT key, value FROM main", -1, | |||||
&m_cursor_stmt, nullptr)) != SQLITE_OK) { | |||||
throw std::runtime_error(strprintf( | |||||
"SQLiteDatabase: Failed to setup SQL statements : %s\n", | |||||
sqlite3_errstr(res))); | |||||
} | |||||
} | |||||
} | |||||
SQLiteDatabase::~SQLiteDatabase() { | SQLiteDatabase::~SQLiteDatabase() { | ||||
Cleanup(); | Cleanup(); | ||||
} | } | ||||
void SQLiteDatabase::Cleanup() noexcept { | void SQLiteDatabase::Cleanup() noexcept { | ||||
Close(); | Close(); | ||||
LOCK(g_sqlite_mutex); | LOCK(g_sqlite_mutex); | ||||
▲ Show 20 Lines • Show All 131 Lines • ▼ Show 20 Lines | |||||
std::unique_ptr<DatabaseBatch> SQLiteDatabase::MakeBatch(bool flush_on_close) { | std::unique_ptr<DatabaseBatch> SQLiteDatabase::MakeBatch(bool flush_on_close) { | ||||
return nullptr; | return nullptr; | ||||
} | } | ||||
SQLiteBatch::SQLiteBatch(SQLiteDatabase &database) : m_database(database) { | SQLiteBatch::SQLiteBatch(SQLiteDatabase &database) : m_database(database) { | ||||
// Make sure we have a db handle | // Make sure we have a db handle | ||||
assert(m_database.m_db); | assert(m_database.m_db); | ||||
SetupSQLStatements(); | |||||
} | } | ||||
void SQLiteBatch::Close() { | void SQLiteBatch::Close() { | ||||
// If m_db is in a transaction (i.e. not in autocommit mode), then abort the | // If m_db is in a transaction (i.e. not in autocommit mode), then abort the | ||||
// transaction in progress | // transaction in progress | ||||
if (m_database.m_db && sqlite3_get_autocommit(m_database.m_db) == 0) { | if (m_database.m_db && sqlite3_get_autocommit(m_database.m_db) == 0) { | ||||
if (TxnAbort()) { | if (TxnAbort()) { | ||||
LogPrintf("SQLiteBatch: Batch closed unexpectedly without the " | LogPrintf("SQLiteBatch: Batch closed unexpectedly without the " | ||||
"transaction being explicitly committed or aborted\n"); | "transaction being explicitly committed or aborted\n"); | ||||
} else { | } else { | ||||
LogPrintf( | LogPrintf( | ||||
"SQLiteBatch: Batch closed and failed to abort transaction\n"); | "SQLiteBatch: Batch closed and failed to abort transaction\n"); | ||||
} | } | ||||
} | } | ||||
// Free all of the prepared statements | |||||
int ret = sqlite3_finalize(m_read_stmt); | |||||
if (ret != SQLITE_OK) { | |||||
LogPrintf("SQLiteBatch: Batch closed but could not finalize read " | |||||
"statement: %s\n", | |||||
sqlite3_errstr(ret)); | |||||
} | |||||
ret = sqlite3_finalize(m_insert_stmt); | |||||
if (ret != SQLITE_OK) { | |||||
LogPrintf("SQLiteBatch: Batch closed but could not finalize insert " | |||||
"statement: %s\n", | |||||
sqlite3_errstr(ret)); | |||||
} | |||||
ret = sqlite3_finalize(m_overwrite_stmt); | |||||
if (ret != SQLITE_OK) { | |||||
LogPrintf("SQLiteBatch: Batch closed but could not finalize overwrite " | |||||
"statement: %s\n", | |||||
sqlite3_errstr(ret)); | |||||
} | |||||
ret = sqlite3_finalize(m_delete_stmt); | |||||
if (ret != SQLITE_OK) { | |||||
LogPrintf("SQLiteBatch: Batch closed but could not finalize delete " | |||||
"statement: %s\n", | |||||
sqlite3_errstr(ret)); | |||||
} | |||||
ret = sqlite3_finalize(m_cursor_stmt); | |||||
if (ret != SQLITE_OK) { | |||||
LogPrintf("SQLiteBatch: Batch closed but could not finalize cursor " | |||||
"statement: %s\n", | |||||
sqlite3_errstr(ret)); | |||||
} | |||||
m_read_stmt = nullptr; | |||||
m_insert_stmt = nullptr; | |||||
m_overwrite_stmt = nullptr; | |||||
m_delete_stmt = nullptr; | |||||
m_cursor_stmt = nullptr; | |||||
} | } | ||||
bool SQLiteBatch::ReadKey(CDataStream &&key, CDataStream &value) { | bool SQLiteBatch::ReadKey(CDataStream &&key, CDataStream &value) { | ||||
return false; | return false; | ||||
} | } | ||||
bool SQLiteBatch::WriteKey(CDataStream &&key, CDataStream &&value, | bool SQLiteBatch::WriteKey(CDataStream &&key, CDataStream &&value, | ||||
bool overwrite) { | bool overwrite) { | ||||
▲ Show 20 Lines • Show All 47 Lines • Show Last 20 Lines |