]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/mathql_interpreter/mQIPostgres.ml
ocaml 3.09 transition
[helm.git] / helm / ocaml / mathql_interpreter / mQIPostgres.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 (*  AUTOR: Ferruccio Guidi <fguidi@cs.unibo.it>
27  *)
28
29 let init () =
30    let connection_string =
31       Helm_registry.get "mathql_interpreter.postgresql_connection_string"
32    in
33    try new Postgres.connection connection_string
34    with _ -> raise (Failure "mqi_connecion")
35
36 let close c = c#close
37
38 let quote s =
39    let rec quote_aux s =
40       try
41          let l = String.length s in
42          let i = String.index s '\'' in
43          String.sub s 0 i ^ "\\'" ^ quote_aux (String.sub s (succ i) (l - (succ i)))
44       with Not_found -> s
45    in
46    "'" ^ quote_aux s ^ "'"
47
48 let exec (c, out) q = out q; (c#exec q)#get_list
49
50 let exec c table cols ct cfl =
51    let rec iter f last sep = function
52       | []           -> last
53       | [head]       -> f head 
54       | head :: tail -> f head ^ sep ^ iter f last sep tail
55    in
56    let pg_cols = iter (fun x -> x) "" ", " cols in
57    let pg_msval v = iter quote "" ", " v in
58    let pg_con (pat, col, v) = 
59       if col <> "" then 
60          let f s = col ^ " ~ " ^ quote ("^" ^ s ^ "$") in
61          if pat then "(" ^ iter f "false" " or " v ^ ")"
62          else match v with 
63             | [s] -> col ^ " = " ^ (quote s)     
64             | v   -> col ^ " in (" ^ pg_msval v ^ ")"
65       else "true"
66    in
67    let pg_cons l = iter pg_con "true" " and " l in
68    let pg_cons_not l = "not (" ^ pg_cons l ^ ")" in
69    let pg_cons_not_l ll = iter pg_cons_not "true" " and " ll in
70    let pg_where = match ct, cfl with
71       | [], []  -> ""
72       | lt, []  -> " where " ^ pg_cons lt
73       | [], llf -> " where " ^ pg_cons_not_l llf
74       | lt, llf -> " where " ^ pg_cons lt ^ " and " ^ pg_cons_not_l llf
75    in
76    if cols = [] then
77       let r = exec c ("select count(source) from " ^ table ^ pg_where) in
78       match r with
79          | [[s]] when int_of_string s > 0 -> [[]]
80          | _                              -> []
81    else
82       exec c ("select " ^ pg_cols ^ " from " ^ table ^ pg_where ^ 
83             " order by " ^ List.hd cols ^ " asc")
84
85 (* funzioni vecchie  ********************************************************)
86 (*
87 let pg_name h s = 
88    let q = "select id from registry where uri = " ^ quote s in
89    match exec h q with
90       | [[id]] -> "t" ^ id
91       | _      -> ""
92
93 let get_id b = if b then ["B"] else ["F"] 
94 *)