]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/metadata/metadataDb.ml
MetadataDb.clean doesn't need ~owner since table names are ownerized ;)
[helm.git] / helm / ocaml / metadata / metadataDb.ml
1 (* Copyright (C) 2004, 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 MetadataTypes
27
28 open Printf
29
30 let prepare_insert () =
31   (*
32   let insert_owner a b =
33     sprintf "INSERT %s VALUES (\"%s\", \"%s\")" (owners_tbl ())a b
34   in
35   *)
36   let insert_sort  a b c d =
37     sprintf "INSERT %s VALUES (\"%s\", \"%s\", %d, \"%s\")" (sort_tbl ())a b c d
38   in
39   let insert_rel a b c =
40     sprintf "INSERT %s VALUES (\"%s\", \"%s\", %d)" (rel_tbl ()) a b c
41   in
42   let insert_obj a b c d =
43     sprintf "INSERT %s VALUES (\"%s\", \"%s\", \"%s\", %s)" (obj_tbl ()) a b c d
44   in
45   ((*insert_owner, *)insert_sort, insert_rel, insert_obj)
46
47 let execute_insert dbd ((*insert_owner, *)insert_sort, insert_rel, insert_obj)
48   uri owner (sort_cols, rel_cols, obj_cols)
49 =
50   (* ignore (Mysql.exec dbd (insert_owner uri owner)); *)
51   List.iter (function
52       | [`String a; `String b; `Int c; `String d] ->
53           ignore (Mysql.exec dbd (insert_sort a b c d))
54       | _ -> assert false)
55     sort_cols;
56   List.iter (function
57       | [`String a; `String b; `Int c] ->
58           ignore (Mysql.exec dbd (insert_rel a b c))
59       | _ -> assert false)
60     rel_cols;
61   List.iter (function
62       | [`String a; `String b; `String c; `Int d] ->
63           ignore (Mysql.exec dbd (insert_obj a b c (string_of_int d)))
64       | [`String a; `String b; `String c; `Null] ->
65           ignore (Mysql.exec dbd (insert_obj a b c "NULL"))
66       | _ -> assert false)
67     obj_cols
68
69 let insert_const_no dbd uri =
70   let inconcl_no =
71     sprintf "INSERT %s SELECT \"%s\", COUNT(DISTINCT h_occurrence) FROM %s WHERE (h_position=\"%s\" OR h_position=\"%s\") AND source LIKE \"%s%%\""
72       (conclno_tbl ()) uri (obj_tbl ()) inconcl_pos mainconcl_pos uri
73   in
74   let concl_hyp =
75     sprintf "INSERT %s
76         SELECT \"%s\",COUNT(DISTINCT h_occurrence)
77         FROM %s
78         WHERE NOT (h_position=\"%s\") AND (source = \"%s\")"
79       (conclno_hyp_tbl ()) uri (obj_tbl ()) inbody_pos uri
80   in
81   ignore (Mysql.exec dbd inconcl_no);
82   ignore (Mysql.exec dbd concl_hyp)
83
84 let insert_name ~dbd ~uri ~name =
85   let query =
86     sprintf "INSERT %s VALUES (\"%s\", \"%s\")" (name_tbl ()) uri name
87   in
88   ignore (Mysql.exec dbd query)
89
90 type columns =
91   MetadataPp.t list list * MetadataPp.t list list * MetadataPp.t list list
92
93 let index_constant ~dbd =
94   let query = prepare_insert () in
95   fun ~owner ~uri ~body ~ty  ->
96     let name = UriManager.name_of_uri uri in
97     let uri = UriManager.string_of_uri uri in
98     let metadata = MetadataExtractor.compute ~body ~ty in
99     let columns = MetadataPp.columns_of_metadata ~about:uri metadata in
100     execute_insert dbd query uri owner (columns :> columns);
101     insert_const_no dbd uri;
102     insert_name ~dbd ~uri ~name
103
104 let index_inductive_def ~dbd =
105   let query = prepare_insert () in
106   fun ~owner ~uri ~types ->
107     let metadata = MetadataExtractor.compute_ind ~uri ~types in
108     let uri_of (a,b,c) = a in
109     let uris = UriManager.string_of_uri uri :: List.map uri_of metadata in
110     let uri = UriManager.string_of_uri uri in
111     let columns = MetadataPp.columns_of_ind_metadata metadata in
112     execute_insert dbd query uri owner (columns :> columns);
113     List.iter (insert_const_no dbd) uris;
114     List.iter (fun (uri, name, _) -> insert_name ~dbd ~uri ~name) metadata
115
116 let clean ~(dbd:Mysql.dbd) =
117   let owned_uris =  (* list of uris in list-of-columns format *)
118     let query =
119       (* sprintf 
120        *  "SELECT source FROM %s WHERE owner = \"%s\"" (owners_tbl ())
121        * owner*)
122       sprintf "SELECT source FROM %s" (obj_tbl ())
123     in
124     let result = Mysql.exec dbd query in
125     let uris = Mysql.map result (fun cols ->
126       match cols.(0) with
127       | Some src -> src
128       | None -> assert false) in
129     (* and now some stuff to remove #xpointers and duplicates *)
130     let cleaned_uris = 
131       List.map (fun x -> Str.replace_first (Str.regexp "\#.*$") "" x) uris
132     in
133     let sorted_uris = List.fast_sort Pervasives.compare cleaned_uris in
134     match sorted_uris with
135     | [] -> sorted_uris
136     | he::tl -> 
137         (* no List.remove dups ?? *)
138         snd(List.fold_left (fun (last,l) cur -> if cur <> last then
139         (cur,cur::l) else (last,l)) (he,[he]) tl)
140   in
141   let del_from tbl =
142     let query s = 
143       sprintf "DELETE FROM %s WHERE source LIKE \"%s%%\"" (tbl ()) s 
144     in
145     List.iter
146       (fun source_col -> ignore (Mysql.exec dbd (query source_col)))
147       owned_uris
148   in
149   List.iter del_from
150     [sort_tbl; rel_tbl; obj_tbl; conclno_tbl; conclno_hyp_tbl; name_tbl(*;
151     owners_tbl*)];
152   List.iter Http_getter.unregister owned_uris  
153