-
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.
- Loading branch information
1 parent
dc190b1
commit e476083
Showing
22 changed files
with
668 additions
and
154 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
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) |
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 |
---|---|---|
@@ -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 |
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 |
---|---|---|
@@ -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 |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -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 |
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 |
---|---|---|
@@ -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 |
Oops, something went wrong.