Developers » PGOCaml

PG'OCaml is a type-safe, simple interface to PostgreSQL from OCaml. It lets you embed SQL statements directly into OCaml code.

New web page / project

Please note that there's a new project page for PG'OCaml here:

http://developer.berlios.de/projects/pgocaml/

The current page will go away soon, so update your bookmarks, links etc. to point above.

Example

 let () =
   let dbh = PGOCaml.connect () in
   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.printf "%ld %S %ld %S\n" id name salary email
   ) rows;
 
   PGOCaml.close dbh

Differences from other PostgreSQL bindings

Please note that this isn't the first or only PGSQL binding. Here are some others you may wish to consider:

PG'OCAML is different from the above bindings:

  1. It isn't just a wrapper around the C libpq library. Instead it's a pure OCaml library which talks the frontend/backend protocol directly with the database.
  2. It has a camlp4 layer which lets you write SQL statements directly in your code, type safe at compile time, with type inference into the SQL, and using the full PostgreSQL SQL grammar (sub-selects, PG-specific SQL, etc.). But the flip side of this is that you need to have access to the database at compile time, so the type checking magic can be done; also if you change the database schema, you will need to recompile your code to check it is still correctly typed.
  3. (A minor point) - It requires PostgreSQL >= 7.4. It's also synchronous, so if you want to have an interactive interface you'll need to use threads.
  4. It doesn't work with other databases, nor will it ever work with other databases.

Download

These versions are now historical. Latest versions can be obtained from this site: http://developer.berlios.de/projects/pgocaml/

PG'OCaml Copyright © 2006 Merjis Ltd, Richard W.M. Jones (rich@merjis.com)

This software is distributed under the GNU LGPL with OCaml linking exception. Please see the file COPYING.LIB for full license.

Requirements

Database configuration

Gabriel de Perthuis writes:

I'd just like to add this to the requirements: Users should make themselve postgresql users, like this:
sudo su postgres -c "createuser $USER"
and allow themselves to create databases. This must be done even before make is called in the pgocaml sources.

Documentation

Dario Teixeira wrote a tutorial for PG'OCaml: http://dario.dse.nl/projects/pgoctut/

There is also documentation included with the package, although it is rather sparse.

Also see the source for COCANWIKI.

Status

Hard problems

It is fairly common to construct SQL statements from string fragments, as in this pseudocode example:

let order_clause =
  match key, reverse with
  | `Author, false -> "author asc"
  | `Author, true -> "author desc"
  | `Title, false -> "title asc"
  | `Title, true -> "title desc"
let sql =
   "select title, author from books order by " ^ order_clause

Such statement-building is not currently permitted by PG'OCaml, unless you ditch the camlp4 extension and use the low-level, unsafe interface. It would be nice to have some sort of "fragment constructor" operator to allow the above to be expressed in a type-safe way. However because it is not possible to compile the fragments, it doesn't look like such a constructor could be written. If anyone has any ideas about this, please contact us through the feedback form on this page.

Merjis

effective web marketing

Merjis specialises in finding interested visitors and helping them to convert into leads or purchases. We use PPC, SEO, Social Media, Analytics, Usability, Design, etc.