juk3n/fluffypublic Fork
⑂ a35288b
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 initializing and executing commands

juk3n committed 2024-07-03T14:45:41+02:00 Browse files
a35288b parent: 1e9340b
modified app/include/database.hpp +21 -4
@@ -1,15 +1,32 @@
11 #pragma once
22
3+#include <sqlite3.h>
4+#include <string>
5+
36 class Database
47 {
58 private:
6- /* data */
7-public:
89 std::string database_initialization_command =
910 "CREATE TABLE games("
1011 "GAME_ID INTEGER PRIMARY KEY AUTOINCREMENT, "
1112 "GAME_NAME TEXT NOT NULL, "
1213 "GAME_PATH TEXT NOT NULL);";
13- Database(/* args */);
14- ~Database();
14+public:
15+ std::string select_all_games_command = "SELECT * FROM games;";
16+
17+ void execute_sql_command(sqlite3 *database, std::string command) {
18+ int exit = 0;
19+ char *messageError;
20+ exit = sqlite3_exec(database, command.c_str(), nullptr, nullptr, &messageError);
21+
22+ if (exit != SQLITE_OK) {
23+ //printMessage("Error with command: " + command + " " + messageError);
24+ sqlite3_free(messageError);
25+ }
26+ }
27+
28+ void initialize_database(sqlite3 *database) {
29+ execute_sql_command(database, database_initialization_command);
30+ //printMessage("Database Initialized");
31+ }
1532 };
@@ -1,15 +1,32 @@
1 #pragma once1 #pragma once
2 2
3+#include <sqlite3.h>
4+#include <string>
5+
3 class Database6 class Database
4 {7 {
5 private:8 private:
6- /* data */
7-public:
8 std::string database_initialization_command =9 std::string database_initialization_command =
9 "CREATE TABLE games("10 "CREATE TABLE games("
10 "GAME_ID INTEGER PRIMARY KEY AUTOINCREMENT, "11 "GAME_ID INTEGER PRIMARY KEY AUTOINCREMENT, "
11 "GAME_NAME TEXT NOT NULL, "12 "GAME_NAME TEXT NOT NULL, "
12 "GAME_PATH TEXT NOT NULL);";13 "GAME_PATH TEXT NOT NULL);";
13- Database(/* args */);14+public:
14- ~Database();15+ std::string select_all_games_command = "SELECT * FROM games;";
16+
17+ void execute_sql_command(sqlite3 *database, std::string command) {
18+ int exit = 0;
19+ char *messageError;
20+ exit = sqlite3_exec(database, command.c_str(), nullptr, nullptr, &messageError);
21+
22+ if (exit != SQLITE_OK) {
23+ //printMessage("Error with command: " + command + " " + messageError);
24+ sqlite3_free(messageError);
25+ }
26+ }
27+
28+ void initialize_database(sqlite3 *database) {
29+ execute_sql_command(database, database_initialization_command);
30+ //printMessage("Database Initialized");
31+ }
15 };32 };
modified app/src/main.cpp +5 -21
@@ -25,7 +25,6 @@ std::vector<Game> games;
2525
2626 Database databaseHandler;
2727
28-std::string select_all_games_command = "SELECT * FROM games;";
2928
3029 bool debugRun = false;
3130
@@ -35,34 +34,20 @@ void printMessage(std::string message) {
3534 }
3635 }
3736
38-void execute_sql_command(sqlite3 *database, std::string command) {
39- int exit = 0;
40- char *messageError;
41- exit = sqlite3_exec(database, command.c_str(), nullptr, nullptr, &messageError);
42-
43- if (exit != SQLITE_OK) {
44- printMessage("Error with command: " + command + " " + messageError);
45- sqlite3_free(messageError);
46- }
47-}
48-
4937 void addGame(sqlite3 *database, std::string name, std::string path) {
5038 std::string command =
5139 "INSERT INTO games (GAME_NAME, GAME_PATH) VALUES (";
5240 command += "'" + name + "', \"" + path + "\");";
53- execute_sql_command(database, command);
41+ databaseHandler.execute_sql_command(database, command);
5442 }
5543
5644 void removeGame(sqlite3 *database, std::string name) {
5745 std::string command = "DELETE FROM games WHERE GAME_NAME=";
5846 command += "'" + name + "';";
59- execute_sql_command(database, command);
47+ databaseHandler.execute_sql_command(database, command);
6048 }
6149
62-void initialize_database(sqlite3 *database) {
63- execute_sql_command(database, databaseHandler.database_initialization_command);
64- printMessage("Database Initialized");
65-}
50+
6651
6752 void runGame(sqlite3 *database, std::string gameName) {
6853 std::string pathToRun = "";
@@ -177,7 +162,6 @@ auto main(int argc, char const *argv[]) -> int {
177162 std::filesystem::path appPath{ getExecutablePath() };
178163 auto databasePath{ std::filesystem::path(appPath.parent_path().string() + "/data.db")};
179164 bool newCreation = not std::filesystem::exists(databasePath.string());
180- char *messageError;
181165 sqlite3 *database;
182166 sqlite3_stmt *stmt;
183167 int exit = 0;
@@ -192,10 +176,10 @@ auto main(int argc, char const *argv[]) -> int {
192176 printMessage("Opened database successfully!");
193177
194178 if (newCreation) {
195- initialize_database(database);
179+ databaseHandler.initialize_database(database);
196180 }
197181
198- exit = sqlite3_prepare_v2(database, select_all_games_command.c_str(), -1, &stmt, nullptr);
182+ exit = sqlite3_prepare_v2(database, databaseHandler.select_all_games_command.c_str(), -1, &stmt, nullptr);
199183
200184 if (exit) {
201185 std::cerr << "Error retrieving data" << sqlite3_errmsg(database)
@@ -25,7 +25,6 @@ std::vector<Game> games;
25 25
26 Database databaseHandler;26 Database databaseHandler;
27 27
28-std::string select_all_games_command = "SELECT * FROM games;";
29 28
30 bool debugRun = false;29 bool debugRun = false;
31 30
@@ -35,34 +34,20 @@ void printMessage(std::string message) {
35 }34 }
36 }35 }
37 36
38-void execute_sql_command(sqlite3 *database, std::string command) {
39- int exit = 0;
40- char *messageError;
41- exit = sqlite3_exec(database, command.c_str(), nullptr, nullptr, &messageError);
42-
43- if (exit != SQLITE_OK) {
44- printMessage("Error with command: " + command + " " + messageError);
45- sqlite3_free(messageError);
46- }
47-}
48-
49 void addGame(sqlite3 *database, std::string name, std::string path) {37 void addGame(sqlite3 *database, std::string name, std::string path) {
50 std::string command =38 std::string command =
51 "INSERT INTO games (GAME_NAME, GAME_PATH) VALUES (";39 "INSERT INTO games (GAME_NAME, GAME_PATH) VALUES (";
52 command += "'" + name + "', \"" + path + "\");";40 command += "'" + name + "', \"" + path + "\");";
53- execute_sql_command(database, command);41+ databaseHandler.execute_sql_command(database, command);
54 }42 }
55 43
56 void removeGame(sqlite3 *database, std::string name) {44 void removeGame(sqlite3 *database, std::string name) {
57 std::string command = "DELETE FROM games WHERE GAME_NAME=";45 std::string command = "DELETE FROM games WHERE GAME_NAME=";
58 command += "'" + name + "';";46 command += "'" + name + "';";
59- execute_sql_command(database, command);47+ databaseHandler.execute_sql_command(database, command);
60 }48 }
61 49
62-void initialize_database(sqlite3 *database) {50+
63- execute_sql_command(database, databaseHandler.database_initialization_command);
64- printMessage("Database Initialized");
65-}
66 51
67 void runGame(sqlite3 *database, std::string gameName) {52 void runGame(sqlite3 *database, std::string gameName) {
68 std::string pathToRun = "";53 std::string pathToRun = "";
@@ -177,7 +162,6 @@ auto main(int argc, char const *argv[]) -> int {
177 std::filesystem::path appPath{ getExecutablePath() };162 std::filesystem::path appPath{ getExecutablePath() };
178 auto databasePath{ std::filesystem::path(appPath.parent_path().string() + "/data.db")};163 auto databasePath{ std::filesystem::path(appPath.parent_path().string() + "/data.db")};
179 bool newCreation = not std::filesystem::exists(databasePath.string());164 bool newCreation = not std::filesystem::exists(databasePath.string());
180- char *messageError;
181 sqlite3 *database;165 sqlite3 *database;
182 sqlite3_stmt *stmt;166 sqlite3_stmt *stmt;
183 int exit = 0;167 int exit = 0;
@@ -192,10 +176,10 @@ auto main(int argc, char const *argv[]) -> int {
192 printMessage("Opened database successfully!");176 printMessage("Opened database successfully!");
193 177
194 if (newCreation) {178 if (newCreation) {
195- initialize_database(database);179+ databaseHandler.initialize_database(database);
196 }180 }
197 181
198- exit = sqlite3_prepare_v2(database, select_all_games_command.c_str(), -1, &stmt, nullptr);182+ exit = sqlite3_prepare_v2(database, databaseHandler.select_all_games_command.c_str(), -1, &stmt, nullptr);
199 183
200 if (exit) {184 if (exit) {
201 std::cerr << "Error retrieving data" << sqlite3_errmsg(database)185 std::cerr << "Error retrieving data" << sqlite3_errmsg(database)