* http://cs.unibo.it/helm/.
*)
-exception InvalidConnection
+(* AUTOR: Ferruccio Guidi <fguidi@cs.unibo.it>
+ *)
+
+type connection = MySQL_C of Mysql.dbd
+ | Postgres_C of Postgres.connection
+ | No_C
+
+type flag = Galax | Postgres | Queries | Result | Source | Times | Warn
+
+type handle = {
+ log : string -> unit; (* logging function *)
+ set : flag list; (* options *)
+ pgc : connection; (* PG connection *)
+ pgm : MQIMap.pg_map; (* PG conversion function *)
+ pga : MQIMap.pg_alias (* PG table aliases *)
+}
-type flag = Postgres | Galax | Stat | Quiet | Warn
+let tables handle p = MQIMap.get_tables handle.pgm p
-type handle = {log : string -> unit; (* logging function *)
- set : flag list; (* options *)
- pgc : Postgres.connection option (* Postgres connection *)
- }
+let field handle p t = MQIMap.get_field handle.pgm p t
+
+let resolve handle a = MQIMap.resolve handle.pga a
let log handle = handle.log
let set handle flag = List.mem flag handle.set
-let pgc handle =
- match handle.pgc with
- | None -> raise InvalidConnection
- | Some c -> c
+let pgc handle = handle.pgc
+
+let flags handle = handle.set
let string_of_flag = function
- | Postgres -> "P"
| Galax -> "G"
- | Stat -> "S"
- | Quiet -> "Q"
+ | Postgres -> "P"
+ | Queries -> "Q"
+ | Result -> "R"
+ | Source -> "S"
+ | Times -> "T"
| Warn -> "W"
let flag_of_char = function
- | 'P' -> [Postgres]
| 'G' -> [Galax]
- | 'S' -> [Stat]
- | 'Q' -> [Quiet]
+ | 'P' -> [Postgres]
+ | 'Q' -> [Queries]
+ | 'R' -> [Result]
+ | 'S' -> [Source]
+ | 'T' -> [Times]
| 'W' -> [Warn]
| _ -> []
let flags_of_string s =
string_fold_left (fun l c -> l @ flag_of_char c) [] s
-let init myflags mylog =
- let default_connection_string =
- "host=mowgli.cs.unibo.it dbname=helm_mowgli_new_schema user=helm"
+let init ?(flags = []) ?(log = ignore) () =
+ let flags =
+ if flags = [] then
+ flags_of_string (Helm_registry.get "mathql_interpreter.flags")
+ else
+ flags
in
- let connection_string =
- try Sys.getenv "POSTGRESQL_CONNECTION_STRING"
- with Not_found -> default_connection_string
+ let m, a =
+ let g =
+ if List.mem Galax flags
+ then MQIMap.empty_map else MQIMap.read_map
+ in g ()
in
- {log = mylog; set = myflags;
- pgc = if List.mem Galax myflags
- then None else Dbconn.init connection_string
+ {log = log; set = flags;
+ pgc = begin
+ try
+ if List.mem Galax flags then No_C else
+ if List.mem Postgres flags then Postgres_C (MQIPostgres.init ()) else
+ MySQL_C (MQIMySql.init ())
+ with Failure "mqi_connection" -> No_C
+ end;
+ pgm = m; pga = a
}
let close handle =
- if set handle Galax then () else Dbconn.close handle.pgc
+ match pgc handle with
+ | MySQL_C c -> MQIMySql.close c
+ | Postgres_C c -> MQIPostgres.close c
+ | No_C -> ()
+
+let exec handle out table cols ct cfl =
+ match pgc handle with
+ | MySQL_C c -> MQIMySql.exec (c, out) table cols ct cfl
+ | Postgres_C c -> MQIPostgres.exec (c, out) table cols ct cfl
+ | No_C -> []
let connected handle =
- if set handle Galax then false else
- try ignore (pgc handle); true with InvalidConnection -> false
+ pgc handle <> No_C
-let init_if_connected myflags mylog =
- let handle = init myflags mylog in
- ignore (pgc handle); handle
-
+let init_if_connected ?(flags = []) ?(log = ignore) () =
+ let handle = init ~flags:flags ~log:log () in
+ if connected handle then handle else raise (Failure "mqi connection failed")