Skip to content

Commit

Permalink
feat: create database class
Browse files Browse the repository at this point in the history
  • Loading branch information
csirianni committed Oct 20, 2023
1 parent 99fdd6c commit fdb250a
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 30 deletions.
67 changes: 40 additions & 27 deletions backend/src/database.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@

namespace
{
static int printData(void *NotUsed, int argc, char **argv, char **azColName)
// https://stackoverflow.com/questions/31146713/sqlite3-exec-callback-function-clarification
static int printRow(void *unused, int count, char **data, char **columns)
{
for (int i = 0; i < argc; i++)
for (int i = 0; i < count; i++)
{
printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");
printf("%s = %s\n", columns[i], data[i] ? data[i] : "NULL");
}
printf("\n");
return 0;
Expand All @@ -17,47 +18,59 @@ namespace

namespace database
{
void initializeDatabase(const char *file)
Database::Database(const std::string &file_path) : is_closed(false)
{
if (std::filesystem::exists(file))
if (std::filesystem::exists(file_path))
{
std::remove(file);
std::remove(file_path.c_str());
}

sqlite3 *db;
int result = sqlite3_open_v2("passwords.db", &db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL);
// TODO: improve this error handling
int result = sqlite3_open_v2(file_path.c_str(), &db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL);
if (result != SQLITE_OK)
{
const char *errorMsg = sqlite3_errmsg(db);
fprintf(stderr, "SQLite error: %s\n", errorMsg);
const char *error_msg = sqlite3_errmsg(db);
fprintf(stderr, "SQLite error: %s\n", error_msg);
}
}

// TODO: investigate IF NOT EXISTS
const char *table = "CREATE TABLE passwords (password TEXT);";
result = sqlite3_exec(db, table, NULL, NULL, NULL);
void Database::execute(const std::string &command)
{
int result = sqlite3_exec(db, command.c_str(), NULL, NULL, NULL);
if (result != SQLITE_OK)
{
const char *errorMsg = sqlite3_errmsg(db);
fprintf(stderr, "SQLite error: %s\n", errorMsg);
const char *error_msg = sqlite3_errmsg(db);
fprintf(stderr, "SQLite error: %s\n", error_msg);
}
}

const char *insertion = "INSERT INTO passwords (password) VALUES ('chocolate1');";
result = sqlite3_exec(db, insertion, NULL, NULL, NULL);
void Database::printTable(const std::string &table)
{
const std::string query = "SELECT * FROM " + table + ";";
int result = sqlite3_exec(db, query.c_str(), printRow, NULL, NULL);
if (result != SQLITE_OK)
{
const char *errorMsg = sqlite3_errmsg(db);
fprintf(stderr, "SQLite error: %s\n", errorMsg);
const char *error_msg = sqlite3_errmsg(db);
fprintf(stderr, "SQLite error: %s\n", error_msg);
}
}

const char *selection = "SELECT * FROM passwords";
result = sqlite3_exec(db, selection, printData, NULL, NULL);
if (result != SQLITE_OK)
void Database::close()
{
sqlite3_close(db);
is_closed = true;
}

Database::~Database()
{
if (!is_closed)
{
const char *errorMsg = sqlite3_errmsg(db);
fprintf(stderr, "SQLite error: %s\n", errorMsg);
int result = sqlite3_close(db);
if (result != SQLITE_OK)
{
const char *error_msg = sqlite3_errmsg(db);
fprintf(stderr, "SQLite error: %s\n", error_msg);
}
std::abort();
}

sqlite3_close(db);
}
}
43 changes: 41 additions & 2 deletions backend/src/database.hpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,48 @@
#include <sqlite3.h>

#ifndef DATABASE_H
#define DATABASE_H

namespace database
{
void initializeDatabase(const char *file);
}
/**
* @brief Class containing a SQLite database connection
*
*/
class Database
{
public:
/**
* @brief Construct a new Database object
*
* @param file_path
*/
Database(const std::string &file_path);
/**
* @brief
*
* @param command
*/
void execute(const std::string &command);
/**
* @brief
*
*/
void printTable(const std::string &table);
/**
* @brief
*
*/
void close();
/**
* @brief Destroy the Database object
*
*/
~Database();

private:
sqlite3 *db;
bool is_closed;
};
}
#endif // DATABASE_H
6 changes: 5 additions & 1 deletion backend/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@

int main()
{
database::initializeDatabase("passwords.db");
database::Database db = database::Database("passwords.db");
db.execute("CREATE TABLE passwords (password TEXT);");
db.execute("INSERT INTO passwords (password) VALUES ('chocolate1');");
db.printTable("passwords");

// declare crow application
crow::SimpleApp app;
Expand All @@ -23,4 +26,5 @@ int main()

// set the port, set the app to run on multiple threads, and run the app
app.port(18080).multithreaded().run();
db.close();
}

0 comments on commit fdb250a

Please sign in to comment.