]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/mathql_interpreter/mQIConn.ml
MQueryInterpreter: interface updated
[helm.git] / helm / ocaml / mathql_interpreter / mQIConn.ml
1 (* Copyright (C) 2000, HELM Team.
2  * 
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.
6  * 
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.
11  * 
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.
16  *
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,
20  * MA  02111-1307, USA.
21  * 
22  * For details, see the HELM World-Wide-Web page,
23  * http://cs.unibo.it/helm/.
24  *)
25
26 exception InvalidConnection
27
28 type flag = Postgres | Galax | Stat | Quiet | Warn
29
30 type handle = {log : string -> unit;            (* logging function    *)
31                set : flag list;                 (* options             *)
32                pgc : Postgres.connection option (* Postgres connection *)
33               }
34
35 let log handle = handle.log
36
37 let set handle flag = List.mem flag handle.set
38
39 let pgc handle = 
40    match handle.pgc with
41       | None   -> raise InvalidConnection
42       | Some c -> c
43
44 let string_of_flag = function
45       | Postgres -> "P"
46       | Galax    -> "G"
47       | Stat     -> "S"
48       | Quiet    -> "Q"
49       | Warn     -> "W"
50
51 let flag_of_char = function
52       | 'P' -> [Postgres]
53       | 'G' -> [Galax]
54       | 'S' -> [Stat]
55       | 'Q' -> [Quiet] 
56       | 'W' -> [Warn] 
57       | _   -> []
58
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 
62    aux a 0
63
64 let string_of_flags flags =
65    List.fold_left (fun s flag -> s ^ string_of_flag flag) "" flags
66
67 let flags_of_string s =
68    string_fold_left (fun l c -> l @ flag_of_char c) [] s
69
70 let init myflags mylog =
71    let default_connection_string =
72       "host=mowgli.cs.unibo.it dbname=helm_mowgli_new_schema user=helm"
73    in
74    let connection_string =
75       try Sys.getenv "POSTGRESQL_CONNECTION_STRING"
76       with Not_found -> default_connection_string 
77    in
78    {log = mylog; set = myflags; 
79     pgc = if List.mem Galax myflags 
80        then None else Dbconn.init connection_string
81    }       
82
83 let close handle =
84    if set handle Galax then () else Dbconn.close handle.pgc
85
86 let connected handle =
87    if set handle Galax then false else
88    try ignore (pgc handle); true with InvalidConnection -> false 
89
90 let init_if_connected myflags mylog =
91    let handle = init myflags mylog in
92    ignore (pgc handle); handle
93