[plt-scheme] Scsh script <-- Postgres database --> PLT program

From: Vadim Nasardinov (el-vadimo at comcast.net)
Date: Wed Apr 27 08:38:40 EDT 2005

On Tue, 2005-04-26 at 11:58 -0700, nishad at ptolemy.tlg.uci.edu wrote:
> I have a scsh script that does some logfile munging, using grep and
> wc and such like.  This script needs to get a list of IP addresses
> that are stored in a Postgres DB.
> I've managed (if that is the word) to get SchemeQL working on
> Solaris, so now my PLT scheme programs can access database values.
> I want to write one program that does both: retrieve DB values on
> its own, and use the Unix shell tools.  Is there a way to do this,
> without the clunkiness of having the scsh program call the PLT
> program to write the DB values out to file, then read the file, and
> so on...?

There are ways.

One low-tech solution is to use the psql interface.  For the sake of
illustration, I created a table named IP_ADDRESSES and inserted three
rows into it.  You can query it by exec'ing some variation of the
following command from Scsh:

 | $ psql --host localhost --user johnsmith --command 'select * from ip_addresses' sampledb
 | Password: 
 |      ip      
 | -------------
 | (3 rows)

If you want a more high-tech (and correspondingly more difficult to
implement) solution, you can kluge a quick and dirty partial
implementation of the PostgreSQL frontend-backend V3 protocol that
gives you just enough rope to hang yourself by sending simple one-off
SELECT queries to the backend without doing server-side prepared
statements or any of that fancier, robust stuff:


All you need for this approach to work is a good socket library which,
AFAIK, Scsh does have.

Both of these approaches should be doable entirely from within Scsh
without having to rely on PLT Scheme.

Posted on the users mailing list.