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 cfl =
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 ^ ")"
67 | [s] -> col ^ " = " ^ (P.quote s)
68 | v -> col ^ " in (" ^ pg_msval v ^ ")"
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
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
80 if cols = [] then [] else begin
81 let q = "select " ^ pg_cols ^ " from " ^ table ^ pg_where ^
82 " order by " ^ List.hd cols ^ " asc" in
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
91 let grp = U.mql_iter2 (fun p s -> [(p, [s])]) names l in
92 if grp = [] then [] else [grp]
95 if subj = "" then ("", mk_grp l) else (List.hd l, mk_grp (List.tl l))
99 let get_table mc ct cfl el =
102 | head :: tail -> Some head
104 let comp n1 n2 = match n1, n2 with
106 | Some h, Some k when h = k -> n2
108 | _ -> not_supported "comp"
110 let rec get_c prev = function
112 | (_, p, _) :: tail -> get_c (comp prev (fst p)) tail
114 let rec get_e prev = function
116 | (p, _) :: tail -> get_e (comp prev (fst p)) tail
118 get_e (get_c (get_c (fst mc) ct) (List.concat cfl)) el
121 let l = String.length s in
123 let i = String.index s ':' in
124 String.sub s 0 i ^ "_" ^ decolon (String.sub s (succ i) (l - succ i))
129 | ["objectName"] -> "value"
131 | [_; t] -> decolon t
132 | _ -> not_supported "conv"
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)
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
154 postgres_single mc ct cfl el table
157 (* Galax backend ***********************************************************)
159 let galax refine mc ct cfl el = not_supported "Galax"
161 (* funzioni vecchie ********************************************************)
164 let q = "select id from registry where uri = " ^ P.quote s in
169 let get_id b = if b then ["B"] else ["F"]