moved getting games to class
b2c785e parent: e10b023 modified
app/include/database.hpp +34 -6 | @@ -3,6 +3,8 @@ | ||
| 3 | 3 | #include <sqlite3.h> |
| 4 | 4 | #include <string> |
| 5 | 5 | |
| 6 | +#include <game.hpp> | |
| 7 | + | |
| 6 | 8 | class Database |
| 7 | 9 | { |
| 8 | 10 | private: |
| @@ -16,7 +18,7 @@ public: | ||
| 16 | 18 | std::string select_all_games_command = "SELECT * FROM games;"; |
| 17 | 19 | |
| 18 | 20 | Database(std::filesystem::path databasePath) { |
| 19 | - database = load_database(databasePath); | |
| 21 | + database = loadDatabase(databasePath); | |
| 20 | 22 | } |
| 21 | 23 | Database() = default; |
| 22 | 24 | |
| @@ -24,7 +26,7 @@ public: | ||
| 24 | 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 | 30 | int exit = 0; |
| 29 | 31 | char *messageError; |
| 30 | 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 { | |
| 39 | - execute_sql_command(database_initialization_command); | |
| 40 | + auto initializeDatabase() -> void { | |
| 41 | + executeSqlCommand(database_initialization_command); | |
| 40 | 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 | 46 | bool newCreation = not std::filesystem::exists(databasePath.string()); |
| 45 | 47 | |
| 46 | 48 | int exit = 0; |
| @@ -55,8 +57,34 @@ public: | ||
| 55 | 57 | //printMessage("Opened database successfully!"); |
| 56 | 58 | |
| 57 | 59 | if (newCreation) { |
| 58 | - initialize_database(); | |
| 60 | + initializeDatabase(); | |
| 59 | 61 | } |
| 60 | 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 | }; |
| @@ -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 Database | 8 | 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 { | ||
| 35 | 35 | std::string command = |
| 36 | 36 | "INSERT INTO games (GAME_NAME, GAME_PATH) VALUES ("; |
| 37 | 37 | command += "'" + name + "', \"" + path + "\");"; |
| 38 | - database.execute_sql_command(command); | |
| 38 | + database.executeSqlCommand(command); | |
| 39 | 39 | } |
| 40 | 40 | |
| 41 | 41 | auto removeGame(Database& database, std::string name) -> void{ |
| 42 | 42 | std::string command = "DELETE FROM games WHERE GAME_NAME="; |
| 43 | 43 | command += "'" + name + "';"; |
| 44 | - database.execute_sql_command(command); | |
| 44 | + database.executeSqlCommand(command); | |
| 45 | 45 | } |
| 46 | 46 | |
| 47 | 47 | auto runGame(std::string gameName) -> void { |
| @@ -157,29 +157,10 @@ auto main(int argc, char const *argv[]) -> int { | ||
| 157 | 157 | auto databasePath{ std::filesystem::path(getExecutablePath().parent_path().string() + "/data.db")}; |
| 158 | 158 | Database database{databasePath}; |
| 159 | 159 | |
| 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; | |
| 173 | 161 | |
| 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(); | |
| 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 | 165 | if (argc > 1) { |
| 185 | 166 | 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); |