Skip to content

Commit

Permalink
Create Basic DataBase. Fixing DataBaseCommon.pp
Browse files Browse the repository at this point in the history
  • Loading branch information
dominikpalatynski committed Feb 7, 2024
1 parent 162bcfe commit a832645
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 128 deletions.
31 changes: 0 additions & 31 deletions source/DataBase/DataBaseCommon.hpp

This file was deleted.

140 changes: 72 additions & 68 deletions source/DataBase/DataBaseHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,96 +3,100 @@
#include <sstream>

#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<std::string> &columns, const std::vector<std::string> &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<std::string> &columns, const std::vector<std::string> &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);
}
56 changes: 30 additions & 26 deletions source/DataBase/DataBaseHandler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string> &columns, const std::vector<std::string> &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_;
};

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<std::string> &columns, const std::vector<std::string> &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_;
};

}
20 changes: 20 additions & 0 deletions source/DataBase/common/DataBaseCommon.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#pragma once

#include <vector>
#include <string>

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<std::string> userRecordsRecords{"Username", "Password"};
static const std::vector<std::string> categoriesRecords{"Category"};
static const std::vector<std::string> passwordHistoryRecords{"Creation_Time", "Modify_Time", "Expiry_Time"};
static const std::vector<std::string> passwordRecords{
"User_Id", "Category_Id", "Title", "Username", "Password", "Note", "Url"};
};
Binary file modified source/DataBase/password-manager.db
Binary file not shown.
7 changes: 4 additions & 3 deletions source/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string> 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)
Expand Down

0 comments on commit a832645

Please sign in to comment.