-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New DataManagment division into fetching and inserting methods
- Loading branch information
1 parent
26b0b0f
commit f5cd26e
Showing
20 changed files
with
293 additions
and
440 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.