Re: [GENERAL] Variable as a variable name in PL/pgSQL - Mailing list pgsql-general

From tolik@icomm.ru (Anatoly K. Lasareff)
Subject Re: [GENERAL] Variable as a variable name in PL/pgSQL
Date
Msg-id 87r9s971db.fsf@tolikus.hq.aaanet.ru
Whole thread Raw
In response to Variable as a variable name in PL/pgSQL  (Daniele Orlandi <daniele@orlandi.com>)
List pgsql-general
>>>>> "DO" == Daniele Orlandi <daniele@orlandi.com> writes:

 DO> I'm trying to implement a sort of templates for a user database.

 DO> I tought of two possibilities. In the first I have a table like this:

 DO> CREATE TABLE templates
 DO> (
 DO> tplname       varchar(32),
 DO> fieldname     varchar(32),
 DO> value         varchar(32)
 DO> );

 DO> Where I specify the value to be assigned to each field of the user's tuple.
 DO> I started writing a trigger to force the template values in the user's record
 DO> whenever it's updated or inserted. I'm using PL/pgSQL, and I'd like to continue
 DO> using it.

 DO> The function should look like the following:

 DO> CREATE FUNCTION users_trig_func() RETURNS opaque AS '
 DO> BEGIN

 DO> FOR row IN SELECT * FROM templates WHERE tplname = new.template;
 DO> NEW."row.fieldname" := row.fieldvalue;
 DO> END LOOP;

 DO> RETURN NEW;
 DO> END;
 DO> ' LANGUAGE 'plpgsql';


 DO> Obviously the problem is the >>>NEW."row.fieldname" := expr<<< statememt.
 DO> I dont know how (if possible at all) to assign a variabile whose name is
 DO> contained in another variable, something like eval() in PHP3.

 DO> Is there a syntax or a hack ?
 DO> Whould it be hard to implement it at language level ?

Excuse my english :(

I think this is impossible in 'plpgsql', because it is pre-compilrd
language.

But there is, in my mind, no
problem write script on any 'usual' language, such as perl. This
script can take data from your table, and send nesessary SQL queries
to server.


--
Anatoly K. Lasareff              Email:       tolik@icomm.ru
Senior programmer

pgsql-general by date:

Previous
From: Dustin Sallings
Date:
Subject: big bad join problems revisited
Next
From: Daniele Orlandi
Date:
Subject: Re: [GENERAL] Variable as a variable name in PL/pgSQL