Javascript support in the backend, i.e. PL/JS - Mailing list pgsql-hackers

From Sam Mason
Subject Javascript support in the backend, i.e. PL/JS
Date
Msg-id 20071116081043.GL1955@frubble.xen.chris-lamb.co.uk
Whole thread Raw
Responses Re: Javascript support in the backend, i.e. PL/JS  ("Pavel Stehule" <pavel.stehule@gmail.com>)
Re: Javascript support in the backend, i.e. PL/JS  (Andreas Joseph Krogh <andreak@officenet.no>)
Re: Javascript support in the backend, i.e. PL/JS  (Tom Lane <tgl@sss.pgh.pa.us>)
List pgsql-hackers
Hi All,

I've been writing some code[1] to support Javascript in the backend.
I've got the basic bits working, the next job for me is implementing
SPI support.  Currently, it runs simple bits of code like the
following:
 CREATE FUNCTION jsinc(n INTEGER) RETURNS INTEGER LANGUAGE pljs AS $$   return n+1; $$;

It knows to compile the code inside a javascript function, passing the
parameters with the specified names.  If no names are specified, $n
style naming is used--Javascript nicely deviates from C syntax in
respect of allowing $ in parameter names.  It knows how to handle
boolean and numeric (int[248], float[48] and numeric) types are known
about at the moment.  Javascript has only one numeric type, so
everything behaves as a double.

For SPI, I'm thinking that I'd currently like to attempt some object
orientated style interface.  In simplest terms, it would look a bit
like this:
 portal = {   next : function () { return {} }   close : function () { } }
 plan = {   query : function (args,readonly) { return portal; }   execute : function (args) { }   close : function () {
}}
 
 spi = {   prepare : function (sql,argtypes) { return plan; } }

So running some SQL would probably look something like:
 for (row in spi.prepare("SELECT 1 AS n").query()) {   print(row.n); }

The spi object would be passed into the javascript function as an extra
parameter, maybe with the name "__spi" to avoid name clashes.

I may put some shortcuts in if things turn out to be too slow later
on, but I'd prefer not to.  Most other languages seem to expose the
SPI functions directly, but that seems like a bit of a waste in a
language that should be able to do OO stuff.  PL/Java seems to have
its hands tied with JDBC, so I can't look there for much inspiration.
Are there any other OO languages that do things well?


Let me know what you think!

 Sam

p.s. the main reason for doing this is because I think Javascript is a
nice language!.  Having said that, Nulls are handled very badly by
javascript, so (1+null = 1) and ("_"+null+"_" = "_null_")!
[1] http://xen.samason.me.uk/~sam/repos/pljs/
    It's definitely work in progress!  I have fun with header    clashes between Postgres and Spidermonkey---hence the
split   into C files.
 


pgsql-hackers by date:

Previous
From: Peter Eisentraut
Date:
Subject: Re: [COMMITTERS] pgsql: update files for beta3
Next
From: "Pavel Stehule"
Date:
Subject: Re: Javascript support in the backend, i.e. PL/JS