adamc@885
|
1 (* Copyright (c) 2009, Adam Chlipala
|
adamc@885
|
2 * All rights reserved.
|
adamc@885
|
3 *
|
adamc@885
|
4 * Redistribution and use in source and binary forms, with or without
|
adamc@885
|
5 * modification, are permitted provided that the following conditions are met:
|
adamc@885
|
6 *
|
adamc@885
|
7 * - Redistributions of source code must retain the above copyright notice,
|
adamc@885
|
8 * this list of conditions and the following disclaimer.
|
adamc@885
|
9 * - Redistributions in binary form must reproduce the above copyright notice,
|
adamc@885
|
10 * this list of conditions and the following disclaimer in the documentation
|
adamc@885
|
11 * and/or other materials provided with the distribution.
|
adamc@885
|
12 * - The names of contributors may not be used to endorse or promote products
|
adamc@885
|
13 * derived from this software without specific prior written permission.
|
adamc@885
|
14 *
|
adamc@885
|
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
adamc@885
|
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
adamc@885
|
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
adamc@885
|
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
adamc@885
|
19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
adamc@885
|
20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
adamc@885
|
21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
adamc@885
|
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
adamc@885
|
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
adamc@885
|
24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
adamc@885
|
25 * POSSIBILITY OF SUCH DAMAGE.
|
adamc@885
|
26 *)
|
adamc@885
|
27
|
adamc@885
|
28 structure SQLite :> SQLITE = struct
|
adamc@885
|
29
|
adamc@885
|
30 open Settings
|
adamc@885
|
31 open Print.PD
|
adamc@885
|
32 open Print
|
adamc@885
|
33
|
adamc@885
|
34 fun p_sql_type t =
|
adamc@885
|
35 case t of
|
adamc@885
|
36 Int => "integer"
|
adamc@885
|
37 | Float => "real"
|
adamc@885
|
38 | String => "text"
|
adamc@1014
|
39 | Char => "text"
|
adamc@885
|
40 | Bool => "integer"
|
adamc@887
|
41 | Time => "text"
|
adamc@885
|
42 | Blob => "blob"
|
adamc@885
|
43 | Channel => "integer"
|
adamc@885
|
44 | Client => "integer"
|
adamc@885
|
45 | Nullable t => p_sql_type t
|
adamc@885
|
46
|
adamc@885
|
47 val ident = String.translate (fn #"'" => "PRIME"
|
adamc@885
|
48 | ch => str ch)
|
adamc@885
|
49
|
adamc@885
|
50 fun checkRel (table, checkNullable) (s, xts) =
|
adamc@885
|
51 let
|
adamc@885
|
52 val q = "SELECT COUNT(*) FROM sqlite_master WHERE type = '" ^ table ^ "' AND name = '"
|
adamc@885
|
53 ^ s ^ "'"
|
adamc@885
|
54 in
|
adamc@885
|
55 box [string "if (sqlite3_prepare_v2(conn->conn, \"",
|
adamc@885
|
56 string q,
|
adamc@885
|
57 string "\", -1, &stmt, NULL) != SQLITE_OK) {",
|
adamc@885
|
58 newline,
|
adamc@885
|
59 box [string "sqlite3_close(conn->conn);",
|
adamc@885
|
60 newline,
|
adamc@885
|
61 string "uw_error(ctx, FATAL, \"Query preparation failed:\\n",
|
adamc@885
|
62 string q,
|
adamc@885
|
63 string "\");",
|
adamc@885
|
64 newline],
|
adamc@885
|
65 string "}",
|
adamc@885
|
66 newline,
|
adamc@885
|
67 newline,
|
adamc@885
|
68
|
adamc@885
|
69 string "while ((res = sqlite3_step(stmt)) == SQLITE_BUSY)",
|
adamc@885
|
70 newline,
|
adamc@885
|
71 box [string "sleep(1);",
|
adamc@885
|
72 newline],
|
adamc@885
|
73 newline,
|
adamc@885
|
74 string "if (res == SQLITE_DONE) {",
|
adamc@885
|
75 newline,
|
adamc@885
|
76 box [string "sqlite3_finalize(stmt);",
|
adamc@885
|
77 newline,
|
adamc@885
|
78 string "sqlite3_close(conn->conn);",
|
adamc@885
|
79 newline,
|
adamc@885
|
80 string "uw_error(ctx, FATAL, \"No row returned:\\n",
|
adamc@885
|
81 string q,
|
adamc@885
|
82 string "\");",
|
adamc@885
|
83 newline],
|
adamc@885
|
84 string "}",
|
adamc@885
|
85 newline,
|
adamc@885
|
86 newline,
|
adamc@885
|
87 string "if (res != SQLITE_ROW) {",
|
adamc@885
|
88 newline,
|
adamc@885
|
89 box [string "sqlite3_finalize(stmt);",
|
adamc@885
|
90 newline,
|
adamc@885
|
91 string "sqlite3_close(conn->conn);",
|
adamc@885
|
92 newline,
|
adamc@885
|
93 string "uw_error(ctx, FATAL, \"Error getting row:\\n",
|
adamc@885
|
94 string q,
|
adamc@885
|
95 string "\");",
|
adamc@885
|
96 newline],
|
adamc@885
|
97 string "}",
|
adamc@885
|
98 newline,
|
adamc@885
|
99 newline,
|
adamc@885
|
100
|
adamc@885
|
101 string "if (sqlite3_column_count(stmt) != 1) {",
|
adamc@885
|
102 newline,
|
adamc@885
|
103 box [string "sqlite3_finalize(stmt);",
|
adamc@885
|
104 newline,
|
adamc@885
|
105 string "sqlite3_close(conn->conn);",
|
adamc@885
|
106 newline,
|
adamc@885
|
107 string "uw_error(ctx, FATAL, \"Bad column count:\\n",
|
adamc@885
|
108 string q,
|
adamc@885
|
109 string "\");",
|
adamc@885
|
110 newline],
|
adamc@885
|
111 string "}",
|
adamc@885
|
112 newline,
|
adamc@885
|
113 newline,
|
adamc@885
|
114
|
adamc@885
|
115 string "if (sqlite3_column_int(stmt, 0) != 1) {",
|
adamc@885
|
116 newline,
|
adamc@885
|
117 box [string "sqlite3_finalize(stmt);",
|
adamc@885
|
118 newline,
|
adamc@885
|
119 string "sqlite3_close(conn->conn);",
|
adamc@885
|
120 newline,
|
adamc@885
|
121 string "uw_error(ctx, FATAL, \"Table '",
|
adamc@885
|
122 string s,
|
adamc@885
|
123 string "' does not exist.\");",
|
adamc@885
|
124 newline],
|
adamc@885
|
125 string "}",
|
adamc@885
|
126 newline,
|
adamc@885
|
127 newline,
|
adamc@885
|
128 string "sqlite3_finalize(stmt);",
|
adamc@885
|
129 newline]
|
adamc@885
|
130 end
|
adamc@885
|
131
|
adamc@885
|
132 fun init {dbstring, prepared = ss, tables, views, sequences} =
|
adamc@885
|
133 let
|
adamc@885
|
134 val db = ref dbstring
|
adamc@885
|
135 in
|
adamc@885
|
136 app (fn s =>
|
adamc@885
|
137 case String.fields (fn ch => ch = #"=") s of
|
adamc@885
|
138 [name, value] =>
|
adamc@885
|
139 (case name of
|
adamc@885
|
140 "dbname" => db := value
|
adamc@885
|
141 | _ => ())
|
adamc@885
|
142 | _ => ()) (String.tokens Char.isSpace dbstring);
|
adamc@885
|
143
|
adamc@885
|
144 box [string "typedef struct {",
|
adamc@885
|
145 newline,
|
adamc@885
|
146 box [string "sqlite3 *conn;",
|
adamc@885
|
147 newline,
|
adamc@885
|
148 p_list_sepi (box [])
|
adamc@885
|
149 (fn i => fn _ =>
|
adamc@885
|
150 box [string "sqlite3_stmt *p",
|
adamc@885
|
151 string (Int.toString i),
|
adamc@885
|
152 string ";",
|
adamc@885
|
153 newline])
|
adamc@885
|
154 ss],
|
adamc@885
|
155 string "} uw_conn;",
|
adamc@885
|
156 newline,
|
adamc@885
|
157 newline,
|
adamc@885
|
158
|
adamc@1094
|
159 string "static void uw_client_init(void) {",
|
adamc@885
|
160 newline,
|
adamc@885
|
161 box [string "uw_sqlfmtInt = \"%lld%n\";",
|
adamc@885
|
162 newline,
|
adamc@885
|
163 string "uw_sqlfmtFloat = \"%g%n\";",
|
adamc@885
|
164 newline,
|
adamc@885
|
165 string "uw_Estrings = 0;",
|
adamc@885
|
166 newline,
|
adamc@885
|
167 string "uw_sqlsuffixString = \"\";",
|
adamc@885
|
168 newline,
|
adamc@1011
|
169 string "uw_sqlsuffixChar = \"\";",
|
adamc@1011
|
170 newline,
|
adamc@885
|
171 string "uw_sqlsuffixBlob = \"\";",
|
adamc@885
|
172 newline,
|
adamc@885
|
173 string "uw_sqlfmtUint4 = \"%u%n\";",
|
adamc@885
|
174 newline],
|
adamc@885
|
175 string "}",
|
adamc@885
|
176 newline,
|
adamc@885
|
177 newline,
|
adamc@885
|
178
|
adamc@885
|
179 if #persistent (currentProtocol ()) then
|
adamc@885
|
180 box [string "static void uw_db_validate(uw_context ctx) {",
|
adamc@885
|
181 newline,
|
adamc@885
|
182 string "uw_conn *conn = uw_get_db(ctx);",
|
adamc@885
|
183 newline,
|
adamc@885
|
184 string "sqlite3_stmt *stmt;",
|
adamc@885
|
185 newline,
|
adamc@885
|
186 string "int res;",
|
adamc@885
|
187 newline,
|
adamc@885
|
188 newline,
|
adamc@885
|
189 p_list_sep newline (checkRel ("table", true)) tables,
|
adamc@885
|
190 p_list_sep newline (fn name => checkRel ("table", true)
|
adamc@885
|
191 (name, [("id", Settings.Client)])) sequences,
|
adamc@885
|
192 p_list_sep newline (checkRel ("view", false)) views,
|
adamc@885
|
193 string "}",
|
adamc@885
|
194 newline,
|
adamc@885
|
195 newline,
|
adamc@885
|
196
|
adamc@885
|
197 string "static void uw_db_prepare(uw_context ctx) {",
|
adamc@885
|
198 newline,
|
adamc@885
|
199 string "uw_conn *conn = uw_get_db(ctx);",
|
adamc@885
|
200 newline,
|
adamc@885
|
201 newline,
|
adamc@885
|
202
|
adamc@885
|
203 p_list_sepi newline (fn i => fn (s, n) =>
|
adamc@885
|
204 let
|
adamc@885
|
205 fun uhoh this s args =
|
adamc@885
|
206 box [p_list_sepi (box [])
|
adamc@885
|
207 (fn j => fn () =>
|
adamc@885
|
208 box [string
|
adamc@885
|
209 "sqlite3_finalize(conn->p",
|
adamc@885
|
210 string (Int.toString j),
|
adamc@885
|
211 string ");",
|
adamc@885
|
212 newline])
|
adamc@885
|
213 (List.tabulate (i, fn _ => ())),
|
adamc@885
|
214 box (if this then
|
adamc@885
|
215 [string
|
adamc@885
|
216 "sqlite3_finalize(conn->p",
|
adamc@885
|
217 string (Int.toString i),
|
adamc@885
|
218 string ");",
|
adamc@885
|
219 newline]
|
adamc@885
|
220 else
|
adamc@885
|
221 []),
|
adamc@885
|
222 string "sqlite3_close(conn->conn);",
|
adamc@885
|
223 newline,
|
adamc@885
|
224 string "uw_error(ctx, FATAL, \"",
|
adamc@885
|
225 string s,
|
adamc@885
|
226 string "\"",
|
adamc@885
|
227 p_list_sep (box []) (fn s => box [string ", ",
|
adamc@885
|
228 string s]) args,
|
adamc@885
|
229 string ");",
|
adamc@885
|
230 newline]
|
adamc@885
|
231 in
|
adamc@885
|
232 box [string "if (sqlite3_prepare_v2(conn->conn, \"",
|
adamc@885
|
233 string (String.toString s),
|
adamc@885
|
234 string "\", -1, &conn->p",
|
adamc@885
|
235 string (Int.toString i),
|
adamc@885
|
236 string ", NULL) != SQLITE_OK) {",
|
adamc@885
|
237 newline,
|
adamc@886
|
238 box [string "char msg[1024];",
|
adamc@886
|
239 newline,
|
adamc@886
|
240 string "strncpy(msg, sqlite3_errmsg(conn->conn), 1024);",
|
adamc@886
|
241 newline,
|
adamc@886
|
242 string "msg[1023] = 0;",
|
adamc@886
|
243 newline,
|
adamc@886
|
244 uhoh false ("Error preparing statement: "
|
adamc@886
|
245 ^ String.toString s ^ "\\n%s") ["msg"]],
|
adamc@885
|
246 string "}",
|
adamc@885
|
247 newline]
|
adamc@885
|
248 end)
|
adamc@885
|
249 ss,
|
adamc@885
|
250
|
adamc@885
|
251 string "}"]
|
adamc@885
|
252 else
|
adamc@885
|
253 box [string "static void uw_db_prepare(uw_context ctx) { }",
|
adamc@885
|
254 newline,
|
adamc@885
|
255 string "static void uw_db_validate(uw_context ctx) { }"],
|
adamc@885
|
256 newline,
|
adamc@885
|
257 newline,
|
adamc@885
|
258
|
adamc@1094
|
259 string "static void uw_db_init(uw_context ctx) {",
|
adamc@885
|
260 newline,
|
adamc@885
|
261 string "sqlite3 *sqlite;",
|
adamc@885
|
262 newline,
|
adamc@885
|
263 string "uw_conn *conn;",
|
adamc@885
|
264 newline,
|
adamc@885
|
265 newline,
|
adamc@885
|
266 string "if (sqlite3_open(\"",
|
adamc@885
|
267 string (!db),
|
adamc@885
|
268 string "\", &sqlite) != SQLITE_OK) uw_error(ctx, FATAL, ",
|
adamc@885
|
269 string "\"Can't open SQLite database.\");",
|
adamc@885
|
270 newline,
|
adamc@885
|
271 newline,
|
adamc@885
|
272 string "conn = calloc(1, sizeof(uw_conn));",
|
adamc@885
|
273 newline,
|
adamc@885
|
274 string "conn->conn = sqlite;",
|
adamc@885
|
275 newline,
|
adamc@885
|
276 string "uw_set_db(ctx, conn);",
|
adamc@885
|
277 newline,
|
adamc@885
|
278 string "uw_db_validate(ctx);",
|
adamc@885
|
279 newline,
|
adamc@885
|
280 string "uw_db_prepare(ctx);",
|
adamc@885
|
281 newline,
|
adamc@885
|
282 string "}",
|
adamc@885
|
283 newline,
|
adamc@885
|
284 newline,
|
adamc@885
|
285
|
adamc@1094
|
286 string "static void uw_db_close(uw_context ctx) {",
|
adamc@885
|
287 newline,
|
adamc@885
|
288 string "uw_conn *conn = uw_get_db(ctx);",
|
adamc@885
|
289 newline,
|
adamc@885
|
290 p_list_sepi (box [])
|
adamc@885
|
291 (fn i => fn _ =>
|
adamc@885
|
292 box [string "if (conn->p",
|
adamc@885
|
293 string (Int.toString i),
|
adamc@885
|
294 string ") sqlite3_finalize(conn->p",
|
adamc@885
|
295 string (Int.toString i),
|
adamc@885
|
296 string ");",
|
adamc@885
|
297 newline])
|
adamc@885
|
298 ss,
|
adamc@885
|
299 string "sqlite3_close(conn->conn);",
|
adamc@885
|
300 newline,
|
adamc@885
|
301 string "}",
|
adamc@885
|
302 newline,
|
adamc@885
|
303 newline,
|
adamc@885
|
304
|
adamc@1094
|
305 string "static int uw_db_begin(uw_context ctx) {",
|
adamc@885
|
306 newline,
|
adamc@885
|
307 string "uw_conn *conn = uw_get_db(ctx);",
|
adamc@885
|
308 newline,
|
adamc@885
|
309 newline,
|
adamc@885
|
310 string "if (sqlite3_exec(conn->conn, \"BEGIN\", NULL, NULL, NULL) == SQLITE_OK)",
|
adamc@885
|
311 newline,
|
adamc@885
|
312 box [string "return 0;",
|
adamc@885
|
313 newline],
|
adamc@885
|
314 string "else {",
|
adamc@885
|
315 newline,
|
adamc@885
|
316 box [string "fprintf(stderr, \"Begin error: %s\\n\", sqlite3_errmsg(conn->conn));",
|
adamc@885
|
317 newline,
|
adamc@885
|
318 string "return 1;",
|
adamc@885
|
319 newline],
|
adamc@885
|
320 string "}",
|
adamc@885
|
321 newline,
|
adamc@885
|
322 string "}",
|
adamc@885
|
323 newline,
|
adamc@1094
|
324 string "static int uw_db_commit(uw_context ctx) {",
|
adamc@885
|
325 newline,
|
adamc@885
|
326 string "uw_conn *conn = uw_get_db(ctx);",
|
adamc@885
|
327 newline,
|
adamc@885
|
328 string "if (sqlite3_exec(conn->conn, \"COMMIT\", NULL, NULL, NULL) == SQLITE_OK)",
|
adamc@885
|
329 newline,
|
adamc@885
|
330 box [string "return 0;",
|
adamc@885
|
331 newline],
|
adamc@885
|
332 string "else {",
|
adamc@885
|
333 newline,
|
adamc@885
|
334 box [string "fprintf(stderr, \"Commit error: %s\\n\", sqlite3_errmsg(conn->conn));",
|
adamc@885
|
335 newline,
|
adamc@885
|
336 string "return 1;",
|
adamc@885
|
337 newline],
|
adamc@885
|
338 string "}",
|
adamc@885
|
339 newline,
|
adamc@885
|
340 string "}",
|
adamc@885
|
341 newline,
|
adamc@885
|
342 newline,
|
adamc@885
|
343
|
adamc@1094
|
344 string "static int uw_db_rollback(uw_context ctx) {",
|
adamc@885
|
345 newline,
|
adamc@885
|
346 string "uw_conn *conn = uw_get_db(ctx);",
|
adamc@885
|
347 newline,
|
adamc@885
|
348 string "if (sqlite3_exec(conn->conn, \"ROLLBACK\", NULL, NULL, NULL) == SQLITE_OK)",
|
adamc@885
|
349 newline,
|
adamc@885
|
350 box [string "return 0;",
|
adamc@885
|
351 newline],
|
adamc@885
|
352 string "else {",
|
adamc@885
|
353 newline,
|
adamc@885
|
354 box [string "fprintf(stderr, \"Rollback error: %s\\n\", sqlite3_errmsg(conn->conn));",
|
adamc@885
|
355 newline,
|
adamc@885
|
356 string "return 1;",
|
adamc@885
|
357 newline],
|
adamc@885
|
358 string "}",
|
adamc@885
|
359 newline,
|
adamc@885
|
360 string "}",
|
adamc@885
|
361 newline,
|
adamc@885
|
362 newline]
|
adamc@885
|
363 end
|
adamc@885
|
364
|
adamc@885
|
365 fun p_getcol {loc, wontLeakStrings, col = i, typ = t} =
|
adamc@885
|
366 let
|
adamc@885
|
367 fun p_unsql t =
|
adamc@885
|
368 case t of
|
adamc@885
|
369 Int => box [string "sqlite3_column_int64(stmt, ", string (Int.toString i), string ")"]
|
adamc@885
|
370 | Float => box [string "sqlite3_column_double(stmt, ", string (Int.toString i), string ")"]
|
adamc@885
|
371 | String =>
|
adamc@885
|
372 if wontLeakStrings then
|
adamc@1014
|
373 box [string "(uw_Basis_string)sqlite3_column_text(stmt, ", string (Int.toString i), string ")"]
|
adamc@885
|
374 else
|
adamc@1014
|
375 box [string "uw_strdup(ctx, (uw_Basis_string)sqlite3_column_text(stmt, ", string (Int.toString i), string "))"]
|
adamc@1014
|
376 | Char => box [string "sqlite3_column_text(stmt, ", string (Int.toString i), string ")[0]"]
|
adamc@885
|
377 | Bool => box [string "(uw_Basis_bool)sqlite3_column_int(stmt, ", string (Int.toString i), string ")"]
|
adamc@1014
|
378 | Time => box [string "uw_Basis_stringToTime_error(ctx, (uw_Basis_string)sqlite3_column_text(stmt, ", string (Int.toString i), string "))"]
|
adamc@885
|
379 | Blob => box [string "({",
|
adamc@885
|
380 newline,
|
adamc@890
|
381 string "char *data = (char *)sqlite3_column_blob(stmt, ",
|
adamc@885
|
382 string (Int.toString i),
|
adamc@885
|
383 string ");",
|
adamc@885
|
384 newline,
|
adamc@890
|
385 string "int len = sqlite3_column_bytes(stmt, ",
|
adamc@885
|
386 string (Int.toString i),
|
adamc@890
|
387 string ");",
|
adamc@890
|
388 newline,
|
adamc@890
|
389 string "uw_Basis_blob b = {len, uw_memdup(ctx, data, len)};",
|
adamc@885
|
390 newline,
|
adamc@885
|
391 string "b;",
|
adamc@885
|
392 newline,
|
adamc@885
|
393 string "})"]
|
adamc@886
|
394 | Channel => box [string "({",
|
adamc@886
|
395 newline,
|
adamc@886
|
396 string "sqlite3_int64 n = sqlite3_column_int64(stmt, ",
|
adamc@886
|
397 string (Int.toString i),
|
adamc@886
|
398 string ");",
|
adamc@886
|
399 newline,
|
adamc@886
|
400 string "uw_Basis_channel ch = {n >> 32, n & 0xFFFFFFFF};",
|
adamc@886
|
401 newline,
|
adamc@886
|
402 string "ch;",
|
adamc@886
|
403 newline,
|
adamc@886
|
404 string "})"]
|
adamc@885
|
405 | Client => box [string "sqlite3_column_int(stmt, ", string (Int.toString i), string ")"]
|
adamc@885
|
406
|
adamc@885
|
407 | Nullable _ => raise Fail "Postgres: Recursive Nullable"
|
adamc@885
|
408
|
adamc@885
|
409 fun getter t =
|
adamc@885
|
410 case t of
|
adamc@885
|
411 Nullable t =>
|
adamc@885
|
412 box [string "(sqlite3_column_type(stmt, ",
|
adamc@885
|
413 string (Int.toString i),
|
adamc@885
|
414 string ") == SQLITE_NULL ? NULL : ",
|
adamc@885
|
415 case t of
|
adamc@885
|
416 String => getter t
|
adamc@885
|
417 | _ => box [string "({",
|
adamc@885
|
418 newline,
|
adamc@885
|
419 string (p_sql_ctype t),
|
adamc@885
|
420 space,
|
adamc@885
|
421 string "*tmp = uw_malloc(ctx, sizeof(",
|
adamc@885
|
422 string (p_sql_ctype t),
|
adamc@885
|
423 string "));",
|
adamc@885
|
424 newline,
|
adamc@885
|
425 string "*tmp = ",
|
adamc@885
|
426 getter t,
|
adamc@885
|
427 string ";",
|
adamc@885
|
428 newline,
|
adamc@885
|
429 string "tmp;",
|
adamc@885
|
430 newline,
|
adamc@885
|
431 string "})"],
|
adamc@885
|
432 string ")"]
|
adamc@885
|
433 | _ =>
|
adamc@885
|
434 box [string "(sqlite3_column_type(stmt, ",
|
adamc@885
|
435 string (Int.toString i),
|
adamc@885
|
436 string ") == SQLITE_NULL ? ",
|
adamc@885
|
437 box [string "({",
|
adamc@885
|
438 string (p_sql_ctype t),
|
adamc@885
|
439 space,
|
adamc@885
|
440 string "tmp;",
|
adamc@885
|
441 newline,
|
adamc@885
|
442 string "uw_error(ctx, FATAL, \"",
|
adamc@885
|
443 string (ErrorMsg.spanToString loc),
|
adamc@885
|
444 string ": Unexpectedly NULL field #",
|
adamc@885
|
445 string (Int.toString i),
|
adamc@885
|
446 string "\");",
|
adamc@885
|
447 newline,
|
adamc@885
|
448 string "tmp;",
|
adamc@885
|
449 newline,
|
adamc@885
|
450 string "})"],
|
adamc@885
|
451 string " : ",
|
adamc@885
|
452 p_unsql t,
|
adamc@885
|
453 string ")"]
|
adamc@885
|
454 in
|
adamc@885
|
455 getter t
|
adamc@885
|
456 end
|
adamc@885
|
457
|
adamc@885
|
458 fun queryCommon {loc, query, cols, doCols} =
|
adamc@885
|
459 box [string "int r;",
|
adamc@885
|
460 newline,
|
adamc@885
|
461
|
adamc@885
|
462 string "sqlite3_reset(stmt);",
|
adamc@885
|
463 newline,
|
adamc@885
|
464
|
adamc@885
|
465 string "uw_end_region(ctx);",
|
adamc@885
|
466 newline,
|
adamc@885
|
467 string "while ((r = sqlite3_step(stmt)) == SQLITE_ROW) {",
|
adamc@885
|
468 newline,
|
adamc@885
|
469 doCols p_getcol,
|
adamc@885
|
470 string "}",
|
adamc@885
|
471 newline,
|
adamc@885
|
472 newline,
|
adamc@885
|
473
|
adamc@885
|
474 string "if (r == SQLITE_BUSY) {",
|
adamc@885
|
475 box [string "sleep(1);",
|
adamc@885
|
476 newline,
|
adamc@885
|
477 string "uw_error(ctx, UNLIMITED_RETRY, \"Database is busy\");",
|
adamc@885
|
478 newline],
|
adamc@885
|
479 string "}",
|
adamc@885
|
480 newline,
|
adamc@885
|
481 newline,
|
adamc@885
|
482
|
adamc@885
|
483 string "if (r != SQLITE_DONE) uw_error(ctx, FATAL, \"",
|
adamc@885
|
484 string (ErrorMsg.spanToString loc),
|
adamc@885
|
485 string ": query step failed: %s\\n%s\", ",
|
adamc@885
|
486 query,
|
adamc@885
|
487 string ", sqlite3_errmsg(conn->conn));",
|
adamc@885
|
488 newline,
|
adamc@885
|
489 newline]
|
adamc@885
|
490
|
adamc@885
|
491 fun query {loc, cols, doCols} =
|
adamc@885
|
492 box [string "uw_conn *conn = uw_get_db(ctx);",
|
adamc@885
|
493 newline,
|
adamc@886
|
494 string "sqlite3_stmt *stmt;",
|
adamc@885
|
495 newline,
|
adamc@885
|
496 newline,
|
adamc@885
|
497 string "if (sqlite3_prepare_v2(conn->conn, query, -1, &stmt, NULL) != SQLITE_OK) uw_error(ctx, FATAL, \"Error preparing statement: %s\\n%s\", sqlite3_errmsg(conn->conn));",
|
adamc@885
|
498 newline,
|
adamc@885
|
499 newline,
|
adamc@885
|
500 string "uw_push_cleanup(ctx, (void (*)(void *))sqlite3_finalize, stmt);",
|
adamc@885
|
501 newline,
|
adamc@885
|
502 newline,
|
adamc@885
|
503
|
adamc@885
|
504 queryCommon {loc = loc, cols = cols, doCols = doCols, query = string "query"},
|
adamc@885
|
505
|
adamc@885
|
506 string "uw_pop_cleanup(ctx);",
|
adamc@885
|
507 newline]
|
adamc@885
|
508
|
adamc@1014
|
509 val p_pre_inputs =
|
adamc@1014
|
510 p_list_sepi (box [])
|
adamc@1014
|
511 (fn i => fn t =>
|
adamc@1014
|
512 case t of
|
adamc@1014
|
513 Char => box [string "char arg",
|
adamc@1014
|
514 string (Int.toString (i + 1)),
|
adamc@1014
|
515 string "s = {arg",
|
adamc@1014
|
516 string (Int.toString (i + 1)),
|
adamc@1014
|
517 string ", 0};",
|
adamc@1014
|
518 newline]
|
adamc@1014
|
519 | _ => box [])
|
adamc@1014
|
520
|
adamc@885
|
521 fun p_inputs loc =
|
adamc@885
|
522 p_list_sepi (box [])
|
adamc@885
|
523 (fn i => fn t =>
|
adamc@885
|
524 let
|
adamc@885
|
525 fun bind (t, arg) =
|
adamc@885
|
526 case t of
|
adamc@885
|
527 Int => box [string "sqlite3_bind_int64(stmt, ",
|
adamc@885
|
528 string (Int.toString (i + 1)),
|
adamc@885
|
529 string ", ",
|
adamc@885
|
530 arg,
|
adamc@885
|
531 string ")"]
|
adamc@885
|
532 | Float => box [string "sqlite3_bind_double(stmt, ",
|
adamc@885
|
533 string (Int.toString (i + 1)),
|
adamc@885
|
534 string ", ",
|
adamc@885
|
535 arg,
|
adamc@1014
|
536 string ")"]
|
adamc@885
|
537 | String => box [string "sqlite3_bind_text(stmt, ",
|
adamc@885
|
538 string (Int.toString (i + 1)),
|
adamc@885
|
539 string ", ",
|
adamc@885
|
540 arg,
|
adamc@885
|
541 string ", -1, SQLITE_TRANSIENT)"]
|
adamc@1014
|
542 | Char => box [string "sqlite3_bind_text(stmt, ",
|
adamc@1014
|
543 string (Int.toString (i + 1)),
|
adamc@1014
|
544 string ", ",
|
adamc@1014
|
545 arg,
|
adamc@1014
|
546 string "s, -1, SQLITE_TRANSIENT)"]
|
adamc@885
|
547 | Bool => box [string "sqlite3_bind_int(stmt, ",
|
adamc@885
|
548 string (Int.toString (i + 1)),
|
adamc@885
|
549 string ", ",
|
adamc@885
|
550 arg,
|
adamc@885
|
551 string ")"]
|
adamc@887
|
552 | Time => box [string "sqlite3_bind_text(stmt, ",
|
adamc@885
|
553 string (Int.toString (i + 1)),
|
adamc@887
|
554 string ", uw_Basis_attrifyTime(ctx, ",
|
adamc@885
|
555 arg,
|
adamc@887
|
556 string "), -1, SQLITE_TRANSIENT)"]
|
adamc@885
|
557 | Blob => box [string "sqlite3_bind_blob(stmt, ",
|
adamc@885
|
558 string (Int.toString (i + 1)),
|
adamc@885
|
559 string ", ",
|
adamc@885
|
560 arg,
|
adamc@885
|
561 string ".data, ",
|
adamc@885
|
562 arg,
|
adamc@890
|
563 string ".size, SQLITE_TRANSIENT)"]
|
adamc@886
|
564 | Channel => box [string "sqlite3_bind_int64(stmt, ",
|
adamc@885
|
565 string (Int.toString (i + 1)),
|
adamc@886
|
566 string ", ((sqlite3_int64)",
|
adamc@885
|
567 arg,
|
adamc@886
|
568 string ".cli << 32) | ",
|
adamc@886
|
569 arg,
|
adamc@886
|
570 string ".chn)"]
|
adamc@885
|
571 | Client => box [string "sqlite3_bind_int(stmt, ",
|
adamc@885
|
572 string (Int.toString (i + 1)),
|
adamc@885
|
573 string ", ",
|
adamc@885
|
574 arg,
|
adamc@885
|
575 string ")"]
|
adamc@885
|
576 | Nullable t => box [string "(",
|
adamc@885
|
577 arg,
|
adamc@885
|
578 string " == NULL ? sqlite3_bind_null(stmt, ",
|
adamc@885
|
579 string (Int.toString (i + 1)),
|
adamc@885
|
580 string ") : ",
|
adamc@885
|
581 bind (t, case t of
|
adamc@885
|
582 String => arg
|
adamc@885
|
583 | _ => box [string "(*", arg, string ")"]),
|
adamc@885
|
584 string ")"]
|
adamc@885
|
585 in
|
adamc@885
|
586 box [string "if (",
|
adamc@885
|
587 bind (t, box [string "arg", string (Int.toString (i + 1))]),
|
adamc@885
|
588 string " != SQLITE_OK) uw_error(ctx, FATAL, \"",
|
adamc@885
|
589 string (ErrorMsg.spanToString loc),
|
adamc@885
|
590 string ": Error binding parameter #",
|
adamc@885
|
591 string (Int.toString (i + 1)),
|
adamc@885
|
592 string ": %s\", sqlite3_errmsg(conn->conn));",
|
adamc@885
|
593 newline]
|
adamc@885
|
594 end)
|
adamc@885
|
595
|
adamc@885
|
596 fun queryPrepared {loc, id, query, inputs, cols, doCols, nested} =
|
adamc@885
|
597 box [string "uw_conn *conn = uw_get_db(ctx);",
|
adamc@885
|
598 newline,
|
adamc@1014
|
599 p_pre_inputs inputs,
|
adamc@885
|
600 if nested then
|
adamc@885
|
601 box [string "sqlite3_stmt *stmt;",
|
adamc@885
|
602 newline]
|
adamc@885
|
603 else
|
adamc@885
|
604 box [string "sqlite3_stmt *stmt = conn->p",
|
adamc@885
|
605 string (Int.toString id),
|
adamc@885
|
606 string ";",
|
adamc@885
|
607 newline,
|
adamc@885
|
608 newline,
|
adamc@885
|
609
|
adamc@885
|
610 string "if (stmt == NULL) {",
|
adamc@885
|
611 newline],
|
adamc@885
|
612
|
adamc@885
|
613 string "if (sqlite3_prepare_v2(conn->conn, \"",
|
adamc@885
|
614 string (String.toString query),
|
adamc@885
|
615 string "\", -1, &stmt, NULL) != SQLITE_OK) uw_error(ctx, FATAL, \"Error preparing statement: ",
|
adamc@885
|
616 string (String.toString query),
|
adamc@885
|
617 string "\\n%s\", sqlite3_errmsg(conn->conn));",
|
adamc@885
|
618 newline,
|
adamc@885
|
619 if nested then
|
adamc@885
|
620 box [string "uw_push_cleanup(ctx, (void (*)(void *))sqlite3_finalize, stmt);",
|
adamc@885
|
621 newline]
|
adamc@885
|
622 else
|
adamc@885
|
623 box [string "conn->p",
|
adamc@885
|
624 string (Int.toString id),
|
adamc@885
|
625 string " = stmt;",
|
adamc@885
|
626 newline,
|
adamc@885
|
627 string "}",
|
adamc@885
|
628 newline,
|
adamc@885
|
629 newline,
|
adamc@885
|
630 string "uw_push_cleanup(ctx, (void (*)(void *))sqlite3_clear_bindings, stmt);",
|
adamc@885
|
631 newline,
|
adamc@885
|
632 string "uw_push_cleanup(ctx, (void (*)(void *))sqlite3_reset, stmt);",
|
adamc@885
|
633 newline],
|
adamc@885
|
634 newline,
|
adamc@885
|
635
|
adamc@885
|
636 p_inputs loc inputs,
|
adamc@885
|
637 newline,
|
adamc@885
|
638
|
adamc@885
|
639 queryCommon {loc = loc, cols = cols, doCols = doCols, query = box [string "\"",
|
adamc@885
|
640 string (String.toString query),
|
adamc@885
|
641 string "\""]},
|
adamc@885
|
642
|
adamc@885
|
643 string "uw_pop_cleanup(ctx);",
|
adamc@885
|
644 newline,
|
adamc@885
|
645 if nested then
|
adamc@885
|
646 box []
|
adamc@885
|
647 else
|
adamc@885
|
648 box [string "uw_pop_cleanup(ctx);",
|
adamc@885
|
649 newline]]
|
adamc@885
|
650
|
adamc@885
|
651 fun dmlCommon {loc, dml} =
|
adamc@885
|
652 box [string "int r;",
|
adamc@885
|
653 newline,
|
adamc@885
|
654
|
adamc@885
|
655 string "if ((r = sqlite3_step(stmt)) == SQLITE_BUSY) {",
|
adamc@885
|
656 box [string "sleep(1);",
|
adamc@885
|
657 newline,
|
adamc@885
|
658 string "uw_error(ctx, UNLIMITED_RETRY, \"Database is busy\");",
|
adamc@885
|
659 newline],
|
adamc@885
|
660 string "}",
|
adamc@885
|
661 newline,
|
adamc@885
|
662 newline,
|
adamc@885
|
663
|
adamc@885
|
664 string "if (r != SQLITE_DONE) uw_error(ctx, FATAL, \"",
|
adamc@885
|
665 string (ErrorMsg.spanToString loc),
|
adamc@885
|
666 string ": DML step failed: %s\\n%s\", ",
|
adamc@885
|
667 dml,
|
adamc@885
|
668 string ", sqlite3_errmsg(conn->conn));",
|
adamc@885
|
669 newline]
|
adamc@885
|
670
|
adamc@885
|
671 fun dml loc =
|
adamc@885
|
672 box [string "uw_conn *conn = uw_get_db(ctx);",
|
adamc@885
|
673 newline,
|
adamc@886
|
674 string "sqlite3_stmt *stmt;",
|
adamc@885
|
675 newline,
|
adamc@885
|
676 newline,
|
adamc@885
|
677 string "if (sqlite3_prepare_v2(conn->conn, dml, -1, &stmt, NULL) != SQLITE_OK) uw_error(ctx, FATAL, \"Error preparing statement: %s\\n%s\", dml, sqlite3_errmsg(conn->conn));",
|
adamc@885
|
678 newline,
|
adamc@885
|
679 newline,
|
adamc@885
|
680 string "uw_push_cleanup(ctx, (void (*)(void *))sqlite3_finalize, stmt);",
|
adamc@885
|
681 newline,
|
adamc@885
|
682 newline,
|
adamc@885
|
683
|
adamc@885
|
684 dmlCommon {loc = loc, dml = string "dml"},
|
adamc@885
|
685
|
adamc@885
|
686 string "uw_pop_cleanup(ctx);",
|
adamc@885
|
687 newline]
|
adamc@885
|
688
|
adamc@885
|
689 fun dmlPrepared {loc, id, dml, inputs} =
|
adamc@885
|
690 box [string "uw_conn *conn = uw_get_db(ctx);",
|
adamc@885
|
691 newline,
|
adamc@1014
|
692 p_pre_inputs inputs,
|
adamc@885
|
693 string "sqlite3_stmt *stmt = conn->p",
|
adamc@885
|
694 string (Int.toString id),
|
adamc@885
|
695 string ";",
|
adamc@885
|
696 newline,
|
adamc@885
|
697 newline,
|
adamc@885
|
698
|
adamc@885
|
699 string "if (stmt == NULL) {",
|
adamc@885
|
700 newline,
|
adamc@885
|
701 box [string "if (sqlite3_prepare_v2(conn->conn, \"",
|
adamc@885
|
702 string (String.toString dml),
|
adamc@885
|
703 string "\", -1, &stmt, NULL) != SQLITE_OK) uw_error(ctx, FATAL, \"Error preparing statement: ",
|
adamc@885
|
704 string (String.toString dml),
|
adamc@885
|
705 string "\\n%s\", sqlite3_errmsg(conn->conn));",
|
adamc@885
|
706 newline,
|
adamc@885
|
707 string "conn->p",
|
adamc@885
|
708 string (Int.toString id),
|
adamc@885
|
709 string " = stmt;",
|
adamc@885
|
710 newline],
|
adamc@885
|
711 string "}",
|
adamc@885
|
712 newline,
|
adamc@885
|
713
|
adamc@885
|
714 string "uw_push_cleanup(ctx, (void (*)(void *))sqlite3_clear_bindings, stmt);",
|
adamc@885
|
715 newline,
|
adamc@885
|
716 string "uw_push_cleanup(ctx, (void (*)(void *))sqlite3_reset, stmt);",
|
adamc@885
|
717 newline,
|
adamc@885
|
718
|
adamc@885
|
719 p_inputs loc inputs,
|
adamc@885
|
720 newline,
|
adamc@885
|
721
|
adamc@885
|
722 dmlCommon {loc = loc, dml = box [string "\"",
|
adamc@885
|
723 string (String.toString dml),
|
adamc@885
|
724 string "\""]},
|
adamc@885
|
725
|
adamc@885
|
726 string "uw_pop_cleanup(ctx);",
|
adamc@885
|
727 newline,
|
adamc@885
|
728 string "uw_pop_cleanup(ctx);",
|
adamc@885
|
729 newline]
|
adamc@885
|
730
|
adamc@885
|
731 fun nextval {loc, seqE, seqName} =
|
adamc@885
|
732 box [string "uw_conn *conn = uw_get_db(ctx);",
|
adamc@885
|
733 newline,
|
adamc@885
|
734 string "char *insert = ",
|
adamc@885
|
735 case seqName of
|
adamc@886
|
736 SOME s => string ("\"INSERT INTO " ^ s ^ " VALUES (NULL)\"")
|
adamc@885
|
737 | NONE => box [string "uw_Basis_strcat(ctx, \"INSERT INTO \", uw_Basis_strcat(ctx, ",
|
adamc@885
|
738 seqE,
|
adamc@885
|
739 string ", \" VALUES ()\"))"],
|
adamc@885
|
740 string ";",
|
adamc@885
|
741 newline,
|
adamc@885
|
742 string "char *delete = ",
|
adamc@885
|
743 case seqName of
|
adamc@885
|
744 SOME s => string ("\"DELETE FROM " ^ s ^ "\"")
|
adamc@885
|
745 | NONE => box [string "uw_Basis_strcat(ctx, \"DELETE FROM \", ",
|
adamc@885
|
746 seqE,
|
adamc@885
|
747 string ")"],
|
adamc@885
|
748 string ";",
|
adamc@885
|
749 newline,
|
adamc@885
|
750 newline,
|
adamc@885
|
751
|
adamc@886
|
752 string "if (sqlite3_exec(conn->conn, insert, NULL, NULL, NULL) != SQLITE_OK) uw_error(ctx, FATAL, \"'nextval' INSERT failed: %s\", sqlite3_errmsg(conn->conn));",
|
adamc@885
|
753 newline,
|
adamc@885
|
754 string "n = sqlite3_last_insert_rowid(conn->conn);",
|
adamc@885
|
755 newline,
|
adamc@886
|
756 string "if (sqlite3_exec(conn->conn, delete, NULL, NULL, NULL) != SQLITE_OK) uw_error(ctx, FATAL, \"'nextval' DELETE failed: %s\", sqlite3_errmsg(conn->conn));",
|
adamc@885
|
757 newline]
|
adamc@885
|
758
|
adamc@885
|
759 fun nextvalPrepared _ = raise Fail "SQLite.nextvalPrepared called"
|
adamc@1073
|
760 fun setval _ = raise Fail "SQLite.setval called"
|
adamc@885
|
761
|
adamc@885
|
762 fun sqlifyString s = "'" ^ String.translate (fn #"'" => "''"
|
adamc@885
|
763 | ch =>
|
adamc@885
|
764 if Char.isPrint ch then
|
adamc@885
|
765 str ch
|
adamc@885
|
766 else
|
adamc@885
|
767 (ErrorMsg.error
|
adamc@885
|
768 "Non-printing character found in SQL string literal";
|
adamc@885
|
769 ""))
|
adamc@885
|
770 (String.toString s) ^ "'"
|
adamc@885
|
771
|
adamc@885
|
772 fun p_cast (s, _) = s
|
adamc@885
|
773
|
adamc@885
|
774 fun p_blank _ = "?"
|
adamc@885
|
775
|
adamc@885
|
776 val () = addDbms {name = "sqlite",
|
adamc@885
|
777 header = "sqlite3.h",
|
adamc@885
|
778 link = "-lsqlite3",
|
adamc@885
|
779 init = init,
|
adamc@885
|
780 p_sql_type = p_sql_type,
|
adamc@885
|
781 query = query,
|
adamc@885
|
782 queryPrepared = queryPrepared,
|
adamc@885
|
783 dml = dml,
|
adamc@885
|
784 dmlPrepared = dmlPrepared,
|
adamc@885
|
785 nextval = nextval,
|
adamc@885
|
786 nextvalPrepared = nextvalPrepared,
|
adamc@1073
|
787 setval = setval,
|
adamc@885
|
788 sqlifyString = sqlifyString,
|
adamc@885
|
789 p_cast = p_cast,
|
adamc@885
|
790 p_blank = p_blank,
|
adamc@885
|
791 supportsDeleteAs = false,
|
adamc@886
|
792 supportsUpdateAs = false,
|
adamc@886
|
793 createSequence = fn s => "CREATE TABLE " ^ s ^ " (id INTEGER PRIMARY KEY AUTOINCREMENT)",
|
adamc@885
|
794 textKeysNeedLengths = false,
|
adamc@885
|
795 supportsNextval = false,
|
adamc@885
|
796 supportsNestedPrepared = false,
|
adamc@890
|
797 sqlPrefix = "",
|
adamc@1014
|
798 supportsOctetLength = false,
|
adamc@1014
|
799 trueString = "1",
|
adamc@1014
|
800 falseString = "0"}
|
adamc@885
|
801
|
adamc@885
|
802 end
|