]> matita.cs.unibo.it Git - helm.git/blob - components/hmysql/hSqlite3.ml
tagged 0.5.0-rc1
[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
33 let debug = false
34 let debug_print = 
35   if debug then prerr_endline else (fun _ ->())
36 ;;
37
38 type result = Sqlite3.row list
39 type dbd = Sqlite3.db option
40
41 type error_code = 
42  | OK
43  | Table_exists_error
44  | Dup_keyname
45  | No_such_table
46  | No_such_index
47  | Bad_table_error
48  | GENERIC_ERROR of string
49
50 exception Error of string
51
52 let profiler = HExtlib.profile "Sqlite3"
53
54 let quick_connect 
55   is_library
56   ?(host = Helm_registry.get "matita.basedir") 
57   ?(database = "sqlite") ?port ?password ?user () 
58
59   let username = Helm_registry.get "user.name" in
60   let host_mangled = Pcre.replace ~pat:"[/ .]" ~templ:"_" host in
61   let find_db _ = 
62     let base = "matita.db." ^ username ^ "." ^ host_mangled ^ "." in
63     let root = "/dev/shm/" in
64     let files = HExtlib.find ~test:(Pcre.pmatch ~pat:(base^"[0-9]+")) root in
65     let rec aux = function
66       | [] -> 
67          debug_print ("HSqlite3: no valid db files found in memory");
68          let name = root ^ base ^ string_of_int (Unix.getpid ()) in
69          debug_print ("HSqlite3: memory db file name: "^name);
70          name, true
71       | x::tl -> 
72          debug_print ("HSqlite3: found a .db in memory: " ^ x);
73          match Array.to_list (Pcre.extract ~pat:"\\.([0-9]+)$" x) with
74          | [] | _::_::_::_ -> assert false
75          | [_;p] when HExtlib.is_dir ("/proc/" ^ p) -> 
76             debug_print ("HSqlite3: found valid db file: " ^ x);
77             x, false
78          | _ ->
79             HLog.warn ("HSqlite3: dead process db file found: " ^ x);
80             HLog.warn ("HSqlite3: removing: " ^ x);
81             ignore (Sys.command ("rm " ^ x));
82             aux tl
83     in
84       aux files
85   in
86   let db_name = host ^ "/" ^ database in
87   let db_to_open =
88     if HExtlib.is_dir "/dev/shm/" && HExtlib.writable_dir "/dev/shm/" 
89        && (not is_library) 
90     then (
91       let tmp_db_name, first_time = find_db () in      
92       let cp_to_ram_cmd = "cp " ^ db_name ^ " " ^ tmp_db_name in
93       if first_time then 
94         if HExtlib.is_regular db_name then ignore (Sys.command cp_to_ram_cmd)
95         else debug_print ("HSqlite3: no initial db: " ^ db_name)
96       else debug_print "HSqlite3: not copying the db, already in memory";
97       let mv_to_disk_cmd _ = 
98         if first_time then ignore (Sys.command ("mv "^tmp_db_name^" "^db_name)) 
99         else debug_print "HSqlite3: not copying back the db"
100       in
101       at_exit mv_to_disk_cmd;
102       tmp_db_name)
103     else
104       db_name
105   in
106   HExtlib.mkdir (Filename.dirname db_to_open);
107   let db = Sqlite3.db_open db_to_open in
108   (* attach the REGEX function *)
109   Sqlite3.create_fun2 db "REGEXP"
110       (fun s rex -> 
111         try
112          match rex, s with
113          | Sqlite3.Data.TEXT rex, Sqlite3.Data.BLOB s
114          | Sqlite3.Data.TEXT rex, Sqlite3.Data.TEXT s ->
115              let r = Str.regexp rex in
116              if Str.string_match r s 0 then 
117                Sqlite3.Data.INT 1L 
118              else 
119                Sqlite3.Data.INT 0L
120          | _ -> raise (Error "wrong types to 'REGEXP'")
121         with exn -> HLog.error (Printexc.to_string exn); raise exn);
122   Some db
123 ;;
124
125 let disconnect db =
126   match db with
127   | None -> ()
128   | Some db -> 
129       let b = Sqlite3.db_close db in
130       if b=false then debug_print "No Closed DataBase"
131 ;;
132
133 (* XXX hack, sqlite has a print "%q" that should be used, but is not bound *)
134 let escape s = 
135   let s_escaped = Pcre.replace ~pat:"'" ~templ:"''" s in
136   (*let s_escaped = Pcre.replace ~pat:"([^'])'([^'])" ~templ:"$1''$2" s in*)
137   debug_print s;
138   debug_print s_escaped;
139   s_escaped
140 ;;
141
142 let string_of_rc = function
143   |Sqlite3.Rc.OK -> "Sqlite3.Rc.OK" 
144   |Sqlite3.Rc.ERROR -> "Sqlite3.Rc.ERROR" 
145   |Sqlite3.Rc.INTERNAL -> "Sqlite3.Rc.INTERNAL" 
146   |Sqlite3.Rc.PERM -> "Sqlite3.Rc.PERM" 
147   |Sqlite3.Rc.ABORT -> "Sqlite3.Rc.ABORT" 
148   |Sqlite3.Rc.BUSY -> "Sqlite3.Rc.BUSY" 
149   |Sqlite3.Rc.LOCKED -> "Sqlite3.Rc.LOCKED" 
150   |Sqlite3.Rc.NOMEM -> "Sqlite3.Rc.NOMEM" 
151   |Sqlite3.Rc.READONLY -> "Sqlite3.Rc.READONLY" 
152   |Sqlite3.Rc.INTERRUPT -> "Sqlite3.Rc.INTERRUPT" 
153   |Sqlite3.Rc.IOERR -> "Sqlite3.Rc.IOERR" 
154   |Sqlite3.Rc.CORRUPT -> "Sqlite3.Rc.CORRUPT" 
155   |Sqlite3.Rc.NOTFOUND -> "Sqlite3.Rc.NOTFOUND" 
156   |Sqlite3.Rc.FULL -> "Sqlite3.Rc.FULL" 
157   |Sqlite3.Rc.CANTOPEN -> "Sqlite3.Rc.CANTOPEN" 
158   |Sqlite3.Rc.PROTOCOL -> "Sqlite3.Rc.PROTOCOL" 
159   |Sqlite3.Rc.EMPTY -> "Sqlite3.Rc.EMPTY" 
160   |Sqlite3.Rc.SCHEMA -> "Sqlite3.Rc.SCHEMA" 
161   |Sqlite3.Rc.TOOBIG -> "Sqlite3.Rc.TOOBIG" 
162   |Sqlite3.Rc.CONSTRAINT -> "Sqlite3.Rc.CONSTRAINT" 
163   |Sqlite3.Rc.MISMATCH -> "Sqlite3.Rc.MISMATCH" 
164   |Sqlite3.Rc.MISUSE -> "Sqlite3.Rc.MISUSE" 
165   |Sqlite3.Rc.NOFLS -> "Sqlite3.Rc.NOFLS" 
166   |Sqlite3.Rc.AUTH -> "Sqlite3.Rc.AUTH" 
167   |Sqlite3.Rc.FORMAT -> "Sqlite3.Rc.FORMAT" 
168   |Sqlite3.Rc.RANGE -> "Sqlite3.Rc.RANGE" 
169   |Sqlite3.Rc.NOTADB -> "Sqlite3.Rc.NOTADB" 
170   |Sqlite3.Rc.ROW -> "Sqlite3.Rc.ROW" 
171   |Sqlite3.Rc.DONE -> "Sqlite3.Rc.DONE" 
172   |Sqlite3.Rc.UNKNOWN n -> 
173     "Sqlite3.Rc.UNKNOWN " ^ string_of_int (Sqlite3.Rc.int_of_unknown n)
174 ;;
175
176 let pp_rc rc = debug_print (string_of_rc rc);;
177
178 let exec s db =
179  debug_print s;
180   let stored_result = ref [] in
181   let store row =
182     stored_result := row :: !stored_result
183   in
184   match db with
185   | None -> [] 
186   | Some db ->  
187       let rc = 
188         profiler.HExtlib.profile (Sqlite3.exec_no_headers db ~cb:store) s 
189       in
190       match rc with
191       | Sqlite3.Rc.OK -> !stored_result
192       | _ -> raise (Error (string_of_rc rc ^ ": " ^ Sqlite3.errmsg db ))
193 ;;
194
195 let rec map res ~f = 
196   let map f = List.map f res in
197   profiler.HExtlib.profile map f
198 ;;
199
200 let iter res ~f =
201   let iter f = List.iter f res in
202   profiler.HExtlib.profile iter f
203 ;;
204
205 let errno = function 
206   | None -> OK
207   | Some db -> 
208       match Sqlite3.errcode db with
209       |Sqlite3.Rc.OK -> OK
210       |Sqlite3.Rc.ERROR ->
211          let errmsg = (Sqlite3.errmsg db) in
212          if Pcre.pmatch errmsg ~pat:"^table .* already exists" then
213            Table_exists_error
214          else
215          if Pcre.pmatch errmsg ~pat:"^index .* already exists" then Dup_keyname
216          else if Pcre.pmatch errmsg ~pat:"^no such table: .*" then No_such_table
217          else if Pcre.pmatch errmsg ~pat:"^no such index: .*" then No_such_index
218          else GENERIC_ERROR errmsg
219       |Sqlite3.Rc.INTERNAL |Sqlite3.Rc.PERM |Sqlite3.Rc.ABORT
220       |Sqlite3.Rc.BUSY |Sqlite3.Rc.LOCKED |Sqlite3.Rc.NOMEM
221       |Sqlite3.Rc.READONLY |Sqlite3.Rc.INTERRUPT |Sqlite3.Rc.IOERR
222       |Sqlite3.Rc.CORRUPT |Sqlite3.Rc.NOTFOUND |Sqlite3.Rc.FULL
223       |Sqlite3.Rc.CANTOPEN |Sqlite3.Rc.PROTOCOL |Sqlite3.Rc.EMPTY 
224       |Sqlite3.Rc.SCHEMA |Sqlite3.Rc.TOOBIG |Sqlite3.Rc.CONSTRAINT
225       |Sqlite3.Rc.MISMATCH |Sqlite3.Rc.MISUSE |Sqlite3.Rc.NOFLS
226       |Sqlite3.Rc.AUTH |Sqlite3.Rc.FORMAT |Sqlite3.Rc.RANGE
227       |Sqlite3.Rc.NOTADB |Sqlite3.Rc.ROW |Sqlite3.Rc.DONE
228       |Sqlite3.Rc.UNKNOWN _ -> GENERIC_ERROR "Sqlite3_generic_error"
229 ;;
230
231 let isMysql = false
232
233 let escape_string_for_like = ("ESCAPE \"\\\"" : ('a,'b,'c,'a) format4);;