juk3n/fluffypublic Fork
⑂ fb9ea54
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 database pointer inside a class

juk3n committed 2024-07-03T17:40:35+02:00 Browse files
fb9ea54 parent: a35288b
modified app/include/database.hpp +35 -3
@@ -6,15 +6,27 @@
66 class Database
77 {
88 private:
9+
10+
911 std::string database_initialization_command =
1012 "CREATE TABLE games("
1113 "GAME_ID INTEGER PRIMARY KEY AUTOINCREMENT, "
1214 "GAME_NAME TEXT NOT NULL, "
1315 "GAME_PATH TEXT NOT NULL);";
1416 public:
17+ sqlite3 *database;
1518 std::string select_all_games_command = "SELECT * FROM games;";
1619
17- void execute_sql_command(sqlite3 *database, std::string command) {
20+ Database(std::filesystem::path databasePath) {
21+ database = load_database(databasePath);
22+ }
23+ Database() = default;
24+
25+ ~Database() {
26+ sqlite3_close(database);
27+ }
28+
29+ void execute_sql_command(std::string command) {
1830 int exit = 0;
1931 char *messageError;
2032 exit = sqlite3_exec(database, command.c_str(), nullptr, nullptr, &messageError);
@@ -25,8 +37,28 @@ public:
2537 }
2638 }
2739
28- void initialize_database(sqlite3 *database) {
29- execute_sql_command(database, database_initialization_command);
40+ void initialize_database() {
41+ execute_sql_command(database_initialization_command);
3042 //printMessage("Database Initialized");
3143 }
44+
45+ sqlite3* load_database(std::filesystem::path databasePath) {
46+ bool newCreation = not std::filesystem::exists(databasePath.string());
47+
48+ int exit = 0;
49+ exit = sqlite3_open(databasePath.c_str(), &database);
50+
51+ if (exit) {
52+ std::cerr << "Error opening database" << sqlite3_errmsg(database)
53+ << std::endl;
54+ return nullptr;
55+ }
56+
57+ //printMessage("Opened database successfully!");
58+
59+ if (newCreation) {
60+ initialize_database();
61+ }
62+ return database;
63+ }
3264 };
@@ -6,15 +6,27 @@
6 class Database6 class Database
7 {7 {
8 private:8 private:
9+
10+
9 std::string database_initialization_command =11 std::string database_initialization_command =
10 "CREATE TABLE games("12 "CREATE TABLE games("
11 "GAME_ID INTEGER PRIMARY KEY AUTOINCREMENT, "13 "GAME_ID INTEGER PRIMARY KEY AUTOINCREMENT, "
12 "GAME_NAME TEXT NOT NULL, "14 "GAME_NAME TEXT NOT NULL, "
13 "GAME_PATH TEXT NOT NULL);";15 "GAME_PATH TEXT NOT NULL);";
14 public:16 public:
17+ sqlite3 *database;
15 std::string select_all_games_command = "SELECT * FROM games;";18 std::string select_all_games_command = "SELECT * FROM games;";
16 19
17- void execute_sql_command(sqlite3 *database, std::string command) {20+ Database(std::filesystem::path databasePath) {
21+ database = load_database(databasePath);
22+ }
23+ Database() = default;
24+
25+ ~Database() {
26+ sqlite3_close(database);
27+ }
28+
29+ void execute_sql_command(std::string command) {
18 int exit = 0;30 int exit = 0;
19 char *messageError;31 char *messageError;
20 exit = sqlite3_exec(database, command.c_str(), nullptr, nullptr, &messageError);32 exit = sqlite3_exec(database, command.c_str(), nullptr, nullptr, &messageError);
@@ -25,8 +37,28 @@ public:
25 }37 }
26 }38 }
27 39
28- void initialize_database(sqlite3 *database) {40+ void initialize_database() {
29- execute_sql_command(database, database_initialization_command);41+ execute_sql_command(database_initialization_command);
30 //printMessage("Database Initialized");42 //printMessage("Database Initialized");
31 }43 }
44+
45+ sqlite3* load_database(std::filesystem::path databasePath) {
46+ bool newCreation = not std::filesystem::exists(databasePath.string());
47+
48+ int exit = 0;
49+ exit = sqlite3_open(databasePath.c_str(), &database);
50+
51+ if (exit) {
52+ std::cerr << "Error opening database" << sqlite3_errmsg(database)
53+ << std::endl;
54+ return nullptr;
55+ }
56+
57+ //printMessage("Opened database successfully!");
58+
59+ if (newCreation) {
60+ initialize_database();
61+ }
62+ return database;
63+ }
32 };64 };
modified app/src/main.cpp +21 -37
@@ -23,9 +23,6 @@ using namespace ftxui;
2323
2424 std::vector<Game> games;
2525
26-Database databaseHandler;
27-
28-
2926 bool debugRun = false;
3027
3128 void printMessage(std::string message) {
@@ -34,22 +31,22 @@ void printMessage(std::string message) {
3431 }
3532 }
3633
37-void addGame(sqlite3 *database, std::string name, std::string path) {
34+void addGame(Database& database, std::string name, std::string path) {
3835 std::string command =
3936 "INSERT INTO games (GAME_NAME, GAME_PATH) VALUES (";
4037 command += "'" + name + "', \"" + path + "\");";
41- databaseHandler.execute_sql_command(database, command);
38+ database.execute_sql_command(command);
4239 }
4340
44-void removeGame(sqlite3 *database, std::string name) {
41+void removeGame(Database& database, std::string name) {
4542 std::string command = "DELETE FROM games WHERE GAME_NAME=";
4643 command += "'" + name + "';";
47- databaseHandler.execute_sql_command(database, command);
44+ database.execute_sql_command(command);
4845 }
4946
5047
5148
52-void runGame(sqlite3 *database, std::string gameName) {
49+void runGame(std::string gameName) {
5350 std::string pathToRun = "";
5451 for (auto &game : games) {
5552 if (gameName == game.getName()) {
@@ -59,7 +56,7 @@ void runGame(sqlite3 *database, std::string gameName) {
5956 system(pathToRun.c_str());
6057 }
6158
62-void handleCommand(int argc, char const *argv[], sqlite3 *database) {
59+void handleCommand(int argc, char const *argv[], Database& database) {
6360 std::string command{};
6461 switch (argc) {
6562 case 2:
@@ -77,7 +74,7 @@ void handleCommand(int argc, char const *argv[], sqlite3 *database) {
7774 removeGame(database, gameName);
7875 } else if (command == "play") {
7976 std::string gameName{argv[2]};
80- runGame(database, gameName);
77+ runGame(gameName);
8178 }
8279 case 4:
8380 command = argv[1];
@@ -114,7 +111,7 @@ auto Style() -> ButtonOption {
114111 return option;
115112 }
116113
117-auto runConsoleApp(sqlite3* database) -> void {
114+auto runConsoleApp() -> void {
118115 system("clear");
119116
120117 std::vector<std::string> localGames = {};
@@ -126,7 +123,7 @@ auto runConsoleApp(sqlite3* database) -> void {
126123 auto gamesContainer{Container::Vertical({})};
127124 for (auto &game : games) {
128125 gamesContainer->Add(Button(
129- game.getName(), [&] { runGame(database, game.getName()); }, Style()));
126+ game.getName(), [&] { runGame(game.getName()); }, Style()));
130127 }
131128
132129 auto menu_screen = ScreenInteractive::TerminalOutput();
@@ -152,39 +149,27 @@ auto runConsoleApp(sqlite3* database) -> void {
152149
153150 }
154151
155-std::string getExecutablePath() {
152+std::filesystem::path getExecutablePath() {
156153 char result[PATH_MAX];
157154 ssize_t count = readlink("/proc/self/exe", result, PATH_MAX);
158- return std::string(result, (count > 0) ? count : 0);
155+ return std::filesystem::path(std::string(result, (count > 0) ? count : 0));
159156 }
160157
161158 auto main(int argc, char const *argv[]) -> int {
162- std::filesystem::path appPath{ getExecutablePath() };
163- auto databasePath{ std::filesystem::path(appPath.parent_path().string() + "/data.db")};
164- bool newCreation = not std::filesystem::exists(databasePath.string());
165- sqlite3 *database;
166- sqlite3_stmt *stmt;
167- int exit = 0;
168- exit = sqlite3_open(databasePath.c_str(), &database);
169-
170- if (exit) {
171- std::cerr << "Error opening database" << sqlite3_errmsg(database)
172- << std::endl;
159+ auto databasePath{ std::filesystem::path(getExecutablePath().parent_path().string() + "/data.db")};
160+ Database database{databasePath};
161+
162+ if (!database.database) {
173163 return -1;
174164 }
175-
176- printMessage("Opened database successfully!");
177-
178- if (newCreation) {
179- databaseHandler.initialize_database(database);
180- }
181-
182- exit = sqlite3_prepare_v2(database, databaseHandler.select_all_games_command.c_str(), -1, &stmt, nullptr);
165+ sqlite3_stmt *stmt;
166+ int exit = 0;
167+ exit = sqlite3_prepare_v2(database.database, database.select_all_games_command.c_str(), -1, &stmt, nullptr);
183168
184169 if (exit) {
185- std::cerr << "Error retrieving data" << sqlite3_errmsg(database)
170+ std::cerr << "Error retrieving data" << sqlite3_errmsg(database.database)
186171 << std::endl;
187- sqlite3_close(database);
172+ sqlite3_close(database.database);
188173 return -1;
189174 }
190175
@@ -202,9 +187,8 @@ auto main(int argc, char const *argv[]) -> int {
202187 handleCommand(argc, argv, database);
203188 }
204189 else {
205- runConsoleApp(database);
190+ runConsoleApp();
206191 }
207192
208- sqlite3_close(database);
209193 return EXIT_SUCCESS;
210194 }
\ No newline at end of file
@@ -23,9 +23,6 @@ using namespace ftxui;
23 23
24 std::vector<Game> games;24 std::vector<Game> games;
25 25
26-Database databaseHandler;
27-
28-
29 bool debugRun = false;26 bool debugRun = false;
30 27
31 void printMessage(std::string message) {28 void printMessage(std::string message) {
@@ -34,22 +31,22 @@ void printMessage(std::string message) {
34 }31 }
35 }32 }
36 33
37-void addGame(sqlite3 *database, std::string name, std::string path) {34+void addGame(Database& database, std::string name, std::string path) {
38 std::string command =35 std::string command =
39 "INSERT INTO games (GAME_NAME, GAME_PATH) VALUES (";36 "INSERT INTO games (GAME_NAME, GAME_PATH) VALUES (";
40 command += "'" + name + "', \"" + path + "\");";37 command += "'" + name + "', \"" + path + "\");";
41- databaseHandler.execute_sql_command(database, command);38+ database.execute_sql_command(command);
42 }39 }
43 40
44-void removeGame(sqlite3 *database, std::string name) {41+void removeGame(Database& database, std::string name) {
45 std::string command = "DELETE FROM games WHERE GAME_NAME=";42 std::string command = "DELETE FROM games WHERE GAME_NAME=";
46 command += "'" + name + "';";43 command += "'" + name + "';";
47- databaseHandler.execute_sql_command(database, command);44+ database.execute_sql_command(command);
48 }45 }
49 46
50 47
51 48
52-void runGame(sqlite3 *database, std::string gameName) {49+void runGame(std::string gameName) {
53 std::string pathToRun = "";50 std::string pathToRun = "";
54 for (auto &game : games) {51 for (auto &game : games) {
55 if (gameName == game.getName()) {52 if (gameName == game.getName()) {
@@ -59,7 +56,7 @@ void runGame(sqlite3 *database, std::string gameName) {
59 system(pathToRun.c_str());56 system(pathToRun.c_str());
60 }57 }
61 58
62-void handleCommand(int argc, char const *argv[], sqlite3 *database) {59+void handleCommand(int argc, char const *argv[], Database& database) {
63 std::string command{};60 std::string command{};
64 switch (argc) {61 switch (argc) {
65 case 2:62 case 2:
@@ -77,7 +74,7 @@ void handleCommand(int argc, char const *argv[], sqlite3 *database) {
77 removeGame(database, gameName);74 removeGame(database, gameName);
78 } else if (command == "play") {75 } else if (command == "play") {
79 std::string gameName{argv[2]};76 std::string gameName{argv[2]};
80- runGame(database, gameName);77+ runGame(gameName);
81 }78 }
82 case 4:79 case 4:
83 command = argv[1];80 command = argv[1];
@@ -114,7 +111,7 @@ auto Style() -> ButtonOption {
114 return option;111 return option;
115 }112 }
116 113
117-auto runConsoleApp(sqlite3* database) -> void {114+auto runConsoleApp() -> void {
118 system("clear");115 system("clear");
119 116
120 std::vector<std::string> localGames = {};117 std::vector<std::string> localGames = {};
@@ -126,7 +123,7 @@ auto runConsoleApp(sqlite3* database) -> void {
126 auto gamesContainer{Container::Vertical({})};123 auto gamesContainer{Container::Vertical({})};
127 for (auto &game : games) {124 for (auto &game : games) {
128 gamesContainer->Add(Button(125 gamesContainer->Add(Button(
129- game.getName(), [&] { runGame(database, game.getName()); }, Style()));126+ game.getName(), [&] { runGame(game.getName()); }, Style()));
130 }127 }
131 128
132 auto menu_screen = ScreenInteractive::TerminalOutput();129 auto menu_screen = ScreenInteractive::TerminalOutput();
@@ -152,39 +149,27 @@ auto runConsoleApp(sqlite3* database) -> void {
152 149
153 }150 }
154 151
155-std::string getExecutablePath() {152+std::filesystem::path getExecutablePath() {
156 char result[PATH_MAX];153 char result[PATH_MAX];
157 ssize_t count = readlink("/proc/self/exe", result, PATH_MAX);154 ssize_t count = readlink("/proc/self/exe", result, PATH_MAX);
158- return std::string(result, (count > 0) ? count : 0);155+ return std::filesystem::path(std::string(result, (count > 0) ? count : 0));
159 }156 }
160 157
161 auto main(int argc, char const *argv[]) -> int {158 auto main(int argc, char const *argv[]) -> int {
162- std::filesystem::path appPath{ getExecutablePath() };159+ auto databasePath{ std::filesystem::path(getExecutablePath().parent_path().string() + "/data.db")};
163- auto databasePath{ std::filesystem::path(appPath.parent_path().string() + "/data.db")};160+ Database database{databasePath};
164- bool newCreation = not std::filesystem::exists(databasePath.string());161+
165- sqlite3 *database;162+ if (!database.database) {
166- sqlite3_stmt *stmt;
167- int exit = 0;
168- exit = sqlite3_open(databasePath.c_str(), &database);
169-
170- if (exit) {
171- std::cerr << "Error opening database" << sqlite3_errmsg(database)
172- << std::endl;
173 return -1;163 return -1;
174 }164 }
175-165+ sqlite3_stmt *stmt;
176- printMessage("Opened database successfully!");166+ int exit = 0;
177-167+ exit = sqlite3_prepare_v2(database.database, database.select_all_games_command.c_str(), -1, &stmt, nullptr);
178- if (newCreation) {
179- databaseHandler.initialize_database(database);
180- }
181-
182- exit = sqlite3_prepare_v2(database, databaseHandler.select_all_games_command.c_str(), -1, &stmt, nullptr);
183 168
184 if (exit) {169 if (exit) {
185- std::cerr << "Error retrieving data" << sqlite3_errmsg(database)170+ std::cerr << "Error retrieving data" << sqlite3_errmsg(database.database)
186 << std::endl;171 << std::endl;
187- sqlite3_close(database);172+ sqlite3_close(database.database);
188 return -1;173 return -1;
189 }174 }
190 175
@@ -202,9 +187,8 @@ auto main(int argc, char const *argv[]) -> int {
202 handleCommand(argc, argv, database);187 handleCommand(argc, argv, database);
203 }188 }
204 else {189 else {
205- runConsoleApp(database);190+ runConsoleApp();
206 }191 }
207 192
208- sqlite3_close(database);
209 return EXIT_SUCCESS;193 return EXIT_SUCCESS;
210 }194 }
\ No newline at end of file\ No newline at end of file