-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of https://github.com/csirianni/private-data-lookup…
… into 7-create-breached-password-generator-script
- Loading branch information
Showing
6 changed files
with
169 additions
and
8 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
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
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
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,76 @@ | ||
#include <filesystem> | ||
#include <sqlite3.h> | ||
#include "database.hpp" | ||
|
||
namespace | ||
{ | ||
// https://stackoverflow.com/questions/31146713/sqlite3-exec-callback-function-clarification | ||
static int printRow(void *unused, int width, char **data, char **attributes) | ||
{ | ||
for (int i = 0; i < width; i++) | ||
{ | ||
printf("%s = %s\n", attributes[i], data[i] ? data[i] : "NULL"); | ||
} | ||
printf("\n"); | ||
return 0; | ||
} | ||
} | ||
|
||
namespace database | ||
{ | ||
Database::Database(const std::string &file_path) : is_closed(false) | ||
{ | ||
if (std::filesystem::exists(file_path)) | ||
{ | ||
std::remove(file_path.c_str()); | ||
} | ||
|
||
int result = sqlite3_open_v2(file_path.c_str(), &db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL); | ||
if (result != SQLITE_OK) | ||
{ | ||
const char *error_msg = sqlite3_errmsg(db); | ||
fprintf(stderr, "SQLite error: %s\n", error_msg); | ||
} | ||
} | ||
|
||
void Database::execute(const std::string &command) | ||
{ | ||
int result = sqlite3_exec(db, command.c_str(), NULL, NULL, NULL); | ||
if (result != SQLITE_OK) | ||
{ | ||
const char *error_msg = sqlite3_errmsg(db); | ||
fprintf(stderr, "SQLite error: %s\n", error_msg); | ||
} | ||
} | ||
|
||
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 *error_msg = sqlite3_errmsg(db); | ||
fprintf(stderr, "SQLite error: %s\n", error_msg); | ||
} | ||
} | ||
|
||
void Database::close() | ||
{ | ||
sqlite3_close(db); | ||
is_closed = true; | ||
} | ||
|
||
Database::~Database() | ||
{ | ||
if (!is_closed) | ||
{ | ||
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(); | ||
} | ||
} | ||
} |
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,55 @@ | ||
#include <sqlite3.h> | ||
|
||
#ifndef DATABASE_H | ||
#define DATABASE_H | ||
|
||
namespace database | ||
{ | ||
/** | ||
* @brief Class containing a SQLite database connection | ||
* | ||
* @warning Rule of Three/Five not currently defined for this class. | ||
*/ | ||
class Database | ||
{ | ||
public: | ||
/** | ||
* @brief Construct a new Database object | ||
* | ||
* @warning If a file already exists at the provided path, it is replaced. | ||
* @param file_path The path for the .db file. | ||
*/ | ||
Database(const std::string &file_path); | ||
|
||
/** | ||
* @brief Execute the provided SQL command. | ||
* | ||
* @param command The command to be executed. | ||
*/ | ||
void execute(const std::string &command); | ||
|
||
/** | ||
* @brief Print the rows of the provided table. | ||
* | ||
* @param table The table to print | ||
*/ | ||
void printTable(const std::string &table); | ||
|
||
/** | ||
* @brief Close the connection with the SQLite database. | ||
* | ||
*/ | ||
void close(); | ||
|
||
/** | ||
* @brief Destroy the Database object | ||
* | ||
*/ | ||
~Database(); | ||
|
||
private: | ||
sqlite3 *db; | ||
bool is_closed; | ||
}; | ||
} | ||
#endif // DATABASE_H |
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