Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SQL Lite basic database file #26

Merged
merged 12 commits into from
Feb 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
255,680 changes: 255,680 additions & 0 deletions lib/sqlite3.c

Large diffs are not rendered by default.

13,355 changes: 13,355 additions & 0 deletions lib/sqlite3.h

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions source/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
project(PasswordManager)

add_library(sqlite3 STATIC ../lib/sqlite3.c)
find_package(OpenSSL REQUIRED)
include_directories(${OPENSSL_INCLUDE_DIR})

add_executable(passwordManager main.cpp PasswordManager.cpp)
add_subdirectory(test)

target_include_directories(sqlite3 PUBLIC ../lib)
target_link_libraries(passwordManager PRIVATE sqlite3)

add_subdirectory(test)
add_subdirectory(DataBase)

target_link_libraries(passwordManager ${OPENSSL_LIBRARIES})
3 changes: 3 additions & 0 deletions source/DataBase/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
project(PasswordManager)

target_sources(passwordManager PRIVATE DataBaseHandler.cpp)
102 changes: 102 additions & 0 deletions source/DataBase/DataBaseHandler.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
#include <iostream>
#include <memory>
#include <sstream>

#include "DataBaseHandler.hpp"
namespace dataBase
{

DataBaseHandler::DataBaseHandler(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_)));
}
}

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

void DataBaseHandler::executeQuery(const char *query)
{
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.c_str());
}

void DataBaseHandler::insertData(const std::string &tableName, const std::vector<std::string> &columns, const std::vector<std::string> &values)
{
if (columns.size() != values.size())
{
throw std::invalid_argument("Columns and values sizes do not match.");
}

std::ostringstream queryStream;
queryStream << "INSERT INTO " << tableName << " (";
for (size_t i = 0; i < columns.size(); ++i)
{
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());
}

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::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::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());
}

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

#include <iostream>
#include <stdexcept>
#include <memory>
#include <vector>

#include "sqlite3.h"

namespace 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 added source/DataBase/password-manager.db
Binary file not shown.
9 changes: 5 additions & 4 deletions source/PasswordManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

#include "PasswordManager.h"

PasswordManager::PasswordManager(const std::string& password)
: password_(password)
PasswordManager::PasswordManager(const std::string &password)
: password_(password)
{
std::cout << "Password is: " << password_ << '\n';
}

PasswordManager::~PasswordManager() {
PasswordManager::~PasswordManager()
{
std::cout << "Password manager destructor\n";
}
}
20 changes: 18 additions & 2 deletions source/main.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,26 @@
#include <string>

#include "PasswordManager.h"
#include "DataBase/DataBaseHandler.hpp"
#include "DataBase/common/DataBaseCommon.hpp"

int main() {
int main()
{
std::string password = "haslo";
PasswordManager passwordManager(password);
const std::vector<std::string> passwordValues{
"1", "test", "test", "test", "test", "test", "test"};
try
{
dataBase::DataBaseHandler dataBaseHandler(dataBase::common::dataBasePath);
dataBaseHandler.insertData(dataBase::common::passwords, dataBase::common::passwordRecords, passwordValues);
std::cout << "dataBase is working" << std::endl;
dominikpalatynski marked this conversation as resolved.
Show resolved Hide resolved
}
catch (const std::exception &e)
{
std::cout << "Error occured" << std::endl;
std::cerr << "Error: " << e.what() << std::endl;
}

return 0;
}
}
Loading