Re: PSQL commands: \quit_if, \quit_unless

From: Robert Haas <robertmhaas(at)gmail(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Fabien COELHO <coelho(at)cri(dot)ensmp(dot)fr>, Corey Huinker <corey(dot)huinker(at)gmail(dot)com>, Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>, Andrew Dunstan <andrew(at)dunslane(dot)net>, PostgreSQL <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: PSQL commands: \quit_if, \quit_unless
Date: 2016-12-17 18:52:19
Message-ID: CA+TgmoaVfcyBRLVvfPi7gPu2sMv=6Jk3qjMA0m-e9sWdNQFrRw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Fri, Dec 16, 2016 at 12:40 PM, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:
> Robert Haas <robertmhaas(at)gmail(dot)com> writes:
>> So I think it would be reasonable for somebody to implement \if,
>> \elseif, \endif first, with the argument having to be, precisely, a
>> single variable and nothing else (not even a negator). Then a future
>> patch could allow an expression there instead of a variable. I don't
>> think that would be any harder to review than going all the way to #5
>> in one shot, and actually it might be simpler.
>
> This seems like a reasonable implementation plan to me, not least because
> it tackles the hard part first. There's no doubt that we can build an
> expression evaluator, but I'm not entirely sure how we're going to wedge
> conditional eval or loops into psql's command reader.
>
> (Or in other words, let's see \while ... \endloop in the minimal proposal
> as well, or at least a sketch of how to get there.)

It seems to me that we could implement \if ... \else ...\endif by
having some kind of stack that indicates which constructs we're inside
of and whether we're currently executing commands or discarding them.
I think we want to avoid waiting until we see \endif to start running
commands; it's better to execute or skip/discard each command as we
see it.

To implement \while, we'd also need to remember previous commands so
that when we reach the end of the loop, we can rewind and put all of
those commands back on the stack to be executed again, or perhaps to
be skipped if the \while condition turns out now to be false.

--
Robert Haas
EnterpriseDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Robert Haas 2016-12-17 18:55:13 Re: Proposal for changes to recovery.conf API
Previous Message Tom Lane 2016-12-17 18:36:48 Rethinking our fulltext phrase-search implementation