]> matita.cs.unibo.it Git - helm.git/blob - helm/matita/matitaDb.ml
sync with SqlStatements
[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     Mysql.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
41 let clean_owner_environment () =
42   let dbd = instance () in
43   let owner = (Helm_registry.get "matita.owner") in
44   let obj_tbl = MetadataTypes.obj_tbl () in
45   let sort_tbl = MetadataTypes.sort_tbl () in
46   let rel_tbl = MetadataTypes.rel_tbl () in
47   let name_tbl =  MetadataTypes.name_tbl () in
48   let count_tbl = MetadataTypes.count_tbl () in
49   let tbls = [ 
50     (obj_tbl,`RefObj) ; (sort_tbl,`RefSort) ; (rel_tbl,`RefRel) ;
51     (name_tbl,`ObjectName) ; (count_tbl,`Count) ] 
52   in
53   let statements = 
54     (SqlStatements.drop_tables tbls) @ (SqlStatements.drop_indexes tbls)
55   in
56   let owned_uris =
57     try
58       MetadataDb.clean ~dbd
59     with Mysql.Error _ as exn ->
60       match Mysql.errno dbd with 
61       | Mysql.No_such_table -> []
62       | _ -> raise exn
63   in
64   List.iter
65     (fun uri ->
66       let uri = Pcre.replace ~rex:xpointer_RE ~templ:"" uri in
67       List.iter
68         (fun suffix -> Http_getter.unregister (uri ^ suffix))
69         [""; ".body"; ".types"])
70     owned_uris;
71   List.iter (fun statement -> 
72     try
73       ignore (Mysql.exec dbd statement)
74     with Mysql.Error _ as exn ->
75       match Mysql.errno dbd with 
76       | Mysql.Bad_table_error 
77       | Mysql.No_such_index | Mysql.No_such_table -> () 
78       | _ -> raise exn
79     ) statements;
80 ;;
81
82 let create_owner_environment () = 
83   let dbd = instance () in
84   let obj_tbl = MetadataTypes.obj_tbl () in
85   let sort_tbl = MetadataTypes.sort_tbl () in
86   let rel_tbl = MetadataTypes.rel_tbl () in
87   let name_tbl =  MetadataTypes.name_tbl () in
88   let count_tbl = MetadataTypes.count_tbl () in
89   let tbls = [ 
90     (obj_tbl,`RefObj) ; (sort_tbl,`RefSort) ; (rel_tbl,`RefRel) ;
91     (name_tbl,`ObjectName) ; (count_tbl,`Count) ] 
92   in
93   let statements = 
94     (SqlStatements.create_tables tbls) @ (SqlStatements.create_indexes tbls)
95   in
96   List.iter (fun statement -> 
97     try
98       ignore (Mysql.exec dbd statement)
99     with
100       exn -> 
101          let status = Mysql.status dbd in
102          match status with 
103          | Mysql.StatusError Mysql.Table_exists_error -> ()
104          | Mysql.StatusError _ -> raise exn
105          | _ -> ()
106       ) statements
107 ;;
108
109 (* removes uri from the ownerized tables, and returns the list of other objects
110  * (theyr uris) that ref the one removed. 
111  * AFAIK there is no need to return it, since the MatitaTypes.staus should
112  * contain all defined objects. but to double ckeck we do not garbage the
113  * metadata...
114  *)
115 let remove_uri uri =
116   let obj_tbl = MetadataTypes.obj_tbl () in
117   let sort_tbl = MetadataTypes.sort_tbl () in
118   let rel_tbl = MetadataTypes.rel_tbl () in
119   let name_tbl =  MetadataTypes.name_tbl () in
120   (*let conclno_tbl = MetadataTypes.conclno_tbl () in
121   let conclno_hyp_tbl = MetadataTypes.fullno_tbl () in*)
122   let count_tbl = MetadataTypes.count_tbl () in
123   
124   let dbd = instance () in
125   let suri = UriManager.string_of_uri uri in 
126   let query table suri = sprintf 
127     "DELETE FROM %s WHERE source LIKE '%s%%'" table (Mysql.escape suri)
128   in
129   List.iter (fun t -> 
130     try 
131       ignore (Mysql.exec dbd (query t suri))
132     with
133       exn -> raise exn (* no errors should be accepted *)
134     )
135   [obj_tbl;sort_tbl;rel_tbl;name_tbl;(*conclno_tbl;conclno_hyp_tbl*)count_tbl];
136   (* and now the debug job *)  
137   let dbg_q = 
138     sprintf "SELECT source FROM %s WHERE h_occurrence LIKE '%s%%'" obj_tbl suri
139   in
140   try 
141     let rc = Mysql.exec dbd dbg_q in
142     let l = ref [] in
143     Mysql.iter rc (fun a -> match a.(0) with None ->()|Some a -> l := a:: !l);
144     let l = List.sort Pervasives.compare !l in
145     let rec uniq = function 
146       | [] -> []
147       | h::[] -> [h]
148       | h1::h2::tl when h1 = h2 -> uniq (h2 :: tl) 
149       | h1::tl (* when h1 <> h2 *) -> h1 :: uniq tl
150     in
151     uniq l
152   with
153     exn -> raise exn (* no errors should be accepted *)
154