pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
plpgsql function question
7+ messages / 4 participants
[nested] [flat]

* plpgsql function question
@ 2007-04-03 17:42 Karthikeyan Sundaram <skarthi98@hotmail.com>
  2007-04-03 18:18 ` Re: plpgsql function question Andreas Kretschmer <akretschmer@spamfence.net>
  0 siblings, 1 reply; 7+ messages in thread

From: Karthikeyan Sundaram @ 2007-04-03 17:42 UTC (permalink / raw)
  To: pgsql-admin@postgresql.org; pgsql-sql




Hi,
 
   I am having a requirement here.
 
   1) I need to write a plpgsql function where it takes the input parameter of a structure of a table.
   2) The table has 15 columns
   3) It does lots of validation based on the parameter and finally returns an integer as output parameters
 
  Q) How will I passe the table structure as as parameter
      2) Do I need to create a type?
 
   Please help me.
 
Regards
skarthi
 
 
_________________________________________________________________
Take a break and play crossword puzzles - FREE!
http://games.msn.com/en/flexicon/default.htm?icid=flexicon_ wlmemailtaglinemarch07=

^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* Re: plpgsql function question
  2007-04-03 17:42 plpgsql function question Karthikeyan Sundaram <skarthi98@hotmail.com>
@ 2007-04-03 18:18 ` Andreas Kretschmer <akretschmer@spamfence.net>
  0 siblings, 0 replies; 7+ messages in thread

From: Andreas Kretschmer @ 2007-04-03 18:18 UTC (permalink / raw)
  To: pgsql-sql

Karthikeyan Sundaram <skarthi98@hotmail.com> schrieb:

> 
>      Hi,
>       
>         I am having a requirement here.
>       
>         1) I need to write a plpgsql function where it takes the input
>      parameter of a structure of a table.

Because? To build this table? You can pass an ascii-text with the
table-definition and EXECUTE this string.


>         2) The table has 15 columns

Okay. And the problem is?


>         3) It does lots of validation based on the parameter and finally
>      returns an integer as output parameters

Okay. create function ... returns int as $$ ... return 1; end; $$
language plpgsql;


>       
>        Q) How will I passe the table structure as as parameter

As i said, for instance as simple text and EXECUTE this.


>            2) Do I need to create a type?

No.


Andreas
-- 
Really, I'm not out to destroy Microsoft. That will just be a completely
unintentional side effect.                              (Linus Torvalds)
"If I was god, I would recompile penguin with --enable-fly."    (unknow)
Kaufbach, Saxony, Germany, Europe.              N 51.05082°, E 13.56889°



^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* Re: plpgsql function question
@ 2007-04-03 18:33 Karthikeyan Sundaram <skarthi98@hotmail.com>
  2007-04-03 19:24 ` Re: plpgsql function question A. Kretschmer <andreas.kretschmer@schollglas.com>
  2007-04-03 20:27 ` Re: plpgsql function question John DeSoi <desoi@pgedit.com>
  0 siblings, 2 replies; 7+ messages in thread

From: Karthikeyan Sundaram @ 2007-04-03 18:33 UTC (permalink / raw)
  To: Andreas Kretschmer <akretschmer@spamfence.net>; pgsql-sql


Hi,
 
  I guess there is some misunderstanding from my question
 
  Let me elaborate more clearly.
 
   My Table is 
 
 Create table a (
 i int,
 j varchar(20),
 k date);
 
Create or replace function a_func (in p_i int, in p_j varchar, in p_k date) returns int as
$$
  ----- do the validation
  return 1;
$$
language 'plpgsql';
 
This works fine
 
What I want is something like this
create or replace functinon a_func (in a%rowtype) returns int as
$$
   ---- do the validation
$$
language 'plpgsql';
 
