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