cycling through custom type fields

Started by Julius Tuskenisalmost 15 years ago3 messagesgeneral
Jump to latest
#1Julius Tuskenis
julius@nsoft.lt

Hello,

I would like to ask if there is a possibility in postgresql (v9) to
cycle through the fields of some custom type parameter in plpgsql function.

I have a type defined:
CREATE TYPE naims.frt_pwd_ed AS
(pwd_id integer,
pwd_sub_id integer,
pwd_login character varying(100));

In pseudocode thats what I'd like to achieve:

function Changes(prm_old_pwd naims.frt_pwd_ed, prm_new_pwd
naims.frt_pwd_ed)
returns set of text
begin
FOR i = 0 to naims.frt_pwd_ed.FieldCount -1 LOOP
IF prm_old_pwd.field(i).value <> prm_new_pwd.field(i).value THEN
Return Next prm_old_pwd.field(i).fieldname;
END IF;
END LOOP;
Return;
end;

The functrion should check each field of 2 provided parameters and
return the fieldnames where the values are different. For this I need
some tools to extract field value and field name. Can this be done?

Thank You in advance!

--
Julius Tuskenis
Programavimo skyriaus vadovas
UAB nSoft
mob. +37068233050

#2Pavel Stehule
pavel.stehule@gmail.com
In reply to: Julius Tuskenis (#1)
Re: cycling through custom type fields

Hello

2011/5/19 Julius Tuskenis <julius@nsoft.lt>:

Hello,

I would like to ask if there is a possibility in postgresql (v9) to cycle
through the fields of some custom type parameter in plpgsql function.

I have a type defined:
CREATE TYPE naims.frt_pwd_ed AS
  (pwd_id integer,
   pwd_sub_id integer,
   pwd_login character varying(100));

In pseudocode thats what I'd like to achieve:

function Changes(prm_old_pwd  naims.frt_pwd_ed, prm_new_pwd
naims.frt_pwd_ed)
returns set of text
begin
 FOR i = 0 to naims.frt_pwd_ed.FieldCount -1 LOOP
   IF prm_old_pwd.field(i).value <> prm_new_pwd.field(i).value THEN
     Return Next prm_old_pwd.field(i).fieldname;
   END IF;
 END LOOP;
 Return;
end;

The functrion should check each field of 2 provided parameters and return
the fieldnames where the values are different. For this I need some tools to
extract field value and field name. Can this be done?

Thank You in advance!

--

It isn't possible directly

you can

a) use a PLPerl or a PLPython
b) use some lib like pltoolbox http://www.pgsql.cz/index.php/PL_toolbox_%28en%29
c) use a EXECUTE USING trick
http://okbob.blogspot.com/2008/06/execute-using-feature-in-postgresql-84.html
d) use a hstore contrib module
http://okbob.blogspot.com/2009/10/dynamic-access-to-record-fields-in.html

Regards

Pavel Stehule

Show quoted text

Julius Tuskenis
Programavimo skyriaus vadovas
UAB nSoft
mob. +37068233050

--
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general

#3Julius Tuskenis
julius@nsoft.lt
In reply to: Pavel Stehule (#2)
Re: cycling through custom type fields

2011.05.19 10:05, Pavel Stehule rašė:

It isn't possible directly

Pitty

c) use a EXECUTE USING trick
http://okbob.blogspot.com/2008/06/execute-using-feature-in-postgresql-84.html

I like the example of Tom Lane on triggers... Too bad something tg_argv
is unavailable outside of trigger...

Thank You once again. I can stop google'ing now :)

--
Julius Tuskenis
Programavimo skyriaus vadovas
UAB nSoft
mob. +37068233050