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