From: | Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com> |
---|---|
To: | David G Johnston <david(dot)g(dot)johnston(at)gmail(dot)com> |
Cc: | PostgreSQL Hackers <pgsql-hackers(at)postgresql(dot)org> |
Subject: | Re: PL/pgSQL 2 |
Date: | 2014-09-03 19:16:47 |
Message-ID: | CAFj8pRAsh=CwLcKny_RPKitLn7Mx05GFW2vVGXGaAXrZQK8TrQ@mail.gmail.com |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-hackers |
2014-09-03 21:01 GMT+02:00 David G Johnston <david(dot)g(dot)johnston(at)gmail(dot)com>:
> This is more of an SQL request the pl/pgsql but is/has there been thought
> to
> adding the ternary if/then opeator? Something like:
>
> boolean_exp ?> val_if_true : val_if_false
>
> using "?" by itself would be OK but not ideal - and the addition of the ">"
> doesn't seem hateful...
>
> Sorry if this is deemed off-topic but I just went to write
>
> CASE WHEN boolean_exp THEN val_if_true ELSE val_if_false END
>
> And the fact there is as much standard code as there is custom bothered me
> just as is being discussed on this thread.
>
> I'm going to go write a "ifthen(bool, anyelement, anyelement)" function
> now....
>
>
if you use a SQL (SQL macro, then it can be effective)
postgres=# CREATE OR REPLACE FUNCTION if(bool, anyelement, anyelement)
RETURNS anyelement AS $$SELECT CASE WHEN $1 THEN $2 ELSE $3 END $$ LANGUAGE
sql;
CREATE FUNCTION
postgres=# CREATE OR REPLACE FUNCTION fx(text) RETURNS text AS $$ BEGIN
RAISE NOTICE '%', $1; RETURN $1; END$$ LANGUAGE plpgsql;
CREATE FUNCTION
postgres=# SELECT if(false, fx('msg1'), fx('msg2'));
NOTICE: msg2
if
------
msg2
(1 row)
postgres=# SELECT if(true, fx('msg1'), fx('msg2'));
NOTICE: msg1
if
------
msg1
(1 row)
Only necessary parameters are evaluated
Pavel
> David J.
>
>
>
>
> --
> View this message in context:
> http://postgresql.1045698.n5.nabble.com/PL-pgSQL-2-tp5817121p5817608.html
> Sent from the PostgreSQL - hackers mailing list archive at Nabble.com.
>
>
> --
> Sent via pgsql-hackers mailing list (pgsql-hackers(at)postgresql(dot)org)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-hackers
>
From | Date | Subject | |
---|---|---|---|
Next Message | Jeff Janes | 2014-09-03 19:36:24 | Re: pgcrypto: PGP signatures |
Previous Message | David G Johnston | 2014-09-03 19:01:42 | Re: PL/pgSQL 2 |