Thread: doing %-expansion in plpgsql RAISE USING

doing %-expansion in plpgsql RAISE USING

From
Alvaro Herrera
Date:
Hi,

It seems there's no way to do %-expansion in plpgsql when one is using
RAISE USING:

alvherre=# create or replace function f () returns void language plpgsql as $$
begin raise using message = 'hello %' || 'world'; return;
end;
$$;
CREATE FUNCTION
alvherre=# select f();
ERROR:  hello %world


I would like the % to be expanded to some argument, but obviously
there's no way to pass the arguments that it should expand to.  We could
do something like

RAISE USING message = 'hello %st %', args = 1, 'world'

but this is obviously going to be difficult, if not impossible, to
implement in the grammar.  Perhaps 
RAISE USING message = 'brave %st %', args = (1, 'world')

Thoughts?

-- 
Alvaro Herrera                                http://www.CommandPrompt.com/
The PostgreSQL Company - Command Prompt, Inc.


Re: doing %-expansion in plpgsql RAISE USING

From
Tom Lane
Date:
Alvaro Herrera <alvherre@commandprompt.com> writes:
> It seems there's no way to do %-expansion in plpgsql when one is using
> RAISE USING:

That's intentional.  Just use string concatenation if you need a
run-time-variable message.
        regards, tom lane


Re: doing %-expansion in plpgsql RAISE USING

From
Pavel Stehule
Date:
Hello

2009/8/4 Alvaro Herrera <alvherre@commandprompt.com>:
> Hi,
>
> It seems there's no way to do %-expansion in plpgsql when one is using
> RAISE USING:
>
> alvherre=# create or replace function f () returns void language plpgsql as $$
> begin
>  raise using message = 'hello %' || 'world';
>  return;
> end;
> $$;
> CREATE FUNCTION
> alvherre=# select f();
> ERROR:  hello %world
>
>

parameter is expression, so we could to define operator % like

text % any

% isn't defined for text so this is possible. This operator should be
generally used, not only in RAISE attribs.


> I would like the % to be expanded to some argument, but obviously
> there's no way to pass the arguments that it should expand to.  We could
> do something like
>
> RAISE USING message = 'hello %st %', args = 1, 'world'
>

RAISE USING message = 'hello %st %' % (1, 'world')

??

or simple use custom variadic function

RAISE USING message= subst('hello %st %', 1, 'world')

good example for parser hook :)

Pavel

> but this is obviously going to be difficult, if not impossible, to
> implement in the grammar.  Perhaps
> RAISE USING message = 'brave %st %', args = (1, 'world')
>
> Thoughts?
>
> --
> Alvaro Herrera                                http://www.CommandPrompt.com/
> The PostgreSQL Company - Command Prompt, Inc.
>
> --
> Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-hackers
>


Re: doing %-expansion in plpgsql RAISE USING

From
Alvaro Herrera
Date:
Tom Lane wrote:
> Alvaro Herrera <alvherre@commandprompt.com> writes:
> > It seems there's no way to do %-expansion in plpgsql when one is using
> > RAISE USING:
> 
> That's intentional.  Just use string concatenation if you need a
> run-time-variable message.

Yes, I can do that, but it's really ugly and gets unmaintainable
quickly.

-- 
Alvaro Herrera                                http://www.CommandPrompt.com/
PostgreSQL Replication, Consulting, Custom Development, 24x7 support