1 (* Copyright (C) 2005, HELM Team.
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.
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.
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.
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,
22 * For details, see the HELM World-Wide-Web page,
23 * http://cs.unibo.it/helm/.
26 (* $Id: hMysql.ml 5769 2006-01-08 18:00:22Z sacerdot $ *)
29 type result = Mysql.result option
32 type result = Sqlite3.row list
33 type dbd = Sqlite3.db option
42 | GENERIC_ERROR of string
44 exception Error of string
46 let prerr_endline s = ()(*HLog.debug s;;*)
48 let profiler = HExtlib.profile "Sqlite3"
50 let quick_connect ?host ?(database = "sqlite") ?port ?password ?user () =
51 let username = Helm_registry.get "user.name" in
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 ()))
57 (HLog.debug "/dev/shm not available";
58 Helm_registry.get "matita.basedir" ^ "/matita.db." ^ username ^ "." ^
59 string_of_int (Unix.getpid ()))
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
65 if HExtlib.is_regular db_name then
66 "cp " ^ db_name ^ " " ^ tmp_db_name
68 (* we start from the standard library *)
69 "cp " ^ standard_db_name ^ " " ^ tmp_db_name
71 ignore (Sys.command cp_to_ram_cmd);
72 let mv_to_disk_cmd _ =
73 ignore (Sys.command ("mv " ^ tmp_db_name ^ " " ^ db_name))
75 at_exit mv_to_disk_cmd;
76 Some (Sqlite3.db_open tmp_db_name)
83 let b = Sqlite3.db_close db in
84 if b=false then prerr_endline "No Closed DataBase"
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
90 let string_of_rc = function
91 |Sqlite3.Rc.OK -> "Sqlite3.Rc.OK"
92 |Sqlite3.Rc.ERROR -> "Sqlite3.Rc.ERROR"
93 |Sqlite3.Rc.INTERNAL -> "Sqlite3.Rc.INTERNAL"
94 |Sqlite3.Rc.PERM -> "Sqlite3.Rc.PERM"
95 |Sqlite3.Rc.ABORT -> "Sqlite3.Rc.ABORT"
96 |Sqlite3.Rc.BUSY -> "Sqlite3.Rc.BUSY"
97 |Sqlite3.Rc.LOCKED -> "Sqlite3.Rc.LOCKED"
98 |Sqlite3.Rc.NOMEM -> "Sqlite3.Rc.NOMEM"
99 |Sqlite3.Rc.READONLY -> "Sqlite3.Rc.READONLY"
100 |Sqlite3.Rc.INTERRUPT -> "Sqlite3.Rc.INTERRUPT"
101 |Sqlite3.Rc.IOERR -> "Sqlite3.Rc.IOERR"
102 |Sqlite3.Rc.CORRUPT -> "Sqlite3.Rc.CORRUPT"
103 |Sqlite3.Rc.NOTFOUND -> "Sqlite3.Rc.NOTFOUND"
104 |Sqlite3.Rc.FULL -> "Sqlite3.Rc.FULL"
105 |Sqlite3.Rc.CANTOPEN -> "Sqlite3.Rc.CANTOPEN"
106 |Sqlite3.Rc.PROTOCOL -> "Sqlite3.Rc.PROTOCOL"
107 |Sqlite3.Rc.EMPTY -> "Sqlite3.Rc.EMPTY"
108 |Sqlite3.Rc.SCHEMA -> "Sqlite3.Rc.SCHEMA"
109 |Sqlite3.Rc.TOOBIG -> "Sqlite3.Rc.TOOBIG"
110 |Sqlite3.Rc.CONSTRAINT -> "Sqlite3.Rc.CONSTRAINT"
111 |Sqlite3.Rc.MISMATCH -> "Sqlite3.Rc.MISMATCH"
112 |Sqlite3.Rc.MISUSE -> "Sqlite3.Rc.MISUSE"
113 |Sqlite3.Rc.NOFLS -> "Sqlite3.Rc.NOFLS"
114 |Sqlite3.Rc.AUTH -> "Sqlite3.Rc.AUTH"
115 |Sqlite3.Rc.FORMAT -> "Sqlite3.Rc.FORMAT"
116 |Sqlite3.Rc.RANGE -> "Sqlite3.Rc.RANGE"
117 |Sqlite3.Rc.NOTADB -> "Sqlite3.Rc.NOTADB"
118 |Sqlite3.Rc.ROW -> "Sqlite3.Rc.ROW"
119 |Sqlite3.Rc.DONE -> "Sqlite3.Rc.DONE"
120 |Sqlite3.Rc.UNKNOWN n ->
121 "Sqlite3.Rc.UNKNOWN " ^ string_of_int (Sqlite3.Rc.int_of_unknown n)
124 let pp_rc rc = prerr_endline (string_of_rc rc);;
128 let stored_result = ref [] in
130 stored_result := row :: !stored_result
136 profiler.HExtlib.profile (Sqlite3.exec_no_headers db ~cb:store) s
139 | Sqlite3.Rc.OK -> !stored_result
140 | _ -> raise (Error (string_of_rc rc))
144 let map f = List.map f res in
145 profiler.HExtlib.profile map f
149 let iter f = List.iter f res in
150 profiler.HExtlib.profile iter f
156 match Sqlite3.errcode db with
159 let errmsg = (Sqlite3.errmsg db) in
160 if Pcre.pmatch errmsg ~pat:"^table .* already exists" then
163 if Pcre.pmatch errmsg ~pat:"^index .* already exists" then Dup_keyname
164 else if Pcre.pmatch errmsg ~pat:"^no such table: .*" then No_such_table
165 else if Pcre.pmatch errmsg ~pat:"^no such index: .*" then No_such_index
166 else GENERIC_ERROR errmsg
167 |Sqlite3.Rc.INTERNAL |Sqlite3.Rc.PERM |Sqlite3.Rc.ABORT
168 |Sqlite3.Rc.BUSY |Sqlite3.Rc.LOCKED |Sqlite3.Rc.NOMEM
169 |Sqlite3.Rc.READONLY |Sqlite3.Rc.INTERRUPT |Sqlite3.Rc.IOERR
170 |Sqlite3.Rc.CORRUPT |Sqlite3.Rc.NOTFOUND |Sqlite3.Rc.FULL
171 |Sqlite3.Rc.CANTOPEN |Sqlite3.Rc.PROTOCOL |Sqlite3.Rc.EMPTY
172 |Sqlite3.Rc.SCHEMA |Sqlite3.Rc.TOOBIG |Sqlite3.Rc.CONSTRAINT
173 |Sqlite3.Rc.MISMATCH |Sqlite3.Rc.MISUSE |Sqlite3.Rc.NOFLS
174 |Sqlite3.Rc.AUTH |Sqlite3.Rc.FORMAT |Sqlite3.Rc.RANGE
175 |Sqlite3.Rc.NOTADB |Sqlite3.Rc.ROW |Sqlite3.Rc.DONE
176 |Sqlite3.Rc.UNKNOWN _ -> GENERIC_ERROR "Sqlite3_generic_error"