X-Git-Url: http://matita.cs.unibo.it/gitweb/?a=blobdiff_plain;f=helm%2Focaml%2Fmathql_interpreter%2FmQIPostgres.ml;h=916f787321daf86eada1f1224d1cd0be2efcc383;hb=4167cea65ca58897d1a3dbb81ff95de5074700cc;hp=fe4e3f6f95ac47d51dc9b9487e90de6687fd5191;hpb=48b9bb5e9504aba97cff28a9d7e2797feb42972e;p=helm.git diff --git a/helm/ocaml/mathql_interpreter/mQIPostgres.ml b/helm/ocaml/mathql_interpreter/mQIPostgres.ml index fe4e3f6f9..916f78732 100644 --- a/helm/ocaml/mathql_interpreter/mQIPostgres.ml +++ b/helm/ocaml/mathql_interpreter/mQIPostgres.ml @@ -23,29 +23,18 @@ * http://cs.unibo.it/helm/. *) -let default_connection_string = - "dbname=mowgli_test user=helm" - -let connection = ref None - -let connection_string = - try Sys.getenv "POSTGRESQL_CONNECTION_STRING" - with Not_found -> default_connection_string +(* AUTOR: Ferruccio Guidi + *) let init () = - try connection := Some (new Postgres.connection connection_string); - with _ -> raise (Failure ("MQIPostgres.init: " ^ connection_string)) - -let close () = match ! connection with - | None -> () - | Some c -> c#close + let connection_string = + Helm_registry.get "mathql_interpreter.postgresql_connection_string" + in + try new Postgres.connection connection_string + with _ -> raise (Failure "mqi_connecion") -let check () = ! connection <> None +let close c = c#close -let exec q = match ! connection with - | None -> [] - | Some c -> (c#exec q)#get_list - let quote s = let rec quote_aux s = try @@ -55,3 +44,51 @@ let quote s = with Not_found -> s in "'" ^ quote_aux s ^ "'" + +let exec (c, out) q = out q; (c#exec q)#get_list + +let exec c table cols ct cfl = + let rec iter f last sep = function + | [] -> last + | [head] -> f head + | head :: tail -> f head ^ sep ^ iter f last sep tail + in + let pg_cols = iter (fun x -> x) "" ", " cols in + let pg_msval v = iter quote "" ", " v in + let pg_con (pat, col, v) = + if col <> "" then + let f s = col ^ " ~ " ^ quote ("^" ^ s ^ "$") in + if pat then "(" ^ iter f "false" " or " v ^ ")" + else match v with + | [s] -> col ^ " = " ^ (quote s) + | v -> col ^ " in (" ^ pg_msval v ^ ")" + else "true" + in + let pg_cons l = iter pg_con "true" " and " l in + let pg_cons_not l = "not (" ^ pg_cons l ^ ")" in + let pg_cons_not_l ll = iter pg_cons_not "true" " and " ll in + let pg_where = match ct, cfl with + | [], [] -> "" + | lt, [] -> " where " ^ pg_cons lt + | [], llf -> " where " ^ pg_cons_not_l llf + | lt, llf -> " where " ^ pg_cons lt ^ " and " ^ pg_cons_not_l llf + in + if cols = [] then + let r = exec c ("select count(source) from " ^ table ^ pg_where) in + match r with + | [[s]] when int_of_string s > 0 -> [[]] + | _ -> [] + else + exec c ("select " ^ pg_cols ^ " from " ^ table ^ pg_where ^ + " order by " ^ List.hd cols ^ " asc") + +(* funzioni vecchie ********************************************************) +(* +let pg_name h s = + let q = "select id from registry where uri = " ^ quote s in + match exec h q with + | [[id]] -> "t" ^ id + | _ -> "" + +let get_id b = if b then ["B"] else ["F"] +*)