moved initializing and executing commands
a35288b parent: 1e9340b modified
app/include/database.hpp +21 -4 | @@ -1,15 +1,32 @@ | ||
| 1 | 1 | #pragma once |
| 2 | 2 | |
| 3 | +#include <sqlite3.h> | |
| 4 | +#include <string> | |
| 5 | + | |
| 3 | 6 | class Database |
| 4 | 7 | { |
| 5 | 8 | private: |
| 6 | - /* data */ | |
| 7 | -public: | |
| 8 | 9 | std::string database_initialization_command = |
| 9 | 10 | "CREATE TABLE games(" |
| 10 | 11 | "GAME_ID INTEGER PRIMARY KEY AUTOINCREMENT, " |
| 11 | 12 | "GAME_NAME TEXT NOT NULL, " |
| 12 | 13 | "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 | + } | |
| 15 | 32 | }; |
| @@ -1,15 +1,32 @@ | |||
| 1 | #pragma once | 1 | #pragma once |
| 2 | 2 | ||
| 3 | +#include <sqlite3.h> | ||
| 4 | +#include <string> | ||
| 5 | + | ||
| 3 | class Database | 6 | 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; | ||
| 25 | 25 | |
| 26 | 26 | Database databaseHandler; |
| 27 | 27 | |
| 28 | -std::string select_all_games_command = "SELECT * FROM games;"; | |
| 29 | 28 | |
| 30 | 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 | 37 | void addGame(sqlite3 *database, std::string name, std::string path) { |
| 50 | 38 | std::string command = |
| 51 | 39 | "INSERT INTO games (GAME_NAME, GAME_PATH) VALUES ("; |
| 52 | 40 | command += "'" + name + "', \"" + path + "\");"; |
| 53 | - execute_sql_command(database, command); | |
| 41 | + databaseHandler.execute_sql_command(database, command); | |
| 54 | 42 | } |
| 55 | 43 | |
| 56 | 44 | void removeGame(sqlite3 *database, std::string name) { |
| 57 | 45 | std::string command = "DELETE FROM games WHERE GAME_NAME="; |
| 58 | 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) { | |
| 63 | - execute_sql_command(database, databaseHandler.database_initialization_command); | |
| 64 | - printMessage("Database Initialized"); | |
| 65 | -} | |
| 50 | + | |
| 66 | 51 | |
| 67 | 52 | void runGame(sqlite3 *database, std::string gameName) { |
| 68 | 53 | std::string pathToRun = ""; |
| @@ -177,7 +162,6 @@ auto main(int argc, char const *argv[]) -> int { | ||
| 177 | 162 | std::filesystem::path appPath{ getExecutablePath() }; |
| 178 | 163 | auto databasePath{ std::filesystem::path(appPath.parent_path().string() + "/data.db")}; |
| 179 | 164 | bool newCreation = not std::filesystem::exists(databasePath.string()); |
| 180 | - char *messageError; | |
| 181 | 165 | sqlite3 *database; |
| 182 | 166 | sqlite3_stmt *stmt; |
| 183 | 167 | int exit = 0; |
| @@ -192,10 +176,10 @@ auto main(int argc, char const *argv[]) -> int { | ||
| 192 | 176 | printMessage("Opened database successfully!"); |
| 193 | 177 | |
| 194 | 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 | 184 | if (exit) { |
| 201 | 185 | 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) |