]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/mathql_interpreter/mQIProperty.ml
new syntax for "property" enabled:
[helm.git] / helm / ocaml / mathql_interpreter / mQIProperty.ml
1
2 (* Copyright (C) 2000, HELM Team.
3  * 
4  * This file is part of HELM, an Hypertextual, Electronic
5  * Library of Mathematics, developed at the Computer Science
6  * Department, University of Bologna, Italy.
7  * 
8  * HELM is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  * 
13  * HELM is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with HELM; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
21  * MA  02111-1307, USA.
22  * 
23  * For details, see the HELM World-Wide-Web page,
24  * http://cs.unibo.it/helm/.
25  *)
26
27 module M = MathQL
28 module P = MQIPostgres
29 module U = MQIUtil
30
31 let not_supported s = 
32    raise (Failure ("MQIProperty: feature not supported: " ^ s)) 
33
34 (* debugging  ***************************************************************)
35
36 let pg_print l =
37    let rec pg_record = function 
38       | []           -> prerr_newline ()
39       | head :: tail -> prerr_string (head ^ " "); pg_record tail
40    in
41    List.iter pg_record l
42
43 let cl_print l =  
44    let c_print (b, p, v) =
45       prerr_string (if b then "match " else "in ");
46       List.iter (fun x -> prerr_string ("/" ^ x)) p;
47       prerr_newline ();
48       List.iter prerr_endline v
49    in
50    List.iter c_print l
51
52 (* PostgreSQL backend  ******************************************************)
53
54 let pg_query table cols ct cfl =
55    let rec iter f sep = function
56       | []           -> ""
57       | [head]       -> f head 
58       | head :: tail -> f head ^ sep ^ iter f sep tail
59    in
60    let pg_cols = iter (fun x -> x) ", " cols in
61    let pg_msval v = iter P.quote ", " v in
62    let pg_con (pat, col, v) = 
63       if col <> "" then 
64          let f s = col ^ " ~ " ^ P.quote ("^" ^ s ^ "$") in
65          if pat then "(" ^ iter f " or " v ^ ")"
66          else match v with 
67             | [s] -> col ^ " = " ^ (P.quote s)     
68             | v   -> col ^ " in (" ^ pg_msval v ^ ")"
69       else "true"
70    in
71    let pg_cons l = iter pg_con " and " l in
72    let pg_cons_not l = "not (" ^ pg_cons l ^ ")" in
73    let pg_cons_not_l ll = iter pg_cons_not " and " ll in
74    let pg_where = match ct, cfl with
75       | [], []  -> ""
76       | lt, []  -> " where " ^ pg_cons lt
77       | [], llf -> " where " ^ pg_cons_not_l llf
78       | lt, llf -> " where " ^ pg_cons lt ^ " and " ^ pg_cons_not_l llf
79    in
80    if cols = [] then [] else begin
81       let q = "select " ^ pg_cols ^ " from " ^ table ^ pg_where ^ 
82               " order by " ^ List.hd cols ^ " asc" in
83       prerr_endline q;      
84       P.exec q end
85    
86 let pg_result distinct subj el res =
87   let compose = if distinct then List.map else fun f -> U.mql_iter (fun x -> [f x]) in 
88   let get_name = function (p, None) -> p | (_, Some p) -> p in
89   let names = List.map get_name el in
90   let mk_grp l = 
91      let grp = U.mql_iter2 (fun p s -> [(p, [s])]) names l in 
92      if grp = [] then [] else [grp]
93   in
94   let mk_avs l =
95      if subj = "" then ("", mk_grp l) else (List.hd l, mk_grp (List.tl l))
96   in
97   compose mk_avs res
98
99 let get_table mc ct cfl el =
100    let fst = function
101       | []           -> None
102       | head :: tail -> Some head
103    in
104    let comp n1 n2 = match n1, n2 with
105       | None, _                   -> n2 
106       | Some h, Some k when h = k -> n2
107       | _, None                   -> n1
108       | _                         -> not_supported "comp"
109    in   
110    let rec get_c prev = function
111       | []                -> prev
112       | (_, p, _) :: tail -> get_c (comp prev (fst p)) tail
113    in
114    let rec get_e prev = function
115       | []             -> prev
116       | (p, _) :: tail -> get_e (comp prev (fst p)) tail
117    in
118    get_e (get_c (get_c (fst mc) ct) (List.concat cfl)) el  
119
120 let rec decolon s =
121    let l = String.length s in
122    try 
123       let i = String.index s ':' in
124       String.sub s 0 i ^ "_" ^ decolon (String.sub s (succ i) (l - succ i))
125    with Not_found -> s
126
127 let conv = function
128    | []             -> "source"
129    | ["objectName"] -> "value" 
130    | [t]            -> ""
131    | [_; t]         -> decolon t
132    | _              -> not_supported "conv" 
133
134 let postgres_single mc ct cfl el t =
135    let table = match t with Some t -> decolon t | None -> "objectName" in
136    let first = conv mc in
137    let mk_con l = List.map (fun (pat, p, v) -> (pat, conv p, v)) l in
138    let cons_true = mk_con ct in 
139    let cons_false = List.map mk_con cfl in
140    let other_cols = List.map (fun (p, _) -> conv p) el in 
141    let cols = if first = "" then other_cols else first :: other_cols in 
142    pg_result false first el (pg_query table cols cons_true cons_false) 
143    
144 let deadline = 100
145
146 let postgres refine mc ct cfl el =
147    if refine <> M.RefineExact then not_supported "postgres";   
148    let table = get_table mc ct cfl el in
149    let rec postgres_aux ct = match ct with 
150       | (pat, p, v) :: tail when List.length v > deadline ->
151          let single s = postgres_aux ((pat, p, [s]) :: tail) in
152          U.mql_iter single v
153       | _                                                 ->
154          postgres_single mc ct cfl el table
155    in postgres_aux ct    
156
157 (* Galax backend  ***********************************************************)
158
159 let galax refine mc ct cfl el = not_supported "Galax"
160
161 (* funzioni vecchie  ********************************************************)
162
163 let pg_name s = 
164    let q = "select id from registry where uri = " ^ P.quote s in
165    match P.exec q with
166       | [[id]] -> "t" ^ id
167       | _      -> ""
168
169 let get_id b = if b then ["B"] else ["F"]