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 return data;
16 }
17
18 print_result_code(v);
19 return NULL;
20}
21
22
23void free_data(Data* data) {
24 sqlite3_close(data->db);
25 free(data);
26}
27
28void insert(Data* data, char* line, int len) {
29 sqlite3_stmt *stmt;
30 int r = sqlite3_prepare_v2(data->db, insert_into, -1, &stmt, NULL);
31
32 if (r != SQLITE_OK) {
33 printf("Error executing insert: ");
34 print_result_code(r);
35 printf("\n");
36 return;
37 }
38
39 // binds the paremets to the statement, in this case the line;
40 sqlite3_bind_text(stmt, 1, line, len, NULL);
41
42 int c = sqlite3_step(stmt);
43 if (c != SQLITE_DONE) {
44 printf("Error executing insert: ");
45 print_result_code(r);
46 printf("\n");
47 }
48
49 sqlite3_finalize(stmt);
50}
51
52void bootstrap(Data* data) {
53 sqlite3_stmt *stmt;
54 int r = sqlite3_prepare_v2(data->db, create_table, -1, &stmt, NULL);
55
56 if (r != SQLITE_OK) {
57 printf("Error preparing bootstrap: ");
58 print_result_code(r);
59 printf("\n");
60 return;
61 }
62
63 int c = sqlite3_step(stmt);
64 if (c != SQLITE_DONE) {
65 printf("Error executing bootstrap: ");
66 print_result_code(r);
67 printf("\n");
68 }
69
70 sqlite3_finalize(stmt);
71}
72
73LIST* data_select(Data* data) {
74 sqlite3_stmt *stmt;
75 int r = sqlite3_prepare_v2(data->db, select_words, -1, &stmt, NULL);
76
77 if (r != SQLITE_OK) {
78 printf("Error executing select: ");
79 print_result_code(r);
80 printf("\n");
81 return NULL;
82 }
83
84 LIST *list = NULL;
85
86 int m = sqlite3_step(stmt);
87 while(m == SQLITE_ROW) {
88 Word *word = (Word*)malloc(sizeof(Word));
89
90 int id = sqlite3_column_int(stmt, 0);
91 const unsigned char *line = sqlite3_column_text(stmt, 1);
92
93 word->Id = id;
94 word->Line = line;
95 list = list_add(list, word);
96
97 m = sqlite3_step(stmt);
98 }
99
100 sqlite3_finalize(stmt);
101
102 return list;
103}
104
105void print_result_code(int code) {
106 switch(code) {
107 // Primary result code
108 case SQLITE_ABORT:
109 printf("SQLITE_ABORT");
110 break;
111 case SQLITE_AUTH:
112 printf("SQLITE_AUTH");
113 break;
114 case SQLITE_BUSY:
115 printf("SQLITE_BUSY");
116 break;
117 case SQLITE_CANTOPEN:
118 printf("SQLITE_CANTOPEN");
119 break;
120 case SQLITE_CONSTRAINT:
121 printf("SQLITE_CONSTRAINT");
122 break;
123 case SQLITE_CORRUPT:
124 printf("SQLITE_CORRUPT");
125 break;
126 case SQLITE_DONE:
127 printf("SQLITE_DONE");
128 break;
129 case SQLITE_EMPTY:
130 printf("SQLITE_EMPTY");
131 break;
132 case SQLITE_ERROR:
133 printf("SQLITE_ERROR");
134 break;
135 case SQLITE_FORMAT:
136 printf("SQLITE_FORMAT");
137 break;
138 case SQLITE_INTERNAL:
139 printf("SQLITE_INTERNAL");
140 break;
141 case SQLITE_INTERRUPT:
142 printf("SQLITE_INTERRUPT");
143 break;
144 case SQLITE_IOERR:
145 printf("SQLITE_IOERR");
146 break;
147 case SQLITE_LOCKED:
148 printf("SQLITE_LOCKED");
149 break;
150 case SQLITE_MISMATCH:
151 printf("SQLITE_MISMATCH");
152 break;
153 case SQLITE_MISUSE:
154 printf("SQLITE_MISUSE");
155 break;
156 case SQLITE_NOLFS:
157 printf("SQLITE_NOLFS");
158 break;
159 case SQLITE_NOMEM:
160 printf("SQLITE_NOMEM");
161 break;
162 case SQLITE_NOTADB:
163 printf("SQLITE_NOTADB");
164 break;
165 case SQLITE_NOTFOUND:
166 printf("SQLITE_NOTFOUND");
167 break;
168 case SQLITE_NOTICE:
169 printf("SQLITE_NOTICE");
170 break;
171 case SQLITE_OK:
172 printf("SQLITE_OK");
173 break;
174 case SQLITE_PERM:
175 printf("SQLITE_PERM");
176 break;
177 case SQLITE_SCHEMA:
178 printf("SQLITE_SCHEMA");
179 break;
180 case SQLITE_TOOBIG:
181 printf("SQLITE_TOOBIG");
182 break;
183 case SQLITE_WARNING:
184 printf("SQLITE_WARNING");
185 break;
186 }
187}