From d102e028aee6571c0fd9dfd4074cfb3c15f4594e Mon Sep 17 00:00:00 2001 From: "Gabriel A. Giovanini" Date: Sat, 17 Feb 2024 11:41:23 +0100 Subject: 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 --- lib/data.c | 117 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 117 insertions(+) create mode 100644 lib/data.c (limited to 'lib/data.c') 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 +#include +#include + +#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)); +} -- cgit v1.2.3