dict @ 0a2e62f57734f820cd20e151d1150342408552ed

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