Re: \if, \elseif, \else, \endif (was Re: PSQL commands: \quit_if, \quit_unless)

Fabien COELHO <coelho@cri.ensmp.fr>

From: Fabien COELHO <coelho@cri.ensmp.fr>
To: Corey Huinker <corey.huinker@gmail.com>
Cc: Erik Rijkers <er@xs4all.nl>, Tom Lane <tgl@sss.pgh.pa.us>, Robert Haas <robertmhaas@gmail.com>, Daniel Verite <daniel@manitou-mail.org>, Jim Nasby <Jim.Nasby@bluetreble.com>, PostgreSQL <pgsql-hackers@postgresql.org>, pgsql-hackers-owner@postgresql.org
Date: 2017-02-10T21:36:53Z
Lists: pgsql-hackers
Hello,

I'm looking forward to the doc update.

My 0.02€ about prompting:

>> Given that there is no more barking, then having some prompt indication
>> that the code is inside a conditional branch becomes more important, so
>> ISTM that there should be some plan to add it.
>
> Yeah, prompting just got more important. I see a few ways to go about this:
>
> 1. Add a new prompt type, either %T for true (heh, pun) or %Y for
> branching. It would print a string of chained 't' (branch is true), 'f'
> (branch is false), 'z' (branch already had its true section). The depth
> traversal would have a limit, say 3 levels deep, and if the tree goes more
> than that deep, then '...' would be printed in the stead of any deeper
> values. So the prompt would change through a  session like:
>
> command       prompt is now
> -----------   ---------------------------------------
> \echo bob     ''   = initial state, no branch going on at all
> \if yes       't' = inside a true branch
> \if no        'tf' = false inside a true
> \endif        't' = back to just the true branch
> \if yes       'tt'
> \if yes       'ttt'
> \if yes       '...ttt' = only show the last 3, but let it be known that
> there's at least one more'
> \else         '...ttz' = past the point of a true bit of this branch

I like the "tfz" idea. I'm not sure whether the up to 6 characters is a 
good, though.

> 2. The printing of #1 could be integrated into %R only in PROMPT_READY
> cases, either prepended or appended to the !/=/^, possibly separated by a :

Hmmm. Logically I would say prepend, but the default prompt is with the 
dbname, which is mostly letters, so it means adding a separator as well.

> 3. Like #2, but prepended/appended in all circumstances

I would say yes.

> 4. Keep %T (or %Y), and reflect the state of pset.active_branch within %R,
> a single t/f/z

Yep, but with a separator?

> 5. Like #4, but also printing the if-stack depth if > 1

Hmmm, not sure...

Based on the your ideas above, I would suggest the following:

   calvin=> \if true
   calvin?t=> SELECT 1 +
   calvin?t->   2;
     3
   calvin?t=> \if true
   calvin?t=>   \echo hello
     hello
   calvin?t=> \endif
   calvin?t=> \else
   calvin?z=>   \echo ignored
   calvin?t=> \endif
   calvin=>

Or maybe use "?.t" for the nested if...

-- 
Fabien

Commits

  1. Support \if ... \elif ... \else ... \endif in psql scripting.

  2. Add a "void *" passthrough pointer for psqlscan.l's callback functions.