Received: from localhost (unknown [200.46.204.191]) by postgresql.org (Postfix) with ESMTP id 272D72E0043 for ; Wed, 21 Nov 2007 04:14:39 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.191]) (amavisd-maia, port 10024) with ESMTP id 83061-02 for ; Wed, 21 Nov 2007 04:14:33 -0400 (AST) Received: from interscan.indicator.com (mail.indicator.be [193.121.199.3]) by postgresql.org (Postfix) with ESMTP id C4CFD2E0039 for ; Wed, 21 Nov 2007 04:14:32 -0400 (AST) Received: from webaccess.indicator.be ([10.100.1.7]) by interscan.indicator.com with InterScan Message Security Suite; Wed, 21 Nov 2007 09:12:07 +0100 Received: from DOMEXT-MTA by webaccess.indicator.bewith Novell_GroupWise; Wed, 21 Nov 2007 09:14:29 +0100 Message-Id: <4743F6E5.A3DD.0030.0@indicator.be> X-Mailer: Novell GroupWise Internet Agent 7.0.1 Date: Wed, 21 Nov 2007 09:14:14 +0100 From: "Bart Degryse" To: Subject: Re: dynmic column names inside trigger? References: <20071120161258.GA27106@apartia.fr> In-Reply-To: <20071120161258.GA27106@apartia.fr> Mime-Version: 1.0 Content-Type: multipart/alternative; boundary="=__Part42644BC6.0__=" X-imss-version: 2.049 X-imss-result: Passed X-imss-approveListMatch: *@indicator.be X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200711/100 X-Sequence-Number: 29628 --=__Part42644BC6.0__= Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: quoted-printable I would do something like this (not tested, but conceptually working): =20 CREATE or replace FUNCTION sanitize_text(webtext IN text, cleantext OUT = text) AS $body$ BEGIN cleantext =3D translate(webtext, E'\x92\x96', '''-'); cleantext =3D regexp_replace(cleantext, E'\x9c', 'oe', 'g'); cleantext =3D regexp_replace(cleantext, E'\x85', '...', 'g'); END; $body$ LANGUAGE plpgsql VOLATILE RETURNS NULL ON NULL INPUT; CREATE or replace FUNCTION func_trig_tbl_x() RETURNS "trigger" AS=20 $body$ BEGIN if old.story is not null and new.story !=3D old.story then new.story =3D sanitize_text(new.story); end if; --checks on other field can be included here, eg if old.otherfield is not null and new.otherfield !=3D old.otherfield then new.otherfield =3D sanitize_text(new.otherfield); end if; return new; END; $body$ LANGUAGE plpgsql; CREATE TRIGGER trig_tbl_x BEFORE INSERT or update ON tbl_x FOR EACH ROW EXECUTE PROCEDURE func_trig_tbl_x(); Now for tbl_y you can create a function func_trig_tbl_y (which again calls = sanitize_text for any field you like) and a trigger trig_tbl_x. It might be necessary to replace "cleantext OUT text" by "cleantext IN OUT = text". I haven't tested it. >>> Louis-David Mitterrand 2007-11-20 = 17:12 >>> Hi, I've got this trigger to clean up text entered in web forms: CREATE or replace FUNCTION sanitize_text() RETURNS "trigger" AS $$ declare begin if old.story is not null and new.story !=3D old.story then new.story =3D translate(new.story, E'\x92\x96', '''-'); new.story =3D regexp_replace(new.story, E'\x9c', 'oe', 'g'); new.story =3D regexp_replace(new.story, E'\x85', '...', 'g'); end if; return new; end; $$ LANGUAGE plpgsql; CREATE TRIGGER sanitize_text_trig BEFORE INSERT or update ON story FOR EACH ROW EXECUTE PROCEDURE sanitize_text(); I'd like to use it on other tables an columns but how can the column=20 name be dynamic inside the procedure. Passing the column name in the=20 trigger declaration and using it as NEW.TG_ARGV[0] seems out of the=20 question. Is there another solution out there? Thanks, ---------------------------(end of broadcast)--------------------------- TIP 3: Have you checked our extensive FAQ? http://www.postgresql.org/docs/faq --=__Part42644BC6.0__= Content-Type: text/html; charset=US-ASCII Content-Transfer-Encoding: quoted-printable Content-Description: HTML
I would do something like this (not tested, but conceptually = working):
 
CREATE or replace FUNCTION sanitize_text(webtext IN text, cleantext = OUT text) AS
$body$
BEGIN
cleantext =3D translate(webtext, = E'\x92\x96', '''-');
cleantext =3D regexp_replace(cleantext, E'\x9c', = 'oe', 'g');
cleantext =3D regexp_replace(cleantext, E'\x85', '...', = 'g');
END;
$body$
LANGUAGE plpgsql VOLATILE RETURNS NULL ON NULL = INPUT;
CREATE or replace FUNCTION func_trig_tbl_x() RETURNS "trigger" AS =
$body$
BEGIN
if old.story is not null and new.story !=3D = old.story then
  new.story =3D sanitize_text(new.story);
end = if;
--checks on other field can be included here, eg
if old.otherfield is not null and new.otherfield !=3D old.otherfield = then
  new.otherfield =3D sanitize_text(new.otherfield);
end if;
return new;
END;
$body$
LANGUAGE plpgsql;
CREATE TRIGGER trig_tbl_x
BEFORE INSERT or update ON tbl_x
FOR = EACH ROW
EXECUTE PROCEDURE func_trig_tbl_x();
Now for tbl_y you can create a function func_trig_tbl_y (which again = calls sanitize_text for any field you like) and a trigger trig_tbl_x.
=
It might be necessary to replace "cleantext OUT text" by "cleantext = IN OUT text". I haven't tested it.

>>> Louis-David = Mitterrand <vindex+lists-pgsql-sql@apartia.org> 2007-11-20 17:12 = >>>
Hi,

I've got this trigger to clean up text entered = in web forms:

CREATE or replace FUNCTION sanitize_text() RETURNS = "trigger"
AS $$
declare
begin
if old.story is not null and = new.story !=3D old.story
then
new.story =3D translate(new.story, = E'\x92\x96', '''-');
new.story =3D regexp_replace(new.story, E'\x9c', = 'oe', 'g');
new.story =3D regexp_replace(new.story, E'\x85', '...', = 'g');
end if;
return new;
end;
$$
LANGUAGE plpgsql;

<= BR>CREATE TRIGGER sanitize_text_trig
BEFORE INSERT or update ON = story
FOR EACH ROW
EXECUTE PROCEDURE sanitize_text();


I'd = like to use it on other tables an columns but how can the column
name = be dynamic inside the procedure. Passing the column name in the
trigger= declaration and using it as NEW.TG_ARGV[0] seems out of the
question.<= BR>
Is there another solution out there?

Thanks,

---------= ------------------(end of broadcast)---------------------------
TIP 3: = Have you checked our extensive FAQ?

     &n= bsp;         http://www.postgresql.org/docs/faq
--=__Part42644BC6.0__=--