Skip to content

Commit

Permalink
New DataBaseManagment
Browse files Browse the repository at this point in the history
  • Loading branch information
dominikpalatynski committed Mar 14, 2024
1 parent dc190b1 commit e476083
Show file tree
Hide file tree
Showing 22 changed files with 668 additions and 154 deletions.
6 changes: 5 additions & 1 deletion source/Backend/DataBase/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
project(PasswordManager)

target_sources(passwordManager PRIVATE DataBaseHandler.cpp)
target_include_directories(passwordManager PRIVATE DataBase/interface)
target_include_directories(passwordManager PRIVATE DataBase/common)

target_sources(passwordManager PRIVATE
DataBaseConnection.cpp DataBaseManager.cpp QueryOperationBuilder.cpp)
57 changes: 57 additions & 0 deletions source/Backend/DataBase/DataBaseConnection.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#include "DataBaseConnection.hpp"

#include "sqlite3.h"

namespace dataBase {
DataBaseConnection::DataBaseConnection(const char *dbName)
: database_(nullptr) {
int result = sqlite3_open(dbName, &database_);
if (result != SQLITE_OK) {
throw std::runtime_error("Failed to open database: " +
std::string(sqlite3_errmsg(database_)));
}
}

DataBaseConnection::~DataBaseConnection() {
if (database_ != nullptr) {
sqlite3_close(database_);
}
}

void DataBaseConnection::executeQuery(const std::string &query) {
char *errMsg = nullptr;

int result =
sqlite3_exec(database_, query.c_str(), nullptr, nullptr, &errMsg);
checkResult(result, errMsg);
}

std::vector<std::string> DataBaseConnection::executeQueryAndGetData(
const std::string &query) {
char *errMsg = nullptr;

auto callback = [](void *data, int argc, char **argv,
char **azColName) -> int {
auto records = reinterpret_cast<std::vector<std::string> *>(data);
for (int i = 0; i < argc; i++) {
records->push_back(argv[i] ? argv[i] : "");
}
return 0;
};

std::vector<std::string> records;
int result =
sqlite3_exec(database_, query.c_str(), callback, &records, &errMsg);

checkResult(result, errMsg);
return records;
}

void DataBaseConnection::checkResult(int result, char *errMsg) {
if (result != SQLITE_OK) {
std::string error_msg = errMsg ? errMsg : sqlite3_errmsg(database_);
sqlite3_free(errMsg);
throw std::runtime_error("Failed to execute query: " + error_msg);
}
}
} // namespace dataBase
27 changes: 27 additions & 0 deletions source/Backend/DataBase/DataBaseConnection.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#pragma once

#include <stdexcept>
#include <string>

#include "interface/IDataBaseConnection.hpp"
#include "sqlite3.h"

namespace dataBase {
class DataBaseConnection : public interface::IDataBaseConnection {
public:
DataBaseConnection(const char *dbName);

~DataBaseConnection();

void executeQuery(const std::string &query) override;
std::vector<std::string> executeQueryAndGetData(
const std::string &query) override;

private:
void checkResult(int result, char *errMsg);

private:
sqlite3 *database_;
};

} // namespace dataBase
93 changes: 0 additions & 93 deletions source/Backend/DataBase/DataBaseHandler.cpp

This file was deleted.

36 changes: 0 additions & 36 deletions source/Backend/DataBase/DataBaseHandler.hpp

This file was deleted.

43 changes: 43 additions & 0 deletions source/Backend/DataBase/DataBaseManager.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@

#include "DataBaseManager.hpp"

#include "QueryOperationBuilder.hpp"
namespace dataBase {
DataBaseManager::DataBaseManager(
std::shared_ptr<interface::IDataBaseConnection> dataBaseConnection,
std::shared_ptr<interface::IQueryOperationBuilder> queryOperationBuilder)
: dataBaseConnection_(std::move(dataBaseConnection)),
queryOperationBuilder_(std::move(queryOperationBuilder)) {}

void DataBaseManager::executeOperation(
const common::DataBaseRequest &requestData) {
std::string query = queryOperationBuilder_->buildQuery(requestData);

if (requestData.operationType_ !=
common::DataBaseRequest::OperationType::Select &&
!query.empty())

try {
dataBaseConnection_->executeQuery(query);
} catch (const std::runtime_error &e) {
std::cerr << "Error executing query: " << e.what() << std::endl;
}
}

std::vector<std::string> DataBaseManager::executeAndGetOperation(
const common::DataBaseRequest &requestData) {
std::string query = queryOperationBuilder_->buildQuery(requestData);

try {
if (requestData.operationType_ ==
common::DataBaseRequest::OperationType::Select &&
!query.empty())
return dataBaseConnection_->executeQueryAndGetData(query);

return {};
} catch (const std::runtime_error &e) {
std::cerr << "Error executing query: " << e.what() << std::endl;
return {};
}
}
} // namespace dataBase
29 changes: 29 additions & 0 deletions source/Backend/DataBase/DataBaseManager.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#pragma once

#include <functional>
#include <iostream>
#include <memory>
#include <stdexcept>

#include "common/DataBaseRequest.hpp"
#include "interface/IDataBaseConnection.hpp"
#include "interface/IDataBaseManager.hpp"
#include "interface/IQueryOperationBuilder.hpp"

namespace dataBase {
class DataBaseManager : public interface::IDataBaseManager {
public:
DataBaseManager(
std::shared_ptr<interface::IDataBaseConnection> dataBaseConnection,
std::shared_ptr<interface::IQueryOperationBuilder> queryOperationBuilder);
~DataBaseManager() = default;

void executeOperation(const common::DataBaseRequest &requestData) override;
std::vector<std::string> executeAndGetOperation(
const common::DataBaseRequest &requestData) override;

private:
std::shared_ptr<interface::IDataBaseConnection> dataBaseConnection_;
std::shared_ptr<interface::IQueryOperationBuilder> queryOperationBuilder_;
};
} // namespace dataBase
Loading

0 comments on commit e476083

Please sign in to comment.