Connecting Tech Pros Worldwide Help | Site Map
 
 
LinkBack Thread Tools Search this Thread
  #1  
Old November 22nd, 2005, 08:53 AM
Bruno BAGUETTE
Guest
 
Posts: n/a
Default Unable to create a PL/PGSL function : Did I miss something ?

Hello,

I'm trying to build a PL/PGSQL function that will be called by a trigger
which will update a table named 'mview_contacts'. That table plays the
role of a materialized view.

The 'plpgsql' language is installed on that database (there are already
several functions inside) but I don't understand why I can't store that
function in the database.

When I cut/paste the code, psql seems to wait for something at the end:

levure(> ' LANGUAGE 'plpgsql';
levure'>

I've counted 178 ' quotes in that function, so I don't think that is a
missing quote.
There are 38 opening ( and 38 closing ).
All the IF are closed by a END IF.

Do you see what I've missed to get that function to be saved inside the
database ?



CREATE OR REPLACE function update_mview_contacts() RETURN OPAQUE AS '
BEGIN
IF TG_OP = ''INSERT'' THEN
-- Add the new contact into the materialized
view

IF TG_RELNAME = ''people'' THEN
EXECUTE ''INSERT INTO mview_contacts
(pk_fk_cnt_id,cnt_name,cnt_type,cnt_initial)'' ||
quote_literal('VALUES(NEW.pk_fk_cnt_id, (COALESCE(NEW.l_name,\'\') || \'
\' || COALESCE(NEW.f_name,\'\')),
\'people\',LOWER(SUBSTR((COALESCE(NEW.l_name,\'\') || \' \' ||
COALESCE(NEW.f_name,\'\')), 1, 1)))');
ELSIF TG_RELNAME = ''organizations'' THEN
EXECUTE ''INSERT INTO mview_contacts
(pk_fk_cnt_id,cnt_name,cnt_type,cnt_initial)'' ||
quote_literal('VALUES(NEW.pk_fk_cnt_id, NEW.org_name,
\'organization\',LOWER(SUBSTR(NEW.org_name, 1, 1)))');
ELSE
RAISE EXCEPTION ''The [%] table is
unsupported by this trigger function'', TG_RELNAME;
END IF;
ELSIF TG_OP = ''UPDATE'' THEN
-- Update the contact infos in the materialized
view

IF TG_RELNAME = ''people'' THEN
EXECUTE ''UPDATE mview_contacts SET ''
|| quote_ident('cnt_name') || '' = '' ||
quote_literal('(COALESCE(l_name,\'\') || \' \' ||
COALESCE(f_name,\'\'))') || '',''
|| quote_ident('cnt_type') || '' = '' ||
quote_literal('people') || '',''
|| quote_ident('cnt_initial') || '' = ''
|| quote_literal('LOWER(SUBSTR(NEW.org_name, 1, 1))')
|| '' WHERE mview_contacts.pk_fk_cnt_id
= '' || quote_literal(OLD.pk_fk_cnt_id);
ELSIF TG_RELNAME = ''organizations'' THEN
EXECUTE ''UPDATE mview_contacts SET ''
|| quote_ident('cnt_name') || '' = '' ||
quote_literal('NEW.org_name') || '',''
|| quote_ident('cnt_type') || '' = '' ||
quote_literal('organization') || '',''
|| quote_ident('cnt_initial') || '' = ''
|| quote_literal('LOWER(SUBSTR(NEW.org_name, 1, 1))')
|| '' WHERE mview_contacts.pk_fk_cnt_id
= '' || quote_literal(OLD.pk_fk_cnt_id);
ELSE
RAISE EXCEPTION ''The [%] table is
unsupported by this trigger function'', TG_RELNAME;
END IF;
ELSIF TG_OP = ''DELETE'' THEN
-- Remove the contact from the materialized view

IF TG_RELNAME = ''people'' THEN
EXECUTE ''DELETE mview_contacts WHERE
pk_fk_cnt_id = OLD.pk_fk_cnt_id'';
ELSIF TG_RELNAME = ''organizations'' THEN
EXECUTE ''DELETE mview_contacts WHERE
pk_fk_cnt_id = OLD.pk_fk_cnt_id'';
ELSE
RAISE EXCEPTION ''The [%] table is
unsupported by this trigger function'', TG_RELNAME;
END IF;
ELSE
-- Unknown trigger operation
-- ==> Raise an exception
RAISE EXCEPTION ''Unknown trigger function
operation [%]'', TG_OP;
END IF;
END;
' LANGUAGE 'plpgsql';


Thanks you very much in advance for your help.

Regards,

---------------------------------------
Bruno BAGUETTE - pgsql-ml@baguette.net


---------------------------(end of broadcast)---------------------------
TIP 7: don't forget to increase your free space map settings




  #2  
Old November 22nd, 2005, 08:54 AM
Martijn van Oosterhout
Guest
 
Posts: n/a
Default Re: Unable to create a PL/PGSL function : Did I miss something ?

On Sun, Feb 08, 2004 at 01:47:51AM +0100, Bruno BAGUETTE wrote:[color=blue]
> Hello,
>
> I'm trying to build a PL/PGSQL function that will be called by a trigger
> which will update a table named 'mview_contacts'. That table plays the
> role of a materialized view.
>
> The 'plpgsql' language is installed on that database (there are already
> several functions inside) but I don't understand why I can't store that
> function in the database.[/color]

You forgot to escape some quotes:
[color=blue]
> quote_literal('people') || '',''[/color]
^ ^[color=blue]
> || quote_ident('cnt_initial') || '' = ''[/color]
^ ^[color=blue]
> || quote_literal('LOWER(SUBSTR(NEW.org_name, 1, 1))')[/color]
^ ^
[color=blue]
> || quote_ident('cnt_name') || '' = '' ||
> quote_literal('NEW.org_name') || '',''
> || quote_ident('cnt_type') || '' = '' ||
> quote_literal('organization') || '',''
> || quote_ident('cnt_initial') || '' = ''
> || quote_literal('LOWER(SUBSTR(NEW.org_name, 1, 1))')[/color]

Several in there

Hope this helps,
--
Martijn van Oosterhout <kleptog@svana.org> http://svana.org/kleptog/[color=blue]
> (... have gone from d-i being barely usable even by its developers
> anywhere, to being about 20% done. Sweet. And the last 80% usually takes
> 20% of the time, too, right?) -- Anthony Towns, debian-devel-announce[/color]

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: For info see http://www.gnupg.org

iD8DBQFAJY1XY5Twig3Ge+YRAvW1AJ9GtZQ3MGpcqakdYm47lA TJFxXt5gCfagzO
gLzOe3nZGsufv+rITFeMnec=
=Zgto
-----END PGP SIGNATURE-----

 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 205,414 network members.