dict @ 59f03c5663d07f87993d682e7fd07b453fc19a11

  1#include <stdlib.h>
  2#include <stdio.h>
  3
  4#include "data.h"
  5
  6const char *insert_into = "INSERT INTO words (LINE) VALUES($VVV);";
  7const char *select_words = "SELECT Id, Line FROM words LIMIT 10;";
  8const char *create_table =  "CREATE TABLE IF NOT EXISTS words (ID INTEGER PRIMARY KEY AUTOINCREMENT, LINE TEXT NOT NULL);";
  9
 10Data* new_data(const char* con) {
 11    Data* data = (Data*)malloc(sizeof(Data));
 12
 13    int v = sqlite3_open(con, &(data->db));
 14    if (v != SQLITE_OK) {
 15        print_result_code(v);
 16        return NULL;
 17    }
 18
 19    sqlite3_enable_load_extension(data->db, 1);
 20    v = sqlite3_load_extension(data->db, "ext/libsqlite3ext", "sqlite3_spellfix_init",0);
 21    if (v != SQLITE_OK) {
 22        print_result_code(v);
 23        return NULL;
 24    }
 25
 26    return data;
 27}
 28
 29void free_data(Data* data) {
 30    sqlite3_close(data->db);
 31    free(data);
 32}
 33
 34void insert(Data* data, char* line, int len) {
 35    sqlite3_stmt *stmt; 
 36    int r = sqlite3_prepare_v2(data->db, insert_into, -1, &stmt, NULL);
 37
 38    if (r != SQLITE_OK) {
 39        printf("Error executing insert: ");
 40        print_result_code(r);
 41        printf("\n");
 42        return;
 43    }
 44
 45    // binds the paremets to the statement, in this case the line;
 46    sqlite3_bind_text(stmt, 1, line, len, NULL);
 47
 48    int c = sqlite3_step(stmt);
 49    if (c != SQLITE_DONE) {
 50        printf("Error executing insert: ");
 51        print_result_code(r);
 52        printf("\n");
 53    }
 54
 55    sqlite3_finalize(stmt);
 56}
 57
 58void bootstrap(Data* data) {
 59    sqlite3_stmt *stmt; 
 60    int r = sqlite3_prepare_v2(data->db, create_table, -1, &stmt, NULL);
 61
 62    if (r != SQLITE_OK) {
 63        printf("Error preparing bootstrap: ");
 64        print_result_code(r);
 65        printf("\n");
 66        return;
 67    }
 68
 69    int c = sqlite3_step(stmt);
 70    if (c != SQLITE_DONE) {
 71        printf("Error executing bootstrap: ");
 72        print_result_code(r);
 73        printf("\n");
 74    }
 75
 76    sqlite3_finalize(stmt);
 77}
 78
 79LIST* data_select(Data* data) {
 80    sqlite3_stmt *stmt; 
 81    int r = sqlite3_prepare_v2(data->db, select_words, -1, &stmt, NULL);
 82
 83    if (r != SQLITE_OK) {
 84        printf("Error executing select: ");
 85        print_result_code(r);
 86        printf("\n");
 87        return NULL;
 88    }
 89
 90    LIST *list = NULL;
 91
 92    int m =  sqlite3_step(stmt);
 93    while(m == SQLITE_ROW) {
 94        Word *word = (Word*)malloc(sizeof(Word));
 95
 96        int id = sqlite3_column_int(stmt, 0);
 97        const unsigned char *line = sqlite3_column_text(stmt, 1);
 98
 99        word->Id = id;
100        word->Line = line;
101        list = list_add(list, word);
102
103        m = sqlite3_step(stmt);
104    }
105
106    sqlite3_finalize(stmt);
107
108    return list;
109}
110
111void print_result_code(int code) {
112    printf(sqlite3_errstr(code));
113}