Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

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