From 1e8c4eac5f0a3cf5624df39c83b74941454f6d6b Mon Sep 17 00:00:00 2001 From: Martin Brodbeck Date: Fri, 6 Jul 2018 13:30:23 +0200 Subject: [PATCH] Database class added --- src/core/CMakeLists.txt | 6 +++++- src/core/database.cpp | 24 ++++++++++++++++++++++++ src/core/database.h | 20 ++++++++++++++++++++ 3 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 src/core/database.cpp create mode 100644 src/core/database.h diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index 01edba3..421ab10 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -1,13 +1,17 @@ find_package(Boost 1.62 REQUIRED) +find_package(SQLite3 REQUIRED) set(CORE_HEADERS entity.h + database.h ) set(CORE_SOURCES entity.cpp + database.cpp ) add_library(core STATIC ${CORE_SOURCES}) -target_link_libraries(core Boost::boost) \ No newline at end of file +target_link_libraries(core Boost::boost) +target_link_libraries(core sqlite3) \ No newline at end of file diff --git a/src/core/database.cpp b/src/core/database.cpp new file mode 100644 index 0000000..b017565 --- /dev/null +++ b/src/core/database.cpp @@ -0,0 +1,24 @@ +#include "database.h" + +#include + +Database::Database(const std::string& dbname) : db(nullptr) +{ + const int errCode = sqlite3_open(dbname.c_str(), &db); + if(errCode) { + throw std::runtime_error("Could not open database file."); + } +} + +Database::~Database() +{ + sqlite3_close(db); +} + +void Database::exec(const std::string& sql) +{ + const int errCode = sqlite3_exec(db, sql.c_str(), nullptr, nullptr, nullptr); + if(errCode) { + throw std::runtime_error("Error in SQL execution."); + } +} \ No newline at end of file diff --git a/src/core/database.h b/src/core/database.h new file mode 100644 index 0000000..dcb2858 --- /dev/null +++ b/src/core/database.h @@ -0,0 +1,20 @@ +#ifndef DATABASE_H +#define DATABASE_H + +#include + +#include + +class Database +{ +public: + Database(const std::string& dbname); + ~Database(); + Database(const Database&) = delete; + Database& operator=(const Database&) = delete; + void exec(const std::string& sql); +private: + sqlite3 *db; +}; + +#endif // DATABASE_H \ No newline at end of file