From 980a9931d3f7b8ce073247c88d7ecd42a672ad74 Mon Sep 17 00:00:00 2001 From: Ferruccio Guidi Date: Fri, 27 Feb 2004 15:12:18 +0000 Subject: [PATCH] MySql database support added --- helm/ocaml/mathql_interpreter/.depend | 14 ++-- helm/ocaml/mathql_interpreter/Makefile | 10 +-- helm/ocaml/mathql_interpreter/mQIMySql.ml | 23 +++++-- helm/ocaml/mathql_interpreter/mQIPostgres.ml | 21 ++++-- helm/ocaml/mathql_interpreter/mQIProperty.ml | 68 +------------------- helm/ocaml/mathql_interpreter/mQITypes.ml | 2 +- 6 files changed, 49 insertions(+), 89 deletions(-) diff --git a/helm/ocaml/mathql_interpreter/.depend b/helm/ocaml/mathql_interpreter/.depend index 739f2f90e..82921d322 100644 --- a/helm/ocaml/mathql_interpreter/.depend +++ b/helm/ocaml/mathql_interpreter/.depend @@ -1,18 +1,22 @@ -mQIConn.cmi: mQIMap.cmi +mQIMySql.cmi: mQITypes.cmo +mQIPostgres.cmi: mQITypes.cmo +mQIConn.cmi: mQIMap.cmi mQITypes.cmo mQILib.cmi: mQIConn.cmi mQIProperty.cmi: mQIConn.cmi mQueryIO.cmi: mQILib.cmi mQueryInterpreter.cmi: mQIConn.cmi +mQIMySql.cmo: mQIMySql.cmi +mQIMySql.cmx: mQIMySql.cmi mQIPostgres.cmo: mQIPostgres.cmi mQIPostgres.cmx: mQIPostgres.cmi mQIMap.cmo: mQIMap.cmi mQIMap.cmx: mQIMap.cmi -mQIConn.cmo: mQIMap.cmi mQIPostgres.cmi mQIConn.cmi -mQIConn.cmx: mQIMap.cmx mQIPostgres.cmx mQIConn.cmi +mQIConn.cmo: mQIMap.cmi mQIMySql.cmi mQIPostgres.cmi mQIConn.cmi +mQIConn.cmx: mQIMap.cmx mQIMySql.cmx mQIPostgres.cmx mQIConn.cmi mQILib.cmo: mQIConn.cmi mQILib.cmi mQILib.cmx: mQIConn.cmx mQILib.cmi -mQIProperty.cmo: mQIConn.cmi mQIMap.cmi mQIPostgres.cmi mQIProperty.cmi -mQIProperty.cmx: mQIConn.cmx mQIMap.cmx mQIPostgres.cmx mQIProperty.cmi +mQIProperty.cmo: mQIConn.cmi mQIMap.cmi mQIProperty.cmi +mQIProperty.cmx: mQIConn.cmx mQIMap.cmx mQIProperty.cmi mQueryTParser.cmo: mQILib.cmi mQueryTParser.cmi mQueryTParser.cmx: mQILib.cmx mQueryTParser.cmi mQueryTLexer.cmo: mQueryTParser.cmi diff --git a/helm/ocaml/mathql_interpreter/Makefile b/helm/ocaml/mathql_interpreter/Makefile index f2514f8e0..e9f0cdb1a 100644 --- a/helm/ocaml/mathql_interpreter/Makefile +++ b/helm/ocaml/mathql_interpreter/Makefile @@ -1,21 +1,23 @@ PACKAGE = mathql_interpreter -REQUIRES = postgres helm-registry helm-mathql +REQUIRES = postgres mysql helm-registry helm-mathql #natile-galax PREDICATES = -PRE_IFILES = mQIPostgres.mli mQIMap.mli mQIConn.mli \ +PRE_IFILES = mQIMySql.mli mQIPostgres.mli mQIMap.mli mQIConn.mli \ mQILib.mli mQIProperty.mli POST_IFILES = mQueryIO.mli mQueryInterpreter.mli mQueryStandard.mli INTERFACE_FILES = $(PRE_IFILES) $(POST_IFILES) -IMPLEMENTATION_FILES = $(PRE_IFILES:%.mli=%.ml) \ +IMPLEMENTATION_FILES = mQITypes.ml \ + $(PRE_IFILES:%.mli=%.ml) \ mQueryTParser.ml mQueryTLexer.ml \ $(POST_IFILES:%.mli=%.ml) -EXTRA_OBJECTS_TO_INSTALL = mQITypes.cmi mQueryTLexer.cmi \ +EXTRA_OBJECTS_TO_INSTALL = mQITypes.ml mQITypes.cmi \ + mQITypes.cmi mQueryTLexer.cmi \ mQITypes.ml mQueryTLexer.mll mQueryTParser.mly EXTRA_OBJECTS_TO_CLEAN = mQueryTParser.ml mQueryTParser.mli \ diff --git a/helm/ocaml/mathql_interpreter/mQIMySql.ml b/helm/ocaml/mathql_interpreter/mQIMySql.ml index af50af8ff..591bfe879 100644 --- a/helm/ocaml/mathql_interpreter/mQIMySql.ml +++ b/helm/ocaml/mathql_interpreter/mQIMySql.ml @@ -26,6 +26,8 @@ (* AUTOR: Ferruccio Guidi *) +module I = MathQL.I + let init () = try Mysql.quick_connect ~host:"mowgli.cs.unibo.it" ~database:"mowgli" ~user:"helm" () @@ -54,15 +56,22 @@ let exec c table cols ct cfl = | [head] -> f head | head :: tail -> f head ^ sep ^ iter f sep tail in + let avs_iter f sep v = + let aux a s = function + | true -> a ^ (f s) ^ sep + | false -> a ^ (f s) + in + I.iter aux "" v + in let pg_cols = iter (fun x -> x) ", " cols in - let pg_msval v = iter quote ", " v in + let pg_msval v = avs_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 ^ ")" - else match v with - | [s] -> "binary " ^ col ^ " = " ^ (quote s) - | v -> "binary " ^ col ^ " in (" ^ pg_msval v ^ ")" + let f s = col ^ " regexp " ^ quote ("^" ^ s ^ "$") in + if pat then "(" ^ avs_iter f " or " v ^ ")" + else match I.single v with + | Some s -> "binary " ^ col ^ " = " ^ (quote s) + | None -> "binary " ^ col ^ " in (" ^ pg_msval v ^ ")" else "1" in let pg_cons l = iter pg_con " and " l in @@ -81,4 +90,4 @@ let exec c table cols ct cfl = | _ -> [], q else exec c ("select " ^ pg_cols ^ " from " ^ table ^ pg_where ^ - " order by " ^ List.hd cols ^ " asc") + " order by " ^ List.hd cols ^ " asc") (* desc *) diff --git a/helm/ocaml/mathql_interpreter/mQIPostgres.ml b/helm/ocaml/mathql_interpreter/mQIPostgres.ml index cf82814e9..7b7345128 100644 --- a/helm/ocaml/mathql_interpreter/mQIPostgres.ml +++ b/helm/ocaml/mathql_interpreter/mQIPostgres.ml @@ -26,6 +26,8 @@ (* AUTOR: Ferruccio Guidi *) +module I = MathQL.I + let init () = let connection_string = Helm_registry.get "mathql_interpreter.postgresql_connection_string" @@ -53,15 +55,22 @@ let exec c table cols ct cfl = | [head] -> f head | head :: tail -> f head ^ sep ^ iter f sep tail in + let avs_iter f sep v = + let aux a s = function + | true -> a ^ (f s) ^ sep + | false -> a ^ (f s) + in + I.iter aux "" v + in let pg_cols = iter (fun x -> x) ", " cols in - let pg_msval v = iter quote ", " v in + let pg_msval v = avs_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 " or " v ^ ")" - else match v with - | [s] -> col ^ " = " ^ (quote s) - | v -> col ^ " in (" ^ pg_msval v ^ ")" + if pat then "(" ^ avs_iter f " or " v ^ ")" + else match I.single v with + | Some s -> col ^ " = " ^ (quote s) + | None -> col ^ " in (" ^ pg_msval v ^ ")" else "true" in let pg_cons l = iter pg_con " and " l in @@ -80,7 +89,7 @@ let exec c table cols ct cfl = | _ -> [], q else exec c ("select " ^ pg_cols ^ " from " ^ table ^ pg_where ^ - " order by " ^ List.hd cols ^ " asc") + " order by " ^ List.hd cols ^ " asc") (* desc *) (* funzioni vecchie ********************************************************) (* diff --git a/helm/ocaml/mathql_interpreter/mQIProperty.ml b/helm/ocaml/mathql_interpreter/mQIProperty.ml index a8b2e028d..e721606de 100644 --- a/helm/ocaml/mathql_interpreter/mQIProperty.ml +++ b/helm/ocaml/mathql_interpreter/mQIProperty.ml @@ -29,7 +29,6 @@ module M = MathQL module I = M.I module U = AvsUtil -module P = MQIPostgres module C = MQIConn module A = MQIMap @@ -54,58 +53,6 @@ let cl_print l = in List.iter c_print l -(* PostgreSQL backend ******************************************************) - -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 - | head :: tail -> f head ^ sep ^ iter f sep tail - in - let avs_iter f sep v = - let aux a s = function - | true -> a ^ (f s) ^ sep - | false -> a ^ (f s) - in - I.iter aux "" v - in - let pg_cols = iter (fun x -> x) ", " cols in - let pg_msval v = avs_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 "(" ^ avs_iter f " or " v ^ ")" - else match I.single v with - | Some s -> col ^ " = " ^ (P.quote s) - | None -> 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_cons_not_l ll = iter pg_cons_not " 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 ("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") (* desc *) - -(* Galax backend ***********************************************************) - -let gx_query h table cols ct cfl = not_supported "Galax" - (* Common functions ********************************************************) let pg_result subj el res = @@ -134,8 +81,8 @@ let exec_single h mc ct cfl el table = 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 - 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 + let result, q = C.exec h (C.resolve h table) cols cons_true cons_false in + if C.set h C.Queries then C.log h (q ^ "\n"); pg_result first el result let deadline = 100 @@ -150,14 +97,3 @@ let exec h refine mc ct cfl el = | _ -> exec_single h mc ct cfl el table in exec_aux ct - -(* funzioni vecchie ********************************************************) - -let pg_name h s = - let q = "select id from registry where uri = " ^ P.quote s in - match P.exec h q with - | [[id]] -> "t" ^ id - | _ -> "" - -let get_id b = if b then ["B"] else ["F"] - diff --git a/helm/ocaml/mathql_interpreter/mQITypes.ml b/helm/ocaml/mathql_interpreter/mQITypes.ml index 44b21ce18..67b59e68c 100644 --- a/helm/ocaml/mathql_interpreter/mQITypes.ml +++ b/helm/ocaml/mathql_interpreter/mQITypes.ml @@ -26,7 +26,7 @@ (* AUTOR: Ferruccio Guidi *) -type 'a con = MathQL.pattern * 'a * MathQL.value +type 'a con = MathQL.pattern * 'a * MathQL.result type 'a con_true = 'a con list -- 2.39.2