juk3n/fluffypublic Fork
⑂ 770783e
Commits
⬇ Clone ▾
git clone https://git.rickub.com/juk3n/fluffy.git
git clone ssh://git@rickub.com/juk3n/fluffy.git

Host key fingerprint (ed25519): SHA256:iycHnxEyq0Q7uyVpB7JlznP0G7JrTPXLYRcAU5CSLhc — verify it before your first connect.

refactored

juk3n committed 2024-07-03T18:30:07+02:00 Browse files
770783e parent: b2c785e
modified app/include/database.hpp +16 -13
@@ -8,19 +8,26 @@
88 class Database
99 {
1010 private:
11+ sqlite3 *database;
12+
1113 std::string database_initialization_command =
1214 "CREATE TABLE games("
1315 "GAME_ID INTEGER PRIMARY KEY AUTOINCREMENT, "
1416 "GAME_NAME TEXT NOT NULL, "
1517 "GAME_PATH TEXT NOT NULL);";
16-public:
17- sqlite3 *database;
1818 std::string select_all_games_command = "SELECT * FROM games;";
1919
20+ auto initializeDatabase() -> void {
21+ executeSqlCommand(database_initialization_command);
22+ //printMessage("Database Initialized");
23+ }
24+
25+public:
26+ bool successfullyCreated{ true };
27+
2028 Database(std::filesystem::path databasePath) {
21- database = loadDatabase(databasePath);
29+ loadDatabase(databasePath);
2230 }
23- Database() = default;
2431
2532 ~Database() {
2633 sqlite3_close(database);
@@ -34,15 +41,13 @@ public:
3441 if (exit != SQLITE_OK) {
3542 //printMessage("Error with command: " + command + " " + messageError);
3643 sqlite3_free(messageError);
44+ successfullyCreated = false;
3745 }
3846 }
3947
40- auto initializeDatabase() -> void {
41- executeSqlCommand(database_initialization_command);
42- //printMessage("Database Initialized");
43- }
4448
45- auto loadDatabase(std::filesystem::path databasePath) -> sqlite3* {
49+
50+ auto loadDatabase(std::filesystem::path databasePath) -> void {
4651 bool newCreation = not std::filesystem::exists(databasePath.string());
4752
4853 int exit = 0;
@@ -51,7 +56,7 @@ public:
5156 if (exit) {
5257 std::cerr << "Error opening database" << sqlite3_errmsg(database)
5358 << std::endl;
54- return nullptr;
59+ successfullyCreated = false;
5560 }
5661
5762 //printMessage("Opened database successfully!");
@@ -59,10 +64,9 @@ public:
5964 if (newCreation) {
6065 initializeDatabase();
6166 }
62- return database;
6367 }
6468
65- auto gettingData() -> std::vector<Game> {
69+ auto getGames() -> std::vector<Game> {
6670 sqlite3_stmt *stmt;
6771 int exit = 0;
6872 exit = sqlite3_prepare_v2(database, select_all_games_command.c_str(), -1, &stmt, nullptr);
@@ -85,6 +89,5 @@ public:
8589 }
8690 sqlite3_finalize(stmt);
8791 return games;
88-
8992 }
9093 };
@@ -8,19 +8,26 @@
8 class Database8 class Database
9 {9 {
10 private:10 private:
11+ sqlite3 *database;
12+
11 std::string database_initialization_command =13 std::string database_initialization_command =
12 "CREATE TABLE games("14 "CREATE TABLE games("
13 "GAME_ID INTEGER PRIMARY KEY AUTOINCREMENT, "15 "GAME_ID INTEGER PRIMARY KEY AUTOINCREMENT, "
14 "GAME_NAME TEXT NOT NULL, "16 "GAME_NAME TEXT NOT NULL, "
15 "GAME_PATH TEXT NOT NULL);";17 "GAME_PATH TEXT NOT NULL);";
16-public:
17- sqlite3 *database;
18 std::string select_all_games_command = "SELECT * FROM games;";18 std::string select_all_games_command = "SELECT * FROM games;";
19 19
20+ auto initializeDatabase() -> void {
21+ executeSqlCommand(database_initialization_command);
22+ //printMessage("Database Initialized");
23+ }
24+
25+public:
26+ bool successfullyCreated{ true };
27+
20 Database(std::filesystem::path databasePath) {28 Database(std::filesystem::path databasePath) {
21- database = loadDatabase(databasePath);29+ loadDatabase(databasePath);
22 }30 }
23- Database() = default;
24 31
25 ~Database() {32 ~Database() {
26 sqlite3_close(database);33 sqlite3_close(database);
@@ -34,15 +41,13 @@ public:
34 if (exit != SQLITE_OK) {41 if (exit != SQLITE_OK) {
35 //printMessage("Error with command: " + command + " " + messageError);42 //printMessage("Error with command: " + command + " " + messageError);
36 sqlite3_free(messageError);43 sqlite3_free(messageError);
44+ successfullyCreated = false;
37 }45 }
38 }46 }
39 47
40- auto initializeDatabase() -> void {
41- executeSqlCommand(database_initialization_command);
42- //printMessage("Database Initialized");
43- }
44 48
45- auto loadDatabase(std::filesystem::path databasePath) -> sqlite3* {49+
50+ auto loadDatabase(std::filesystem::path databasePath) -> void {
46 bool newCreation = not std::filesystem::exists(databasePath.string());51 bool newCreation = not std::filesystem::exists(databasePath.string());
47 52
48 int exit = 0;53 int exit = 0;
@@ -51,7 +56,7 @@ public:
51 if (exit) {56 if (exit) {
52 std::cerr << "Error opening database" << sqlite3_errmsg(database)57 std::cerr << "Error opening database" << sqlite3_errmsg(database)
53 << std::endl;58 << std::endl;
54- return nullptr;59+ successfullyCreated = false;
55 }60 }
56 61
57 //printMessage("Opened database successfully!");62 //printMessage("Opened database successfully!");
@@ -59,10 +64,9 @@ public:
59 if (newCreation) {64 if (newCreation) {
60 initializeDatabase();65 initializeDatabase();
61 }66 }
62- return database;
63 }67 }
64 68
65- auto gettingData() -> std::vector<Game> {69+ auto getGames() -> std::vector<Game> {
66 sqlite3_stmt *stmt;70 sqlite3_stmt *stmt;
67 int exit = 0;71 int exit = 0;
68 exit = sqlite3_prepare_v2(database, select_all_games_command.c_str(), -1, &stmt, nullptr);72 exit = sqlite3_prepare_v2(database, select_all_games_command.c_str(), -1, &stmt, nullptr);
@@ -85,6 +89,5 @@ public:
85 }89 }
86 sqlite3_finalize(stmt);90 sqlite3_finalize(stmt);
87 return games;91 return games;
88-
89 }92 }
90 };93 };
modified app/src/main.cpp +2 -3
@@ -157,10 +157,9 @@ auto main(int argc, char const *argv[]) -> int {
157157 auto databasePath{ std::filesystem::path(getExecutablePath().parent_path().string() + "/data.db")};
158158 Database database{databasePath};
159159
160- if (!database.database) return EXIT_FAILURE;
161-
162- games = database.gettingData();
160+ if (!database.successfullyCreated) return EXIT_FAILURE;
163161
162+ games = database.getGames();
164163
165164 if (argc > 1) {
166165 handleCommand(argc, argv, database);
@@ -157,10 +157,9 @@ auto main(int argc, char const *argv[]) -> int {
157 auto databasePath{ std::filesystem::path(getExecutablePath().parent_path().string() + "/data.db")};157 auto databasePath{ std::filesystem::path(getExecutablePath().parent_path().string() + "/data.db")};
158 Database database{databasePath};158 Database database{databasePath};
159 159
160- if (!database.database) return EXIT_FAILURE;160+ if (!database.successfullyCreated) return EXIT_FAILURE;
161-
162- games = database.gettingData();
163 161
162+ games = database.getGames();
164 163
165 if (argc > 1) {164 if (argc > 1) {
166 handleCommand(argc, argv, database);165 handleCommand(argc, argv, database);