Re: [PATCHES] tg_table_name and tg_table_schema for plpgsql

2006-05-27 Thread Andrew Dunstan

Tom Lane wrote:

Andrew Dunstan <[EMAIL PROTECTED]> writes:
  
Here's a patch for tg_table_name and tg_table_schema for plpgsql, which 
I would appreciate a quick review of just to make sure I haven't missed 
something or done something silly.



Looks all right other than odd indentation ;-)
  


Did you mean in pl_exec.c? It looks better when applied - but it's not 
easy to fit that long call into 80 cols neatly. I guess if pg_indent 
doesn't like it it will tidy thing up when we do a run.



If you want to test tg_relid, what about casting it to regclass to get a
reasonable symbolic printout?

  


Good idea - I'll do that for plpgsql where it's easier. I don't think 
it's worth the trouble for the other PLs.



cheers

andrew

---(end of broadcast)---
TIP 4: Have you searched our list archives?

  http://archives.postgresql.org


Re: [PATCHES] tg_table_name and tg_table_schema for plpgsql

2006-05-27 Thread Tom Lane
Andrew Dunstan <[EMAIL PROTECTED]> writes:
> Here's a patch for tg_table_name and tg_table_schema for plpgsql, which 
> I would appreciate a quick review of just to make sure I haven't missed 
> something or done something silly.

Looks all right other than odd indentation ;-)

If you want to test tg_relid, what about casting it to regclass to get a
reasonable symbolic printout?

regards, tom lane

---(end of broadcast)---
TIP 4: Have you searched our list archives?

   http://archives.postgresql.org


[PATCHES] tg_table_name and tg_table_schema for plpgsql

2006-05-27 Thread Andrew Dunstan


Here's a patch for tg_table_name and tg_table_schema for plpgsql, which 
I would appreciate a quick review of just to make sure I haven't missed 
something or done something silly. This will complete this exercise - I 
have already committed analogous changes for plperl, plpython and pltcl.


cheers

andrew
Index: doc/src/sgml/plpgsql.sgml
===
RCS file: /cvsroot/pgsql/doc/src/sgml/plpgsql.sgml,v
retrieving revision 1.88
diff -c -r1.88 plpgsql.sgml
*** doc/src/sgml/plpgsql.sgml	10 Mar 2006 19:10:48 -	1.88
--- doc/src/sgml/plpgsql.sgml	27 May 2006 22:19:28 -
***
*** 2745,2751 
   

 Data type name; the name of the table that caused the trigger
!invocation.

   
  
--- 2745,2772 
   

 Data type name; the name of the table that caused the trigger
!invocation. This is now deprecated, and could disappear in a future 
! 	   release. Use TG_TABLE_NAME instead.
!   
!  
! 
! 
! 
!  TG_TABLE_NAME
!  
!   
!Data type name; the name of the table that 
! 	   caused the trigger invocation.
!   
!  
! 
! 
! 
!  TG_TABLE_SCHEMA
!  
!   
!Data type name; the name of the schema of the 
! 	   table that caused the trigger invocation.

   
  
Index: src/pl/plpgsql/src/pl_comp.c
===
RCS file: /cvsroot/pgsql/src/pl/plpgsql/src/pl_comp.c,v
retrieving revision 1.101
diff -c -r1.101 pl_comp.c
*** src/pl/plpgsql/src/pl_comp.c	14 Mar 2006 22:48:23 -	1.101
--- src/pl/plpgsql/src/pl_comp.c	27 May 2006 22:19:32 -
***
*** 581,586 
--- 581,600 
  		 true);
  			function->tg_relname_varno = var->dno;
  
+ 			/* tg_table_name is now preferred to tg_relname */
+ 			var = plpgsql_build_variable("tg_table_name", 0,
+ 		 plpgsql_build_datatype(NAMEOID, -1),
+ 		 true);
+ 			function->tg_table_name_varno = var->dno;
+ 
+ 			
+ 			/* add variable tg_table_schema */
+ 			var = plpgsql_build_variable("tg_table_schema", 0,
+ 		 plpgsql_build_datatype(NAMEOID, -1),
+ 		 true);
+ 			function->tg_table_schema_varno = var->dno;
+ 
+ 			
  			/* Add the variable tg_nargs */
  			var = plpgsql_build_variable("tg_nargs", 0,
  		 plpgsql_build_datatype(INT4OID, -1),
Index: src/pl/plpgsql/src/pl_exec.c
===
RCS file: /cvsroot/pgsql/src/pl/plpgsql/src/pl_exec.c,v
retrieving revision 1.164
diff -c -r1.164 pl_exec.c
*** src/pl/plpgsql/src/pl_exec.c	22 Apr 2006 01:26:01 -	1.164
--- src/pl/plpgsql/src/pl_exec.c	27 May 2006 22:19:34 -
***
*** 539,544 
--- 539,559 
  	var->isnull = false;
  	var->freeval = true;
  
+ 	var = (PLpgSQL_var *) (estate.datums[func->tg_table_name_varno]);
+ 	var->value = DirectFunctionCall1(namein,
+ 			CStringGetDatum(RelationGetRelationName(trigdata->tg_relation)));
+ 	var->isnull = false;
+ 	var->freeval = true;
+ 
+ 	var = (PLpgSQL_var *) (estate.datums[func->tg_table_schema_varno]);
+ 	var->value = DirectFunctionCall1(namein,
+ 	 CStringGetDatum(
+ 		 get_namespace_name(
+ 			 RelationGetNamespace(
+  trigdata->tg_relation;
+ 	var->isnull = false;
+ 	var->freeval = true;
+ 
  	var = (PLpgSQL_var *) (estate.datums[func->tg_nargs_varno]);
  	var->value = Int16GetDatum(trigdata->tg_trigger->tgnargs);
  	var->isnull = false;
Index: src/pl/plpgsql/src/plpgsql.h
===
RCS file: /cvsroot/pgsql/src/pl/plpgsql/src/plpgsql.h,v
retrieving revision 1.69
diff -c -r1.69 plpgsql.h
*** src/pl/plpgsql/src/plpgsql.h	9 Mar 2006 21:29:38 -	1.69
--- src/pl/plpgsql/src/plpgsql.h	27 May 2006 22:19:35 -
***
*** 572,577 
--- 572,579 
  	int			tg_op_varno;
  	int			tg_relid_varno;
  	int			tg_relname_varno;
+ 	int tg_table_name_varno;
+ 	int tg_table_schema_varno;
  	int			tg_nargs_varno;
  
  	int			ndatums;
Index: src/test/regress/expected/triggers.out
===
RCS file: /cvsroot/pgsql/src/test/regress/expected/triggers.out,v
retrieving revision 1.20
diff -c -r1.20 triggers.out
*** src/test/regress/expected/triggers.out	27 Feb 2006 16:09:50 -	1.20
--- src/test/regress/expected/triggers.out	27 May 2006 22:19:36 -
***
*** 386,388 
--- 386,485 
  
  drop table trigtest2;
  drop table trigtest;
+ -- dump trigger data
+ CREATE TABLE trigger_test (
+ i int,
+ v varchar
+ );
+ CREATE OR REPLACE FUNCTION trigger_data()  RETURNS trigger 
+ LANGUAGE plpgsql AS $$
+ 
+ declare
+ 
+ 	argstr text;
+ 
+ begin
+ 
+ 	-- plpgsql can't discover it's trigger data in a hash like perl and python
+ 	-- can, or by a sort of reflection like tcl can, 
+ 	-- s