]> matita.cs.unibo.it Git - helm.git/blob - components/hmysql/hSqlite3.ml
added alternative implementation for hMysql relying
[helm.git] / components / hmysql / hSqlite3.ml
1 (* Copyright (C) 2005, HELM Team.
2  * 
3  * This file is part of HELM, an Hypertextual, Electronic
4  * Library of Mathematics, developed at the Computer Science
5  * Department, University of Bologna, Italy.
6  * 
7  * HELM is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  * 
12  * HELM is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with HELM; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
20  * MA  02111-1307, USA.
21  * 
22  * For details, see the HELM World-Wide-Web page,
23  * http://cs.unibo.it/helm/.
24  *)
25
26 (* $Id: hMysql.ml 5769 2006-01-08 18:00:22Z sacerdot $ *)
27
28 (*
29 type result = Mysql.result option
30 *)
31
32 type result = Sqlite3.row list
33 type dbd = Sqlite3.db option
34
35 type error_code = 
36  | OK
37  | Table_exists_error
38  | Dup_keyname
39  | No_such_table
40  | No_such_index
41  | Bad_table_error
42  | GENERIC_ERROR of string
43 exception Error
44
45 let prerr_endline s = ()(*HLog.debug s;;*)
46
47 let profiler = HExtlib.profile "Sqlite3"
48
49 let quick_connect ?host ?(database = "sqlite") ?port ?password ?user () = 
50   let username = Helm_registry.get "user.name" in
51   let ram_db_name =
52     "/dev/shm/matita.db." ^ username ^ "." ^ string_of_int (Unix.getpid ())  
53   in
54   let db_name = Helm_registry.get "matita.basedir" ^ "/" ^ database ^ ".db" in
55   let cp_to_ram_cmd = "cp " ^ db_name ^ " " ^ ram_db_name in
56   ignore (Sys.command cp_to_ram_cmd);
57   let mv_to_disk_cmd _ = ignore (Sys.command ("mv " ^ ram_db_name ^ " " ^ db_name)) in
58   at_exit mv_to_disk_cmd;
59   Some (Sqlite3.db_open ram_db_name)
60 ;;
61
62 let disconnect db =
63   match db with
64   | None -> ()
65   | Some db -> 
66         let b = Sqlite3.db_close db in
67         if b=false then prerr_endline "No Closed DataBase"
68 ;;
69
70 let escape s = s
71
72 let exec db s =
73  prerr_endline s;
74   let stored_result = ref [] in
75   let store row =
76     stored_result := row :: !stored_result
77   in
78   match db with
79   | None -> [] 
80   | Some db ->  
81       let rc = 
82         profiler.HExtlib.profile (Sqlite3.exec_no_headers db ~cb:store) s 
83       in
84       match rc with
85       | Sqlite3.Rc.OK -> !stored_result
86       | _ -> raise Error
87 ;;
88
89 let rec map res ~f = 
90   let map f = List.map f res in
91   profiler.HExtlib.profile map f
92
93 let iter res ~f =
94   let iter f = List.iter f res in
95   profiler.HExtlib.profile iter f
96
97 let pp_rc = function
98         |Sqlite3.Rc.OK -> prerr_endline "Sqlite3.Rc.OK" 
99         |Sqlite3.Rc.ERROR -> prerr_endline "Sqlite3.Rc.ERROR" 
100         |Sqlite3.Rc.INTERNAL -> prerr_endline "Sqlite3.Rc.INTERNAL" 
101         |Sqlite3.Rc.PERM -> prerr_endline "Sqlite3.Rc.PERM" 
102         |Sqlite3.Rc.ABORT -> prerr_endline "Sqlite3.Rc.ABORT" 
103         |Sqlite3.Rc.BUSY -> prerr_endline "Sqlite3.Rc.BUSY" 
104         |Sqlite3.Rc.LOCKED -> prerr_endline "Sqlite3.Rc.LOCKED" 
105         |Sqlite3.Rc.NOMEM -> prerr_endline "Sqlite3.Rc.NOMEM" 
106         |Sqlite3.Rc.READONLY -> prerr_endline "Sqlite3.Rc.READONLY" 
107         |Sqlite3.Rc.INTERRUPT -> prerr_endline "Sqlite3.Rc.INTERRUPT" 
108         |Sqlite3.Rc.IOERR -> prerr_endline "Sqlite3.Rc.IOERR" 
109         |Sqlite3.Rc.CORRUPT -> prerr_endline "Sqlite3.Rc.CORRUPT" 
110         |Sqlite3.Rc.NOTFOUND -> prerr_endline "Sqlite3.Rc.NOTFOUND" 
111         |Sqlite3.Rc.FULL -> prerr_endline "Sqlite3.Rc.FULL" 
112         |Sqlite3.Rc.CANTOPEN -> prerr_endline "Sqlite3.Rc.CANTOPEN" 
113         |Sqlite3.Rc.PROTOCOL -> prerr_endline "Sqlite3.Rc.PROTOCOL" 
114         |Sqlite3.Rc.EMPTY -> prerr_endline "Sqlite3.Rc.EMPTY" 
115         |Sqlite3.Rc.SCHEMA -> prerr_endline "Sqlite3.Rc.SCHEMA" 
116         |Sqlite3.Rc.TOOBIG -> prerr_endline "Sqlite3.Rc.TOOBIG" 
117         |Sqlite3.Rc.CONSTRAINT -> prerr_endline "Sqlite3.Rc.CONSTRAINT" 
118         |Sqlite3.Rc.MISMATCH -> prerr_endline "Sqlite3.Rc.MISMATCH" 
119         |Sqlite3.Rc.MISUSE -> prerr_endline "Sqlite3.Rc.MISUSE" 
120         |Sqlite3.Rc.NOFLS -> prerr_endline "Sqlite3.Rc.NOFLS" 
121         |Sqlite3.Rc.AUTH -> prerr_endline "Sqlite3.Rc.AUTH" 
122         |Sqlite3.Rc.FORMAT -> prerr_endline "Sqlite3.Rc.FORMAT" 
123         |Sqlite3.Rc.RANGE -> prerr_endline "Sqlite3.Rc.RANGE" 
124         |Sqlite3.Rc.NOTADB -> prerr_endline "Sqlite3.Rc.NOTADB" 
125         |Sqlite3.Rc.ROW -> prerr_endline "Sqlite3.Rc.ROW" 
126         |Sqlite3.Rc.DONE -> prerr_endline "Sqlite3.Rc.DONE" 
127         |Sqlite3.Rc.UNKNOWN n -> 
128         prerr_endline ("Sqlite3.Rc.UNKNOWN " ^ string_of_int (Sqlite3.Rc.int_of_unknown n))
129 ;;
130
131 let errno = function 
132   | None -> OK
133   | Some db -> 
134       (* pp_rc (Sqlite3.errcode db);  *)
135       (* prerr_endline(Sqlite3.errmsg db); *)
136       match Sqlite3.errcode db with
137       |Sqlite3.Rc.OK -> OK
138       |Sqlite3.Rc.ERROR ->
139          if Pcre.pmatch(Sqlite3.errmsg db) ~pat:"^table .* already exists" then
140            Table_exists_error
141          else
142          if Pcre.pmatch(Sqlite3.errmsg db) ~pat:"^index .* already exists" then
143            Dup_keyname
144          else
145          if Pcre.pmatch(Sqlite3.errmsg db) ~pat:"^no such table: .*" then
146            No_such_table
147          else
148          if Pcre.pmatch(Sqlite3.errmsg db) ~pat:"^no such index: .*" then
149            No_such_index
150          else   
151          GENERIC_ERROR "ciao"
152
153       |Sqlite3.Rc.INTERNAL
154       |Sqlite3.Rc.PERM
155       |Sqlite3.Rc.ABORT
156       |Sqlite3.Rc.BUSY
157       |Sqlite3.Rc.LOCKED
158       |Sqlite3.Rc.NOMEM
159       |Sqlite3.Rc.READONLY
160       |Sqlite3.Rc.INTERRUPT
161       |Sqlite3.Rc.IOERR
162       |Sqlite3.Rc.CORRUPT
163       |Sqlite3.Rc.NOTFOUND
164       |Sqlite3.Rc.FULL
165       |Sqlite3.Rc.CANTOPEN
166       |Sqlite3.Rc.PROTOCOL
167       |Sqlite3.Rc.EMPTY
168       |Sqlite3.Rc.SCHEMA
169       |Sqlite3.Rc.TOOBIG
170       |Sqlite3.Rc.CONSTRAINT
171       |Sqlite3.Rc.MISMATCH
172       |Sqlite3.Rc.MISUSE
173       |Sqlite3.Rc.NOFLS
174       |Sqlite3.Rc.AUTH
175       |Sqlite3.Rc.FORMAT
176       |Sqlite3.Rc.RANGE
177       |Sqlite3.Rc.NOTADB
178       |Sqlite3.Rc.ROW
179       |Sqlite3.Rc.DONE
180       |Sqlite3.Rc.UNKNOWN _ -> GENERIC_ERROR "Sqlite3_generic_error"
181 ;;
182
183 let isMysql = false