Skip to content

Commit 7aa4562

Browse files
committed
Add SetupSQLStatements
1 parent 6636a26 commit 7aa4562

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

src/wallet/sqlite.cpp

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,36 @@ SQLiteDatabase::SQLiteDatabase(const fs::path& dir_path, const fs::path& file_pa
6161
}
6262
}
6363

64+
void SQLiteBatch::SetupSQLStatements()
65+
{
66+
int res;
67+
if (!m_read_stmt) {
68+
if ((res = sqlite3_prepare_v2(m_database.m_db, "SELECT value FROM main WHERE key = ?", -1, &m_read_stmt, nullptr)) != SQLITE_OK) {
69+
throw std::runtime_error(strprintf("SQLiteDatabase: Failed to setup SQL statements: %s\n", sqlite3_errstr(res)));
70+
}
71+
}
72+
if (!m_insert_stmt) {
73+
if ((res = sqlite3_prepare_v2(m_database.m_db, "INSERT INTO main VALUES(?, ?)", -1, &m_insert_stmt, nullptr)) != SQLITE_OK) {
74+
throw std::runtime_error(strprintf("SQLiteDatabase: Failed to setup SQL statements: %s\n", sqlite3_errstr(res)));
75+
}
76+
}
77+
if (!m_overwrite_stmt) {
78+
if ((res = sqlite3_prepare_v2(m_database.m_db, "INSERT or REPLACE into main values(?, ?)", -1, &m_overwrite_stmt, nullptr)) != SQLITE_OK) {
79+
throw std::runtime_error(strprintf("SQLiteDatabase: Failed to setup SQL statements: %s\n", sqlite3_errstr(res)));
80+
}
81+
}
82+
if (!m_delete_stmt) {
83+
if ((res = sqlite3_prepare_v2(m_database.m_db, "DELETE FROM main WHERE key = ?", -1, &m_delete_stmt, nullptr)) != SQLITE_OK) {
84+
throw std::runtime_error(strprintf("SQLiteDatabase: Failed to setup SQL statements: %s\n", sqlite3_errstr(res)));
85+
}
86+
}
87+
if (!m_cursor_stmt) {
88+
if ((res = sqlite3_prepare_v2(m_database.m_db, "SELECT key, value FROM main", -1, &m_cursor_stmt, nullptr)) != SQLITE_OK) {
89+
throw std::runtime_error(strprintf("SQLiteDatabase: Failed to setup SQL statements : %s\n", sqlite3_errstr(res)));
90+
}
91+
}
92+
}
93+
6494
SQLiteDatabase::~SQLiteDatabase()
6595
{
6696
Cleanup();
@@ -178,6 +208,8 @@ SQLiteBatch::SQLiteBatch(SQLiteDatabase& database)
178208
{
179209
// Make sure we have a db handle
180210
assert(m_database.m_db);
211+
212+
SetupSQLStatements();
181213
}
182214

183215
void SQLiteBatch::Close()
@@ -190,6 +222,33 @@ void SQLiteBatch::Close()
190222
LogPrintf("SQLiteBatch: Batch closed and failed to abort transaction\n");
191223
}
192224
}
225+
226+
// Free all of the prepared statements
227+
int ret = sqlite3_finalize(m_read_stmt);
228+
if (ret != SQLITE_OK) {
229+
LogPrintf("SQLiteBatch: Batch closed but could not finalize read statement: %s\n", sqlite3_errstr(ret));
230+
}
231+
ret = sqlite3_finalize(m_insert_stmt);
232+
if (ret != SQLITE_OK) {
233+
LogPrintf("SQLiteBatch: Batch closed but could not finalize insert statement: %s\n", sqlite3_errstr(ret));
234+
}
235+
ret = sqlite3_finalize(m_overwrite_stmt);
236+
if (ret != SQLITE_OK) {
237+
LogPrintf("SQLiteBatch: Batch closed but could not finalize overwrite statement: %s\n", sqlite3_errstr(ret));
238+
}
239+
ret = sqlite3_finalize(m_delete_stmt);
240+
if (ret != SQLITE_OK) {
241+
LogPrintf("SQLiteBatch: Batch closed but could not finalize delete statement: %s\n", sqlite3_errstr(ret));
242+
}
243+
ret = sqlite3_finalize(m_cursor_stmt);
244+
if (ret != SQLITE_OK) {
245+
LogPrintf("SQLiteBatch: Batch closed but could not finalize cursor statement: %s\n", sqlite3_errstr(ret));
246+
}
247+
m_read_stmt = nullptr;
248+
m_insert_stmt = nullptr;
249+
m_overwrite_stmt = nullptr;
250+
m_delete_stmt = nullptr;
251+
m_cursor_stmt = nullptr;
193252
}
194253

195254
bool SQLiteBatch::ReadKey(CDataStream&& key, CDataStream& value)

src/wallet/sqlite.h

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

21+
sqlite3_stmt* m_read_stmt{nullptr};
22+
sqlite3_stmt* m_insert_stmt{nullptr};
23+
sqlite3_stmt* m_overwrite_stmt{nullptr};
24+
sqlite3_stmt* m_delete_stmt{nullptr};
25+
sqlite3_stmt* m_cursor_stmt{nullptr};
26+
27+
void SetupSQLStatements();
28+
2129
bool ReadKey(CDataStream&& key, CDataStream& value) override;
2230
bool WriteKey(CDataStream&& key, CDataStream&& value, bool overwrite = true) override;
2331
bool EraseKey(CDataStream&& key) override;

0 commit comments

Comments
 (0)