X-Git-Url: http://matita.cs.unibo.it/gitweb/?a=blobdiff_plain;f=helm%2Focaml%2Fmathql_interpreter%2FmQIMySql.ml;h=46f350e210dc25169ccb5c7f49e018d9ef520c70;hb=4167cea65ca58897d1a3dbb81ff95de5074700cc;hp=170a2dfe548fb065dcd763b00d8930bff0bd3739;hpb=4a324741ecde2a78df8d0ef31d197af96b97472d;p=helm.git diff --git a/helm/ocaml/mathql_interpreter/mQIMySql.ml b/helm/ocaml/mathql_interpreter/mQIMySql.ml index 170a2dfe5..46f350e21 100644 --- a/helm/ocaml/mathql_interpreter/mQIMySql.ml +++ b/helm/ocaml/mathql_interpreter/mQIMySql.ml @@ -27,11 +27,21 @@ *) let init () = - try Mysql.quick_connect - ~host:"mowgli.cs.unibo.it" ~database:"mowgli" ~user:"helm" () + let module HR = Helm_registry in + let host = + HR.get_opt HR.get_string "mathql_interpreter.mysql_connection.host" in + let database = + HR.get_opt HR.get_string "mathql_interpreter.mysql_connection.database" in + let user = + HR.get_opt HR.get_string "mathql_interpreter.mysql_connection.user" in + let port = + HR.get_opt HR.get_int "mathql_interpreter.mysql_connection.port" in + let password = + HR.get_opt HR.get_string "mathql_interpreter.mysql_connection.password" in + try HMysql.quick_connect ?host ?database ?user ?port ?password () with _ -> raise (Failure "mqi_connecion") -let close c = Mysql.disconnect c +let close c = HMysql.disconnect c let quote s = let rec quote_aux s = @@ -43,33 +53,31 @@ let quote s = in "'" ^ quote_aux s ^ "'" -let exec c q = +let exec (c, out) q = let g = function None -> "" | Some v -> v in let f a = List.map g (Array.to_list a) in -prerr_endline ("###\n" ^ q); - Mysql.map ~f:f (Mysql.exec c q), q + out q; HMysql.map ~f:f (Mysql.exec c q) let exec c table cols ct cfl = - let rec iter f sep = function - | [] -> "" + let rec iter f last sep = function + | [] -> last | [head] -> f head - | head :: tail -> f head ^ sep ^ iter f sep tail + | 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_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 ^ " regexp " ^ quote ("^" ^ s ^ "$") in - if pat then "(" ^ iter f " or " v ^ ")" + if pat then "(" ^ iter f "0" " or " v ^ ")" else match v with | [s] -> col ^ " = " ^ (quote s) | v -> col ^ " in (" ^ pg_msval v ^ ")" else "1" in - let pg_cons l = iter pg_con " and " l in - let pg_cons_not l = - match l with [] -> "1" | _ -> "not (" ^ pg_cons l ^ ")" in - let pg_cons_not_l ll = iter pg_cons_not " and " ll in + let pg_cons l = iter pg_con "1" " and " l in + let pg_cons_not l = "not (" ^ pg_cons l ^ ")" in + let pg_cons_not_l ll = iter pg_cons_not "1" " and " ll in let pg_where = match ct, cfl with | [], [] -> "" | lt, [] -> " where " ^ pg_cons lt @@ -77,10 +85,10 @@ let exec c table cols ct cfl = | lt, llf -> " where " ^ pg_cons lt ^ " and " ^ pg_cons_not_l llf in if cols = [] then - let r, q = exec c ("select count(source) from " ^ table ^ pg_where) in + let r = exec c ("select count(source) from " ^ table ^ pg_where) in match r with - | [[s]] when int_of_string s > 0 -> [[]], q - | _ -> [], q + | [[s]] when int_of_string s > 0 -> [[]] + | _ -> [] else exec c ("select " ^ pg_cols ^ " from " ^ table ^ pg_where ^ " order by " ^ List.hd cols ^ " asc")