[Haskell-cafe] Re: Using Takusen is a real intellectual challenge

oleg at okmij.org oleg at okmij.org
Tue May 5 01:20:18 EDT 2009

	Perhaps you might find the following code, relying on
low-level Takusen's functions, useful

The code is a stand-alone program that does the following:

docstrings = [
 "A helper to access a PostgreSQL from a `scripting' language.",
 "This function attempts to closely emulate `dbaccess' of Informix.",
 "The goal is to have my Scheme-to-Informix interface work with",
 "PostgreSQL with minimal changes.",
 "   dbaccess <conn-parms>",
 "where <conn-parms> is the string of database connection parameters.",
 "The program connects to the database and listens on its standard",
 "input for a SQL command. A command is an ASCII sequence terminated by",
 "If the command is other than SELECT or COPY, we submit it to the database.",
 "On success, no reply is generated, and we wait for a new command.",
 "If the command starts with a /*simple*/ SELECT, we expect few rows.",
 "We submit the query to the database and write the result to stdout.",
 "If the command was SELECT, we make a query and write the result,",
 "in groups, to the stdout. Each group of results has the form:",
 "  0 NL",
 " for the last group of results",
 " <nrows> <space> <ncols> NL",
 " followed by the matrix of <nrows> * <ncols> field values in the",
 " row-major order. Each field value has the form",
 " <length>NL<value>",
 " where <length> is the length of the value proper (no NL).",
 "If the command was COPY, we assume ",
 "   COPY /*<len> fname*/ table ... FROM STDIN...",
 " where <len> is the length of the file name. We open the file and",
 " send its contents to the server",
 "Any error causes the termination of this program.",
 "We log all the results to stderr."

The above was my attempt at comments shared between compile-time and
run-time. HERE documents would have certainly helped; I hope Haskell
will get them one day.

As the comments show, pgaccess is a very simple program to endow any
scripting language (Scheme in my case) to communicate with a
PostgreSQL database. The scripting language communicates with pgaccess
via a pipe; thus the language itself needs to know nothing about
databases and need not depend on any database libraries.  The code has
been designed keeping incremental processing firmly in mind. It was
written about three years ago and in constant use. The program runs
for days and indeed moves quite large amount of data to and from the
database (the amount of data in a transaction can be larger
than the amount of available memory).

More information about the Haskell-Cafe mailing list