aboutsummaryrefslogtreecommitdiff
path: root/lib/data.c
diff options
context:
space:
mode:
authorGabriel A. Giovanini <mail@gabrielgio.me>2024-04-15 22:16:28 +0200
committerGabriel A. Giovanini <mail@gabrielgio.me>2024-04-15 22:16:28 +0200
commit6dd0c4747aa57227b5898fc639e3f2b643ce013c (patch)
treee43ee077469b5c28fcf46dba51eb03e53214fad5 /lib/data.c
parent6ed576974dec969ad2745a451a6f680a3cdbcfc4 (diff)
downloaddict-6dd0c4747aa57227b5898fc639e3f2b643ce013c.tar.gz
dict-6dd0c4747aa57227b5898fc639e3f2b643ce013c.tar.bz2
dict-6dd0c4747aa57227b5898fc639e3f2b643ce013c.zip
feat: Remove C implementation
Diffstat (limited to 'lib/data.c')
-rw-r--r--lib/data.c125
1 files changed, 0 insertions, 125 deletions
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 <string.h>
-#include <stdlib.h>
-#include <stdio.h>
-
-#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));
-}