Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Data Bases > Pgsql General > Re: Is this pos...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 6 of 8 Topic 15474 of 16301
Post > Topic >>

Re: Is this possible in a trigger?

by kreno@[EMAIL PROTECTED] ("Kerri Reno") May 7, 2008 at 08:16 AM

------=_Part_12773_30458110.1210169761487
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

Fernando,

Below is a function that I hope gets you started.  It hasn't been tested,
I
cut and pasted from our procedure, which is rather more complex.  You
didn'=
t
say what you wanted to do with the changes when you found them, this puts
them in a log_audit table.  The thing to remember about python is that
it's
completely based on indentation, so if you have trouble, it's probably
because the indent isn't correct.  Also, # means comment.   Feel free to
contact me if you have questions or problems.  I'm trying to turn the
world
on to python!

If you don't have the python programming language installed on your db, I
think this should do it:
create language plpythonu

These links could be helpful too:
http://rgruet.free.fr/PQR25/PQR2.5.html
http://www.postgresql.org/docs/8.2/static/plpython.html

Hope this helps!
Kerri

CREATE OR REPLACE FUNCTION logchange()
  RETURNS "trigger" AS
$BODY$

    plpy.debug('function: logchange')
    #check to make sure i'm called correctly, error will stop the trigger
    if TD['when'] !=3D 'AFTER':
        plpy.error('logchange:not called AFTER')
    if TD['level'] !=3D 'ROW':
        plpy.error('logchange:not called per ROW')

    if TD['event'] =3D=3D 'UPDATE':
      # get the name of the current table.
      result =3D plpy.execute("select relname from pg_class where
oid=3D'%s=
'" %
TD['relid'])
      if len(result) !=3D 1:
        plpy.error('no table name found in pg_class')
      tblname =3D result[0]['relname']

      changes =3D ''
      # TD['new'] and 'old' are python dictionaries, so they can be
traversed, in this case by the
      # dictionary keys
       for k in TD['new'].keys():
          if TD['new'][k] !=3D TD['old'][k]:
             changes +=3D '%s was: %s now is: %s\n\r' %
(k,TD['old'][k],TD['new'][k])

      if len(changes) > 0:
        # this assumes the table has an oid, if you have your own id #,
use
it
        qry =3D 'INSERT INTO log_audit (table, id, change) values
(%s,%s,'%s')" % \
          (tblname,TD['new'][oid],changes )
        plpy.debug('qry:',qry)
        result =3D plpy.execute(qry)
        plpy.execute('NOTIFY LOGAUDITCHANGE')
  return None
$BODY$
LANGUAGE 'plpythonu';


--=20
Yuma Educational Computer Consortium
Compass Development Team
Kerri Reno
kreno@[EMAIL PROTECTED]
      (928) 502-4240
..=B7:*=A8=A8*:=B7.   .=B7:*=A8=A8*:=B7.   .=B7:*=A8=A8*:=B7.

------=_Part_12773_30458110.1210169761487
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

Fernando,<br><br>Below is a function that I hope gets you started.&nbsp;
It=
 hasn&#39;t been tested, I cut and pasted from our procedure, which is
rath=
er more complex.&nbsp; You didn&#39;t say what you wanted to do with the
ch=
anges when you found them, this puts them in a log_audit table.&nbsp; The
t=
hing to remember about python is that it&#39;s completely based on
indentat=
ion, so if you have trouble, it&#39;s probably because the indent
isn&#39;t=
 correct.&nbsp; Also, # means comment.&nbsp;&nbsp; Feel free to contact me
=
if you have questions or problems.&nbsp; I&#39;m trying to turn the world
o=
n to python! <br>
<br>If you don&#39;t have the python programming language installed on
your=
 db, I think this should do it:<br>create language plpythonu<br><br>These
