]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/mathql_interpreter/mQIProperty.ml
functor added
[helm.git] / helm / ocaml / mathql_interpreter / mQIProperty.ml
1 (* Copyright (C) 2000, 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://cs.unibo.it/helm/.
24  *)
25
26 (*  AUTOR: Ferruccio Guidi <fguidi@cs.unibo.it>
27  *)
28
29 module M = MathQL
30 module I = M.I
31 module U = AvsUtil
32 module P = MQIPostgres
33 module C = MQIConn
34 module A = MQIMap
35
36 let not_supported s = 
37    raise (Failure ("MQIProperty: feature not supported: " ^ s)) 
38
39 (* debugging  ***************************************************************)
40
41 let pg_print l =
42    let rec pg_record = function 
43       | []           -> prerr_newline ()
44       | head :: tail -> prerr_string (head ^ " "); pg_record tail
45    in
46    List.iter pg_record l
47
48 let cl_print l =  
49    let c_print (b, p, v) =
50       prerr_string (if b then "match " else "in ");
51       List.iter (fun x -> prerr_string ("/" ^ x)) p;
52       prerr_newline ();
53       List.iter prerr_endline v
54    in
55    List.iter c_print l
56
57 (* PostgreSQL backend  ******************************************************)
58
59 let pg_query h table cols ct cfl =
60    let exec q = 
61       if C.set h C.Log then C.log h (q ^ "\n");
62       P.exec (C.pgc h) q 
63    in
64    let rec iter f sep = function
65       | []           -> ""
66       | [head]       -> f head 
67       | head :: tail -> f head ^ sep ^ iter f sep tail
68    in
69    let avs_iter f sep v =
70       let aux a s = function
71          | true  -> a ^ (f s) ^ sep
72          | false -> a ^ (f s)
73       in
74       I.iter aux "" v
75    in
76    let pg_cols = iter (fun x -> x) ", " cols in
77    let pg_msval v = avs_iter P.quote ", " v in
78    let pg_con (pat, col, v) = 
79       if col <> "" then 
80          let f s = col ^ " ~ " ^ P.quote ("^" ^ s ^ "$") in
81          if pat then "(" ^ avs_iter f " or " v ^ ")"
82          else match I.single v with 
83             | Some s -> col ^ " = " ^ (P.quote s)     
84             | None   -> col ^ " in (" ^ pg_msval v ^ ")"
85       else "true"
86    in
87    let pg_cons l = iter pg_con " and " l in
88    let pg_cons_not l = "not (" ^ pg_cons l ^ ")" in
89    let pg_cons_not_l ll = iter pg_cons_not " and " ll in
90    let pg_where = match ct, cfl with
91       | [], []  -> ""
92       | lt, []  -> " where " ^ pg_cons lt
93       | [], llf -> " where " ^ pg_cons_not_l llf
94       | lt, llf -> " where " ^ pg_cons lt ^ " and " ^ pg_cons_not_l llf
95    in
96    if cols = [] then
97       let r = exec ("select count (source) from " ^ table ^ pg_where) in
98       match r with
99          | [[s]] when int_of_string s > 0 -> [[]]
100          | _                              -> []
101    else
102       exec ("select " ^ pg_cols ^ " from " ^ table ^ pg_where ^ 
103             " order by " ^ List.hd cols ^ " asc")
104
105 (* Galax backend  ***********************************************************)
106
107 let gx_query h table cols ct cfl = not_supported "Galax"
108
109 (* Common functions  ********************************************************)
110
111 let pg_result distinct subj el res =
112   let res, compose =
113      if distinct then List.rev res, U.append_iter else res, U.iter 
114   in 
115   let get_name = function (p, None) -> p | (_, Some p) -> p in
116   let names = List.map get_name el in
117   let mk_grp l = U.grp_iter2 I.grp_make names l in
118   let mk_avs l = 
119      if subj = "" then I.make "" (mk_grp l) 
120      else I.make (List.hd l) (mk_grp (List.tl l))
121   in
122   compose mk_avs res
123
124 let get_table h mc ct cfl el =
125    let aux_c ts (_, p, _) = A.refine_tables ts (C.tables h p) in
126    let aux_e ts (p, _) = A.refine_tables ts (C.tables h p) in
127    let fst = C.tables h mc in
128    let snd = List.fold_left aux_c fst (ct @ (List.concat cfl)) in
129    let trd = List.fold_left aux_e snd el in
130    A.default_table trd
131
132 let exec_single h mc ct cfl el table =
133    let conv p = C.field h p table in
134    let first = conv mc in
135    let mk_con l = List.map (fun (pat, p, v) -> (pat, conv p, v)) l in
136    let cons_true = mk_con ct in 
137    let cons_false = List.map mk_con cfl in
138    let other_cols = List.map (fun (p, _) -> conv p) el in 
139    let cols = if first = "" then other_cols else first :: other_cols in
140    let low_level = if C.set h C.Galax then gx_query else pg_query in
141    let result = low_level h (C.resolve h table) cols cons_true cons_false in
142    pg_result false first el result 
143    
144 let deadline = 100
145
146 let exec h refine mc ct cfl el =
147    if refine <> M.RefineExact then not_supported "exec";   
148    let table = get_table h mc ct cfl el in
149    let rec exec_aux ct = match ct with 
150       | (pat, p, v) :: tail when U.count v > deadline ->
151          let single a s _ = I.union a (exec_aux ((pat, p, I.make s I.grp_empty) :: tail)) in
152          I.iter single U.val_false v
153       | _                                                 ->
154          exec_single h mc ct cfl el table
155    in exec_aux ct        
156
157 (* funzioni vecchie  ********************************************************)
158
159 let pg_name h s = 
160    let q = "select id from registry where uri = " ^ P.quote s in
161    match P.exec h q with
162       | [[id]] -> "t" ^ id
163       | _      -> ""
164
165 let get_id b = if b then ["B"] else ["F"] 
166