execute a_func(1, 'good','04/02/2007');
 
 > Date: Tue, 3 Apr 2007 20:18:43 +0200> From: akretschmer@spamfence.net> To: pgsql-sql@postgresql.org> Subject: Re: [SQL] plpgsql function question> > Karthikeyan Sundaram <skarthi98@hotmail.com> schrieb:> > > > > Hi,> > > > I am having a requirement here.> > > > 1) I need to write a plpgsql function where it takes the input> > parameter of a structure of a table.> > Because? To build this table? You can pass an ascii-text with the> table-definition and EXECUTE this string.> > > > 2) The table has 15 columns> > Okay. And the problem is?> > > > 3) It does lots of validation based on the parameter and finally> > returns an integer as output parameters> > Okay. create function ... returns int as $$ ... return 1; end; $$> language plpgsql;> > > > > > Q) How will I passe the table structure as as parameter> > As i said, for instance as simple text and EXECUTE this.> > > > 2) Do I need to create a type?> > No.> > > Andreas> -- > Really, I'm not out to destroy Microsoft. That will just be a completely> unintentional side effect. (Linus Torvalds)> "If I was god, I would recompile penguin with --enable-fly." (unknow)> Kaufbach, Saxony, Germany, Europe. N 51.05082°, E 13.56889°> > ---------------------------(end of broadcast)---------------------------> TIP 9: In versions below 8.0, the planner will ignore your desire to> choose an index scan if your joining column's datatypes do not> match
_________________________________________________________________
i'm making a difference. Make every IM count for the cause of your choice. Join Now.
http://clk.atdmt.com/MSN/go/msnnkwme0080000001msn/direct/01/?href=http://im.live.com/messenger/im/ho...

^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* Re: plpgsql function question
  2007-04-03 18:33 Re: plpgsql function question Karthikeyan Sundaram <skarthi98@hotmail.com>
@ 2007-04-03 19:24 ` A. Kretschmer <andreas.kretschmer@schollglas.com>
  1 sibling, 0 replies; 7+ messages in thread

From: A. Kretschmer @ 2007-04-03 19:24 UTC (permalink / raw)
  To: pgsql-sql

am  Tue, dem 03.04.2007, um 11:33:39 -0700 mailte Karthikeyan Sundaram folgendes:
> Hi,
>  
>   I guess there is some misunderstanding from my question

Maybe.

>  
>   Let me elaborate more clearly.
>  
>    My Table is
>  
>  Create table a (
>  i int,
>  j varchar(20),
>  k date);
>  
> Create or replace function a_func (in p_i int, in p_j varchar, in p_k date)
> returns int as
> $$
>   ----- do the validation
>   return 1;
> $$
> language 'plpgsql';
>  
> This works fine
>  
> What I want is something like this
> create or replace functinon a_func (in a%rowtype) returns int as
> $$
>    ---- do the validation
> $$
> language 'plpgsql';

What's the reason? For an INSERT or UPDATE - check? You can use a
TRIGGER and check the NEW-structure. Or create a new type, based on the
table-structure. Than you can create your function.

test=# create type a as ( i int, j varchar(20), k date);
CREATE TYPE
test=*# create function my_a (IN foo a) returns int as $$begin return 1; end; $$ language plpgsql;
CREATE FUNCTION
test=*# 


> > Date: Tue, 3 Apr 2007 20:18:43 +0200
> > From: akretschmer@spamfence.net
> > To: pgsql-sql@postgresql.org
> > Subject: Re: [SQL] plpgsql function question

Please, no silly text above and fullquote below, i'm reading from top to
bottom...


Andreas
-- 
Andreas Kretschmer
Kontakt:  Heynitz: 035242/47150,   D1: 0160/7141639 (mehr: -> Header)
GnuPG-ID:   0x3FFF606C, privat 0x7F4584DA   http://wwwkeys.de.pgp.net



^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* Re: plpgsql function question
  2007-04-03 18:33 Re: plpgsql function question Karthikeyan Sundaram <skarthi98@hotmail.com>
@ 2007-04-03 20:27 ` John DeSoi <desoi@pgedit.com>
  1 sibling, 0 replies; 7+ messages in thread

From: John DeSoi @ 2007-04-03 20:27 UTC (permalink / raw)
  To: Karthikeyan Sundaram <skarthi98@hotmail.com>; +Cc: Andreas Kretschmer <akretschmer@spamfence.net>; pgsql-sql

