--- /dev/null
+
+(* Copyright (C) 2000, HELM Team.
+ *
+ * This file is part of HELM, an Hypertextual, Electronic
+ * Library of Mathematics, developed at the Computer Science
+ * Department, University of Bologna, Italy.
+ *
+ * HELM is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * HELM is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with HELM; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
+ * MA 02111-1307, USA.
+ *
+ * For details, see the HELM World-Wide-Web page,
+ * http://cs.unibo.it/helm/.
+ *)
+
+module M = MathQL
+module P = MQIPostgres
+module U = MQIUtil
+
+let not_supported s =
+ raise (Failure ("MQIProperty: feature not supported: " ^ s))
+
+(* debugging ***************************************************************)
+
+let pg_print l =
+ let rec pg_record = function
+ | [] -> prerr_newline ()
+ | head :: tail -> prerr_string (head ^ " "); pg_record tail
+ in
+ List.iter pg_record l
+
+let cl_print l =
+ let c_print (b, p, v) =
+ prerr_string (if b then "match " else "in ");
+ List.iter (fun x -> prerr_string ("/" ^ x)) p;
+ prerr_newline ();
+ List.iter prerr_endline v
+ in
+ List.iter c_print l
+
+(* PostgreSQL backend ******************************************************)
+
+let pg_query table cols ct cf =
+ let rec iter f sep = function
+ | [] -> ""
+ | [head] -> f head
+ | head :: tail -> f head ^ sep ^ iter f sep tail
+ in
+ let pg_cols = iter (fun x -> x) ", " cols in
+ let pg_msval v = iter P.quote ", " v in
+ let pg_con (pat, col, v) =
+ if col <> "" then
+ let f s = col ^ " ~ " ^ P.quote ("^" ^ s ^ "$") in
+ if pat then "(" ^ iter f " or " v ^ ")"
+ else col ^ " in (" ^ pg_msval v ^ ")"
+ else "true"
+ in
+ let pg_cons l = iter pg_con " and " l in
+ let pg_cons_not l = "not (" ^ pg_cons l ^ ")" in
+ let pg_where = match ct, cf with
+ | [], [] -> ""
+ | lt, [] -> " where " ^ pg_cons lt
+ | [], lf -> " where " ^ pg_cons_not lf
+ | lt, lf -> " where " ^ pg_cons lt ^ " and " ^ pg_cons_not lf
+ in
+ if cols = [] then [] else begin
+ let q = "select " ^ pg_cols ^ " from " ^ table ^ pg_where ^
+ " order by " ^ List.hd cols ^ " asc" in
+ prerr_endline q;
+ P.exec q end
+
+let pg_result distinct subj el res =
+ let compose = if distinct then List.map else fun f -> U.mql_iter (fun x -> [f x]) in
+ let get_name = function (p, None) -> p | (_, Some p) -> p in
+ let names = List.map get_name el in
+ let mk_grp l =
+ let grp = U.mql_iter2 (fun p s -> [(p, [s])]) names l in
+ if grp = [] then [] else [grp]
+ in
+ let mk_avs l =
+ if subj = "" then ("", mk_grp l) else (List.hd l, mk_grp (List.tl l))
+ in
+ compose mk_avs res
+
+let get_table mc ct cf el =
+ let fst = function
+ | [] -> None
+ | head :: tail -> Some head
+ in
+ let comp n1 n2 = match n1, n2 with
+ | None, _ -> n2
+ | Some h, Some k when h = k -> n2
+ | _, None -> n1
+ | _ -> not_supported "comp"
+ in
+ let rec get_c prev = function
+ | [] -> prev
+ | (_, p, _) :: tail -> get_c (comp prev (fst p)) tail
+ in
+ let rec get_e prev = function
+ | [] -> prev
+ | (p, _) :: tail -> get_e (comp prev (fst p)) tail
+ in
+ get_e (get_c (get_c (fst mc) ct) cf) el
+
+let rec decolon s =
+ let l = String.length s in
+ try
+ let i = String.index s ':' in
+ String.sub s 0 i ^ "_" ^ decolon (String.sub s (succ i) (l - succ i))
+ with Not_found -> s
+
+let conv = function
+ | [] -> "source"
+ | ["objectname"] -> "value"
+ | [t] -> ""
+ | [_; t] -> decolon t
+ | _ -> not_supported "conv"
+
+let postgres_single mc ct cf el t =
+ let table = match t with Some t -> decolon t | None -> "objectname" in
+ let first = conv mc in
+ let mk_con l = List.map (fun (pat, p, v) -> (pat, conv p, v)) l in
+ let cons_true = mk_con ct in
+ let cons_false = mk_con cf in
+ let other_cols = List.map (fun (p, _) -> conv p) el in
+ let cols = if first = "" then other_cols else first :: other_cols in
+ pg_result false first el (pg_query table cols cons_true cons_false)
+
+let deadline = 100
+
+let postgres refine mc ct cf el =
+ if refine <> M.RefineExact then not_supported "postgres";
+ cl_print ct;
+ let table = get_table mc ct cf el in
+ let rec postgres_aux ct = match ct with
+ | (pat, p, v) :: tail when List.length v > deadline ->
+ let single s = postgres_aux ((pat, p, [s]) :: tail) in
+ U.mql_iter single v
+ | _ ->
+ postgres_single mc ct cf el table
+ in postgres_aux ct
+
+(* Galax backend ***********************************************************)
+
+let galax refine mc ct cf el = not_supported "Galax"
+
+(* funzioni vecchie ********************************************************)
+
+let pg_name s =
+ let q = "select id from registry where uri = " ^ P.quote s in
+ match P.exec q with
+ | [[id]] -> "t" ^ id
+ | _ -> ""
+
+let get_id b = if b then ["B"] else ["F"]