l=
inks could be helpful too:<br><a
href=3D"http://rgruet.free.fr/PQR25/PQR2.5=
..html">http://rgruet.free.fr/PQR25/PQR2.5.html</a><br>
<a
href=3D"http://www.postgresql.org/docs/8.2/static/plpython.html">http://=
www.postgresql.org/docs/8.2/static/plpython.html</a><br><br>Hope this
helps=
!<br>Kerri<br><br>CREATE OR REPLACE FUNCTION logchange()<br>&nbsp; RETURNS
=
&quot;trigger&quot; AS<br>
$BODY$<br><br>&nbsp;&nbsp;&nbsp; plpy.debug(&#39;function:
logchange&#39;)<=
br>&nbsp;&nbsp;&nbsp; #check to make sure i&#39;m called correctly, error
w=
ill stop the trigger<br>&nbsp;&nbsp;&nbsp; if TD[&#39;when&#39;] !=3D
&#39;=
AFTER&#39;:<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; plpy.error(&#39;logchange:not called
=
AFTER&#39;)<br>&nbsp;&nbsp;&nbsp; if TD[&#39;level&#39;] !=3D
&#39;ROW&#39;=
:<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; plpy.error(&#39;logchange:not
ca=
lled per ROW&#39;)<br><br>&nbsp;&nbsp;&nbsp; if TD[&#39;event&#39;] =3D=3D
=
&#39;UPDATE&#39;:<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # get the name of the current
table.<br>&nbs=
p;&nbsp;&nbsp;&nbsp;&nbsp; result =3D plpy.execute(&quot;select relname
fro=
m pg_class where oid=3D&#39;%s&#39;&quot; %
TD[&#39;relid&#39;])<br>&nbsp;&=
nbsp;&nbsp;&nbsp;&nbsp; if len(result) !=3D
1:<br>&nbsp;&nbsp;&nbsp;&nbsp;&=
nbsp;&nbsp;&nbsp; plpy.error(&#39;no table name found in
pg_class&#39;)<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; tblname =3D
result[0][&#39;relname&#39;]<br>=
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; changes =3D
&#39;&#39;<br>&nbsp;&nbsp;&nbsp;=
&nbsp;&nbsp; # TD[&#39;new&#39;] and &#39;old&#39; are python
dictionaries,=
 so they can be traversed, in this case by
the<br>&nbsp;&nbsp;&nbsp;&nbsp;&=
nbsp; # dictionary keys<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for k in
TD=
[&#39;new&#39;].keys():<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if
TD[&#39;new&#39;]=
[k] !=3D
TD[&#39;old&#39;][k]:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp=
;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; changes +=3D &#39;%s was: %s now is:
%s\n\r=
&#39; % (k,TD[&#39;old&#39;][k],TD[&#39;new&#39;][k])<br>
<br>&nbsp; &nbsp; &nbsp; if len(changes) &gt;
0:<br>&nbsp;&nbsp;&nbsp;&nbsp=
;&nbsp;&nbsp;&nbsp; # this assumes the table has an oid, if you have your
o=
wn id #, use it<br>&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; qry =3D &#39;INSERT
INT=
O log_audit (table, id, change) values (%s,%s,&#39;%s&#39;)&quot; % \<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
(tblname,TD[&#39;new=
&#39;][oid],changes )<br>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;
plpy.debug(&#39;q=
ry:&#39;,qry)<br>&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; result =3D
plpy.execute(q=
ry)<br>&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; plpy.execute(&#39;NOTIFY
LOGAUDITCH=
ANGE&#39;)<br>&nbsp; return None<br>
$BODY$<br>LANGUAGE &#39;plpythonu&#39;;<br><br><br>-- <br>Yuma Educational
=
Computer Consortium<br>Compass Development Team<br>Kerri Reno<br><a
href=3D=
"mailto:kreno@[EMAIL PROTECTED]
" target=3D"_blank" onclick=3D"return
top.js.OpenEx=
tLink(window,event,this)">kreno@[EMAIL PROTECTED]
>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp=
;&nbsp;(928) 502-4240<br>
..=B7:*=A8=A8*:=B7.&nbsp;&nbsp; .=B7:*=A8=A8*:=B7.&nbsp;&nbsp;
.=B7:*=A8=A8*=
:=B7.

------=_Part_12773_30458110.1210169761487--
 




 8 Posts in Topic:
Is this possible in a trigger?
fernando@[EMAIL PROTECTED  2008-05-06 17:05:37 
Re: Is this possible in a trigger?
kgore4@[EMAIL PROTECTED]   2008-05-07 10:10:50 
Re: Is this possible in a trigger?
kreno@[EMAIL PROTECTED]   2008-05-06 19:13:29 
Re: Is this possible in a trigger?
xzilla@[EMAIL PROTECTED]   2008-05-07 01:12:25 
Re: Is this possible in a trigger?
fernando@[EMAIL PROTECTED  2008-05-07 09:37:21 
Re: Is this possible in a trigger?
kreno@[EMAIL PROTECTED]   2008-05-07 08:16:01 
Re: Is this possible in a trigger?
depesz@[EMAIL PROTECTED]   2008-05-07 13:01:35 
Re: Is this possible in a trigger?
valgog <valgog@[EMAIL   2008-05-07 04:04:21 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Wed Aug 20 8:15:39 CDT 2008.