Thread: Unable to create a PL/PGSL function : Did I miss something ?

Unable to create a PL/PGSL function : Did I miss something ?

From
"Bruno BAGUETTE"
Date:
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


Re: Unable to create a PL/PGSL function : Did I miss something ?

From
Martijn van Oosterhout
Date:
On Sun, Feb 08, 2004 at 01:47:51AM +0100, Bruno BAGUETTE wrote:
> 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.

You forgot to escape some quotes:

> quote_literal('people') || '',''
                ^      ^
>                 || quote_ident('cnt_initial') || '' = ''
                                               ^           ^
> || quote_literal('LOWER(SUBSTR(NEW.org_name, 1, 1))')
                   ^                                 ^

>                 || 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))')

Several in there

Hope this helps,
--
Martijn van Oosterhout   <kleptog@svana.org>   http://svana.org/kleptog/
> (... 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

Attachment