X-Git-Url: http://matita.cs.unibo.it/gitweb/?a=blobdiff_plain;f=helm%2Focaml%2Fmathql_interpreter%2FmQIProperty.ml;h=0433afd5eb37fdd051be9136204a15e81f5ede6c;hb=330d38a70331b13205cd3aabc420b3a112819346;hp=56a9d64212d44ea9bd0e4de0472b54da0957402f;hpb=99bd3d38d021bb5c618883e8a484891f227082c3;p=helm.git diff --git a/helm/ocaml/mathql_interpreter/mQIProperty.ml b/helm/ocaml/mathql_interpreter/mQIProperty.ml index 56a9d6421..0433afd5e 100644 --- a/helm/ocaml/mathql_interpreter/mQIProperty.ml +++ b/helm/ocaml/mathql_interpreter/mQIProperty.ml @@ -1,4 +1,3 @@ - (* Copyright (C) 2000, HELM Team. * * This file is part of HELM, an Hypertextual, Electronic @@ -24,9 +23,14 @@ * http://cs.unibo.it/helm/. *) +(* AUTOR: Ferruccio Guidi + *) + module M = MathQL -module P = MQIPostgres +module P = MQIDataBase +module C = MQIConn module U = MQIUtil +module A = MQIMap let not_supported s = raise (Failure ("MQIProperty: feature not supported: " ^ s)) @@ -51,7 +55,11 @@ let cl_print l = (* 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.Queries then C.log h (q ^ "\n"); + P.exec (C.pgc h) q + in let rec iter f sep = function | [] -> "" | [head] -> f head @@ -77,12 +85,21 @@ let pg_query table cols ct cfl = | [], 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 @@ -96,73 +113,44 @@ let pg_result distinct subj el res = in compose mk_avs res -let get_table mc ct cfl 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) (List.concat cfl)) 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 cfl el t = - let table = match t with Some t -> decolon t | None -> "objectName" in +let get_table h mc ct cfl el = + let aux_c ts (_, p, _) = A.refine_tables ts (C.tables h p) in + let aux_e ts (p, _) = A.refine_tables ts (C.tables h p) in + let fst = C.tables h mc in + let snd = List.fold_left aux_c fst (ct @ (List.concat cfl)) in + let trd = List.fold_left aux_e snd el in + A.default_table trd + +let exec_single h mc ct cfl el table = + let conv p = C.field h p table 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 + let result = low_level h (C.resolve h table) cols cons_true cons_false in + pg_result false first el result let deadline = 100 -let postgres refine mc ct cfl el = - if refine <> M.RefineExact then not_supported "postgres"; - let table = get_table mc ct cfl el in - let rec postgres_aux ct = match ct with +let exec h refine mc ct cfl el = + if refine <> M.RefineExact then not_supported "exec"; + let table = get_table h mc ct cfl el in + 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 | _ -> ""