]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/metadata/metadataDb.ml
- changed metadata type so that positions contains depth
[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 (dbh: Dbi.connection) =
31   let insert_owner =
32     dbh#prepare (sprintf "INSERT INTO %s VALUES (\"?\",\"?\")" owners_tbl)
33   in
34   let insert_sort =
35     dbh#prepare (sprintf "INSERT INTO %s VALUES (\"?\",\"?\",?,\"?\")" sort_tbl)
36   in
37   let insert_rel =
38     dbh#prepare (sprintf "INSERT INTO %s VALUES (\"?\",\"?\",?)" rel_tbl)
39   in
40   let insert_obj =
41     dbh#prepare (sprintf "INSERT INTO %s VALUES (\"?\",\"?\",\"?\",?)" obj_tbl)
42   in
43   (insert_owner, insert_sort, insert_rel, insert_obj)
44
45 let execute_insert dbh (insert_owner, insert_sort, insert_rel, insert_obj)
46   uri owner (sort_cols, rel_cols, obj_cols)
47 =
48   insert_owner#execute [`String uri; `String owner];
49   List.iter insert_sort#execute sort_cols;
50   List.iter insert_rel#execute rel_cols;
51   List.iter insert_obj#execute obj_cols
52
53 let insert_const_no dbh uri =
54   let inconcl_no =
55     sprintf "INSERT INTO %s SELECT \"%s\", COUNT(DISTINCT h_occurrence) FROM %s WHERE (h_position=\"%s\" OR h_position=\"%s\") AND source LIKE \"%s%%\""
56       conclno_tbl uri obj_tbl inconcl_pos mainconcl_pos uri
57   in
58   let concl_hyp =
59     sprintf "INSERT INTO %s
60         SELECT \"%s\",COUNT(DISTINCT h_occurrence)
61         FROM %s
62         WHERE NOT (h_position=\"%s\") AND (source = \"%s\")"
63       conclno_hyp_tbl uri obj_tbl inbody_pos uri
64   in
65   (dbh#prepare inconcl_no)#execute [];
66   (dbh#prepare concl_hyp)#execute []
67
68 let insert_name ~dbh ~uri ~name =
69   let query =
70     dbh#prepare
71       (sprintf "INSERT INTO %s VALUES (\"%s\", \"%s\")" name_tbl uri name)
72   in
73   query#execute []
74
75 type dbi_columns =
76   Dbi.sql_t list list * Dbi.sql_t list list * Dbi.sql_t list list
77
78 let index_constant ~dbh =
79   let query = prepare_insert dbh in
80   fun ~owner ~uri ~body ~ty  ->
81     let name = UriManager.name_of_uri uri in
82     let uri = UriManager.string_of_uri uri in
83     let metadata = MetadataExtractor.compute ~body ~ty in
84     let columns = MetadataPp.columns_of_metadata ~about:uri metadata in
85     execute_insert dbh query uri owner (columns :> dbi_columns);
86     insert_const_no dbh uri;
87     insert_name ~dbh ~uri ~name
88
89 let index_inductive_def ~dbh =
90   let query = prepare_insert dbh in
91   fun ~owner ~uri ~types ->
92     let metadata = MetadataExtractor.compute_ind ~uri ~types in
93     let uri_of (a,b,c) = a in
94     let uris = UriManager.string_of_uri uri :: List.map uri_of metadata in
95     let uri = UriManager.string_of_uri uri in
96     let columns = MetadataPp.columns_of_ind_metadata metadata in
97     execute_insert dbh query uri owner (columns :> dbi_columns);
98     List.iter (insert_const_no dbh) uris;
99     List.iter (fun (uri, name, _) -> insert_name ~dbh ~uri ~name) metadata
100
101 let clean ~(dbh: Dbi.connection) ~owner =
102   let owned_uris =  (* list of uris in list-of-columns format *)
103     let query =
104       dbh#prepare (sprintf
105         "SELECT source FROM %s WHERE owner = \"%s\"" owners_tbl owner)
106     in
107     query#execute [];
108     List.map
109       (function [`String source_col] -> source_col | _ -> assert false)
110       (query#fetchall ())
111   in
112   let del_from tbl =
113     let query =
114       dbh#prepare (sprintf "DELETE FROM %s WHERE source LIKE \"?%%\"" tbl)
115     in
116     List.iter (fun source_col -> query#execute [`String source_col]) owned_uris
117   in
118   List.iter del_from
119     [sort_tbl; rel_tbl; obj_tbl; conclno_tbl; conclno_hyp_tbl; name_tbl;
120     owners_tbl]
121