From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Delivered-To: caml-list@yquem.inria.fr Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by yquem.inria.fr (Postfix) with ESMTP id 596F7BCAE for ; Mon, 18 Jul 2005 22:56:17 +0200 (CEST) Received: from pauillac.inria.fr (pauillac.inria.fr [128.93.11.35]) by nez-perce.inria.fr (8.13.0/8.13.0) with ESMTP id j6IKuGK2031350 for ; Mon, 18 Jul 2005 22:56:17 +0200 Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id WAA04410 for ; Mon, 18 Jul 2005 22:56:16 +0200 (MET DST) Received: from furbychan.cocan.org (furbychan.cocan.org [80.68.91.176]) by nez-perce.inria.fr (8.13.0/8.13.0) with ESMTP id j6IKuFUK031342 (version=TLSv1/SSLv3 cipher=EDH-RSA-DES-CBC3-SHA bits=168 verify=NO) for ; Mon, 18 Jul 2005 22:56:16 +0200 Received: from rich by furbychan.cocan.org with local (Exim 3.35 #1 (Debian)) id 1Ducnf-00050m-00 for ; Mon, 18 Jul 2005 22:05:19 +0100 Date: Mon, 18 Jul 2005 22:05:19 +0100 To: caml-list@inria.fr Subject: Idea for another type safe PostgreSQL interface Message-ID: <20050718210518.GA10051@furbychan.cocan.org> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.3.28i From: Richard Jones X-Miltered: at nez-perce with ID 42DC1770.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Miltered: at nez-perce with ID 42DC176F.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; subset:01 ocaml:01 inference:01 printf:01 iter:01 printf:01 notepad:01 ...:98 ...:98 compile:01 expression:01 fragments:01 int:01 int:01 parameter:02 X-Spam-Checker-Version: SpamAssassin 3.0.2 (2004-11-16) on yquem.inria.fr X-Spam-Status: No, score=0.0 required=5.0 tests=none autolearn=disabled version=3.0.2 X-Spam-Level: [I just throwing this idea out there to see if people find it interesting, or want to shoot it down ... There're only fragments of working code at the moment] I'm thinking about a type safe interface to PostgreSQL. One such interface at the moment is in Xcaml, but it only supports a very small subset of SQL, and I like to use complex SQL. It seems that there is a way to support the whole of PostgreSQL's language from within OCaml programs, in a type safe way. The general plan would be to have a camlp4 extension which would use Postgres's new "PREPARE" feature to actually prepare the statements, and Postgres's other new feature, "Describe Statement", to pull out the parameter types and result types from the prepared statement. This allows the camlp4 extension to replace the statement string with a type safe expression, and allow type inference to find mismatches. How a typical program would look is shown at the end of this message. The advantages are: * Complete support for PostgreSQL statements with hardly any programming effort. * Type safe. * Schema is stored in one place - the database - and doesn't need to be duplicated. The obvious disadvantages stem from the fact that at _compile time_, you need to have access to the database. Perhaps others will think of other disadvantages. (I currently don't care about databases which aren't Postgres ...) Thoughts? Rich. ---------------------------------------------------------------------- open Printf open Postgresql_typed (* create temporary test ( id serial not null primary key, str text not null, num int4 not null ) *) let () = let dbh = new Postgresql.connection ~dbname:"rich" in let insert t i = PGSQL(dbh) "insert into test (str, num) values ($t, $i)" in insert "hello" 10; insert "world" 20; let rows = PGSQL(dbh) "select id, str, num from test order by 3, 2, 1" in (* 'rows' would have a type like (int, string, int) Rows.t *) Rows.iter ( fun (id, text, num) -> printf "id = %ld, text = %s, num = %ld\n" id text num ) rows -- Richard Jones, CTO Merjis Ltd. Merjis - web marketing and technology - http://merjis.com Team Notepad - intranets and extranets for business - http://team-notepad.com