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/
33 ~host:(Helm_registry.get "db.host")
34 ~user:(Helm_registry.get "db.user")
35 ~database:(Helm_registry.get "db.database")
38 fun () -> Lazy.force dbd
41 let xpointer_RE = Pcre.regexp "#.*$"
42 let file_scheme_RE = Pcre.regexp "^file://"
44 let clean_owner_environment () =
45 let dbd = instance () in
46 let obj_tbl = MetadataTypes.obj_tbl () in
47 let sort_tbl = MetadataTypes.sort_tbl () in
48 let rel_tbl = MetadataTypes.rel_tbl () in
49 let name_tbl = MetadataTypes.name_tbl () in
50 let count_tbl = MetadataTypes.count_tbl () in
52 (obj_tbl,`RefObj) ; (sort_tbl,`RefSort) ; (rel_tbl,`RefRel) ;
53 (name_tbl,`ObjectName) ; (count_tbl,`Count) ]
56 (SqlStatements.drop_tables tbls) @ (SqlStatements.drop_indexes tbls)
61 with (HSql.Error _) as exn ->
62 match HSql.errno dbd with
63 | HSql.No_such_table -> []
68 let uri = Pcre.replace ~rex:xpointer_RE ~templ:"" uri in
73 (Http_getter.resolve ~writable:true (uri ^ suffix))
74 with Http_getter_types.Key_not_found _ -> ())
75 [""; ".body"; ".types"])
77 List.iter (fun statement ->
79 ignore (HSql.exec dbd statement)
80 with (HSql.Error _) as exn ->
81 match HSql.errno dbd with
83 | HSql.Bad_table_error
84 | HSql.No_such_index -> prerr_endline statement; ()
89 let create_owner_environment () =
90 let dbd = instance () in
91 let obj_tbl = MetadataTypes.obj_tbl () in
92 let sort_tbl = MetadataTypes.sort_tbl () in
93 let rel_tbl = MetadataTypes.rel_tbl () in
94 let name_tbl = MetadataTypes.name_tbl () in
95 let count_tbl = MetadataTypes.count_tbl () in
96 let l_obj_tbl = MetadataTypes.library_obj_tbl in
97 let l_sort_tbl = MetadataTypes.library_sort_tbl in
98 let l_rel_tbl = MetadataTypes.library_rel_tbl in
99 let l_name_tbl = MetadataTypes.library_name_tbl in
100 let l_count_tbl = MetadataTypes.library_count_tbl in
102 (obj_tbl,`RefObj) ; (sort_tbl,`RefSort) ; (rel_tbl,`RefRel) ;
103 (name_tbl,`ObjectName) ; (count_tbl,`Count) ]
106 (l_obj_tbl,`RefObj) ; (l_sort_tbl,`RefSort) ; (l_rel_tbl,`RefRel) ;
107 (l_name_tbl,`ObjectName) ; (l_count_tbl,`Count) ]
110 (SqlStatements.create_tables system_tbls) @
111 (SqlStatements.create_tables tbls) @
112 (SqlStatements.create_indexes system_tbls) @
113 (SqlStatements.create_indexes tbls)
115 List.iter (fun statement ->
117 ignore (HSql.exec dbd statement)
119 (HSql.Error _) as exc ->
120 let status = HSql.errno dbd in
122 | HSql.Table_exists_error -> ()
123 | HSql.Dup_keyname -> ()
124 | HSql.GENERIC_ERROR _ ->
125 prerr_endline statement;
133 (* removes uri from the ownerized tables, and returns the list of other objects
134 * (theyr uris) that ref the one removed.
135 * AFAIK there is no need to return it, since the MatitaTypes.staus should
136 * contain all defined objects. but to double check we do not garbage the
140 let obj_tbl = MetadataTypes.obj_tbl () in
141 let sort_tbl = MetadataTypes.sort_tbl () in
142 let rel_tbl = MetadataTypes.rel_tbl () in
143 let name_tbl = MetadataTypes.name_tbl () in
144 (*let conclno_tbl = MetadataTypes.conclno_tbl () in
145 let conclno_hyp_tbl = MetadataTypes.fullno_tbl () in*)
146 let count_tbl = MetadataTypes.count_tbl () in
148 let dbd = instance () in
149 let suri = UriManager.string_of_uri uri in
150 let query table suri =
152 sprintf "DELETE QUICK LOW_PRIORITY FROM %s WHERE source='%s'" table (HSql.escape suri)
153 else sprintf "DELETE FROM %s WHERE source='%s'" table (HSql.escape suri)
157 ignore (HSql.exec dbd (query t suri))
159 exn -> raise exn (* no errors should be accepted *)
161 [obj_tbl;sort_tbl;rel_tbl;name_tbl;(*conclno_tbl;conclno_hyp_tbl*)count_tbl];
164 let xpointers_of_ind uri =
165 let dbd = instance () in
166 let name_tbl = MetadataTypes.name_tbl () in
168 "SELECT source FROM %s WHERE source LIKE '%s#xpointer%%'" name_tbl
169 (HSql.escape (UriManager.string_of_uri uri))
171 let rc = HSql.exec dbd query in
173 HSql.iter rc (fun a -> match a.(0) with None ->()|Some a -> l := a:: !l);
174 List.map UriManager.uri_of_string !l