diff --git a/src/lib/md/SecurityDB.cpp b/src/lib/md/SecurityDB.cpp index d84fbe4d4..64b33cf11 100644 --- a/src/lib/md/SecurityDB.cpp +++ b/src/lib/md/SecurityDB.cpp @@ -34,16 +34,17 @@ sqlite3_ptr open_db() { } // Create table if it does not exist - auto dbptr = utils::open_db(db_path.c_str()); + auto dbptr = sqlite::open_db(db_path.c_str()); + bts_assert(dbptr); auto db = dbptr.get(); sqlite3_busy_timeout(db, 9999); - sqlite3_exec_nofail(db, "PRAGMA journal_mode=OFF"); - sqlite3_exec_nofail(db, "PRAGMA syncronous=0"); + sqlite::exec_nofail(db, "PRAGMA journal_mode=OFF"); + sqlite::exec_nofail(db, "PRAGMA syncronous=0"); - sqlite3_exec_nofail(db, + sqlite::exec_nofail(db, "CREATE TABLE IF NOT EXISTS securities (key INTEGER primary key, symbol TEXT NOT NULL, venue INTEGER NOT NULL, type INTEGER NOT NULL, security_blob BLOB NOT NULL, expiration INTEGER NOT NULL);"); - sqlite3_exec_nofail(db, + sqlite::exec_nofail(db, "CREATE TABLE IF NOT EXISTS metadata (key INTEGER PRIMARY KEY, data BLOB NOT NULL);"); return dbptr; @@ -67,7 +68,7 @@ size_t load_securities( // Load all existing securities and call the callback auto db = dbptr.get(); - auto select_stmt = prepare_query(db, "select key,symbol,venue,type,security_blob from securities where expiration > ?"); + auto select_stmt = sqlite::prepare_query(db, "select key,symbol,venue,type,security_blob from securities where expiration > ?"); auto select = select_stmt.get(); sqlite3_bind_int(select, 1, sec_expiration_cut_off().to_short()); auto result = sqlite3_step(select); @@ -112,8 +113,8 @@ size_t load_securities( void store_if_new(const std::vector& securities) { auto dbptr = open_db(); - auto insert = utils::prepare_query(dbptr, "insert or ignore into securities (key, symbol, venue, type, security_blob, expiration) values (?,?,?,?,?,?)"); - sqlite3_exec_nofail(dbptr, "BEGIN TRANSACTION"); + auto insert = sqlite::prepare_query(dbptr, "insert or ignore into securities (key, symbol, venue, type, security_blob, expiration) values (?,?,?,?,?,?)"); + sqlite::exec_nofail(dbptr, "BEGIN TRANSACTION"); std::string buffer; for (const api::Security* psecurity : securities) { if (psecurity && psecurity->SerializeToString(&buffer)) { @@ -127,7 +128,7 @@ void store_if_new(const std::vector& securities) { sqlite3_reset(insert.get()); } } - sqlite3_exec_nofail(dbptr, "COMMIT TRANSACTION"); + sqlite::exec_nofail(dbptr, "COMMIT TRANSACTION"); } @@ -135,7 +136,7 @@ std::vector load_metadata() { std::vector values; auto dbptr = open_db(); auto db = dbptr.get(); - auto select_stmt = prepare_query(db, "select key,data from metadata"); + auto select_stmt = sqlite::prepare_query(db, "select key,data from metadata"); auto select = select_stmt.get(); auto result = sqlite3_step(select); while(result == SQLITE_ROW) { @@ -156,7 +157,7 @@ std::vector load_metadata() { } bool store_metadata(const api::synthetic::Metadata& data) { auto dbptr = open_db(); - auto insert = utils::prepare_query(dbptr, "insert or replace into metadata (key, data) values (?,?)"); + auto insert = sqlite::prepare_query(dbptr, "insert or replace into metadata (key, data) values (?,?)"); SQLITE_CHK(sqlite3_bind_int(insert.get(), 1, data.security_key()), dbptr); std::string buffer = data.SerializeAsString(); diff --git a/src/lib/md/SettlementRecorder.cpp b/src/lib/md/SettlementRecorder.cpp index dfa1eb549..0d7680bbd 100644 --- a/src/lib/md/SettlementRecorder.cpp +++ b/src/lib/md/SettlementRecorder.cpp @@ -94,13 +94,14 @@ SettlementRecorder::SettlementRecorder(Platform platform) : } // initialize database - _self->db = open_db(db_path.c_str()); + _self->db = sqlite::open_db(db_path.c_str()); + bts_assert_msg(_self->db, db_path.c_str()); sqlite3* db = _self->db.get(); sqlite3_busy_timeout(db, 9999); - sqlite3_exec_nofail(db, "PRAGMA journal_mode=OFF"); - sqlite3_exec_nofail(db, "PRAGMA syncronous=0"); + sqlite::exec_nofail(db, "PRAGMA journal_mode=OFF"); + sqlite::exec_nofail(db, "PRAGMA syncronous=0"); // key=(security_key+date) - sqlite3_exec_nofail(db, + sqlite::exec_nofail(db, "CREATE TABLE IF NOT EXISTS settlements2 (key INTEGER NOT NULL, date INTEGER NOT NULL, mantissa INTEGER NOT NULL, exponent INTEGER NOT NULL, PRIMARY KEY (key,date));"); // Delete any records > 10 days old @@ -108,11 +109,11 @@ SettlementRecorder::SettlementRecorder(Platform platform) : auto encoded = datetime::ll_to_date(datetime::ptime_to_ll(cutoff)).to_short(); char query[256]; sprintf(query, "delete from settlements2 where date < %i", encoded); - sqlite3_exec_nofail(db, query); + sqlite::exec_nofail(db, query); // statements - _self->select_stmt = prepare_query(db, "select mantissa,exponent,date from settlements2 where key=? order by date desc limit 2"); - _self->insert_stmt = prepare_query(db, "insert or replace into settlements2(key, date, mantissa,exponent) values(?,?,?,?)"); + _self->select_stmt = sqlite::prepare_query(db, "select mantissa,exponent,date from settlements2 where key=? order by date desc limit 2"); + _self->insert_stmt = sqlite::prepare_query(db, "insert or replace into settlements2(key, date, mantissa,exponent) values(?,?,?,?)"); } SettlementRecorder::~SettlementRecorder() { } diff --git a/src/lib/persistence/SqliteStore.cpp b/src/lib/persistence/SqliteStore.cpp index 814ae95f1..34c91ecd2 100644 --- a/src/lib/persistence/SqliteStore.cpp +++ b/src/lib/persistence/SqliteStore.cpp @@ -57,42 +57,43 @@ void SqliteStore::init_db() { LOG(persist, Warn, "{} database does not exist yet. Creating it at {}", me->db, path.string()); - me->conn = utils::open_db(path.c_str()); + me->conn = sqlite::open_db(path.c_str()); + bts_assert_msg(me->conn, path.c_str()); const char *create_schema_sql = "create table store ( ns text, key text, value text)"; - utils::sqlite3_exec_nofail(me->conn, create_schema_sql); + sqlite::exec_nofail(me->conn, create_schema_sql); const char *key_index_sql = "create unique index key_index on store(ns,key)"; - utils::sqlite3_exec_nofail(me->conn, key_index_sql); + sqlite::exec_nofail(me->conn, key_index_sql); const char *ns_index_sql = "create index ns_index on store(ns)"; - utils::sqlite3_exec_nofail(me->conn, ns_index_sql); + sqlite::exec_nofail(me->conn, ns_index_sql); } else { - me->conn = utils::open_db(path.c_str()); + me->conn = sqlite::open_db(path.c_str()); } // Asynchronous for speed - utils::sqlite3_exec_nofail(me->conn, "PRAGMA synchronous = 0;"); - utils::sqlite3_exec_nofail(me->conn, "PRAGMA journal_mode=WAL"); + sqlite::exec_nofail(me->conn, "PRAGMA synchronous = 0;"); + sqlite::exec_nofail(me->conn, "PRAGMA journal_mode=WAL"); // prepare insert statement const char *insert_sql = "insert into store (ns, key, value) values(?,?,?)"; - me->insert = utils::prepare_query(me->conn, insert_sql); + me->insert = sqlite::prepare_query(me->conn, insert_sql); // prepare update statement const char *update_sql = "update store set value = ? where ns = ? and key = ?"; - me->update = utils::prepare_query(me->conn, update_sql); + me->update = sqlite::prepare_query(me->conn, update_sql); // prepare remove statement const char *remove_all_sql = "delete from store where ns = ?"; - me->remove_all = utils::prepare_query(me->conn, remove_all_sql); + me->remove_all = sqlite::prepare_query(me->conn, remove_all_sql); // prepare remove statement const char *remove_sql = "delete from store where ns = ? and key = ?"; - me->remove = utils::prepare_query(me->conn, remove_sql); + me->remove = sqlite::prepare_query(me->conn, remove_sql); } SqliteStore::~SqliteStore() {} @@ -100,7 +101,7 @@ SqliteStore::~SqliteStore() {} SqliteStore::Data SqliteStore::retrieve_all() { SqliteStore::Data result; sqlite3_stmt_ptr query = - utils::prepare_query(me->conn, "select ns,key,value from store"); + sqlite::prepare_query(me->conn, "select ns,key,value from store"); int sqlite_result; do { @@ -125,7 +126,7 @@ std::pair SqliteStore::retrieve_one(std::string const &ns, std::string const &k) { auto query_str = fmt::format( "select ns,key,value from store where ns='{}' and key='{}'", ns, k); - sqlite3_stmt_ptr query = utils::prepare_query(me->conn, query_str.c_str()); + sqlite3_stmt_ptr query = sqlite::prepare_query(me->conn, query_str.c_str()); std::pair result{false, ""}; diff --git a/src/lib/utils/Sqlite_utils.cpp b/src/lib/utils/Sqlite_utils.cpp index 4d31926a3..f8cc0a013 100644 --- a/src/lib/utils/Sqlite_utils.cpp +++ b/src/lib/utils/Sqlite_utils.cpp @@ -3,22 +3,27 @@ #include "bts_assert.h" #include namespace bts { -namespace utils { +namespace sqlite { -void sqlite3_exec_nofail(sqlite3* db, const char* sql) { +void exec_nofail(sqlite3* db, const char* sql) { char* error_message = nullptr; int result = sqlite3_exec(db, sql, nullptr, nullptr, &error_message); bts_assert_msg(result == SQLITE_OK, error_message); } -void sqlite3_exec_nofail(sqlite3_ptr & db, const char * sql) { - return sqlite3_exec_nofail(db.get(), sql); +void exec_nofail(sqlite3_ptr & db, const char * sql) { + return exec_nofail(db.get(), sql); } sqlite3_ptr open_db(const char* path) { sqlite3* temp; - bts_assert(sqlite3_open(path, &temp) == SQLITE_OK); - return sqlite3_ptr(temp, sqlite3_close); + if (sqlite3_open(path, &temp) == SQLITE_OK) { + return sqlite3_ptr(temp, sqlite3_close); + } + else { + LOG(Gen, Error, "Failed to open database {}", std::string(path)); + return null_db(); + } } sqlite3_stmt_ptr prepare_query(sqlite3 * db, const char * query) { diff --git a/src/lib/utils/Sqlite_utils.h b/src/lib/utils/Sqlite_utils.h index 4cb8e6ae3..1bc3cdbb4 100644 --- a/src/lib/utils/Sqlite_utils.h +++ b/src/lib/utils/Sqlite_utils.h @@ -1,12 +1,9 @@ #pragma once #ifndef WIN32 -#include #include -#include "lib/utils/bts_export.h" +#include #include "lib/utils/bts_assert.h" -typedef struct sqlite3 sqlite3; -typedef struct sqlite3_stmt sqlite3_stmt; typedef std::unique_ptr sqlite3_ptr; typedef std::unique_ptr sqlite3_stmt_ptr; @@ -18,13 +15,15 @@ typedef std::unique_ptr sqlite3_stmt_ptr; } \ } while(0) -namespace bts { -namespace utils { -void sqlite3_exec_nofail(sqlite3* db, const char* sql); -void sqlite3_exec_nofail(sqlite3_ptr& db, const char* sql); +namespace bts { +namespace sqlite { + +void exec_nofail(sqlite3* db, const char* sql); +void exec_nofail(sqlite3_ptr& db, const char* sql); sqlite3_ptr open_db(const char* path); +inline sqlite3_ptr null_db() { return sqlite3_ptr(nullptr, sqlite3_close); } sqlite3_stmt_ptr prepare_query(sqlite3* db, const char* query); sqlite3_stmt_ptr prepare_query(sqlite3_ptr& db, const char* query); }}//namespace