<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE message PUBLIC
  "-//MLarc//DTD MLarc output files//EN"
  "../../mlarc.dtd"[
  <!ATTLIST message
    listname CDATA #REQUIRED
    title CDATA #REQUIRED
  >
]>

  <?xml-stylesheet href="../../mlarc.xsl" type="text/xsl"?>


<message 
  url="2006/02/e15ad84ba8a327339b7cf1b1364b31ca"
  from="Richard Jones &lt;rich@a...&gt;"
  author="Richard Jones"
  date="2006-02-18T16:01:56"
  subject="Re: [Caml-list] Type-safe interface to Postgres&apos;s SQL"
  prev="2006/02/3c23f567cce717e9a1937158a34569ce"
  next="2006/02/b6b3ba9e65e001f4a0dbca1489eda7d0"
  prev-in-thread="2006/01/08aaac82c72cdc32f1f032fad6288fc7"
  prev-thread="2006/01/a76d4927624d01eb1983aa142349d409"
  next-thread="2006/01/e40bc3f0e644ab5d3dbe0c66e82ea618"
  root="../../"
  period="month"
  listname="caml-list"
  title="Archives of the Caml mailing list">

<thread subject="Type-safe interface to Postgres&apos;s SQL">
<msg 
  url="2006/01/08aaac82c72cdc32f1f032fad6288fc7"
  from="Richard Jones &lt;rich@a...&gt;"
  author="Richard Jones"
  date="2006-01-31T13:39:53"
  subject="Type-safe interface to Postgres&apos;s SQL">
<msg 
  url="2006/02/e15ad84ba8a327339b7cf1b1364b31ca"
  from="Richard Jones &lt;rich@a...&gt;"
  author="Richard Jones"
  date="2006-02-18T16:01:56"
  subject="Re: [Caml-list] Type-safe interface to Postgres&apos;s SQL">
</msg>
</msg>
</thread>

<contents>
On Tue, Jan 31, 2006 at 02:07:33PM +0000, Richard Jones wrote:
&gt; (2) PostgreSQL doesn't track NULL types properly so we have to do a
&gt; bit of non-trivial guesswork to try to find out if a database field
&gt; could contain a NULL or not (and therefore whether to map its type to
&gt; 'a or 'a option).  For parameters this isn't possible at all, so all
&gt; parameter variables must have an option type.  For result fields, it
&gt; is usually possible to tell if the result corresponds to a known
&gt; column in the database, which is the case in ninety percent of
&gt; 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) -&gt;
      let email = match email with Some email -&gt; email | None -&gt; "-" 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

</contents>

</message>

