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: Tom Lane <tgl@sss.pgh.pa.us>, Greg Stark <stark@mit.edu>, Erik Rijkers <er@xs4all.nl>, 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-03-01T17:23:00Z
Lists: pgsql-hackers
Hello Corey,

> on elif
>  if misplaced elif
>     misplaced elif error
>  else
>     eval expression
>       => possible eval error
>     set new status if eval fine

Currently it is really:

   switch (state) {
   case NONE:
   case ELSE_TRUE:
   case ELSE_FALSE:
      success = false;
      show some error
   default:
   }
   if (success) {
     success = evaluate_expression(...);
     if (success) {
        switch (state) {
        case ...:
        default:
        }
     }
   }

Which I do not find so neat. The previous one with nested switch-if-switch 
looked as bad.

> The issue at hand being the benefit to the user vs code complexity.

Hmmm.

One of my point is that I do not really see the user benefit... for me the 
issue is to have no user benefit and code complexity.

The case we are discussing is for the user who decides to write code with 
*two* errors on the same line:

   \if good-condition
   \else
   \elif bad-condition
   \endif

with an added complexity to show the elif bad position error first. Why 
should we care so much for such a special case?

Maybe an alternative could be to write simpler code anyway, somehow like 
it was before:

   // on "elif"
   switch (peek(state)) {
   case NONE:       error;
   case ELSE_TRUE:  error;
   case ELSE_FALSE: error;
   case IGNORED:    break;
   case TRUE:       poke IGNORED;
   case FALSE:
                    success = evaluate(&is_true)
                    if (!success)
                      error;
                    else if (is_true)
                        poke TRUE
   default:         error;
   }

The only difference is that the evaluation is not done when it is not 
needed (what a draw back) but ISTM that it is significantly easier to 
understand and maintain.

Now if you want to require committer opinion on this one, fine with me.

-- 
Fabien.


Commits

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

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