toc
[sqlgg.git] / sqlgg_sqlite3.ml
blobc7fe9ac91671e9c6dc3d62addcaf8bf2243d680f
1 (** sqlgg + ocaml + sqlite3 *)
3 module S = Sqlite3
5 type statement = S.stmt
6 type connection = S.db
8 type num = int64
9 type text = string
10 type any = string
12 let get_column_Int stmt index =
13 match S.column stmt index with
14 | S.Data.INT i -> i
15 | x -> try Int64.of_string (S.Data.to_string x) with _ -> 0L (* or better fail ? *)
17 let get_column_Text stmt index =
18 let x = S.column stmt index in
19 S.Data.to_string x
21 let test_ok rc = let ok = rc = S.Rc.OK in assert ok; ok
23 let bind_param d stmt index =
24 let rc = S.bind stmt (index+1) d in
25 test_ok rc
27 let set_param_null = bind_param S.Data.NULL
28 let set_param_Text stmt index v = bind_param (S.Data.TEXT v) stmt index
29 let set_param_Any = set_param_Text
30 let set_param_Int stmt index v = bind_param (S.Data.INT v) stmt index
32 let select_exn db sql callback set_params =
33 let stmt = S.prepare db sql in
34 set_params stmt;
36 while S.Rc.ROW = S.step stmt do
37 callback stmt
38 done;
40 test_ok (S.finalize stmt)
42 let execute_exn db sql set_params =
43 let stmt = S.prepare db sql in
44 set_params stmt;
46 let rc = S.step stmt in
47 assert (S.Rc.DONE = rc);
49 test_ok (S.finalize stmt)
51 (** may @raise exceptions other than Error *)
52 let select db sql cb p =
53 try
54 select_exn db sql cb p
55 with
56 | S.Error _ -> false
58 let execute db sql p =
59 try
60 execute_exn db sql p
61 with
62 | S.Error _ -> false