module M = MathQL
module P = MQIPostgres
+module C = MQIConn
module U = MQIUtil
let not_supported s =
(* PostgreSQL backend ******************************************************)
-let pg_query table cols ct cfl =
+let pg_query h table cols ct cfl =
+ let exec q =
+ if C.set h C.Log then C.log h (q ^ "\n");
+ P.exec (C.pgc h) q
+ in
let rec iter f sep = function
| [] -> ""
| [head] -> f head
| [], llf -> " where " ^ pg_cons_not_l llf
| lt, llf -> " where " ^ pg_cons lt ^ " and " ^ pg_cons_not_l llf
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
-
+ if cols = [] then
+ let r = exec ("select count (source) from " ^ table ^ pg_where) in
+ match r with
+ | [[s]] when int_of_string s > 0 -> [[]]
+ | _ -> []
+ else
+ exec ("select " ^ pg_cols ^ " from " ^ table ^ pg_where ^
+ " order by " ^ List.hd cols ^ " asc")
+
+(* Galax backend ***********************************************************)
+
+let gx_query h table cols ct cfl = not_supported "Galax"
+
+(* Common functions ********************************************************)
+
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
| [_; t] -> decolon t
| _ -> not_supported "conv"
-let postgres_single mc ct cfl el t =
+let exec_single h mc ct cfl 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 = List.map mk_con cfl 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 cols = if first = "" then other_cols else first :: other_cols in
+ let low_level = if C.set h C.Galax then gx_query else pg_query in
+ pg_result false first el (low_level h table cols cons_true cons_false)
let deadline = 100
-let postgres refine mc ct cfl el =
- if refine <> M.RefineExact then not_supported "postgres";
+let exec h refine mc ct cfl el =
+ if refine <> M.RefineExact then not_supported "exec";
let table = get_table mc ct cfl el in
- let rec postgres_aux ct = match ct with
+ let rec exec_aux ct = match ct with
| (pat, p, v) :: tail when List.length v > deadline ->
- let single s = postgres_aux ((pat, p, [s]) :: tail) in
+ let single s = exec_aux ((pat, p, [s]) :: tail) in
U.mql_iter single v
| _ ->
- postgres_single mc ct cfl el table
- in postgres_aux ct
-
-(* Galax backend ***********************************************************)
-
-let galax refine mc ct cfl el = not_supported "Galax"
+ exec_single h mc ct cfl el table
+ in exec_aux ct
(* funzioni vecchie ********************************************************)
-let pg_name s =
+let pg_name h s =
let q = "select id from registry where uri = " ^ P.quote s in
- match P.exec q with
+ match P.exec h q with
| [[id]] -> "t" ^ id
| _ -> ""