]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/library/libraryDb.ml
test branch
[helm.git] / helm / ocaml / library / libraryDb.ml
1 (* Copyright (C) 2004-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://helm.cs.unibo.it/
24  *)
25
26 (* $Id$ *)
27
28 open Printf ;;
29
30 let instance =
31   let dbd = lazy (
32     HMysql.quick_connect
33       ~host:(Helm_registry.get "db.host")
34       ~user:(Helm_registry.get "db.user")
35       ~database:(Helm_registry.get "db.database")
36       ())
37   in
38   fun () -> Lazy.force dbd
39
40
41 let xpointer_RE = Pcre.regexp "#.*$"
42 let file_scheme_RE = Pcre.regexp "^file://"
43
44 let clean_owner_environment () =
45   let dbd = instance () in
46   let owner = (Helm_registry.get "matita.owner") in
47   let obj_tbl = MetadataTypes.obj_tbl () in
48   let sort_tbl = MetadataTypes.sort_tbl () in
49   let rel_tbl = MetadataTypes.rel_tbl () in
50   let name_tbl =  MetadataTypes.name_tbl () in
51   let count_tbl = MetadataTypes.count_tbl () in
52   let tbls = [ 
53     (obj_tbl,`RefObj) ; (sort_tbl,`RefSort) ; (rel_tbl,`RefRel) ;
54     (name_tbl,`ObjectName) ; (count_tbl,`Count) ] 
55   in
56   let statements = 
57     (SqlStatements.drop_tables tbls) @ (SqlStatements.drop_indexes tbls)
58   in
59   let owned_uris =
60     try
61       MetadataDb.clean ~dbd
62     with Mysql.Error _ as exn ->
63       match HMysql.errno dbd with 
64       | Mysql.No_such_table -> []
65       | _ -> raise exn
66   in
67   List.iter
68     (fun uri ->
69       let uri = Pcre.replace ~rex:xpointer_RE ~templ:"" uri in
70       List.iter
71         (fun suffix ->
72           try
73            HExtlib.safe_remove (Http_getter.resolve (uri ^ suffix))
74           with Http_getter_types.Key_not_found _ -> ())
75         [""; ".body"; ".types"])
76     owned_uris;
77   List.iter (fun statement -> 
78     try
79       ignore (HMysql.exec dbd statement)
80     with Mysql.Error _ as exn ->
81       match HMysql.errno dbd with 
82       | Mysql.Bad_table_error 
83       | Mysql.No_such_index | Mysql.No_such_table -> () 
84       | _ -> raise exn
85     ) statements;
86 ;;
87
88 let create_owner_environment () = 
89   let dbd = instance () in
90   let obj_tbl = MetadataTypes.obj_tbl () in
91   let sort_tbl = MetadataTypes.sort_tbl () in
92   let rel_tbl = MetadataTypes.rel_tbl () in
93   let name_tbl =  MetadataTypes.name_tbl () in
94   let count_tbl = MetadataTypes.count_tbl () in
95   let tbls = [ 
96     (obj_tbl,`RefObj) ; (sort_tbl,`RefSort) ; (rel_tbl,`RefRel) ;
97     (name_tbl,`ObjectName) ; (count_tbl,`Count) ] 
98   in
99   let statements = 
100     (SqlStatements.create_tables tbls) @ (SqlStatements.create_indexes tbls)
101   in
102   List.iter (fun statement -> 
103     try
104       ignore (HMysql.exec dbd statement)
105     with
106       exn -> 
107          let status = HMysql.status dbd in
108          match status with 
109          | Mysql.StatusError Mysql.Table_exists_error -> ()
110          | Mysql.StatusError Mysql.Dup_keyname -> ()
111          | Mysql.StatusError _ -> raise exn
112          | _ -> ()
113       ) statements
114 ;;
115
116 (* removes uri from the ownerized tables, and returns the list of other objects
117  * (theyr uris) that ref the one removed. 
118  * AFAIK there is no need to return it, since the MatitaTypes.staus should
119  * contain all defined objects. but to double check we do not garbage the
120  * metadata...
121  *)
122 let remove_uri uri =
123   let obj_tbl = MetadataTypes.obj_tbl () in
124   let sort_tbl = MetadataTypes.sort_tbl () in
125   let rel_tbl = MetadataTypes.rel_tbl () in
126   let name_tbl =  MetadataTypes.name_tbl () in
127   (*let conclno_tbl = MetadataTypes.conclno_tbl () in
128   let conclno_hyp_tbl = MetadataTypes.fullno_tbl () in*)
129   let count_tbl = MetadataTypes.count_tbl () in
130   
131   let dbd = instance () in
132   let suri = UriManager.string_of_uri uri in 
133   let query table suri = sprintf 
134     "DELETE FROM %s WHERE source LIKE '%s%%'" table (HMysql.escape suri)
135   in
136   List.iter (fun t -> 
137     try 
138       ignore (HMysql.exec dbd (query t suri))
139     with
140       exn -> raise exn (* no errors should be accepted *)
141     )
142   [obj_tbl;sort_tbl;rel_tbl;name_tbl;(*conclno_tbl;conclno_hyp_tbl*)count_tbl];
143   (* and now the debug job *)  
144   let dbg_q = 
145     sprintf "SELECT source FROM %s WHERE h_occurrence LIKE '%s%%'" obj_tbl
146     (HMysql.escape suri)
147   in
148   try 
149     let rc = HMysql.exec dbd dbg_q in
150     let l = ref [] in
151     HMysql.iter rc (fun a -> match a.(0) with None ->()|Some a -> l := a:: !l);
152     let l = List.sort Pervasives.compare !l in
153     HExtlib.list_uniq l
154   with
155     exn -> raise exn (* no errors should be accepted *)
156
157 let xpointers_of_ind uri =
158   let dbd = instance () in
159   let name_tbl =  MetadataTypes.name_tbl () in
160   let query = sprintf 
161     "SELECT source FROM %s WHERE source LIKE '%s#xpointer%%'" name_tbl 
162       (HMysql.escape (UriManager.string_of_uri uri))
163   in
164   let rc = HMysql.exec dbd query in
165   let l = ref [] in
166   HMysql.iter rc (fun a ->  match a.(0) with None ->()|Some a -> l := a:: !l);
167   List.map UriManager.uri_of_string !l
168