juk3n/fluffypublic Fork
⑂ b2c785e
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.

moved getting games to class

juk3n committed 2024-07-03T18:15:16+02:00 Browse files
b2c785e parent: e10b023
modified app/include/database.hpp +34 -6
@@ -3,6 +3,8 @@
33 #include <sqlite3.h>
44 #include <string>
55
6+#include <game.hpp>
7+
68 class Database
79 {
810 private:
@@ -16,7 +18,7 @@ public:
1618 std::string select_all_games_command = "SELECT * FROM games;";
1719
1820 Database(std::filesystem::path databasePath) {
19- database = load_database(databasePath);
21+ database = loadDatabase(databasePath);
2022 }
2123 Database() = default;
2224
@@ -24,7 +26,7 @@ public:
2426 sqlite3_close(database);
2527 }
2628
27- auto execute_sql_command(std::string command) -> void {
29+ auto executeSqlCommand(std::string command) -> void {
2830 int exit = 0;
2931 char *messageError;
3032 exit = sqlite3_exec(database, command.c_str(), nullptr, nullptr, &messageError);
@@ -35,12 +37,12 @@ public:
3537 }
3638 }
3739
38- auto initialize_database() -> void {
39- execute_sql_command(database_initialization_command);
40+ auto initializeDatabase() -> void {
41+ executeSqlCommand(database_initialization_command);
4042 //printMessage("Database Initialized");
4143 }
4244
43- auto load_database(std::filesystem::path databasePath) -> sqlite3* {
45+ auto loadDatabase(std::filesystem::path databasePath) -> sqlite3* {
4446 bool newCreation = not std::filesystem::exists(databasePath.string());
4547
4648 int exit = 0;
@@ -55,8 +57,34 @@ public:
5557 //printMessage("Opened database successfully!");
5658
5759 if (newCreation) {
58- initialize_database();
60+ initializeDatabase();
5961 }
6062 return database;
6163 }
64+
65+ auto gettingData() -> std::vector<Game> {
66+ sqlite3_stmt *stmt;
67+ int exit = 0;
68+ exit = sqlite3_prepare_v2(database, select_all_games_command.c_str(), -1, &stmt, nullptr);
69+
70+ if (exit) {
71+ std::cerr << "Error retrieving data" << sqlite3_errmsg(database)
72+ << std::endl;
73+ sqlite3_close(database);
74+ return {};
75+ }
76+
77+ std::vector<Game> games;
78+ while (sqlite3_step(stmt) == SQLITE_ROW) {
79+ int id = sqlite3_column_int(stmt, 0);
80+ const unsigned char *gameName = sqlite3_column_text(stmt, 1);
81+ const unsigned char *gamePath = sqlite3_column_text(stmt, 2);
82+
83+ games.emplace_back(Game{id, reinterpret_cast<const char *>(gameName),
84+ reinterpret_cast<const char *>(gamePath)});
85+ }
86+ sqlite3_finalize(stmt);
87+ return games;
88+
89+ }
6290 };
@@ -3,6 +3,8 @@
3 #include <sqlite3.h>3 #include <sqlite3.h>
4 #include <string>4 #include <string>
5 5
6+#include <game.hpp>
7+
6 class Database8 class Database
7 {9 {
8 private:10 private:
@@ -16,7 +18,7 @@ public:
16 std::string select_all_games_command = "SELECT * FROM games;";18 std::string select_all_games_command = "SELECT * FROM games;";
17 19
18 Database(std::filesystem::path databasePath) {20 Database(std::filesystem::path databasePath) {
19- database = load_database(databasePath);21+ database = loadDatabase(databasePath);
20 }22 }
21 Database() = default;23 Database() = default;
22 24
@@ -24,7 +26,7 @@ public:
24 sqlite3_close(database);26 sqlite3_close(database);
25 }27 }
26 28
27- auto execute_sql_command(std::string command) -> void {29+ auto executeSqlCommand(std::string command) -> void {
28 int exit = 0;30 int exit = 0;
29 char *messageError;31 char *messageError;
30 exit = sqlite3_exec(database, command.c_str(), nullptr, nullptr, &messageError);32 exit = sqlite3_exec(database, command.c_str(), nullptr, nullptr, &messageError);
@@ -35,12 +37,12 @@ public:
35 }37 }
36 }38 }
37 39
38- auto initialize_database() -> void {40+ auto initializeDatabase() -> void {
39- execute_sql_command(database_initialization_command);41+ executeSqlCommand(database_initialization_command);
40 //printMessage("Database Initialized");42 //printMessage("Database Initialized");
41 }43 }
42 44
43- auto load_database(std::filesystem::path databasePath) -> sqlite3* {45+ auto loadDatabase(std::filesystem::path databasePath) -> sqlite3* {
44 bool newCreation = not std::filesystem::exists(databasePath.string());46 bool newCreation = not std::filesystem::exists(databasePath.string());
45 47
46 int exit = 0;48 int exit = 0;
@@ -55,8 +57,34 @@ public:
55 //printMessage("Opened database successfully!");57 //printMessage("Opened database successfully!");
56 58
57 if (newCreation) {59 if (newCreation) {
58- initialize_database();60+ initializeDatabase();
59 }61 }
60 return database;62 return database;
61 }63 }
64+
65+ auto gettingData() -> std::vector<Game> {
66+ sqlite3_stmt *stmt;
67+ int exit = 0;
68+ exit = sqlite3_prepare_v2(database, select_all_games_command.c_str(), -1, &stmt, nullptr);
69+
70+ if (exit) {
71+ std::cerr << "Error retrieving data" << sqlite3_errmsg(database)
72+ << std::endl;
73+ sqlite3_close(database);
74+ return {};
75+ }
76+
77+ std::vector<Game> games;
78+ while (sqlite3_step(stmt) == SQLITE_ROW) {
79+ int id = sqlite3_column_int(stmt, 0);
80+ const unsigned char *gameName = sqlite3_column_text(stmt, 1);
81+ const unsigned char *gamePath = sqlite3_column_text(stmt, 2);
82+
83+ games.emplace_back(Game{id, reinterpret_cast<const char *>(gameName),
84+ reinterpret_cast<const char *>(gamePath)});
85+ }
86+ sqlite3_finalize(stmt);
87+ return games;
88+
89+ }
62 };90 };
modified app/src/main.cpp +4 -23
@@ -35,13 +35,13 @@ auto addGame(Database& database, std::string name, std::string path) -> void {
3535 std::string command =
3636 "INSERT INTO games (GAME_NAME, GAME_PATH) VALUES (";
3737 command += "'" + name + "', \"" + path + "\");";
38- database.execute_sql_command(command);
38+ database.executeSqlCommand(command);
3939 }
4040
4141 auto removeGame(Database& database, std::string name) -> void{
4242 std::string command = "DELETE FROM games WHERE GAME_NAME=";
4343 command += "'" + name + "';";
44- database.execute_sql_command(command);
44+ database.executeSqlCommand(command);
4545 }
4646
4747 auto runGame(std::string gameName) -> void {
@@ -157,29 +157,10 @@ 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) {
161- return -1;
162- }
163- sqlite3_stmt *stmt;
164- int exit = 0;
165- exit = sqlite3_prepare_v2(database.database, database.select_all_games_command.c_str(), -1, &stmt, nullptr);
166-
167- if (exit) {
168- std::cerr << "Error retrieving data" << sqlite3_errmsg(database.database)
169- << std::endl;
170- sqlite3_close(database.database);
171- return -1;
172- }
160+ if (!database.database) return EXIT_FAILURE;
173161
174- while (sqlite3_step(stmt) == SQLITE_ROW) {
175- int id = sqlite3_column_int(stmt, 0);
176- const unsigned char *gameName = sqlite3_column_text(stmt, 1);
177- const unsigned char *gamePath = sqlite3_column_text(stmt, 2);
162+ games = database.gettingData();
178163
179- games.emplace_back(Game{id, reinterpret_cast<const char *>(gameName),
180- reinterpret_cast<const char *>(gamePath)});
181- }
182- sqlite3_finalize(stmt);
183164
184165 if (argc > 1) {
185166 handleCommand(argc, argv, database);
@@ -35,13 +35,13 @@ auto addGame(Database& database, std::string name, std::string path) -> void {
35 std::string command =35 std::string command =
36 "INSERT INTO games (GAME_NAME, GAME_PATH) VALUES (";36 "INSERT INTO games (GAME_NAME, GAME_PATH) VALUES (";
37 command += "'" + name + "', \"" + path + "\");";37 command += "'" + name + "', \"" + path + "\");";
38- database.execute_sql_command(command);38+ database.executeSqlCommand(command);
39 }39 }
40 40
41 auto removeGame(Database& database, std::string name) -> void{41 auto removeGame(Database& database, std::string name) -> void{
42 std::string command = "DELETE FROM games WHERE GAME_NAME=";42 std::string command = "DELETE FROM games WHERE GAME_NAME=";
43 command += "'" + name + "';";43 command += "'" + name + "';";
44- database.execute_sql_command(command);44+ database.executeSqlCommand(command);
45 }45 }
46 46
47 auto runGame(std::string gameName) -> void {47 auto runGame(std::string gameName) -> void {
@@ -157,29 +157,10 @@ 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) {160+ if (!database.database) return EXIT_FAILURE;
161- return -1;
162- }
163- sqlite3_stmt *stmt;
164- int exit = 0;
165- exit = sqlite3_prepare_v2(database.database, database.select_all_games_command.c_str(), -1, &stmt, nullptr);
166-
167- if (exit) {
168- std::cerr << "Error retrieving data" << sqlite3_errmsg(database.database)
169- << std::endl;
170- sqlite3_close(database.database);
171- return -1;
172- }
173 161
174- while (sqlite3_step(stmt) == SQLITE_ROW) {162+ games = database.gettingData();
175- int id = sqlite3_column_int(stmt, 0);
176- const unsigned char *gameName = sqlite3_column_text(stmt, 1);
177- const unsigned char *gamePath = sqlite3_column_text(stmt, 2);
178 163
179- games.emplace_back(Game{id, reinterpret_cast<const char *>(gameName),
180- reinterpret_cast<const char *>(gamePath)});
181- }
182- sqlite3_finalize(stmt);
183 164
184 if (argc > 1) {165 if (argc > 1) {
185 handleCommand(argc, argv, database);166 handleCommand(argc, argv, database);