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