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
45 let prerr_endline s = ()(*HLog.debug s;;*)
47 let profiler = HExtlib.profile "Sqlite3"
49 let quick_connect ?host ?(database = "sqlite") ?port ?password ?user () =
50 let username = Helm_registry.get "user.name" in
52 "/dev/shm/matita.db." ^ username ^ "." ^ string_of_int (Unix.getpid ())
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)
66 let b = Sqlite3.db_close db in
67 if b=false then prerr_endline "No Closed DataBase"
74 let stored_result = ref [] in
76 stored_result := row :: !stored_result
82 profiler.HExtlib.profile (Sqlite3.exec_no_headers db ~cb:store) s
85 | Sqlite3.Rc.OK -> !stored_result
90 let map f = List.map f res in
91 profiler.HExtlib.profile map f
94 let iter f = List.iter f res in
95 profiler.HExtlib.profile iter f
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))
134 (* pp_rc (Sqlite3.errcode db); *)
135 (* prerr_endline(Sqlite3.errmsg db); *)
136 match Sqlite3.errcode db with
139 if Pcre.pmatch(Sqlite3.errmsg db) ~pat:"^table .* already exists" then
142 if Pcre.pmatch(Sqlite3.errmsg db) ~pat:"^index .* already exists" then
145 if Pcre.pmatch(Sqlite3.errmsg db) ~pat:"^no such table: .*" then
148 if Pcre.pmatch(Sqlite3.errmsg db) ~pat:"^no such index: .*" then
160 |Sqlite3.Rc.INTERRUPT
170 |Sqlite3.Rc.CONSTRAINT
180 |Sqlite3.Rc.UNKNOWN _ -> GENERIC_ERROR "Sqlite3_generic_error"