refactored
770783e parent: b2c785e modified
app/include/database.hpp +16 -13 | @@ -8,19 +8,26 @@ | ||
| 8 | 8 | class Database |
| 9 | 9 | { |
| 10 | 10 | private: |
| 11 | + sqlite3 *database; | |
| 12 | + | |
| 11 | 13 | std::string database_initialization_command = |
| 12 | 14 | "CREATE TABLE games(" |
| 13 | 15 | "GAME_ID INTEGER PRIMARY KEY AUTOINCREMENT, " |
| 14 | 16 | "GAME_NAME TEXT NOT NULL, " |
| 15 | 17 | "GAME_PATH TEXT NOT NULL);"; |
| 16 | -public: | |
| 17 | - sqlite3 *database; | |
| 18 | 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 | 28 | Database(std::filesystem::path databasePath) { |
| 21 | - database = loadDatabase(databasePath); | |
| 29 | + loadDatabase(databasePath); | |
| 22 | 30 | } |
| 23 | - Database() = default; | |
| 24 | 31 | |
| 25 | 32 | ~Database() { |
| 26 | 33 | sqlite3_close(database); |
| @@ -34,15 +41,13 @@ public: | ||
| 34 | 41 | if (exit != SQLITE_OK) { |
| 35 | 42 | //printMessage("Error with command: " + command + " " + messageError); |
| 36 | 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 | 51 | bool newCreation = not std::filesystem::exists(databasePath.string()); |
| 47 | 52 | |
| 48 | 53 | int exit = 0; |
| @@ -51,7 +56,7 @@ public: | ||
| 51 | 56 | if (exit) { |
| 52 | 57 | std::cerr << "Error opening database" << sqlite3_errmsg(database) |
| 53 | 58 | << std::endl; |
| 54 | - return nullptr; | |
| 59 | + successfullyCreated = false; | |
| 55 | 60 | } |
| 56 | 61 | |
| 57 | 62 | //printMessage("Opened database successfully!"); |
| @@ -59,10 +64,9 @@ public: | ||
| 59 | 64 | if (newCreation) { |
| 60 | 65 | initializeDatabase(); |
| 61 | 66 | } |
| 62 | - return database; | |
| 63 | 67 | } |
| 64 | 68 | |
| 65 | - auto gettingData() -> std::vector<Game> { | |
| 69 | + auto getGames() -> std::vector<Game> { | |
| 66 | 70 | sqlite3_stmt *stmt; |
| 67 | 71 | int exit = 0; |
| 68 | 72 | exit = sqlite3_prepare_v2(database, select_all_games_command.c_str(), -1, &stmt, nullptr); |
| @@ -85,6 +89,5 @@ public: | ||
| 85 | 89 | } |
| 86 | 90 | sqlite3_finalize(stmt); |
| 87 | 91 | return games; |
| 88 | - | |
| 89 | 92 | } |
| 90 | 93 | }; |
| @@ -8,19 +8,26 @@ | |||
| 8 | class Database | 8 | 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 { | ||
| 157 | 157 | auto databasePath{ std::filesystem::path(getExecutablePath().parent_path().string() + "/data.db")}; |
| 158 | 158 | Database database{databasePath}; |
| 159 | 159 | |
| 160 | - if (!database.database) return EXIT_FAILURE; | |
| 161 | - | |
| 162 | - games = database.gettingData(); | |
| 160 | + if (!database.successfullyCreated) return EXIT_FAILURE; | |
| 163 | 161 | |
| 162 | + games = database.getGames(); | |
| 164 | 163 | |
| 165 | 164 | if (argc > 1) { |
| 166 | 165 | 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); |