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

Поиск
Список
Период
Сортировка
От Corey Huinker
Тема Re: \if, \elseif, \else, \endif (was Re: [HACKERS] PSQL commands:\quit_if, \quit_unless)
Дата
Msg-id CADkLM=cr2z935TF3eB9jByWiijv-q2B7--QggfUUB12gCFFByQ@mail.gmail.com
обсуждение исходный текст
Ответ на 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>)
Список pgsql-hackers


On Thu, Jan 26, 2017 at 3:55 PM, Fabien COELHO <coelho@cri.ensmp.fr> wrote:

Hello Daniel,

A comment about control flow and variables: in branches that are not taken, variables are expanded nonetheless, in a way that can be surprising. Case in point:

\set var 'ab''cd'
-- select :var;
\if false
 select :var ;
\else
 select 1;
\endif

To avoid that kind of trouble, would it make sense not to expand
variables in untaken branches?

Hmmm. This case is somehow contrived (for a string, :'var' could be used, in which case escaping would avoid the hazard), but I think that what you suggest is a better behavior, if easy to implement.

--
Fabien.

Good question, Daniel. Variable expansion seems to be done via psql_get_variable which is invoked via callback, which means that I might have to move branch_block_active into PsqlSettings. That's slightly different because the existing boolean is scoped with MainLoop(), but there's no way to get to a new MainLoop unless you're in an executing branch, and no way to legally exit a MainLoop with an unbalanced if-endif state. In short, I think it's better behavior. It does prevent someone from setting a variable to '\endif' and expecting that to work, like this:

select case
     when random() < 0.5 then '\endif'
     else E'\else\n\echo fooled you\n\endif'
end as contrived_metaprogramming
\gset

\if false
  :contrived_metaprogramming

I'm sure someone will argue that preventing that is a good thing. Unless someone sees a good reason not to move that PsqlSettings, I'll make that change.




 

В списке pgsql-hackers по дате отправления:

Предыдущее
От: Fabien COELHO
Дата:
Сообщение: Re: \if, \elseif, \else, \endif (was Re: [HACKERS] PSQL commands:\quit_if, \quit_unless)
Следующее
От: David Rowley
Дата:
Сообщение: Re: [HACKERS] Performance improvement for joins where outer side is unique