aboutsummaryrefslogtreecommitdiff
path: root/lib/data.c
diff options
context:
space:
mode:
authorGabriel A. Giovanini <mail@gabrielgio.me>2024-02-17 11:41:23 +0100
committergabrielgio <gabrielgio@workstation.lan>2024-02-17 16:48:54 +0100
commitd102e028aee6571c0fd9dfd4074cfb3c15f4594e (patch)
treee0f3bdadc96019de0a7576ea591f8b304a962e67 /lib/data.c
parentc573d3b7954296d95a0f8a79b8ac2ca261d86a02 (diff)
downloaddict-d102e028aee6571c0fd9dfd4074cfb3c15f4594e.tar.gz
dict-d102e028aee6571c0fd9dfd4074cfb3c15f4594e.tar.bz2
dict-d102e028aee6571c0fd9dfd4074cfb3c15f4594e.zip
ref: Refactor newer folder structure
Create a lib dict and importer project. * dict: holds the main application * importer: code to read from source to a common database. * lib: shared code
Diffstat (limited to 'lib/data.c')
-rw-r--r--lib/data.c117
1 files changed, 117 insertions, 0 deletions
diff --git a/lib/data.c b/lib/data.c
new file mode 100644
index 0000000..7ebf597
--- /dev/null
+++ b/lib/data.c
@@ -0,0 +1,117 @@
+#include <string.h>
+#include <stdlib.h>
+#include <stdio.h>
+
+#include "data.h"
+
+const char *insert_into = "INSERT INTO words (LINE) VALUES($VVV);";
+const char *select_words = "SELECT Id, Line FROM words WHERE line like $VVV LIMIT 10;";
+const char *create_table = "CREATE TABLE IF NOT EXISTS words (ID INTEGER PRIMARY KEY AUTOINCREMENT, LINE TEXT NOT NULL);";
+
+Data* new_data(const char* con) {
+ Data* data = (Data*)malloc(sizeof(Data));
+
+ int v = sqlite3_open(con, &(data->db));
+ if (v != SQLITE_OK) {
+ print_result_code(v);
+ return NULL;
+ }
+
+ sqlite3_enable_load_extension(data->db, 1);
+ v = sqlite3_load_extension(data->db, "ext/libsqlite3ext", "sqlite3_spellfix_init",0);
+ if (v != SQLITE_OK) {
+ print_result_code(v);
+ return NULL;
+ }
+
+ return data;
+}
+
+void free_data(Data* data) {
+ sqlite3_close(data->db);
+ free(data);
+}
+
+void insert(Data* data, char* line, int len) {
+ sqlite3_stmt *stmt;
+ int r = sqlite3_prepare_v2(data->db, insert_into, -1, &stmt, NULL);
+
+ if (r != SQLITE_OK) {
+ printf("Error executing insert: ");
+ print_result_code(r);
+ printf("\n");
+ return;
+ }
+
+ sqlite3_bind_text(stmt, 1, line, len, NULL);
+
+ int c = sqlite3_step(stmt);
+ if (c != SQLITE_DONE) {
+ printf("Error executing insert: ");
+ print_result_code(r);
+ printf("\n");
+ }
+
+ sqlite3_finalize(stmt);
+}
+
+void bootstrap(Data* data) {
+ sqlite3_stmt *stmt;
+ int r = sqlite3_prepare_v2(data->db, create_table, -1, &stmt, NULL);
+
+ if (r != SQLITE_OK) {
+ printf("Error preparing bootstrap: ");
+ print_result_code(r);
+ printf("\n");
+ return;
+ }
+
+ int c = sqlite3_step(stmt);
+ if (c != SQLITE_DONE) {
+ printf("Error executing bootstrap: ");
+ print_result_code(r);
+ printf("\n");
+ }
+
+ sqlite3_finalize(stmt);
+}
+
+LIST* data_select(Data* data, char *sch, int len) {
+ sqlite3_stmt *stmt;
+ int r = sqlite3_prepare_v2(data->db, select_words, -1, &stmt, NULL);
+
+ if (r != SQLITE_OK) {
+ printf("Error executing select: ");
+ print_result_code(r);
+ printf("\n");
+ return NULL;
+ }
+
+ LIST *list = NULL;
+
+ sqlite3_bind_text(stmt, 1, sch, len, NULL);
+
+ int m = sqlite3_step(stmt);
+ while(m == SQLITE_ROW) {
+ Word *word = (Word*)malloc(sizeof(Word));
+
+ int id = sqlite3_column_int(stmt, 0);
+ const unsigned char *line = sqlite3_column_text(stmt, 1);
+ unsigned char *line2 = malloc(sizeof(char*)+strlen((char*)line));
+ memcpy(line2, line, strlen((char*)line));
+
+ word->Id = id;
+ word->Line = line2;
+ list = list_add(list, word);
+
+ m = sqlite3_step(stmt);
+ }
+
+ sqlite3_finalize(stmt);
+
+ return list;
+}
+
+void print_result_code(int code) {
+ printf(sqlite3_errstr(code));
+}