]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/mathql_interpreter/mQIMySql.ml
Bug fixed: instead of generating "not ()", I generate "true" (for PostGresql)
[helm.git] / helm / ocaml / mathql_interpreter / mQIMySql.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    try Mysql.quick_connect
31        ~host:"mowgli.cs.unibo.it" ~database:"mowgli" ~user:"helm" ()
32    with _ -> raise (Failure "mqi_connecion")
33
34 let close c = Mysql.disconnect c
35
36 let quote s =
37    let rec quote_aux s =
38       try
39          let l = String.length s in
40          let i = String.index s '\'' in
41          String.sub s 0 i ^ "\\'" ^ quote_aux (String.sub s (succ i) (l - (succ i)))
42       with Not_found -> s
43    in
44    "'" ^ quote_aux s ^ "'"
45
46 let exec c q = 
47    let g = function None -> "" | Some v -> v in
48    let f a = List.map g (Array.to_list a) in
49 prerr_endline ("###\n" ^ q);
50    Mysql.map ~f:f (Mysql.exec c q), q
51
52 let exec c table cols ct cfl =
53    let rec iter f sep = function
54       | []           -> ""
55       | [head]       -> f head 
56       | head :: tail -> f head ^ sep ^ iter f sep tail
57    in
58    let pg_cols = iter (fun x -> x) ", " cols in
59    let pg_msval v = iter quote ", " v in
60    let pg_con (pat, col, v) = 
61       if col <> "" then 
62          let f s = col ^ " regexp " ^ quote ("^" ^ s ^ "$") in
63          if pat then "(" ^ iter f " or " v ^ ")"
64          else match v with 
65             | [s] -> col ^ " = " ^ (quote s)     
66             | v   -> col ^ " in (" ^ pg_msval v ^ ")"
67       else "1"
68    in
69    let pg_cons l = iter pg_con " and " l in
70    let pg_cons_not l =
71     match l with [] -> "1"  | _ -> "not (" ^ pg_cons l ^ ")" in
72    let pg_cons_not_l ll = iter pg_cons_not " and " ll in
73    let pg_where = match ct, cfl with
74       | [], []  -> ""
75       | lt, []  -> " where " ^ pg_cons lt
76       | [], llf -> " where " ^ pg_cons_not_l llf
77       | lt, llf -> " where " ^ pg_cons lt ^ " and " ^ pg_cons_not_l llf
78    in
79    if cols = [] then
80       let r, q = exec c ("select count(source) from " ^ table ^ pg_where) in
81       match r with
82          | [[s]] when int_of_string s > 0 -> [[]], q
83          | _                              -> [], q
84    else
85       exec c ("select " ^ pg_cols ^ " from " ^ table ^ pg_where ^ 
86             " order by " ^ List.hd cols ^ " asc")