]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/mathql_interpreter/mQIProperty.ml
MathQL query generator: new interface
[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 cf =
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 col ^ " in (" ^ pg_msval v ^ ")"
67       else "true"
68    in
69    let pg_cons l = iter pg_con " and " l in
70    let pg_cons_not l = "not (" ^ pg_cons l ^ ")" in
71    let pg_where = match ct, cf with
72       | [], [] -> ""
73       | lt, [] -> " where " ^ pg_cons lt
74       | [], lf -> " where " ^ pg_cons_not lf
75       | lt, lf -> " where " ^ pg_cons lt ^ " and " ^ pg_cons_not lf
76    in
77    if cols = [] then [] else begin
78       let q = "select " ^ pg_cols ^ " from " ^ table ^ pg_where ^ 
79               " order by " ^ List.hd cols ^ " asc" in
80       prerr_endline q;      
81       P.exec q end
82    
83 let pg_result distinct subj el res =
84   let compose = if distinct then List.map else fun f -> U.mql_iter (fun x -> [f x]) in 
85   let get_name = function (p, None) -> p | (_, Some p) -> p in
86   let names = List.map get_name el in
87   let mk_grp l = 
88      let grp = U.mql_iter2 (fun p s -> [(p, [s])]) names l in 
89      if grp = [] then [] else [grp]
90   in
91   let mk_avs l =
92      if subj = "" then ("", mk_grp l) else (List.hd l, mk_grp (List.tl l))
93   in
94   compose mk_avs res
95
96 let get_table mc ct cf el =
97    let fst = function
98       | []           -> None
99       | head :: tail -> Some head
100    in
101    let comp n1 n2 = match n1, n2 with
102       | None, _                   -> n2 
103       | Some h, Some k when h = k -> n2
104       | _, None                   -> n1
105       | _                         -> not_supported "comp"
106    in   
107    let rec get_c prev = function
108       | []                -> prev
109       | (_, p, _) :: tail -> get_c (comp prev (fst p)) tail
110    in
111    let rec get_e prev = function
112       | []             -> prev
113       | (p, _) :: tail -> get_e (comp prev (fst p)) tail
114    in
115    get_e (get_c (get_c (fst mc) ct) cf) el  
116
117 let rec decolon s =
118    let l = String.length s in
119    try 
120       let i = String.index s ':' in
121       String.sub s 0 i ^ "_" ^ decolon (String.sub s (succ i) (l - succ i))
122    with Not_found -> s
123
124 let conv = function
125    | []             -> "source"
126    | ["objectname"] -> "value" 
127    | [t]            -> ""
128    | [_; t]         -> decolon t
129    | _              -> not_supported "conv" 
130
131 let postgres_single mc ct cf el t =
132    let table = match t with Some t -> decolon t | None -> "objectname" in
133    let first = conv mc in
134    let mk_con l = List.map (fun (pat, p, v) -> (pat, conv p, v)) l in
135    let cons_true = mk_con ct in 
136    let cons_false = mk_con cf in
137    let other_cols = List.map (fun (p, _) -> conv p) el in 
138    let cols = if first = "" then other_cols else first :: other_cols in 
139    pg_result false first el (pg_query table cols cons_true cons_false) 
140    
141 let deadline = 100
142
143 let postgres refine mc ct cf el =
144    if refine <> M.RefineExact then not_supported "postgres";
145    cl_print ct;   
146    let table = get_table mc ct cf el in
147    let rec postgres_aux ct = match ct with 
148       | (pat, p, v) :: tail when List.length v > deadline ->
149          let single s = postgres_aux ((pat, p, [s]) :: tail) in
150          U.mql_iter single v
151       | _                                                 ->
152          postgres_single mc ct cf el table
153    in postgres_aux ct    
154
155 (* Galax backend  ***********************************************************)
156
157 let galax refine mc ct cf el = not_supported "Galax"
158
159 (* funzioni vecchie  ********************************************************)
160
161 let pg_name s = 
162    let q = "select id from registry where uri = " ^ P.quote s in
163    match P.exec q with
164       | [[id]] -> "t" ^ id
165       | _      -> ""
166
167 let get_id b = if b then ["B"] else ["F"]