summaryrefslogtreecommitdiff
path: root/dbhandling.c
diff options
context:
space:
mode:
authoryuzu-eva <cafebabe@disroot.org>2025-05-02 20:54:23 +0200
committeryuzu-eva <cafebabe@disroot.org>2025-05-02 20:54:23 +0200
commit3731ed237f1c983000f07ad585e82f603db69ccd (patch)
tree4cbba62777b2ffdf17d539bad76b7496c880dee5 /dbhandling.c
parent29410e3943e3e0da6279708d19b4459578b49f5d (diff)
fixed broken queries
Diffstat (limited to 'dbhandling.c')
-rw-r--r--dbhandling.c67
1 files changed, 51 insertions, 16 deletions
diff --git a/dbhandling.c b/dbhandling.c
index a112b8e..d990102 100644
--- a/dbhandling.c
+++ b/dbhandling.c
@@ -8,6 +8,21 @@ void exit_with_error(sqlite3 *db, const char *msg)
exit(69);
}
+/*
+ TODO: write function for preparing the statement
+ Should take entry_t *entry, args_e target (,args_e mode?)
+ I already differentiate the mode in main, so passing it here
+ would be redundant. Find another solution.
+*/
+
+/*POSSIBLE OTHER SOLUTION:
+ TODO: try to get rid of args_e target and args_e mode and instead
+ use a flag. 3 possible tables with 3 different modes = 9 bits. Maybe
+ do 16bits in preparation for additional modes and tables. Not sure how
+ to go about setting them, though... that will be one hell of an
+ if-else block, lmao.
+*/
+
void select_from_table(sqlite3 *db, args_e target, entry_t *entry)
{
char *sql;
@@ -18,7 +33,7 @@ void select_from_table(sqlite3 *db, args_e target, entry_t *entry)
const char *type;
printf("Name: ");
- fgets(entry->name, sizeof(entry->name), stdin);
+ fgets(entry->name, MAX_NAME_LEN, stdin);
// remove newline char, otherwise it messes with the query
entry->name[strcspn(entry->name, "\n")] = 0;
@@ -90,38 +105,50 @@ void update_entry(sqlite3 *db, args_e target, entry_t *entry)
sqlite3_stmt *stmt;
printf("Name: ");
- fgets(entry->name, sizeof(entry->name), stdin);
+ fgets(entry->name, MAX_NAME_LEN, stdin);
+
+ // remove newline char, otherwise it messes with the query
+ entry->name[strcspn(entry->name, "\n")] = 0;
+
+ // status is currently never NULL, so the if-else makes no sense
+ // would be addressed by the TODO: switching to flags
switch (target) {
case ANIME:
printf("Episode: ");
- fgets(entry->value, sizeof(entry->value), stdin);
+ fgets(entry->value, MAX_VALUE_LEN, stdin);
+ entry->value[strcspn(entry->value, "\n")] = 0;
if (entry->status == NULL) {
sql = "UPDATE anime SET episode=?1 WHERE name=?2;";
} else {
printf("Status: ");
- fgets(entry->status, sizeof(entry->status), stdin);
+ fgets(entry->status, MAX_STATUS_LEN, stdin);
+ entry->status[strcspn(entry->status, "\n")] = 0;
sql = "UPDATE anime SET episode=?1, status=?2 WHERE name=?3;";
}
break;
case MANGA:
printf("Chapter: ");
- fgets(entry->value, sizeof(entry->value), stdin);
+ fgets(entry->value, MAX_VALUE_LEN, stdin);
+ entry->value[strcspn(entry->value, "\n")] = 0;
if (entry->status == NULL) {
sql = "UPDATE manga SET chapter=?1 WHERE name=?2;";
} else {
printf("Status: ");
- fgets(entry->status, sizeof(entry->status), stdin);
+ fgets(entry->status, MAX_STATUS_LEN, stdin);
+ entry->status[strcspn(entry->status, "\n")] = 0;
sql = "UPDATE manga SET chapter=?1, status=?2 WHERE name=?3;";
}
break;
case BOOK:
printf("Chapter: ");
- fgets(entry->value, sizeof(entry->value), stdin);
+ fgets(entry->value, MAX_VALUE_LEN, stdin);
+ entry->value[strcspn(entry->value, "\n")] = 0;
if (entry->status == NULL) {
sql = "UPDATE book SET chapter=?1 WHERE name=?2;";
} else {
printf("Status: ");
- fgets(entry->status, sizeof(entry->status), stdin);
+ fgets(entry->status, MAX_STATUS_LEN, stdin);
+ entry->status[strcspn(entry->status, "\n")] = 0;
sql = "UPDATE book SET chapter=?1, status=?2 WHERE name=?3;";
}
break;
@@ -161,29 +188,37 @@ void add_entry(sqlite3 *db, args_e target, entry_t *entry)
sqlite3_stmt *stmt;
printf("Name: ");
- fgets(entry->name, sizeof(entry->name), stdin);
+ fgets(entry->name, MAX_NAME_LEN, stdin);
+ entry->name[strcspn(entry->name, "\n")] = 0;
switch (target) {
case ANIME:
printf("Episode: ");
- fgets(entry->value, sizeof(entry->value), stdin);
+ fgets(entry->value, MAX_VALUE_LEN, stdin);
+ entry->value[strcspn(entry->value, "\n")] = 0;
printf("Status: ");
- fgets(entry->status, sizeof(entry->status), stdin);
+ fgets(entry->status, MAX_STATUS_LEN, stdin);
+ entry->status[strcspn(entry->status, "\n")] = 0;
sql = "INSERT INTO anime (name, episode, status) VALUES (?1, ?2, ?3);";
break;
case MANGA:
printf("Chapter: ");
- fgets(entry->value, sizeof(entry->value), stdin);
+ fgets(entry->value, MAX_VALUE_LEN, stdin);
+ entry->value[strcspn(entry->value, "\n")] = 0;
printf("Status: ");
- fgets(entry->status, sizeof(entry->status), stdin);
+ fgets(entry->status, MAX_STATUS_LEN, stdin);
+ entry->status[strcspn(entry->status, "\n")] = 0;
sql = "INSERT INTO manga (name, chapter, status) VALUES (?1, ?2, ?3);";
break;
case BOOK:
printf("Author: ");
- fgets(entry->author, sizeof(entry->author), stdin);
+ fgets(entry->author, MAX_AUTHOR_LEN, stdin);
+ entry->author[strcspn(entry->author, "\n")] = 0;
printf("Chapter: ");
- fgets(entry->value, sizeof(entry->value), stdin);
+ fgets(entry->value, MAX_VALUE_LEN, stdin);
+ entry->value[strcspn(entry->value, "\n")] = 0;
printf("Status: ");
- fgets(entry->status, sizeof(entry->status), stdin);
+ fgets(entry->status, MAX_STATUS_LEN, stdin);
+ entry->status[strcspn(entry->status, "\n")] = 0;
sql = "INSERT INTO book (name, author, chapter, status) VALUES (?1, ?2, ?3, ?4);";
break;
default: