Re: \if, \elseif, \else, \endif (was Re: [HACKERS] PSQL commands:\quit_if, \quit_unless) - Mailing list pgsql-hackers

From Fabien COELHO
Subject Re: \if, \elseif, \else, \endif (was Re: [HACKERS] PSQL commands:\quit_if, \quit_unless)
Date
Msg-id alpine.DEB.2.20.1702031300100.4856@lancre
Whole thread Raw
In response to Re: \if, \elseif, \else, \endif (was Re: [HACKERS] PSQL commands:\quit_if, \quit_unless)  (Erik Rijkers <er@xs4all.nl>)
Responses Re: \if, \elseif, \else, \endif (was Re: [HACKERS] PSQL commands:\quit_if, \quit_unless)  (Fabien COELHO <coelho@cri.ensmp.fr>)
Re: \if, \elseif, \else, \endif (was Re: [HACKERS] PSQL commands:\quit_if, \quit_unless)  (Corey Huinker <corey.huinker@gmail.com>)
List pgsql-hackers
Hello Erik,

> Still, it would be an improvement to be able to break out of an inactive 
> \if-branch with Ctrl-C.

Yes.

> '\endif' is too long to type, /and/ you have to know it.

Yep. \if is shorter but has been rejected. Ctrl-C should be the way out.

> 2. Inside an \if block \q should be given precedence and cause a direct 
> exit of psql (or at the very least exit the if block(s)), as in regular 
> SQL statements (compare: 'select * from t \q' which will immediately 
> exit psql -- this is good. )

One use case if to be able to write "\if ... \q \endif" in scripts. If \q 
is always executed, then the use case is blown. I cannot think of any 
language that would execute anything in a false branch. So Ctrl-C or 
Ctrl-D is the way out, and \if control must really have precedence over 
its contents.

> 3. I think the 'barking' is OK because interactive use is certainly not the 
> first use-case.
> But nonetheless it could be made a bit more terse without losing its 
> function.

> [...] It really is a bit too wordy, [...]

Maybe.

> (or alternatively, just  mention 'if: active' or 'elif: inactive', etc., 
> which has the advantage of being shorter)

This last version is too terse I think. The point is that the user 
understands whether their commands are going to be executed or ignored, 
and "active/inactive" is not very clear.

> 5. A real bug, I think:
> #\if asdasd
> unrecognized value "asdasd" for "\if <expr>": boolean expected
> # \q;
> inside inactive branch, command ignored.
> #
>
> That 'unrecognized value' message is fair enough but it is counterintuitive 
> that after an erroneous opening \if-expression, the if-modus should be 
> entered into. ( and now I have to type \endif again... )

Hmmm.

It should tell that it is in an unclosed if and that it is 
currently ignoring commands, so the "barking" is missing.

Otherwise that is really the currently desired behavior for scripting use:
  \if syntax-error...    DROP USER foo;  \elif ...    DROP DATABASE foo;  \else    ...  \endif

If the "\if" is simply ignored, then it is going to execute everything 
that appears after that, whereas the initial condition failed to be 
checked, and it will proceed to ignore all further \elif and \else in 
passing.

Also, I do not think that implementing a different behavior for 
interactive is a good idea, because then typing directly and reading a 
file would result in different behaviors, which would not help debugging.

So, as Tom suggested (I think), the feature is not designed for 
interactive use, so it does not need to be optimized for that purpose,
although it should be sane enough.

-- 
Fabien.



pgsql-hackers by date:

Previous
From: Tomas Vondra
Date:
Subject: Re: [HACKERS] Checksums by default?
Next
From: Neha Sharma
Date:
Subject: Re: [HACKERS] Gather Merge