mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-02-08 10:31:50 -05:00
Add SetupSQLStatements
This commit is contained in:
parent
6636a2608a
commit
7aa45620e2
2 changed files with 67 additions and 0 deletions
|
@ -61,6 +61,36 @@ SQLiteDatabase::SQLiteDatabase(const fs::path& dir_path, const fs::path& file_pa
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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();
|
||||||
|
@ -178,6 +208,8 @@ SQLiteBatch::SQLiteBatch(SQLiteDatabase& 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()
|
||||||
|
@ -190,6 +222,33 @@ void SQLiteBatch::Close()
|
||||||
LogPrintf("SQLiteBatch: Batch closed and failed to abort transaction\n");
|
LogPrintf("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)
|
||||||
|
|
|
@ -18,6 +18,14 @@ class SQLiteBatch : public DatabaseBatch
|
||||||
private:
|
private:
|
||||||
SQLiteDatabase& m_database;
|
SQLiteDatabase& m_database;
|
||||||
|
|
||||||
|
sqlite3_stmt* m_read_stmt{nullptr};
|
||||||
|
sqlite3_stmt* m_insert_stmt{nullptr};
|
||||||
|
sqlite3_stmt* m_overwrite_stmt{nullptr};
|
||||||
|
sqlite3_stmt* m_delete_stmt{nullptr};
|
||||||
|
sqlite3_stmt* m_cursor_stmt{nullptr};
|
||||||
|
|
||||||
|
void SetupSQLStatements();
|
||||||
|
|
||||||
bool ReadKey(CDataStream&& key, CDataStream& value) override;
|
bool ReadKey(CDataStream&& key, CDataStream& value) override;
|
||||||
bool WriteKey(CDataStream&& key, CDataStream&& value, bool overwrite = true) override;
|
bool WriteKey(CDataStream&& key, CDataStream&& value, bool overwrite = true) override;
|
||||||
bool EraseKey(CDataStream&& key) override;
|
bool EraseKey(CDataStream&& key) override;
|
||||||
|
|
Loading…
Add table
Reference in a new issue