1 (* Copyright (C) 2000, HELM Team.
3 * This file is part of HELM, an Hypertextual, Electronic
4 * Library of Mathematics, developed at the Computer Science
5 * Department, University of Bologna, Italy.
7 * HELM is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * HELM is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with HELM; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
22 * For details, see the HELM World-Wide-Web page,
23 * http://cs.unibo.it/helm/.
26 exception InvalidConnection
28 type flag = Postgres | Galax | Stat | Quiet | Warn
30 type handle = {log : string -> unit; (* logging function *)
31 set : flag list; (* options *)
32 pgc : Postgres.connection option (* Postgres connection *)
35 let log handle = handle.log
37 let set handle flag = List.mem flag handle.set
41 | None -> raise InvalidConnection
44 let string_of_flag = function
51 let flag_of_char = function
59 let string_fold_left f a s =
60 let l = String.length s in
61 let rec aux b i = if i = l then b else aux (f b s.[i]) (succ i) in
64 let string_of_flags flags =
65 List.fold_left (fun s flag -> s ^ string_of_flag flag) "" flags
67 let flags_of_string s =
68 string_fold_left (fun l c -> l @ flag_of_char c) [] s
70 let init myflags mylog =
71 let default_connection_string =
72 "host=mowgli.cs.unibo.it dbname=helm_mowgli_new_schema user=helm"
74 let connection_string =
75 try Sys.getenv "POSTGRESQL_CONNECTION_STRING"
76 with Not_found -> default_connection_string
78 {log = mylog; set = myflags;
79 pgc = if List.mem Galax myflags
80 then None else Dbconn.init connection_string
84 if set handle Galax then () else Dbconn.close handle.pgc
86 let connected handle =
87 if set handle Galax then false else
88 try ignore (pgc handle); true with InvalidConnection -> false
90 let init_if_connected myflags mylog =
91 let handle = init myflags mylog in
92 ignore (pgc handle); handle