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 2 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: 6 additions & 1 deletion source/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
project(PasswordManager)

add_executable(passwordManager main.cpp PasswordManager.cpp)
add_library(sqlite3 STATIC ../lib/sqlite3.c)

add_executable(passwordManager main.cpp PasswordManager.cpp DataBase/DataBaseHandler.cpp DataBase/DataBaseCommon.cpp)
dominikpalatynski marked this conversation as resolved.
Show resolved Hide resolved

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

add_subdirectory(test)
dominikpalatynski marked this conversation as resolved.
Show resolved Hide resolved
9 changes: 9 additions & 0 deletions source/DataBase/DataBaseCommon.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

#include "DataBaseCommon.hpp"

const std::vector<std::string> DataBaseCommon::PasswordRecords{"User_Id", "Category", "Username", "Password", "Url", "Expire_Time"};
const std::vector<std::string> DataBaseCommon::UserRecord{"Username", "Password"};

const std::string DataBaseCommon::dataBasePath{"../../../source/DataBase/password-manager.db"};
const std::string DataBaseCommon::Users{"Users"};
const std::string DataBaseCommon::Passwords{"Passwords"};
14 changes: 14 additions & 0 deletions source/DataBase/DataBaseCommon.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#pragma once

#include <iostream>
dominikpalatynski marked this conversation as resolved.
Show resolved Hide resolved
#include <vector>

struct DataBaseCommon
dominikpalatynski marked this conversation as resolved.
Show resolved Hide resolved
{
static const std::vector<std::string> UserRecord;
dominikpalatynski marked this conversation as resolved.
Show resolved Hide resolved
static const std::vector<std::string> PasswordRecords;
dominikpalatynski marked this conversation as resolved.
Show resolved Hide resolved

static const std::string dataBasePath;
static const std::string Users;
dominikpalatynski marked this conversation as resolved.
Show resolved Hide resolved
static const std::string Passwords;
dominikpalatynski marked this conversation as resolved.
Show resolved Hide resolved
};
92 changes: 92 additions & 0 deletions source/DataBase/DataBaseHandler.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
#include <iostream>
#include <memory>
#include <sstream>

#include "DataBaseHandler.hpp"

DataBaseHandler::DataBaseHandler(const std::string &dbName)
: database_(nullptr)
{
int result = sqlite3_open(dbName.c_str(), &database_);
dominikpalatynski marked this conversation as resolved.
Show resolved Hide resolved
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 std::string &query)
{
char *errMsg = nullptr;
int result = sqlite3_exec(database_, query.c_str(), 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())
{
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);
}

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;

executeQuery(query);
}
35 changes: 35 additions & 0 deletions source/DataBase/DataBaseHandler.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#pragma once

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

#include "sqlite3.h"

class DataBaseHandler
{
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);

dominikpalatynski marked this conversation as resolved.
Show resolved Hide resolved
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_;
};
dominikpalatynski marked this conversation as resolved.
Show resolved Hide resolved
Binary file added source/DataBase/password-manager.db
Binary file not shown.
19 changes: 17 additions & 2 deletions source/main.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,25 @@
#include <string>

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

int main() {
int main()
{
std::string password = "haslo";
PasswordManager passwordManager(password);

try
{
DataBaseHandler dataBaseHandler(DataBaseCommon::dataBasePath);

std::cout << "Db 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