]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/metadata/metadataDb.ml
single INSERT on multiple tuples
[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 execute_insert dbd uri (sort_cols, rel_cols, obj_cols) =
31   let sort_tuples = 
32     List.fold_left (fun s l -> match l with
33       | [`String a; `String b; `Int c; `String d] -> 
34           sprintf "(\"%s\", \"%s\", %d, \"%s\")" a b c d :: s
35       | _ -> assert false )
36     [] sort_cols
37   in
38   let rel_tuples =
39     List.fold_left (fun s l -> match l with
40       | [`String a; `String b; `Int c] ->
41           sprintf "(\"%s\", \"%s\", %d)" a b c :: s
42       | _ -> assert false)
43     [] rel_cols  
44   in
45   let obj_tuples = List.fold_left (fun s l -> match l with
46       | [`String a; `String b; `String c; `Int d] ->
47           sprintf "(\"%s\", \"%s\", \"%s\", %d)" a b c d :: s
48       | [`String a; `String b; `String c; `Null] ->
49           sprintf "(\"%s\", \"%s\", \"%s\", %s)" a b c "NULL" :: s
50       | _ -> assert false)
51     [] obj_cols
52   in
53   if sort_tuples <> [] then
54     begin
55     let query_sort = 
56       sprintf "INSERT %s VALUES %s;" (sort_tbl ()) (String.concat "," sort_tuples) 
57     in
58     ignore (Mysql.exec dbd query_sort)
59     end;
60   if rel_tuples <> [] then
61     begin
62     let query_rel = 
63       sprintf "INSERT %s VALUES %s;" (rel_tbl ()) (String.concat "," rel_tuples) 
64     in
65     ignore (Mysql.exec dbd query_rel)
66     end;
67   if obj_tuples <> [] then
68     begin
69     let query_obj = 
70       sprintf "INSERT %s VALUES %s;" (obj_tbl ()) (String.concat "," obj_tuples) 
71     in
72     ignore (Mysql.exec dbd query_obj)
73     end
74   
75 let insert_const_no dbd uri =
76   let inconcl_no =
77     sprintf "INSERT %s SELECT \"%s\", COUNT(DISTINCT h_occurrence) FROM %s WHERE (h_position=\"%s\" OR h_position=\"%s\") AND source LIKE \"%s%%\""
78       (conclno_tbl ()) uri (obj_tbl ()) inconcl_pos mainconcl_pos uri
79   in
80   let concl_hyp =
81     sprintf "INSERT %s
82         SELECT \"%s\",COUNT(DISTINCT h_occurrence)
83         FROM %s
84         WHERE NOT (h_position=\"%s\") AND (source = \"%s\")"
85       (fullno_tbl ()) uri (obj_tbl ()) inbody_pos uri
86   in
87   ignore (Mysql.exec dbd inconcl_no);
88   ignore (Mysql.exec dbd concl_hyp)
89
90 let insert_name ~dbd ~uri ~name =
91   let query =
92     sprintf "INSERT %s VALUES (\"%s\", \"%s\")" (name_tbl ()) uri name
93   in
94   ignore (Mysql.exec dbd query)
95
96 type columns =
97   MetadataPp.t list list * MetadataPp.t list list * MetadataPp.t list list
98
99   (* TODO ZACK: verify if an object has already been indexed *)
100 let already_indexed _ = false
101 (*
102 let index_constant ~dbd =
103   let query = prepare_insert () in
104   fun ~uri ~body ~ty  ->
105     if not (already_indexed uri) then begin
106       let name = UriManager.name_of_uri uri in
107       let uri = UriManager.string_of_uri uri in
108       let metadata = MetadataExtractor.compute ~body ~ty in
109       let columns = MetadataPp.columns_of_metadata ~about:uri metadata in
110       execute_insert dbd query uri (columns :> columns);
111       insert_const_no dbd uri;
112       insert_name ~dbd ~uri ~name
113     end
114
115 let index_inductive_def ~dbd =
116   let query = prepare_insert () in
117   fun ~uri ~types ->
118     if not (already_indexed uri) then begin
119       let metadata = MetadataExtractor.compute_obj uri in
120       let uri_of (a,b,c) = a in
121       let uris = UriManager.string_of_uri uri :: List.map uri_of metadata in
122       let uri = UriManager.string_of_uri uri in
123       let columns = MetadataPp.columns_of_ind_metadata metadata in
124       execute_insert dbd query uri (columns :> columns);
125       List.iter (insert_const_no dbd) uris;
126       List.iter (fun (uri, name, _) -> insert_name ~dbd ~uri ~name) metadata
127     end
128 *)
129 let index_obj ~dbd ~uri = 
130   if not (already_indexed uri) then begin
131     let metadata = MetadataExtractor.compute_obj uri in
132     let uri_of (a,b,c) = a in
133     let uris = UriManager.string_of_uri uri :: List.map uri_of metadata in
134     let uri = UriManager.string_of_uri uri in
135     let columns = MetadataPp.columns_of_metadata metadata in
136     execute_insert dbd uri (columns :> columns);
137     List.iter (insert_const_no dbd) uris;
138     List.iter (fun (uri, name, _) -> insert_name ~dbd ~uri ~name) metadata
139   end
140   
141
142 let tables_to_clean =
143   [sort_tbl; rel_tbl; obj_tbl; conclno_tbl; fullno_tbl; name_tbl; hypno_tbl]
144
145 let clean ~(dbd:Mysql.dbd) =
146   let owned_uris =  (* list of uris in list-of-columns format *)
147     let query = sprintf "SELECT source FROM %s" (obj_tbl ()) in
148     let result = Mysql.exec dbd query in
149     let uris = Mysql.map result (fun cols ->
150       match cols.(0) with
151       | Some src -> src
152       | None -> assert false) in
153     (* and now some stuff to remove #xpointers and duplicates *)
154     uris
155   in
156   let del_from tbl =
157     let query s = 
158       sprintf "DELETE FROM %s WHERE source LIKE \"%s%%\"" (tbl ()) s 
159     in
160     List.iter
161       (fun source_col -> ignore (Mysql.exec dbd (query source_col)))
162       owned_uris
163   in
164   List.iter del_from tables_to_clean;
165   owned_uris
166
167 let unindex ~dbd ~uri =
168   let uri = UriManager.string_of_uri uri in
169   let del_from tbl =
170     let query tbl =
171       sprintf "DELETE FROM %s WHERE source LIKE \"%s%%\"" (tbl ()) uri
172     in
173     ignore (Mysql.exec dbd (query tbl))
174   in
175   List.iter del_from tables_to_clean
176