forked from ermine/sulci
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sqlgg_sqlite3.ml
83 lines (65 loc) · 1.84 KB
/
sqlgg_sqlite3.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
(** sqlgg + ocaml + sqlite3 *)
open Printf
module S = Sqlite3
type statement = S.stmt
type connection = S.db
type params = statement
type row = statement
type result = unit
type num = int64
type text = string
type any = string
exception Oops of string
let get_column_Int stmt index =
match S.column stmt index with
| S.Data.INT i -> i
| _ -> raise (Oops (sprintf "get_column_Int %u" index))
let get_column_Text stmt index =
let x = S.column stmt index in
S.Data.to_string x
let get_column_Any = get_column_Text
let test_ok rc =
if rc <> S.Rc.OK then
raise (Oops (sprintf "test_ok %s" (S.Rc.to_string rc)))
let bind_param d stmt index =
let rc = S.bind stmt (index+1) d in
test_ok rc
let start_params stmt _ = stmt
let finish_params _ = ()
let set_param_null = bind_param S.Data.NULL
let set_param_Text stmt index v = bind_param (S.Data.TEXT v) stmt index
let set_param_Any = set_param_Text
let set_param_Int stmt index v = bind_param (S.Data.INT v) stmt index
let no_params _ = ()
let finally final f x =
let r =
try f x with exn -> final (); raise exn
in
final ();
r
let select db sql set_params callback =
let stmt = S.prepare db sql in
finally (fun () -> test_ok (S.finalize stmt))
(fun () ->
set_params stmt;
while S.Rc.ROW = S.step stmt do
callback stmt
done) ()
let execute db sql set_params =
let stmt = S.prepare db sql in
finally (fun () -> test_ok (S.finalize stmt))
(fun () ->
set_params stmt;
let rc = S.step stmt in
if rc <> S.Rc.DONE then raise (Oops (sprintf "execute : %s" sql));
0L (* FIXME sqlite3_changes() *)
) ()
let select1 db sql set_params callback =
let stmt = S.prepare db sql in
finally (fun () -> test_ok (S.finalize stmt))
(fun () ->
set_params stmt;
if S.Rc.ROW = S.step stmt then
Some (callback stmt)
else
None) ()