Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] Printf question
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Christophe TROESTLER <debian00@t...>
Subject: Re: [Caml-list] Printf question
On Tue, 30 Sep 2003, Michal Moskal <malekith@pld-linux.org> wrote:
> 
> The example (well, SQL tables behind it), expects integer argument
> to "id = %a", but show that you can pass string (with
> string_conversion function) as argument to "id = %a".

Well, there is absolutely no way to make sure that the SQL types are
correctly matched to Caml types.  Moreover, the two type systems do
not even possess the same basic types (what about DECIMAL, DATE,...?).
So, the better one can do is to have the types as close as possible to
their use in the SQL query to avoid mistakes (they should be catched
at run time by the DB in any case).

> There is simple solution:
> 
> let sth = (fun i s -> dbh#prepare 
>            "select name from employees where id = %a and name = %a"
> 	   int_conversion i string_conversion s)

What I proposed some time ago was

let t = new DB.row_conversion
let q = dbh#prepapre "select name from employees where id = ? and name = ?"
let getsth = dbh#exec (t#int ++ t#string)

which gives the function [getsth : int -> string -> ('param, 'row) result].

This looks a bit complex but is the price to pay I think to be able to
have specific (or even tailor made) conversion functions for each
database (and to bind them directly to the DB structure, without
string intermediates).  Maybe, on top of functions of the kind of the
above, one could add some sugar -- I am particularly thinking of
something like the nice regexp sugar provided by Yukata Oiwa
(http://web.yl.is.s.u-tokyo.ac.jp/~oiwa/caml/).

[One could also try to associate to a source another one,
automatically generated, containing statements to check the queries
against the DB; thus asserting before running the (main) program that
none of its queries will fail because of a type mismatch.  A Camlp4
guru should be able to do that I guess.]

Cheers,
ChriS

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners