From fc26d6542276e17f3206a00b996162397d875e93 Mon Sep 17 00:00:00 2001 From: "Gabriel A. Giovanini" Date: Mon, 12 Feb 2024 13:34:56 +0100 Subject: feat: Initial commit Add initial code form dealing with sqlite. --- data.c | 183 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 183 insertions(+) create mode 100644 data.c (limited to 'data.c') diff --git a/data.c b/data.c new file mode 100644 index 0000000..5a9a103 --- /dev/null +++ b/data.c @@ -0,0 +1,183 @@ +#include +#include + +#include "data.h" + +const char *insert_into = "INSERT INTO words (LINE) VALUES($VVV);"; +const char *select_words = "SELECT Id, Line FROM words 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) { + return data; + } + + print_result_code(v); + return NULL; +} + + +void free_data(Data* data) { + sqlite3_close(data->db); + free(data); +} + +void insert(Data* data, char* line) { + 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; + } + + // binds the paremets to the statement, in this case the line; + sqlite3_bind_text(stmt, 1, line, -1, NULL); + + int c = sqlite3_step(stmt); + if (c != SQLITE_DONE) { + printf("Error executing insert: "); + print_result_code(r); + printf("\n"); + } +} + +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"); + } +} + +LIST* select(Data* data) { + 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; + + 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); + + word->Id = id; + word->Line = line; + list = list_add(list, word); + + m = sqlite3_step(stmt); + } + + sqlite3_finalize(stmt); + + return list; +} + +void print_result_code(int code) { + switch(code) { + // Primary result code + case SQLITE_ABORT: + printf("SQLITE_ABORT"); + break; + case SQLITE_AUTH: + printf("SQLITE_AUTH"); + break; + case SQLITE_BUSY: + printf("SQLITE_BUSY"); + break; + case SQLITE_CANTOPEN: + printf("SQLITE_CANTOPEN"); + break; + case SQLITE_CONSTRAINT: + printf("SQLITE_CONSTRAINT"); + break; + case SQLITE_CORRUPT: + printf("SQLITE_CORRUPT"); + break; + case SQLITE_DONE: + printf("SQLITE_DONE"); + break; + case SQLITE_EMPTY: + printf("SQLITE_EMPTY"); + break; + case SQLITE_ERROR: + printf("SQLITE_ERROR"); + break; + case SQLITE_FORMAT: + printf("SQLITE_FORMAT"); + break; + case SQLITE_INTERNAL: + printf("SQLITE_INTERNAL"); + break; + case SQLITE_INTERRUPT: + printf("SQLITE_INTERRUPT"); + break; + case SQLITE_IOERR: + printf("SQLITE_IOERR"); + break; + case SQLITE_LOCKED: + printf("SQLITE_LOCKED"); + break; + case SQLITE_MISMATCH: + printf("SQLITE_MISMATCH"); + break; + case SQLITE_MISUSE: + printf("SQLITE_MISUSE"); + break; + case SQLITE_NOLFS: + printf("SQLITE_NOLFS"); + break; + case SQLITE_NOMEM: + printf("SQLITE_NOMEM"); + break; + case SQLITE_NOTADB: + printf("SQLITE_NOTADB"); + break; + case SQLITE_NOTFOUND: + printf("SQLITE_NOTFOUND"); + break; + case SQLITE_NOTICE: + printf("SQLITE_NOTICE"); + break; + case SQLITE_OK: + printf("SQLITE_OK"); + break; + case SQLITE_PERM: + printf("SQLITE_PERM"); + break; + case SQLITE_SCHEMA: + printf("SQLITE_SCHEMA"); + break; + case SQLITE_TOOBIG: + printf("SQLITE_TOOBIG"); + break; + case SQLITE_WARNING: + printf("SQLITE_WARNING"); + break; + } +} -- cgit v1.2.3