473,289 Members | 1,896 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,289 software developers and data experts.

Convert a 'pltcl' function to 'plpgsql' function (postgresql)

I have been researching for days about the diff. between pltcl and plpgsql language in postgresql to solve my problem.

I have created a trigger function in 'pltcl' language but now I am trying to convert this in 'plpgsql' language. Any idea on how to convert this piece of trigger function code to plpgsql function? Thanks

Expand|Select|Wrap|Line Numbers
  1. CREATE OR REPLACE FUNCTION "public"."audit_log" () RETURNS trigger AS
  2.     $body$
  3.     spi_exec "SELECT CURRENT_USER AS tguser"
  4.     spi_exec "SELECT relname AS tgname FROM pg_class WHERE relfilenode = $TG_relid"
  5.  
  6.     #skip changes on audit_table
  7.     if {[string equal -nocase $tgname audit_table]} { return OK }
  8.  
  9.     #get PK name
  10.     set pk_name ""
  11.     spi_exec "SELECT a.attname AS pk_name FROM pg_class c, pg_attribute a, pg_index i
  12.      WHERE c.relname = '$tgname'
  13.      AND c.oid=i.indrelid
  14.      AND a.attnum > 0
  15.      AND a.attrelid = i.indexrelid
  16.      AND i.indisprimary='t'"
  17.  
  18.     switch $TG_op {
  19.     INSERT {
  20.       set pk_value ""
  21.  
  22.       #get PK value
  23.       foreach field $TG_relatts {
  24.         if {[string equal -nocase [lindex [array get NEW $field] 0] $pk_name]} {
  25.           set pk_value [lindex [array get NEW $field] 1]
  26.           break;
  27.         }
  28.       }
  29.       #log inserted row values
  30.       foreach field $TG_relatts {
  31.         if {! [string equal -nocase [lindex [array get NEW $field] 0] $pk_name]} {
  32.           set modified_field [lindex [array get NEW $field] 0]
  33.  
  34.          if {[string compare $modified_field ""] != 0} {
  35.  
  36.           set current_value [lindex [array get NEW $field] 1]
  37.           spi_exec -array C "INSERT INTO audit_table(ts, usr, tbl, fld, pk_name, pk_value, mod_type, old_val, new_val)
  38.             VALUES (CURRENT_TIMESTAMP, '[ quote $tguser ]', '[ quote $tgname ]', '[ quote $modified_field ]', '[ quote $pk_name ]', '[ quote $pk_value ]', '$TG_op', NULL, '[ quote $current_value ]')"
  39.           }
  40.         }
  41.       }
  42.     }
  43.     UPDATE {
  44.       set pk_value ""
  45.  
  46.       #get PK value
  47.       foreach field $TG_relatts {
  48.         if {[string equal -nocase [lindex [array get NEW $field] 0] $pk_name]} {
  49.           set pk_value [lindex [array get NEW $field] 1]
  50.           break;
  51.         }
  52.       }
  53.       #log inserted row values
  54.       foreach field $TG_relatts {
  55.         #check changed fields
  56.         if {[string equal -nocase [array get NEW $field] [array get OLD $field]] == 0} {
  57.           set modified_field [lindex [array get OLD $field] 0]
  58.           if {[string compare $modified_field ""] == 0} {
  59.             set modified_field [lindex  [array get NEW $field] 0]
  60.           }
  61.           set previous_value [lindex [array get OLD $field] 1]
  62.           set current_value  [lindex [array get NEW $field] 1]
  63.           spi_exec -array C "INSERT INTO audit_table(ts, usr, tbl, fld, pk_name, pk_value, mod_type, old_val, new_val)
  64.             VALUES (CURRENT_TIMESTAMP, '[ quote $tguser ]', '[ quote $tgname ]', '[ quote $modified_field ]', '[ quote $pk_name ]', '[ quote $pk_value ]', '$TG_op', '[ quote $previous_value ]', '[ quote $current_value ]')"
  65.         }
  66.       }
  67.     }
  68.     DELETE {
  69.       set pk_value ""
  70.  
  71.       #get PK value
  72.       foreach field $TG_relatts {
  73.         if {[string equal -nocase [lindex [array get OLD $field] 0] $pk_name]} {
  74.           set pk_value [lindex [array get OLD $field] 1]
  75.           break;
  76.         }
  77.       }
  78.       #log inserted row values
  79.       foreach field $TG_relatts {
  80.         if {! [string equal -nocase [lindex [array get OLD $field] 0] $pk_name]} {
  81.           set modified_field [lindex [array get OLD $field] 0]
  82.           set previous_value [lindex [array get OLD $field] 1]
  83.           spi_exec -array C "INSERT INTO audit_table(ts, usr, tbl, fld, pk_name, pk_value, mod_type, old_val, new_val)
  84.             VALUES (CURRENT_TIMESTAMP, '[ quote $tguser ]', '[ quote $tgname ]', '[ quote $modified_field ]', '[ quote $pk_name ]', '[ quote $pk_value ]', '$TG_op', '[ quote $previous_value ]', NULL)"
  85.         }
  86.       }
  87.     }
  88.     }
  89.     return OK
  90.     $body$
  91. LANGUAGE 'pltcl' VOLATILE CALLED ON NULL INPUT SECURITY INVOKER;
Jul 16 '15 #1
0 2373

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Rodusa | last post by:
I am having problem to apply updates into this function below. I tried using cursor for updates, etc. but no success. Sql server keeps telling me that I cannot execute insert or update from inside...
1
by: Julie May | last post by:
I have 90% of my function working and I know to get the next 10% it is justa matter of getting the quotations and the escaping quotations correct. Here is the portion that does work: <working...
10
by: lnd | last post by:
After copied pg database from one PC to another -I could not find plpgsql function(s) in the copied database. -had to instal plpgsql language handler again -whilst tables and data moved...
2
by: Karl O. Pinc | last post by:
I'd like to write: SELECT larger(colA, colB) FROM foo and am wondering the best way to go about it. (Really, I'd like the larger() function to take an arbitrary number of arguments but I...
8
by: Ed | last post by:
I want to convert the following VB code to C#: Dim r as datarow Dim i as integer i = fix(r("NumberField")) Fix as you may know truncates a floating point number to the right of the decimal...
5
by: jeremito | last post by:
I am extending python with C++ and need some help. I would like to convert a string to a mathematical function and then make this a C++ function. My C++ code would then refer to this function to...
0
by: setheo | last post by:
To all gurus, I am currently converting some of C++ codes to VB.net The C++ Codes is as follows : ================= C++ CODE ================== typedef struct _tagBBCameraParameter {...
6
by: arti | last post by:
I dont want to use Convert(Char(9),date,106) function to show date in dd/MM/yyyy format. It changes the datatype of my column to char & I cant perform other date operations on it without changing it...
2
by: Emil | last post by:
Is it posiible to create callback for a function, that is automatically called before that function returns? I need it for tracing/profiling purposes. eg.: var execTime = null; var startTime...
0
by: Ellen P | last post by:
Hi there, I've been trying to execute \COPY from within a plpsql function and keep getting an error. Any hints at where I'm going wrong would be great. I'm using \COPY with a backslash...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.