juk3n/fluffypublic Fork
⑂ 8e1b855
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.

added autoincrement to prime key

Adrian Duraj committed 2024-06-25T10:19:01+02:00 Browse files
8e1b855 parent: c41059b
modified src/main.cpp +7 -31
@@ -21,12 +21,12 @@ using namespace ftxui;
2121 std::vector<Game> games;
2222
2323 std::string database_initialization_command =
24- "CREATE TABLE GAME("
25- "GAME_ID INT PRIMARY KEY NOT NULL, "
24+ "CREATE TABLE games("
25+ "GAME_ID INTEGER PRIMARY KEY AUTOINCREMENT, "
2626 "GAME_NAME TEXT NOT NULL, "
2727 "GAME_PATH TEXT NOT NULL);";
2828
29-std::string select_all_games_command = "SELECT * FROM GAME;";
29+std::string select_all_games_command = "SELECT * FROM games;";
3030
3131 bool debugRun = true;
3232
@@ -47,39 +47,15 @@ void execute_sql_command(sqlite3 *database, std::string command) {
4747 }
4848 }
4949
50-auto calculateFirstGameIdAvailable() -> int {
51- int availableId{};
52-
53- std::vector<int> usedIds{};
54- for (auto &game : games) {
55- usedIds.emplace_back(game.getId());
56- }
57-
58- bool found{true};
59- while (found) {
60- found = std::find(usedIds.begin(), usedIds.end(), availableId) !=
61- std::end(usedIds);
62- availableId++;
63- }
64-
65- return availableId;
66-}
67-
68-void addGameToDatabase(sqlite3 *database, int id, std::string name,
69- std::string path) {
50+void addGame(sqlite3 *database, std::string name, std::string path) {
7051 std::string command =
71- "INSERT INTO GAME (GAME_ID, GAME_NAME, GAME_PATH) VALUES (";
72- command += std::to_string(id) + ", '" + name + "', \"" + path + "\");";
52+ "INSERT INTO games (GAME_NAME, GAME_PATH) VALUES (";
53+ command += "'" + name + "', \"" + path + "\");";
7354 execute_sql_command(database, command);
7455 }
7556
76-void addGame(sqlite3 *database, std::string name, std::string path) {
77- int id = calculateFirstGameIdAvailable();
78- addGameToDatabase(database, id, name, path);
79-}
80-
8157 void removeGame(sqlite3 *database, std::string name) {
82- std::string command = "DELETE FROM GAME WHERE GAME_NAME=";
58+ std::string command = "DELETE FROM games WHERE GAME_NAME=";
8359 command += "'" + name + "';";
8460 execute_sql_command(database, command);
8561 }
@@ -21,12 +21,12 @@ using namespace ftxui;
21 std::vector<Game> games;21 std::vector<Game> games;
22 22
23 std::string database_initialization_command =23 std::string database_initialization_command =
24- "CREATE TABLE GAME("24+ "CREATE TABLE games("
25- "GAME_ID INT PRIMARY KEY NOT NULL, "25+ "GAME_ID INTEGER PRIMARY KEY AUTOINCREMENT, "
26 "GAME_NAME TEXT NOT NULL, "26 "GAME_NAME TEXT NOT NULL, "
27 "GAME_PATH TEXT NOT NULL);";27 "GAME_PATH TEXT NOT NULL);";
28 28
29-std::string select_all_games_command = "SELECT * FROM GAME;";29+std::string select_all_games_command = "SELECT * FROM games;";
30 30
31 bool debugRun = true;31 bool debugRun = true;
32 32
@@ -47,39 +47,15 @@ void execute_sql_command(sqlite3 *database, std::string command) {
47 }47 }
48 }48 }
49 49
50-auto calculateFirstGameIdAvailable() -> int {50+void addGame(sqlite3 *database, std::string name, std::string path) {
51- int availableId{};
52-
53- std::vector<int> usedIds{};
54- for (auto &game : games) {
55- usedIds.emplace_back(game.getId());
56- }
57-
58- bool found{true};
59- while (found) {
60- found = std::find(usedIds.begin(), usedIds.end(), availableId) !=
61- std::end(usedIds);
62- availableId++;
63- }
64-
65- return availableId;
66-}
67-
68-void addGameToDatabase(sqlite3 *database, int id, std::string name,
69- std::string path) {
70 std::string command =51 std::string command =
71- "INSERT INTO GAME (GAME_ID, GAME_NAME, GAME_PATH) VALUES (";52+ "INSERT INTO games (GAME_NAME, GAME_PATH) VALUES (";
72- command += std::to_string(id) + ", '" + name + "', \"" + path + "\");";53+ command += "'" + name + "', \"" + path + "\");";
73 execute_sql_command(database, command);54 execute_sql_command(database, command);
74 }55 }
75 56
76-void addGame(sqlite3 *database, std::string name, std::string path) {
77- int id = calculateFirstGameIdAvailable();
78- addGameToDatabase(database, id, name, path);
79-}
80-
81 void removeGame(sqlite3 *database, std::string name) {57 void removeGame(sqlite3 *database, std::string name) {
82- std::string command = "DELETE FROM GAME WHERE GAME_NAME=";58+ std::string command = "DELETE FROM games WHERE GAME_NAME=";
83 command += "'" + name + "';";59 command += "'" + name + "';";
84 execute_sql_command(database, command);60 execute_sql_command(database, command);
85 }61 }