1 (* Copyright (C) 2004-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://helm.cs.unibo.it/
31 ~host:(Helm_registry.get "db.host")
32 ~user:(Helm_registry.get "db.user")
33 ~database:(Helm_registry.get "db.database")
36 fun () -> Lazy.force dbd
39 let xpointer_RE = Pcre.regexp "#.*$"
40 let file_scheme_RE = Pcre.regexp "^file://"
42 let clean_owner_environment () =
43 let dbd = instance () in
44 let owner = (Helm_registry.get "matita.owner") in
45 let obj_tbl = MetadataTypes.obj_tbl () in
46 let sort_tbl = MetadataTypes.sort_tbl () in
47 let rel_tbl = MetadataTypes.rel_tbl () in
48 let name_tbl = MetadataTypes.name_tbl () in
49 let count_tbl = MetadataTypes.count_tbl () in
51 (obj_tbl,`RefObj) ; (sort_tbl,`RefSort) ; (rel_tbl,`RefRel) ;
52 (name_tbl,`ObjectName) ; (count_tbl,`Count) ]
55 (SqlStatements.drop_tables tbls) @ (SqlStatements.drop_indexes tbls)
60 with Mysql.Error _ as exn ->
61 match Mysql.errno dbd with
62 | Mysql.No_such_table -> []
67 let uri = Pcre.replace ~rex:xpointer_RE ~templ:"" uri in
69 (fun suffix -> MatitaMisc.safe_remove (uri ^ suffix ^ ".xml.gz"))
70 [""; ".body"; ".types"])
72 List.iter (fun statement ->
74 ignore (Mysql.exec dbd statement)
75 with Mysql.Error _ as exn ->
76 match Mysql.errno dbd with
77 | Mysql.Bad_table_error
78 | Mysql.No_such_index | Mysql.No_such_table -> ()
83 let create_owner_environment () =
84 let dbd = instance () in
85 let obj_tbl = MetadataTypes.obj_tbl () in
86 let sort_tbl = MetadataTypes.sort_tbl () in
87 let rel_tbl = MetadataTypes.rel_tbl () in
88 let name_tbl = MetadataTypes.name_tbl () in
89 let count_tbl = MetadataTypes.count_tbl () in
91 (obj_tbl,`RefObj) ; (sort_tbl,`RefSort) ; (rel_tbl,`RefRel) ;
92 (name_tbl,`ObjectName) ; (count_tbl,`Count) ]
95 (SqlStatements.create_tables tbls) @ (SqlStatements.create_indexes tbls)
97 List.iter (fun statement ->
99 ignore (Mysql.exec dbd statement)
102 let status = Mysql.status dbd in
104 | Mysql.StatusError Mysql.Table_exists_error -> ()
105 | Mysql.StatusError _ -> raise exn
110 (* removes uri from the ownerized tables, and returns the list of other objects
111 * (theyr uris) that ref the one removed.
112 * AFAIK there is no need to return it, since the MatitaTypes.staus should
113 * contain all defined objects. but to double check we do not garbage the
117 let obj_tbl = MetadataTypes.obj_tbl () in
118 let sort_tbl = MetadataTypes.sort_tbl () in
119 let rel_tbl = MetadataTypes.rel_tbl () in
120 let name_tbl = MetadataTypes.name_tbl () in
121 (*let conclno_tbl = MetadataTypes.conclno_tbl () in
122 let conclno_hyp_tbl = MetadataTypes.fullno_tbl () in*)
123 let count_tbl = MetadataTypes.count_tbl () in
125 let dbd = instance () in
126 let suri = UriManager.string_of_uri uri in
127 let query table suri = sprintf
128 "DELETE FROM %s WHERE source LIKE '%s%%'" table (Mysql.escape suri)
132 ignore (Mysql.exec dbd (query t suri))
134 exn -> raise exn (* no errors should be accepted *)
136 [obj_tbl;sort_tbl;rel_tbl;name_tbl;(*conclno_tbl;conclno_hyp_tbl*)count_tbl];
137 (* and now the debug job *)
139 sprintf "SELECT source FROM %s WHERE h_occurrence LIKE '%s%%'" obj_tbl
143 let rc = Mysql.exec dbd dbg_q in
145 Mysql.iter rc (fun a -> match a.(0) with None ->()|Some a -> l := a:: !l);
146 let l = List.sort Pervasives.compare !l in
147 MatitaMisc.list_uniq l
149 exn -> raise exn (* no errors should be accepted *)
151 let xpointers_of_ind uri =
152 let dbd = instance () in
153 let name_tbl = MetadataTypes.name_tbl () in
155 "SELECT source FROM %s WHERE source LIKE '%s#xpointer%%'" name_tbl
156 (Mysql.escape (UriManager.string_of_uri uri))
158 let rc = Mysql.exec dbd query in
160 Mysql.iter rc (fun a -> match a.(0) with None ->()|Some a -> l := a:: !l);
161 List.map UriManager.uri_of_string !l
163 let reset_owner_environment () =
164 clean_owner_environment ();
165 create_owner_environment ()