Skip to content

Commit

Permalink
New DataManagment division into fetching and inserting methods
Browse files Browse the repository at this point in the history
  • Loading branch information
dominikpalatynski committed Mar 5, 2024
1 parent 26b0b0f commit f5cd26e
Show file tree
Hide file tree
Showing 20 changed files with 293 additions and 440 deletions.
2 changes: 1 addition & 1 deletion source/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ add_executable(passwordManager

target_include_directories(sqlite3 PUBLIC ../lib)
target_link_libraries(passwordManager PRIVATE sqlite3 ${OPENSSL_LIBRARIES})

target_link_libraries(passwordManager PRIVATE sqlite3)
add_subdirectory(test)
add_subdirectory(DataBase)

Expand Down
2 changes: 1 addition & 1 deletion source/DataBase/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ project(PasswordManager)

target_include_directories(passwordManager PRIVATE DataBase/interface)

target_sources(passwordManager PRIVATE
target_sources(passwordManager PRIVATE
DataBaseHandler.cpp DataBaseConnection.cpp DataBaseManager.cpp QueryOperationBuilder.cpp)
68 changes: 37 additions & 31 deletions source/DataBase/DataBaseConnection.cpp
Original file line number Diff line number Diff line change
@@ -1,42 +1,48 @@
#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_)));
}
}
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_);
}
}
DataBaseConnection::~DataBaseConnection() {
if (database_ != nullptr) {
sqlite3_close(database_);
}
}

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

int DataBaseConnection::executeQuery(const std::string &query, sqlite3_callback selectCallback, std::shared_ptr<common::ITable> records)
{
char *errMsg = nullptr;
int result;
result = sqlite3_exec(database_, query.c_str(), nullptr, nullptr, &errMsg);

int result;
return result;
}

if (records)
{
result = sqlite3_exec(database_, query.c_str(), selectCallback, &records, &errMsg);
}
else
{
result = sqlite3_exec(database_, query.c_str(), nullptr, nullptr, &errMsg);
}
int DataBaseConnection::executeQueryAndGetData(
const std::string &query, std::vector<std::string> &records) {
char *errMsg = nullptr;

return result;
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;
};

int result;
result = sqlite3_exec(database_, query.c_str(), callback, &records, &errMsg);

return result;
}
} // namespace dataBase
26 changes: 13 additions & 13 deletions source/DataBase/DataBaseConnection.hpp
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
#pragma once

#include <string>
#include <stdexcept>
#include <string>

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

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

~DataBaseConnection();
~DataBaseConnection();

int executeQuery(const std::string &query, sqlite3_callback selectCallback = nullptr, std::shared_ptr<common::ITable> records = nullptr) override;
int executeQuery(const std::string &query) override;
int executeQueryAndGetData(const std::string &query,
std::vector<std::string> &records) override;

private:
sqlite3 *database_;
};
private:
sqlite3 *database_;
};

}
} // namespace dataBase
56 changes: 27 additions & 29 deletions source/DataBase/DataBaseManager.cpp
Original file line number Diff line number Diff line change
@@ -1,34 +1,32 @@

#include "DataBaseManager.hpp"

#include "QueryOperationBuilder.hpp"
#include "common/UserTable.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))
{
}

int DataBaseManager::executeOperation(const common::DataBaseRequest &requestData,
std::shared_ptr<common::ITable> records)
{
std::string result = queryOperationBuilder_->buildQuery(requestData);

if (requestData.operationType_ != common::DataBaseRequest::OperationType::Select)
{
dataBaseConnection_->executeQuery(result);
return static_cast<int>(common::DataBaseResponse::ResponseSuccess);
}

auto callback = [](void *data, int argc, char **argv, char **azColName) -> int
{
auto recordsPtr = *static_cast<std::shared_ptr<common::ITable> *>(data);
return recordsPtr->selectCallback(data, argc, argv, azColName);
};

dataBaseConnection_->executeQuery(result, callback, records);
return static_cast<int>(common::DataBaseResponse::ResponseSuccess);
}
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) {
dataBaseConnection_->executeQuery(query);
}
}

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

std::vector<std::string> result;

dataBaseConnection_->executeQueryAndGetData(query, result);

return result;
}
} // namespace dataBase
35 changes: 17 additions & 18 deletions source/DataBase/DataBaseManager.hpp
Original file line number Diff line number Diff line change
@@ -1,30 +1,29 @@
#pragma once

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

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

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

int executeOperation(const common::DataBaseRequest &requestData,
std::shared_ptr<common::ITable> records = nullptr) override;
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_;
};
}
private:
std::shared_ptr<interface::IDataBaseConnection> dataBaseConnection_;
std::shared_ptr<interface::IQueryOperationBuilder> queryOperationBuilder_;
};
} // namespace dataBase
Loading

0 comments on commit f5cd26e

Please sign in to comment.