From a832645a7cbf9ade65a76087133f5fc13cad282d Mon Sep 17 00:00:00 2001 From: dominikpalatynski Date: Tue, 6 Feb 2024 20:39:13 +0100 Subject: [PATCH] Create Basic DataBase. Fixing DataBaseCommon.pp --- source/DataBase/DataBaseCommon.hpp | 31 ----- source/DataBase/DataBaseHandler.cpp | 140 +++++++++++----------- source/DataBase/DataBaseHandler.hpp | 56 +++++---- source/DataBase/common/DataBaseCommon.hpp | 20 ++++ source/DataBase/password-manager.db | Bin 24576 -> 24576 bytes source/main.cpp | 7 +- 6 files changed, 126 insertions(+), 128 deletions(-) delete mode 100644 source/DataBase/DataBaseCommon.hpp create mode 100644 source/DataBase/common/DataBaseCommon.hpp diff --git a/source/DataBase/DataBaseCommon.hpp b/source/DataBase/DataBaseCommon.hpp deleted file mode 100644 index 65bcfe6..0000000 --- a/source/DataBase/DataBaseCommon.hpp +++ /dev/null @@ -1,31 +0,0 @@ -#pragma once - -#include -#include -struct DataBaseCommon -{ - static const std::vector userRecordsRecords; - static const std::vector categoriesRecords; - static const std::vector passwordHistoryRecords; - static const std::vector passwordRecords; - - static const std::string dataBasePath; - - static const std::string users; - static const std::string passwords; - static const std::string passwordHistory; - static const std::string categories; -}; - -const std::vector DataBaseCommon::userRecordsRecords{"Username", "Password"}; -const std::vector DataBaseCommon::categoriesRecords{"Category"}; -const std::vector DataBaseCommon::passwordHistoryRecords{"Creation_Time", "Modify_Time", "Expiry_Time"}; -const std::vector DataBaseCommon::passwordRecords{ - "User_Id", "Category_Id", "Title", "Username", "Password", "Note", "Url"}; - -const std::string DataBaseCommon::dataBasePath{"../../../source/dataBase/password-manager.db"}; - -const std::string DataBaseCommon::users{"Users"}; -const std::string DataBaseCommon::passwords{"Passwords"}; -const std::string DataBaseCommon::passwordHistory{"PasswordHistory"}; -const std::string DataBaseCommon::categories{"Categories"}; \ No newline at end of file diff --git a/source/DataBase/DataBaseHandler.cpp b/source/DataBase/DataBaseHandler.cpp index 9801dc7..5fe14e4 100644 --- a/source/DataBase/DataBaseHandler.cpp +++ b/source/DataBase/DataBaseHandler.cpp @@ -3,96 +3,100 @@ #include #include "DataBaseHandler.hpp" - -DataBaseHandler::DataBaseHandler(const std::string &dbName) - : database_(nullptr) +namespace dataBase { - int result = sqlite3_open(dbName.c_str(), &database_); - if (result != SQLITE_OK) + + DataBaseHandler::DataBaseHandler(const char *dbName) + : database_(nullptr) { - throw std::runtime_error("Failed to open database: " + std::string(sqlite3_errmsg(database_))); + int result = sqlite3_open(dbName, &database_); + if (result != SQLITE_OK) + { + throw std::runtime_error("Failed to open database: " + std::string(sqlite3_errmsg(database_))); + } } -} -DataBaseHandler::~DataBaseHandler() -{ - if (database_ != nullptr) + DataBaseHandler::~DataBaseHandler() { - sqlite3_close(database_); + if (database_ != nullptr) + { + sqlite3_close(database_); + } } -} -void DataBaseHandler::executeQuery(const std::string &query) -{ - char *errMsg = nullptr; - int result = sqlite3_exec(database_, query.c_str(), nullptr, nullptr, &errMsg); - if (result != SQLITE_OK) + void DataBaseHandler::executeQuery(const char *query) { - throw std::runtime_error("Query execution failed: " + std::string(errMsg)); + char *errMsg = nullptr; + int result = sqlite3_exec(database_, query, nullptr, nullptr, &errMsg); + if (result != SQLITE_OK) + { + throw std::runtime_error("Query execution failed: " + std::string(errMsg)); + } } -} -void DataBaseHandler::createTable(const std::string &tableName, const std::string &columns) -{ - std::string query = "CREATE TABLE IF NOT EXISTS " + tableName + " (" + columns + ");"; - executeQuery(query); -} - -void DataBaseHandler::insertData(const std::string &tableName, const std::vector &columns, const std::vector &values) -{ - if (columns.size() != values.size()) + void DataBaseHandler::createTable(const std::string &tableName, const std::string &columns) { - throw std::invalid_argument("Columns and values sizes do not match."); + std::string query = "CREATE TABLE IF NOT EXISTS " + tableName + " (" + columns + ");"; + executeQuery(query.c_str()); } - std::ostringstream queryStream; - queryStream << "INSERT INTO " << tableName << " ("; - for (size_t i = 0; i < columns.size(); ++i) + void DataBaseHandler::insertData(const std::string &tableName, const std::vector &columns, const std::vector &values) { - queryStream << columns[i]; - if (i < columns.size() - 1) + if (columns.size() != values.size()) { - queryStream << ", "; + throw std::invalid_argument("Columns and values sizes do not match."); } - } - queryStream << ") VALUES ("; - for (size_t i = 0; i < values.size(); ++i) - { - queryStream << "'" << values[i] << "'"; - if (i < values.size() - 1) + + std::ostringstream queryStream; + queryStream << "INSERT INTO " << tableName << " ("; + for (size_t i = 0; i < columns.size(); ++i) { - queryStream << ", "; + queryStream << columns[i]; + if (i < columns.size() - 1) + { + queryStream << ", "; + } } + queryStream << ") VALUES ("; + for (size_t i = 0; i < values.size(); ++i) + { + queryStream << "'" << values[i] << "'"; + if (i < values.size() - 1) + { + queryStream << ", "; + } + } + queryStream << ");"; + + std::string query = queryStream.str(); + executeQuery(query.c_str()); } - queryStream << ");"; - std::string query = queryStream.str(); - executeQuery(query); -} + void DataBaseHandler::deleteData(const std::string &tableName, const std::string &columns, const std::string &value) + { + std::string query = "DELETE FROM " + tableName + " WHERE " + columns + " = '" + value + "';"; + executeQuery(query.c_str()); + } -void DataBaseHandler::deleteData(const std::string &tableName, const std::string &columns, const std::string &value) -{ - std::string query = "DELETE FROM " + tableName + " WHERE " + columns + " = '" + value + "';"; - executeQuery(query); -} + void DataBaseHandler::updateData( + const std::string &tableName, + const std::string &setRecord, + const std::string &setValue, + const std::string &findRecord, + const std::string &findValue) + { + std::string query = "UPDATE " + tableName + " SET " + setRecord + " ='" + setValue + "' WHERE " + findRecord + " = '" + findValue + "';"; + executeQuery(query.c_str()); + } -void DataBaseHandler::updateData( - const std::string &tableName, - const std::string &setRecord, - const std::string &setValue, - const std::string &findRecord, - const std::string &findValue) -{ - std::string query = "UPDATE " + tableName + " SET " + setRecord + " ='" + setValue + "' WHERE " + findRecord + " = '" + findValue + "';"; - executeQuery(query); -} + void DataBaseHandler::selectData( + const std::string &tableName, + const std::string &findRecord, + const std::string &findValue) + { + std::string query = "SELECT " + findRecord + " FROM " + tableName + " WHERE " + findValue; -void DataBaseHandler::selectData( - const std::string &tableName, - const std::string &findRecord, - const std::string &findValue) -{ - std::string query = "SELECT " + findRecord + " FROM " + tableName + " WHERE " + findValue; + executeQuery(query.c_str()); + } - executeQuery(query); } diff --git a/source/DataBase/DataBaseHandler.hpp b/source/DataBase/DataBaseHandler.hpp index 2a5c697..03c5b51 100644 --- a/source/DataBase/DataBaseHandler.hpp +++ b/source/DataBase/DataBaseHandler.hpp @@ -7,30 +7,34 @@ #include "sqlite3.h" -class DataBaseHandler +namespace dataBase { -public: - DataBaseHandler(const std::string &dbName); - ~DataBaseHandler(); - - void createTable(const std::string &tableName, const std::string &columns); - void insertData(const std::string &tableName, const std::vector &columns, const std::vector &values); - void deleteData(const std::string &tableName, const std::string &columns, const std::string &value); - - void updateData( - const std::string &tableName, - const std::string &setRecord, - const std::string &setValue, - const std::string &findRecord, - const std::string &findValue); - - void selectData( - const std::string &tableName, - const std::string &findRecord, - const std::string &findValue); - -private: - void executeQuery(const std::string &query); - - sqlite3 *database_; -}; \ No newline at end of file + + class DataBaseHandler + { + public: + DataBaseHandler(const char *dbName); + ~DataBaseHandler(); + + void createTable(const std::string &tableName, const std::string &columns); + void insertData(const std::string &tableName, const std::vector &columns, const std::vector &values); + void deleteData(const std::string &tableName, const std::string &columns, const std::string &value); + + void updateData( + const std::string &tableName, + const std::string &setRecord, + const std::string &setValue, + const std::string &findRecord, + const std::string &findValue); + + void selectData( + const std::string &tableName, + const std::string &findRecord, + const std::string &findValue); + + private: + void executeQuery(const char *query); + sqlite3 *database_; + }; + +} diff --git a/source/DataBase/common/DataBaseCommon.hpp b/source/DataBase/common/DataBaseCommon.hpp new file mode 100644 index 0000000..a0d62ef --- /dev/null +++ b/source/DataBase/common/DataBaseCommon.hpp @@ -0,0 +1,20 @@ +#pragma once + +#include +#include + +namespace dataBase::common +{ + + static constexpr char dataBasePath[] = "../../../source/dataBase/password-manager.db"; + static constexpr char users[] = "Users"; + static constexpr char passwords[] = "Passwords"; + static constexpr char passwordHistory[] = "PasswordHistory"; + static constexpr char categories[] = "Categories"; + + static const std::vector userRecordsRecords{"Username", "Password"}; + static const std::vector categoriesRecords{"Category"}; + static const std::vector passwordHistoryRecords{"Creation_Time", "Modify_Time", "Expiry_Time"}; + static const std::vector passwordRecords{ + "User_Id", "Category_Id", "Title", "Username", "Password", "Note", "Url"}; +}; diff --git a/source/DataBase/password-manager.db b/source/DataBase/password-manager.db index 6a28d51402e0b17255e8a5226ea332a0f9e83e55..f8d1dcd80bdf6dbe196b2bb2274e0e6b01ead8cd 100644 GIT binary patch delta 54 zcmZoTz}Rqrae_3X^h6nFR%r&kvd)bu3-s9-`R_9D-`&h*aG9T5k&%;uQxps)pVOCE Hu|Gnc_-{)rQ0**HNwhD8nv06z!| AY5)KL diff --git a/source/main.cpp b/source/main.cpp index 1d78cf9..9dece5d 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -2,17 +2,18 @@ #include "PasswordManager.h" #include "DataBase/DataBaseHandler.hpp" -#include "DataBase/DataBaseCommon.hpp" +#include "DataBase/common/DataBaseCommon.hpp" int main() { std::string password = "haslo"; PasswordManager passwordManager(password); const std::vector passwordValues{ - "1", "test", "test", "test", "test", "test", "test", "test", "Modify_Time", "Expiry_Time"}; + "1", "test", "test", "test", "test", "test", "test"}; try { - DataBaseHandler dataBaseHandler(DataBaseCommon::dataBasePath); + dataBase::DataBaseHandler dataBaseHandler(dataBase::common::dataBasePath); + dataBaseHandler.insertData(dataBase::common::passwords, dataBase::common::passwordRecords, passwordValues); std::cout << "dataBase is working" << std::endl; } catch (const std::exception &e)