From a2d4e27d732196c6ce3c145be0966a459dd08f21 Mon Sep 17 00:00:00 2001
From: SuperNovaa41 <supernovaa41@gmx.com>
Date: Wed, 5 Mar 2025 14:11:27 -0500
Subject: [PATCH] more db stuff!

---
 src/db.c         | 46 ++++++++++++++++++++++++++++++++++++++++++++--
 src/include/db.h |  2 +-
 2 files changed, 45 insertions(+), 3 deletions(-)

diff --git a/src/db.c b/src/db.c
index 9e44c8e..03b6441 100644
--- a/src/db.c
+++ b/src/db.c
@@ -2,6 +2,7 @@
 #include <stdio.h>
 #include <sqlite3.h>
 #include <string.h>
+#include <math.h>
 
 #include "include/db.h"
 
@@ -40,7 +41,7 @@ void setup_db(sqlite3* db)
 	err = sqlite3_exec(db,
 			"CREATE TABLE IF NOT EXISTS books (id UNSIGNED INT PRIMARY KEY, \
 			title TEXT, subtitle TEXT, isbn TEXT, authors TEXT, subjects TEXT, \
-			langs TEXT, date_added TEXT, date_completed TEXT, pub_date TEXT, \
+			langs TEXT, date_added TEXT, pub_date TEXT, \
 			image_code TEXT, year_of_pub YEAR, page_len UNSIGNED INT);",
 			0, 0, &err_buf);
 	if (err != SQLITE_OK) {
@@ -53,7 +54,47 @@ void setup_db(sqlite3* db)
 	sqlite3_close(db);
 }
 
-#define INSERT_STR "INSERT INTO books (id, title, subtitle, isbn, authors, subjects, langs, date_added, date_completed, pub_date, image_code, year_of_pub, page_len) VALUES("
+static void add_str_entry(char** dst, char** src)
+{
+	size_t len;
+	len = strlen(*src) + strlen(*dst) + 1;
+
+	*dst = malloc(sizeof(char) * len);
+
+	*dst = strcat(*dst, *src);
+}
+
+static void add_int_entry(char** dst, int src)
+{
+	size_t len_int, len;
+
+	if (src == 0)
+		len_int = 1;
+	else 
+		len_int = floor(log10(abs(src))) + 1;
+
+	len = strlen(*dst) + len_int + 1;
+	*dst = malloc(sizeof(char) * len);
+	snprintf(*dst, len, "%s%d", *dst, src);
+}
+
+static void string_cat_vals(char** dst, book_t* book, int id)
+{
+	add_int_entry(dst, id);
+	add_str_entry(dst, &(book->title));
+	add_str_entry(dst, &(book->subtitle));
+	add_str_entry(dst, &(book->isbn));
+	add_str_entry(dst, &(book->authors));
+	add_str_entry(dst, &(book->subjects));
+	add_str_entry(dst, &(book->langs));
+	add_str_entry(dst, &(book->date_added));
+	add_str_entry(dst, &(book->pub_date));
+	add_str_entry(dst, &(book->image_code));
+	add_int_entry(dst, book->page_len);
+}
+
+
+#define INSERT_STR "INSERT INTO books (id, title, subtitle, isbn, authors, subjects, langs, date_added, pub_date, image_code, page_len) VALUES("
 
 void add_to_db(book_t* book, sqlite3* db)
 {
@@ -67,4 +108,5 @@ void add_to_db(book_t* book, sqlite3* db)
 	cmd = malloc(sizeof(char) * (strlen(INSERT_STR) + 1));
 	snprintf(cmd, strlen(INSERT_STR), "%s", INSERT_STR);
 	
+	string_cat_vals(&cmd, book, id);
 }
diff --git a/src/include/db.h b/src/include/db.h
index 33b3f77..9fa1c6a 100644
--- a/src/include/db.h
+++ b/src/include/db.h
@@ -9,7 +9,7 @@ typedef struct {
 	char *date_added, *pub_date;
 	char *image_code;
 
-	int year_of_pub, page_len;
+	int page_len;
 } book_t;
 
 void setup_db(sqlite3* db);