From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by yquem.inria.fr (Postfix) with ESMTP id E243EBB81 for ; Sat, 18 Feb 2006 17:01:56 +0100 (CET) Received: from pauillac.inria.fr (pauillac.inria.fr [128.93.11.35]) by concorde.inria.fr (8.13.0/8.13.0) with ESMTP id k1IG1u73005241 for ; Sat, 18 Feb 2006 17:01:56 +0100 Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id RAA16265 for ; Sat, 18 Feb 2006 17:01:55 +0100 (MET) Received: from furbychan.cocan.org (furbychan.cocan.org [80.68.91.176]) by concorde.inria.fr (8.13.0/8.13.0) with ESMTP id k1IG1skB005238 (version=TLSv1/SSLv3 cipher=EDH-RSA-DES-CBC3-SHA bits=168 verify=NO) for ; Sat, 18 Feb 2006 17:01:55 +0100 Received: from rich by furbychan.cocan.org with local (Exim 3.35 #1 (Debian)) id 1FAUWv-0007IY-00 for ; Sat, 18 Feb 2006 16:01:53 +0000 Date: Sat, 18 Feb 2006 16:01:53 +0000 To: caml-list@inria.fr Subject: Re: [Caml-list] Type-safe interface to Postgres's SQL Message-ID: <20060218160151.GA28024@furbychan.cocan.org> References: <20060131140733.GA31772@furbychan.cocan.org> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20060131140733.GA31772@furbychan.cocan.org> User-Agent: Mutt/1.5.9i From: Richard Jones X-Miltered: at concorde with ID 43F744F4.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Miltered: at concorde with ID 43F744F2.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; caml-list:01 type-safe:01 non-trivial:01 corresponds:01 fwiw:01 tar:01 rewriting:01 printf:01 iter:01 printf:01 notepad:01 1.4:98 wrote:01 syntactic:01 compile:01 X-Spam-Checker-Version: SpamAssassin 3.0.3 (2005-04-27) on yquem.inria.fr X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=none autolearn=disabled version=3.0.3 On Tue, Jan 31, 2006 at 02:07:33PM +0000, Richard Jones wrote: > (2) PostgreSQL doesn't track NULL types properly so we have to do a > bit of non-trivial guesswork to try to find out if a database field > could contain a NULL or not (and therefore whether to map its type to > 'a or 'a option). For parameters this isn't possible at all, so all > parameter variables must have an option type. For result fields, it > is usually possible to tell if the result corresponds to a known > column in the database, which is the case in ninety percent of > queries. FWIW I have solved this problem with a small syntactic change. You now use $varname when you don't want NULLs and $?varname when you do.[1] A modified example program is attached. You can also download the full program from here: http://merjis.com/tmp/pgocaml-0.2.tar.gz Rich. [1] It is still possible to insert a NULL into a NOT NULL field, as was the case before, but I can't see any way to solve that without rewriting Postgres. ---------------------------------------------------------------------- (* Example program using typesafe calls to PostgreSQL. * $Id: test.ml,v 1.4 2006/02/18 15:54:06 rich Exp $ *) open Printf let () = let dbh = PGOCaml.connect () in PGSQL_EXECUTE_ON_COMPILE(dbh) "create temporary table employees ( id serial not null primary key, name text not null, salary int4 not null, email text )"; let insert name salary email = PGSQL(dbh) "insert into employees (name, salary, email) values ($name, $salary, $?email)" in insert "Ann" 10_000_l None; insert "Bob" 45_000_l None; insert "Jim" 20_000_l None; insert "Mary" 30_000_l (Some "mary@example.com"); let rows = PGSQL(dbh) "select id, name, salary, email from employees" in List.iter ( fun (id, name, salary, email) -> let email = match email with Some email -> email | None -> "-" in printf "%ld %S %ld %S\n" id name salary email ) rows; PGOCaml.close dbh -- Richard Jones, CTO Merjis Ltd. Merjis - web marketing and technology - http://merjis.com Team Notepad - intranets and extranets for business - http://team-notepad.com