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