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