Execute SQL statements with 'context'/predefined variables - Mailing list pgsql-sql

From blay bloo
Subject Execute SQL statements with 'context'/predefined variables
Date
Msg-id 66c468170709031102j1e21d313v512c488ccc289505@mail.gmail.com
Whole thread Raw
Responses Re: Execute SQL statements with 'context'/predefined variables
List pgsql-sql
I was wondering if it is possible to set the 'context' for running an
sql command:

For example, say you have a rule which upon update to table TEST of
type [a int, b varchar(10), c varchar(50)]:

INSERT INTO Log(a * 5, substring(b,0,2), randomize(c))

So an update of (5, 'hello', 'world') will add a row (25, 'he',
'##aa2') into the Log table.

Here we can perform operations and functions on the values of the
updated row, as the query engine has reference to the
(fieldname,value) pairs from the update. That is, the engine knows
that a=5, b='hello', etc.

My question - can you setup a similar 'context' for a statement
executed from python..

I.e. Let's say we have a python function, which has a name/value pair
tuple equivilent to the updated row
Is it possible to use the SQL parser to perform the operation in the
same manner?

I.e. from a plpython function can we do a:
plpy.execute("INSERT INTO Log(a * 5, substring(b,1,5), randomize(c))")-so that the db engine executing the insert knows
thata=5, b='hello'
 
and c='world',-and thus perform operations on them

Clearly, we can have python excute the functions, and execute a string
with the values (function results). Unfortunately I can't (easily) do
this, as the 'insert' definition functions are passed defined as
strings, in which case I'd have to write a parser to work out what are
the functions, operations, variable names, etc.

It would be great to leverage from the SQL engine to handle this task,
which I can easily do so long as I can pass some variables to the db
before executing the command...

I guess bottom line, is it possible to execute a bunch of SQL
statements with some predefined variables?

I'm sure you're wondering why not just do it ALL in SQL. Kinda
complicated, so I might stall on that explanation if possible.

Thanks for your help!

Blay


pgsql-sql by date:

Previous
From: PostgreSQL Admin
Date:
Subject: Constraints for grouping
Next
From: Richard Broersma Jr
Date:
Subject: Re: Constraints for grouping