pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Ganesh <ganesh@volleyball.com>
To: pgsql-sql@postgresql.org
Subject: plpgsql dynamic record access
Date: Tue, 31 May 2005 23:54:25 -0700 (PDT)
Message-ID: <20050601065425.46B8937DF0@sitemail.everyone.net> (raw)

<html><body> <font style="font-family: arial,helvetica,sans-serif;" size="2">Hello,
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; I have searched everywere&nbsp;
for a solution pls help me. I am building a trigger which would keep
track of every modification to some table. here i am generlising the
function. the following is the code in plpgsql.
<br>
&nbsp;<br>
&nbsp;<br>
--/* function for giving the field attributes of a given table */
<br>
CREATE FUNCTION get_fld_name(text) RETURNS _varchar AS '
<br>
DECLARE
<br>
&nbsp;&nbsp; out_fld text[20] := array[''name'',''no''];
<br>
&nbsp;<br>
BEGIN
<br>
&nbsp; RETURN out_fld;
<br>
END;
<br>
' LANGUAGE 'plpgsql';
<br>
&nbsp;<br>
--/** function for recording the changes&nbsp; **/
<br>
&nbsp;<br>
CREATE FUNCTION my_func_test() RETURNS OPAQUE AS '
<br>
DECLARE
<br>
&nbsp;<br>
&nbsp;flds text[20];
<br>
&nbsp;fldname text;
<br>
&nbsp;<br>
&nbsp;ofld text;
<br>
&nbsp;nfld text;
<br>
&nbsp;<br>
BEGIN
<br>
&nbsp;<br>
flds := (SELECT *&nbsp; from&nbsp; get_fld_name(TG_RELNAME));
<br>
&nbsp; IF TG_OP=''INSERT'' THEN
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ofld=nfld;
<br>
&nbsp; END IF;
<br>
&nbsp; FOR i IN 1..20 loop
<br>
&nbsp;&nbsp;&nbsp; IF flds[i] &lt;&gt; ''NULL'' THEN
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fldname := flds[i];
<br>
&nbsp;<br>
<span style="background-color: rgb(255, 255, 255); color: rgb(0, 0, 153);">--/**************** Here is the problem ****************/
</span><br style="background-color: rgb(255, 255, 255); color: rgb(0, 0, 153);">
<span style="background-color: rgb(255, 255, 255); color: rgb(0, 0, 153);">&nbsp;</span><br style="background-color: rgb(255, 255, 255); color: rgb(0, 0, 153);">
<span style="background-color: rgb(255, 255, 255); color: rgb(0, 0, 153);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ofld := OLD.fldname;&nbsp; -- this should return Old's value
</span><br style="background-color: rgb(255, 255, 255); color: rgb(0, 0, 153);">
<span style="background-color: rgb(255, 255, 255); color: rgb(0, 0, 153);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; nfld := NEW.fldname;&nbsp; -- this should return New's value
</span><br style="background-color: rgb(255, 255, 255); color: rgb(0, 0, 153);">
<span style="background-color: rgb(255, 255, 255); color: rgb(0, 0, 153);">&nbsp;</span><br style="background-color: rgb(255, 255, 255); color: rgb(0, 0, 153);">
<span style="background-color: rgb(255, 255, 255); color: rgb(0, 0, 153);">--/****I want to get he value from OLD.name or OLD.No *******/
</span><br style="background-color: rgb(255, 255, 255); color: rgb(0, 0, 153);">
<span style="background-color: rgb(255, 255, 255); color: rgb(0, 0, 153);">&nbsp;</span><br style="background-color: rgb(255, 255, 255); color: rgb(0, 0, 153);">
<span style="background-color: rgb(255, 255, 255); color: rgb(0, 0, 153);">--/***********************************************************
</span><br style="background-color: rgb(255, 255, 255); color: rgb(0, 0, 153);">
<span style="background-color: rgb(255, 255, 255); color: rgb(0, 0, 153);">&nbsp;</span><br style="background-color: rgb(255, 255, 255); color: rgb(0, 0, 153);">
<span style="background-color: rgb(255, 255, 255); color: rgb(0, 0, 153);">&nbsp;&nbsp;&nbsp;
Is there any way to concatnate the 2 Objects [(OLD) and (Fldname) ] so
that fldname would be converted to the value but the object OLD remains
same.
</span><br style="background-color: rgb(255, 255, 255); color: rgb(0, 0, 153);">
<span style="background-color: rgb(255, 255, 255); color: rgb(0, 0, 153);">&nbsp;</span><br style="background-color: rgb(255, 255, 255); color: rgb(0, 0, 153);">
<span style="background-color: rgb(255, 255, 255); color: rgb(0, 0, 153);">***********************************************************/</span>
<br>
&nbsp;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ofld := old_data[i];
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; nfld := new_data[i];
<br>
&nbsp;<br>
&nbsp;&nbsp;&nbsp; INSERT INTO Audit_table values (5,TG_RELNAME,''now'',TG_OP,fldname,ofld,nfld,current_user);
<br>
&nbsp;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; RAISE NOTICE ''ss : % '',fldname;
<br>
&nbsp;&nbsp; RAISE NOTICE ''ofldname : % '',ofld;
<br>
&nbsp;&nbsp; RAISE NOTICE ''nfldname : % '',nfld;
<br>
&nbsp;<br>
&nbsp;&nbsp;&nbsp; END IF;
<br>
&nbsp; END LOOP;
<br>
&nbsp;RETURN NEW;
<br>
END;
<br>
&nbsp; ' LANGUAGE 'plpgsql';
<br>
&nbsp;<br>
&nbsp;<br>
&nbsp;<br>
CREATE TRIGGER test_trig AFTER UPDATE ON test
<br>
FOR EACH ROW EXECUTE PROCEDURE my_func_test();
<br>
&nbsp;<br>
&nbsp;<br>
&nbsp;<br>
Please Help me..
<br>
&nbsp;<br>
Thanks ,<br>&nbsp;<br>R.Ganesh<br><br>mail to :&nbsp; ganesh@volleyball.com<br><br></font> <br>&nbsp;<br><hr>Get your FREE email @ http://Volleyball.Com  -  Get everything for volleyball @ http://Shop.Volleyball.Com  -  Post a message @ http://Forum.Volleyball.Com<br></body></html;



view thread (2+ messages)  latest in thread

Message-ID: <20050601065425.46B8937DF0@sitemail.everyone.net>
Permalink:  ../20050601065425.46B8937DF0@sitemail.everyone.net/
Also on:    postgresql.org/message-id/20050601065425.46B8937DF0@sitemail.everyone.net

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: ganesh@volleyball.com
  Subject: Re: plpgsql dynamic record access
  In-Reply-To: <20050601065425.46B8937DF0@sitemail.everyone.net>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox