]> matita.cs.unibo.it Git - helm.git/blob - components/hmysql/hSqlite3.ml
added some hacks for the debian package
[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
44 exception Error
45
46 let prerr_endline s = ()(*HLog.debug s;;*)
47
48 let profiler = HExtlib.profile "Sqlite3"
49
50 let quick_connect ?host ?(database = "sqlite") ?port ?password ?user () = 
51   let username = Helm_registry.get "user.name" in
52   let tmp_db_name =
53     if HExtlib.is_dir "/dev/shm/" && HExtlib.writable_dir "/dev/shm/" then
54       (HLog.debug "using /dev/shm to store the working copy of the db";
55       "/dev/shm/matita.db." ^ username ^ "." ^ string_of_int (Unix.getpid ()))
56     else
57       (HLog.debug "/dev/shm not available";
58       Helm_registry.get "matita.basedir" ^ "/matita.db." ^ username ^ "." ^
59         string_of_int (Unix.getpid ()))
60   in
61   let db_name = Helm_registry.get "matita.basedir" ^ "/" ^ database ^ ".db" in
62   (* XXX big hack XXX *)
63   let standard_db_name = "/usr/share/matita/metadata.db" in
64   let cp_to_ram_cmd = 
65     if HExtlib.is_regular db_name then
66       "cp " ^ db_name ^ " " ^ tmp_db_name 
67     else
68       (* we start from the standard library *)
69       "cp " ^ standard_db_name ^ " " ^ tmp_db_name
70   in
71   ignore (Sys.command cp_to_ram_cmd);
72   let mv_to_disk_cmd _ = 
73     ignore (Sys.command ("mv " ^ tmp_db_name ^ " " ^ db_name)) 
74   in
75   at_exit mv_to_disk_cmd;
76   Some (Sqlite3.db_open tmp_db_name)
77 ;;
78
79 let disconnect db =
80   match db with
81   | None -> ()
82   | Some db -> 
83       let b = Sqlite3.db_close db in
84       if b=false then prerr_endline "No Closed DataBase"
85 ;;
86
87 (* XXX hack, sqlite has a print "%q" that should be used, but is not bound *)
88 let escape s = Pcre.replace ~pat:"([^'])'([^'])" ~templ:"$1''$2" s
89
90 let exec db s =
91  prerr_endline s;
92   let stored_result = ref [] in
93   let store row =
94     stored_result := row :: !stored_result
95   in
96   match db with
97   | None -> [] 
98   | Some db ->  
99       let rc = 
100         profiler.HExtlib.profile (Sqlite3.exec_no_headers db ~cb:store) s 
101       in
102       match rc with
103       | Sqlite3.Rc.OK -> !stored_result
104       | _ -> raise Error
105 ;;
106
107 let rec map res ~f = 
108   let map f = List.map f res in
109   profiler.HExtlib.profile map f
110 ;;
111
112 let iter res ~f =
113   let iter f = List.iter f res in
114   profiler.HExtlib.profile iter f
115 ;;
116
117 let pp_rc = function
118   |Sqlite3.Rc.OK -> prerr_endline "Sqlite3.Rc.OK" 
119   |Sqlite3.Rc.ERROR -> prerr_endline "Sqlite3.Rc.ERROR" 
120   |Sqlite3.Rc.INTERNAL -> prerr_endline "Sqlite3.Rc.INTERNAL" 
121   |Sqlite3.Rc.PERM -> prerr_endline "Sqlite3.Rc.PERM" 
122   |Sqlite3.Rc.ABORT -> prerr_endline "Sqlite3.Rc.ABORT" 
123   |Sqlite3.Rc.BUSY -> prerr_endline "Sqlite3.Rc.BUSY" 
124   |Sqlite3.Rc.LOCKED -> prerr_endline "Sqlite3.Rc.LOCKED" 
125   |Sqlite3.Rc.NOMEM -> prerr_endline "Sqlite3.Rc.NOMEM" 
126   |Sqlite3.Rc.READONLY -> prerr_endline "Sqlite3.Rc.READONLY" 
127   |Sqlite3.Rc.INTERRUPT -> prerr_endline "Sqlite3.Rc.INTERRUPT" 
128   |Sqlite3.Rc.IOERR -> prerr_endline "Sqlite3.Rc.IOERR" 
129   |Sqlite3.Rc.CORRUPT -> prerr_endline "Sqlite3.Rc.CORRUPT" 
130   |Sqlite3.Rc.NOTFOUND -> prerr_endline "Sqlite3.Rc.NOTFOUND" 
131   |Sqlite3.Rc.FULL -> prerr_endline "Sqlite3.Rc.FULL" 
132   |Sqlite3.Rc.CANTOPEN -> prerr_endline "Sqlite3.Rc.CANTOPEN" 
133   |Sqlite3.Rc.PROTOCOL -> prerr_endline "Sqlite3.Rc.PROTOCOL" 
134   |Sqlite3.Rc.EMPTY -> prerr_endline "Sqlite3.Rc.EMPTY" 
135   |Sqlite3.Rc.SCHEMA -> prerr_endline "Sqlite3.Rc.SCHEMA" 
136   |Sqlite3.Rc.TOOBIG -> prerr_endline "Sqlite3.Rc.TOOBIG" 
137   |Sqlite3.Rc.CONSTRAINT -> prerr_endline "Sqlite3.Rc.CONSTRAINT" 
138   |Sqlite3.Rc.MISMATCH -> prerr_endline "Sqlite3.Rc.MISMATCH" 
139   |Sqlite3.Rc.MISUSE -> prerr_endline "Sqlite3.Rc.MISUSE" 
140   |Sqlite3.Rc.NOFLS -> prerr_endline "Sqlite3.Rc.NOFLS" 
141   |Sqlite3.Rc.AUTH -> prerr_endline "Sqlite3.Rc.AUTH" 
142   |Sqlite3.Rc.FORMAT -> prerr_endline "Sqlite3.Rc.FORMAT" 
143   |Sqlite3.Rc.RANGE -> prerr_endline "Sqlite3.Rc.RANGE" 
144   |Sqlite3.Rc.NOTADB -> prerr_endline "Sqlite3.Rc.NOTADB" 
145   |Sqlite3.Rc.ROW -> prerr_endline "Sqlite3.Rc.ROW" 
146   |Sqlite3.Rc.DONE -> prerr_endline "Sqlite3.Rc.DONE" 
147   |Sqlite3.Rc.UNKNOWN n -> 
148      prerr_endline 
149        ("Sqlite3.Rc.UNKNOWN " ^ string_of_int (Sqlite3.Rc.int_of_unknown n))
150 ;;
151
152 let errno = function 
153   | None -> OK
154   | Some db -> 
155       match Sqlite3.errcode db with
156       |Sqlite3.Rc.OK -> OK
157       |Sqlite3.Rc.ERROR ->
158          let errmsg = (Sqlite3.errmsg db) in
159          if Pcre.pmatch errmsg ~pat:"^table .* already exists" then
160            Table_exists_error
161          else
162          if Pcre.pmatch errmsg ~pat:"^index .* already exists" then Dup_keyname
163          else if Pcre.pmatch errmsg ~pat:"^no such table: .*" then No_such_table
164          else if Pcre.pmatch errmsg ~pat:"^no such index: .*" then No_such_index
165          else GENERIC_ERROR errmsg
166       |Sqlite3.Rc.INTERNAL |Sqlite3.Rc.PERM |Sqlite3.Rc.ABORT
167       |Sqlite3.Rc.BUSY |Sqlite3.Rc.LOCKED |Sqlite3.Rc.NOMEM
168       |Sqlite3.Rc.READONLY |Sqlite3.Rc.INTERRUPT |Sqlite3.Rc.IOERR
169       |Sqlite3.Rc.CORRUPT |Sqlite3.Rc.NOTFOUND |Sqlite3.Rc.FULL
170       |Sqlite3.Rc.CANTOPEN |Sqlite3.Rc.PROTOCOL |Sqlite3.Rc.EMPTY 
171       |Sqlite3.Rc.SCHEMA |Sqlite3.Rc.TOOBIG |Sqlite3.Rc.CONSTRAINT
172       |Sqlite3.Rc.MISMATCH |Sqlite3.Rc.MISUSE |Sqlite3.Rc.NOFLS
173       |Sqlite3.Rc.AUTH |Sqlite3.Rc.FORMAT |Sqlite3.Rc.RANGE
174       |Sqlite3.Rc.NOTADB |Sqlite3.Rc.ROW |Sqlite3.Rc.DONE
175       |Sqlite3.Rc.UNKNOWN _ -> GENERIC_ERROR "Sqlite3_generic_error"
176 ;;
177
178 let isMysql = false