2 (* Copyright (C) 2000, HELM Team.
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.
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.
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.
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,
23 * For details, see the HELM World-Wide-Web page,
24 * http://cs.unibo.it/helm/.
28 module P = MQIPostgres
32 raise (Failure ("MQIProperty: feature not supported: " ^ s))
34 (* debugging ***************************************************************)
37 let rec pg_record = function
38 | [] -> prerr_newline ()
39 | head :: tail -> prerr_string (head ^ " "); pg_record tail
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;
48 List.iter prerr_endline v
52 (* PostgreSQL backend ******************************************************)
54 let pg_query table cols ct cf =
55 let rec iter f sep = function
58 | head :: tail -> f head ^ sep ^ iter f sep tail
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) =
64 let f s = col ^ " ~ " ^ P.quote ("^" ^ s ^ "$") in
65 if pat then "(" ^ iter f " or " v ^ ")"
66 else col ^ " in (" ^ pg_msval v ^ ")"
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
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
77 if cols = [] then [] else begin
78 let q = "select " ^ pg_cols ^ " from " ^ table ^ pg_where ^
79 " order by " ^ List.hd cols ^ " asc" in
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
88 let grp = U.mql_iter2 (fun p s -> [(p, [s])]) names l in
89 if grp = [] then [] else [grp]
92 if subj = "" then ("", mk_grp l) else (List.hd l, mk_grp (List.tl l))
96 let get_table mc ct cf el =
99 | head :: tail -> Some head
101 let comp n1 n2 = match n1, n2 with
103 | Some h, Some k when h = k -> n2
105 | _ -> not_supported "comp"
107 let rec get_c prev = function
109 | (_, p, _) :: tail -> get_c (comp prev (fst p)) tail
111 let rec get_e prev = function
113 | (p, _) :: tail -> get_e (comp prev (fst p)) tail
115 get_e (get_c (get_c (fst mc) ct) cf) el
118 let l = String.length s in
120 let i = String.index s ':' in
121 String.sub s 0 i ^ "_" ^ decolon (String.sub s (succ i) (l - succ i))
126 | ["objectname"] -> "value"
128 | [_; t] -> decolon t
129 | _ -> not_supported "conv"
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)
143 let postgres refine mc ct cf el =
144 if refine <> M.RefineExact then not_supported "postgres";
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
152 postgres_single mc ct cf el table
155 (* Galax backend ***********************************************************)
157 let galax refine mc ct cf el = not_supported "Galax"
159 (* funzioni vecchie ********************************************************)
162 let q = "select id from registry where uri = " ^ P.quote s in
167 let get_id b = if b then ["B"] else ["F"]