From 6dd0c4747aa57227b5898fc639e3f2b643ce013c Mon Sep 17 00:00:00 2001 From: "Gabriel A. Giovanini" Date: Mon, 15 Apr 2024 22:16:28 +0200 Subject: feat: Remove C implementation --- lib/data.c | 125 ------------------------------------------------------------- 1 file changed, 125 deletions(-) delete mode 100644 lib/data.c (limited to 'lib/data.c') diff --git a/lib/data.c b/lib/data.c deleted file mode 100644 index afbbbb1..0000000 --- a/lib/data.c +++ /dev/null @@ -1,125 +0,0 @@ -#include -#include -#include - -#include "data.h" -#include "../lib/util.h" - -const char *insert_into = "INSERT INTO words (LINE) VALUES($VVV);"; -const char *select_words = "SELECT Id, Line FROM words WHERE line MATCH $VVV LIMIT $NNN;"; -const char *create_table = "CREATE VIRTUAL TABLE IF NOT EXISTS words USING fts4 (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, int limit) -{ - 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); - sqlite3_bind_int(stmt, 2, limit); - - 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