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
33 raise (Failure ("MQIProperty: feature not supported: " ^ s))
35 (* debugging ***************************************************************)
38 let rec pg_record = function
39 | [] -> prerr_newline ()
40 | head :: tail -> prerr_string (head ^ " "); pg_record tail
45 let c_print (b, p, v) =
46 prerr_string (if b then "match " else "in ");
47 List.iter (fun x -> prerr_string ("/" ^ x)) p;
49 List.iter prerr_endline v
53 (* PostgreSQL backend ******************************************************)
55 let pg_query h table cols ct cfl =
57 if C.set h C.Log then C.log h (q ^ "\n");
60 let rec iter f sep = function
63 | head :: tail -> f head ^ sep ^ iter f sep tail
65 let pg_cols = iter (fun x -> x) ", " cols in
66 let pg_msval v = iter P.quote ", " v in
67 let pg_con (pat, col, v) =
69 let f s = col ^ " ~ " ^ P.quote ("^" ^ s ^ "$") in
70 if pat then "(" ^ iter f " or " v ^ ")"
72 | [s] -> col ^ " = " ^ (P.quote s)
73 | v -> col ^ " in (" ^ pg_msval v ^ ")"
76 let pg_cons l = iter pg_con " and " l in
77 let pg_cons_not l = "not (" ^ pg_cons l ^ ")" in
78 let pg_cons_not_l ll = iter pg_cons_not " and " ll in
79 let pg_where = match ct, cfl with
81 | lt, [] -> " where " ^ pg_cons lt
82 | [], llf -> " where " ^ pg_cons_not_l llf
83 | lt, llf -> " where " ^ pg_cons lt ^ " and " ^ pg_cons_not_l llf
86 let r = exec ("select count (source) from " ^ table ^ pg_where) in
88 | [[s]] when int_of_string s > 0 -> [[]]
91 exec ("select " ^ pg_cols ^ " from " ^ table ^ pg_where ^
92 " order by " ^ List.hd cols ^ " asc")
94 (* Galax backend ***********************************************************)
96 let gx_query h table cols ct cfl = not_supported "Galax"
98 (* Common functions ********************************************************)
100 let pg_result distinct subj el res =
101 let compose = if distinct then List.map else fun f -> U.mql_iter (fun x -> [f x]) in
102 let get_name = function (p, None) -> p | (_, Some p) -> p in
103 let names = List.map get_name el in
105 let grp = U.mql_iter2 (fun p s -> [(p, [s])]) names l in
106 if grp = [] then [] else [grp]
109 if subj = "" then ("", mk_grp l) else (List.hd l, mk_grp (List.tl l))
113 let get_table mc ct cfl el =
116 | head :: tail -> Some head
118 let comp n1 n2 = match n1, n2 with
120 | Some h, Some k when h = k -> n2
122 | _ -> not_supported "comp"
124 let rec get_c prev = function
126 | (_, p, _) :: tail -> get_c (comp prev (fst p)) tail
128 let rec get_e prev = function
130 | (p, _) :: tail -> get_e (comp prev (fst p)) tail
132 get_e (get_c (get_c (fst mc) ct) (List.concat cfl)) el
135 let l = String.length s in
137 let i = String.index s ':' in
138 String.sub s 0 i ^ "_" ^ decolon (String.sub s (succ i) (l - succ i))
143 | ["objectName"] -> "value"
145 | [_; t] -> decolon t
146 | _ -> not_supported "conv"
148 let exec_single h mc ct cfl el t =
149 let table = match t with Some t -> decolon t | None -> "objectName" in
150 let first = conv mc in
151 let mk_con l = List.map (fun (pat, p, v) -> (pat, conv p, v)) l in
152 let cons_true = mk_con ct in
153 let cons_false = List.map mk_con cfl in
154 let other_cols = List.map (fun (p, _) -> conv p) el in
155 let cols = if first = "" then other_cols else first :: other_cols in
156 let low_level = if C.set h C.Galax then gx_query else pg_query in
157 pg_result false first el (low_level h table cols cons_true cons_false)
161 let exec h refine mc ct cfl el =
162 if refine <> M.RefineExact then not_supported "exec";
163 let table = get_table mc ct cfl el in
164 let rec exec_aux ct = match ct with
165 | (pat, p, v) :: tail when List.length v > deadline ->
166 let single s = exec_aux ((pat, p, [s]) :: tail) in
169 exec_single h mc ct cfl el table
172 (* funzioni vecchie ********************************************************)
175 let q = "select id from registry where uri = " ^ P.quote s in
176 match P.exec h q with
180 let get_id b = if b then ["B"] else ["F"]