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 (* AUTOR: Ferruccio Guidi <fguidi@cs.unibo.it>
32 let connection_string =
33 Helm_registry.get "mathql_interpreter.postgresql_connection_string"
35 try new Postgres.connection connection_string
36 with _ -> raise (Failure "mqi_connecion")
43 let l = String.length s in
44 let i = String.index s '\'' in
45 String.sub s 0 i ^ "\\'" ^ quote_aux (String.sub s (succ i) (l - (succ i)))
48 "'" ^ quote_aux s ^ "'"
50 let exec c q = (c#exec q)#get_list, q
52 let exec c table cols ct cfl =
53 let rec iter f sep = function
56 | head :: tail -> f head ^ sep ^ iter f sep tail
58 let avs_iter f sep v =
59 let aux a s = function
60 | true -> a ^ (f s) ^ sep
65 let pg_cols = iter (fun x -> x) ", " cols in
66 let pg_msval v = avs_iter quote ", " v in
67 let pg_con (pat, col, v) =
69 let f s = col ^ " ~ " ^ quote ("^" ^ s ^ "$") in
70 if pat then "(" ^ avs_iter f " or " v ^ ")"
71 else match I.single v with
72 | Some s -> col ^ " = " ^ (quote s)
73 | None -> col ^ " in (" ^ pg_msval v ^ ")"
76 let pg_cons l = iter pg_con " and " l in
77 let pg_cons_not l = "not (" ^ pg_cons l ^ ")" in
78 let pg_cons_not_l ll = iter pg_cons_not " and " ll in
79 let pg_where = match ct, cfl with
81 | lt, [] -> " where " ^ pg_cons lt
82 | [], llf -> " where " ^ pg_cons_not_l llf
83 | lt, llf -> " where " ^ pg_cons lt ^ " and " ^ pg_cons_not_l llf
86 let r, q = exec c ("select count (source) from " ^ table ^ pg_where) in
88 | [[s]] when int_of_string s > 0 -> [[]], q
91 exec c ("select " ^ pg_cols ^ " from " ^ table ^ pg_where ^
92 " order by " ^ List.hd cols ^ " asc") (* desc *)
94 (* funzioni vecchie ********************************************************)
97 let q = "select id from registry where uri = " ^ quote s in
102 let get_id b = if b then ["B"] else ["F"]