It should work pretty much like you have it. You don't need a type;  
the table is already a type.

Something like:

create or replace function a_func (in p_row a) returns int as
$$
   if p_row.i ...
   if p_row.j ...
$$

If it does not work, show the error and I'll try to dig up an example.

John



On Apr 3, 2007, at 2:33 PM, Karthikeyan Sundaram wrote:

> What I want is something like this
> create or replace functinon a_func (in a%rowtype) returns int as
> $$
>    ---- do the validation
> $$
> language 'plpgsql';
>
> execute a_func(1, 'good','04/02/2007');



John DeSoi, Ph.D.
http://pgedit.com/
Power Tools for PostgreSQL




^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* Re: plpgsql function question
@ 2007-04-04 05:01 A. Kretschmer <andreas.kretschmer@schollglas.com>
  2007-04-04 12:19 ` Re: plpgsql function question John DeSoi <desoi@pgedit.com>
  0 siblings, 1 reply; 7+ messages in thread

From: A. Kretschmer @ 2007-04-04 05:01 UTC (permalink / raw)
  To: pgsql-sql

am  Tue, dem 03.04.2007, um 13:19:26 -0700 mailte Karthikeyan Sundaram folgendes:
> Thank you very much. It works.  I am not doing any insert or update hence I
> cannot create a trigger.  But my another question is
>  
> How will I pass the values to Foo parameters.
>  
> I mean
>  
>  I want to pass
>  i = 1
>  j = 'God'
>  K = now()
>  
> which all the three will be used to check in the function
> should I say
>  
> select a_func(1,'good',now());

Because your function expects one parameter of your new type, you have
to CAST your data into this type:



test=# select * from my_a((1, 'foo', current_date)::a);
 my_a
------
    1
(1 row)


[ dumb fullquote deleted ]


Andreas
-- 
Andreas Kretschmer
Kontakt:  Heynitz: 035242/47150,   D1: 0160/7141639 (mehr: -> Header)
GnuPG-ID:   0x3FFF606C, privat 0x7F4584DA   http://wwwkeys.de.pgp.net



^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* Re: plpgsql function question
  2007-04-04 05:01 Re: plpgsql function question A. Kretschmer <andreas.kretschmer@schollglas.com>
@ 2007-04-04 12:19 ` John DeSoi <desoi@pgedit.com>
  0 siblings, 0 replies; 7+ messages in thread

From: John DeSoi @ 2007-04-04 12:19 UTC (permalink / raw)
  To: A. Kretschmer <andreas.kretschmer@schollglas.com>; +Cc: pgsql-sql

If you use a plpgsql function to select the row you want to validate,  
it will make life much easier. Something like

...
$$
declare
   my_row a_row_type;
   is_ok integer;
begin
   select into my_row * from a_row_type where ....
   is_ok := my_a_validate(my_row);
   return is_ok;
$$
...

On Apr 4, 2007, at 1:01 AM, A. Kretschmer wrote:

> Because your function expects one parameter of your new type, you have
> to CAST your data into this type:
>
>
>
> test=# select * from my_a((1, 'foo', current_date)::a);
>  my_a
> ------
>     1
> (1 row)



John DeSoi, Ph.D.
http://pgedit.com/
Power Tools for PostgreSQL




^ permalink  raw  reply  [nested|flat] 7+ messages in thread


end of thread, other threads:[~2007-04-04 12:19 UTC | newest]

Thread overview: 7+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2007-04-03 17:42 plpgsql function question Karthikeyan Sundaram <skarthi98@hotmail.com>
2007-04-03 18:18 ` Andreas Kretschmer <akretschmer@spamfence.net>
2007-04-03 18:33 Re: plpgsql function question Karthikeyan Sundaram <skarthi98@hotmail.com>
2007-04-03 19:24 ` A. Kretschmer <andreas.kretschmer@schollglas.com>
2007-04-03 20:27 ` John DeSoi <desoi@pgedit.com>
2007-04-04 05:01 Re: plpgsql function question A. Kretschmer <andreas.kretschmer@schollglas.com>
2007-04-04 12:19 ` John DeSoi <desoi@pgedit.com>

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