1 (* Copyright (C) 2000, HELM Team.
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.
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.
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.
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,
22 * For details, see the HELM World-Wide-Web page,
23 * http://cs.unibo.it/helm/.
26 (* AUTOR: Ferruccio Guidi <fguidi@cs.unibo.it>
30 module P = MQIPostgres
35 raise (Failure ("MQIProperty: feature not supported: " ^ s))
37 (* debugging ***************************************************************)
40 let rec pg_record = function
41 | [] -> prerr_newline ()
42 | head :: tail -> prerr_string (head ^ " "); pg_record tail
47 let c_print (b, p, v) =
48 prerr_string (if b then "match " else "in ");
49 List.iter (fun x -> prerr_string ("/" ^ x)) p;
51 List.iter prerr_endline v
55 (* PostgreSQL backend ******************************************************)
57 let pg_query h table cols ct cfl =
59 if C.set h C.Log then C.log h (q ^ "\n");
62 let rec iter f sep = function
65 | head :: tail -> f head ^ sep ^ iter f sep tail
67 let pg_cols = iter (fun x -> x) ", " cols in
68 let pg_msval v = iter P.quote ", " v in
69 let pg_con (pat, col, v) =
71 let f s = col ^ " ~ " ^ P.quote ("^" ^ s ^ "$") in
72 if pat then "(" ^ iter f " or " v ^ ")"
74 | [s] -> col ^ " = " ^ (P.quote s)
75 | v -> col ^ " in (" ^ pg_msval v ^ ")"
78 let pg_cons l = iter pg_con " and " l in
79 let pg_cons_not l = "not (" ^ pg_cons l ^ ")" in
80 let pg_cons_not_l ll = iter pg_cons_not " and " ll in
81 let pg_where = match ct, cfl with
83 | lt, [] -> " where " ^ pg_cons lt
84 | [], llf -> " where " ^ pg_cons_not_l llf
85 | lt, llf -> " where " ^ pg_cons lt ^ " and " ^ pg_cons_not_l llf
88 let r = exec ("select count (source) from " ^ table ^ pg_where) in
90 | [[s]] when int_of_string s > 0 -> [[]]
93 exec ("select " ^ pg_cols ^ " from " ^ table ^ pg_where ^
94 " order by " ^ List.hd cols ^ " asc")
96 (* Galax backend ***********************************************************)
98 let gx_query h table cols ct cfl = not_supported "Galax"
100 (* Common functions ********************************************************)
102 let pg_result distinct subj el res =
103 let compose = if distinct then List.map else fun f -> U.mql_iter (fun x -> [f x]) in
104 let get_name = function (p, None) -> p | (_, Some p) -> p in
105 let names = List.map get_name el in
107 let grp = U.mql_iter2 (fun p s -> [(p, [s])]) names l in
108 if grp = [] then [] else [grp]
111 if subj = "" then ("", mk_grp l) else (List.hd l, mk_grp (List.tl l))
115 let get_table mc ct cfl el =
118 | head :: tail -> Some head
120 let comp n1 n2 = match n1, n2 with
122 | Some h, Some k when h = k -> n2
124 | _ -> not_supported "comp"
126 let rec get_c prev = function
128 | (_, p, _) :: tail -> get_c (comp prev (fst p)) tail
130 let rec get_e prev = function
132 | (p, _) :: tail -> get_e (comp prev (fst p)) tail
134 get_e (get_c (get_c (fst mc) ct) (List.concat cfl)) el
137 let l = String.length s in
139 let i = String.index s ':' in
140 String.sub s 0 i ^ "_" ^ decolon (String.sub s (succ i) (l - succ i))
145 | ["objectName"] -> "value"
147 | [_; t] -> decolon t
148 | _ -> not_supported "conv"
150 let exec_single h mc ct cfl el t =
151 let table = match t with Some t -> decolon t | None -> "objectName" in
152 let first = conv mc in
153 let mk_con l = List.map (fun (pat, p, v) -> (pat, conv p, v)) l in
154 let cons_true = mk_con ct in
155 let cons_false = List.map mk_con cfl in
156 let other_cols = List.map (fun (p, _) -> conv p) el in
157 let cols = if first = "" then other_cols else first :: other_cols in
158 let low_level = if C.set h C.Galax then gx_query else pg_query in
159 pg_result false first el (low_level h table cols cons_true cons_false)
163 let exec h refine mc ct cfl el =
164 if refine <> M.RefineExact then not_supported "exec";
165 let table = get_table mc ct cfl el in
166 let rec exec_aux ct = match ct with
167 | (pat, p, v) :: tail when List.length v > deadline ->
168 let single s = exec_aux ((pat, p, [s]) :: tail) in
171 exec_single h mc ct cfl el table
174 (* funzioni vecchie ********************************************************)
177 let q = "select id from registry where uri = " ^ P.quote s in
178 match P.exec h q with
182 let get_id b = if b then ["B"] else ["F"]