]> matita.cs.unibo.it Git - helm.git/blob - components/library/libraryDb.ml
Huge commit for the release. Includes:
[helm.git] / components / 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 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
51   let tbls = [ 
52     (obj_tbl,`RefObj) ; (sort_tbl,`RefSort) ; (rel_tbl,`RefRel) ;
53     (name_tbl,`ObjectName) ; (count_tbl,`Count) ] 
54   in
55   let statements = 
56     (SqlStatements.drop_tables tbls) @ (SqlStatements.drop_indexes tbls)
57   in
58   let owned_uris =
59     try
60       MetadataDb.clean ~dbd
61     with Mysql.Error _ as exn ->
62       match HMysql.errno dbd with 
63       | Mysql.No_such_table -> []
64       | _ -> raise exn
65   in
66   List.iter
67     (fun uri ->
68       let uri = Pcre.replace ~rex:xpointer_RE ~templ:"" uri in
69       List.iter
70         (fun suffix ->
71           try
72            HExtlib.safe_remove 
73              (Http_getter.resolve ~writable:true (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 l_obj_tbl = MetadataTypes.library_obj_tbl  in
96   let l_sort_tbl = MetadataTypes.library_sort_tbl  in
97   let l_rel_tbl = MetadataTypes.library_rel_tbl  in
98   let l_name_tbl =  MetadataTypes.library_name_tbl  in
99   let l_count_tbl = MetadataTypes.library_count_tbl  in
100   let tbls = [ 
101     (obj_tbl,`RefObj) ; (sort_tbl,`RefSort) ; (rel_tbl,`RefRel) ;
102     (name_tbl,`ObjectName) ; (count_tbl,`Count) ]
103   in
104   let system_tbls = [ 
105     (l_obj_tbl,`RefObj) ; (l_sort_tbl,`RefSort) ; (l_rel_tbl,`RefRel) ;
106     (l_name_tbl,`ObjectName) ; (l_count_tbl,`Count) ] 
107   in
108   let statements = 
109     (SqlStatements.create_tables system_tbls) @ 
110     (SqlStatements.create_tables tbls) @ 
111     (SqlStatements.create_indexes system_tbls) @
112     (SqlStatements.create_indexes tbls)
113   in
114   List.iter (fun statement -> 
115     try
116       ignore (HMysql.exec dbd statement)
117     with
118       exn -> 
119          let status = HMysql.status dbd in
120          match status with 
121          | Mysql.StatusError Mysql.Table_exists_error -> ()
122          | Mysql.StatusError Mysql.Dup_keyname -> ()
123          | Mysql.StatusError _ -> raise exn
124          | _ -> ()
125       ) statements
126 ;;
127
128 (* removes uri from the ownerized tables, and returns the list of other objects
129  * (theyr uris) that ref the one removed. 
130  * AFAIK there is no need to return it, since the MatitaTypes.staus should
131  * contain all defined objects. but to double check we do not garbage the
132  * metadata...
133  *)
134 let remove_uri uri =
135   let obj_tbl = MetadataTypes.obj_tbl () in
136   let sort_tbl = MetadataTypes.sort_tbl () in
137   let rel_tbl = MetadataTypes.rel_tbl () in
138   let name_tbl =  MetadataTypes.name_tbl () in
139   (*let conclno_tbl = MetadataTypes.conclno_tbl () in
140   let conclno_hyp_tbl = MetadataTypes.fullno_tbl () in*)
141   let count_tbl = MetadataTypes.count_tbl () in
142   
143   let dbd = instance () in
144   let suri = UriManager.string_of_uri uri in 
145   let query table suri = sprintf 
146     "DELETE FROM %s WHERE source LIKE '%s%%'" table (HMysql.escape suri)
147   in
148   List.iter (fun t -> 
149     try 
150       ignore (HMysql.exec dbd (query t suri))
151     with
152       exn -> raise exn (* no errors should be accepted *)
153     )
154   [obj_tbl;sort_tbl;rel_tbl;name_tbl;(*conclno_tbl;conclno_hyp_tbl*)count_tbl];
155   (* and now the debug job *)  
156   let dbg_q = 
157     sprintf "SELECT source FROM %s WHERE h_occurrence LIKE '%s%%'" obj_tbl
158     (HMysql.escape suri)
159   in
160   try 
161     let rc = HMysql.exec dbd dbg_q in
162     let l = ref [] in
163     HMysql.iter rc (fun a -> match a.(0) with None ->()|Some a -> l := a:: !l);
164     let l = List.sort Pervasives.compare !l in
165     HExtlib.list_uniq l
166   with
167     exn -> raise exn (* no errors should be accepted *)
168
169 let xpointers_of_ind uri =
170   let dbd = instance () in
171   let name_tbl =  MetadataTypes.name_tbl () in
172   let query = sprintf 
173     "SELECT source FROM %s WHERE source LIKE '%s#xpointer%%'" name_tbl 
174       (HMysql.escape (UriManager.string_of_uri uri))
175   in
176   let rc = HMysql.exec dbd query in
177   let l = ref [] in
178   HMysql.iter rc (fun a ->  match a.(0) with None ->()|Some a -> l := a:: !l);
179   List.map UriManager.uri_of_string !l
180