Re: Improvements in psql hooks for variables - Mailing list pgsql-hackers
From | Daniel Verite |
---|---|
Subject | Re: Improvements in psql hooks for variables |
Date | |
Msg-id | 7acababf-8529-4d1f-a4a0-6f234e7a7fce@mm Whole thread Raw |
In response to | Re: Improvements in psql hooks for variables (Stephen Frost <sfrost@snowman.net>) |
List | pgsql-hackers |
Stephen Frost wrote: > That certainly doesn't feel right. I'm thinking that if we're going to > throw an error back to the user about a value being invalid then we > shouldn't change the current value. > > My initial thought was that perhaps we should pass the current value to > ParseVariableBool() and let it return whatever the "right" answer is, > however, your use of ParseVariableBool() for enums that happen to accept > on/off means that won't really work. That's not needed once ParseVariableBool() informs the caller about the validity of the boolean expression, which is what the patch already does. For instance I just implemented it for \timing and the diff consists of just that: if (opt) - pset.timing = ParseVariableBool(opt, "\\timing", NULL); + { + bool newval = ParseVariableBool(opt, "\\timing", &success); + if (success) + pset.timing = newval; + } else pset.timing = !pset.timing; That makes \timing foobar being rejected as a bad command with a proper error message and no change of state, which is just what we want. > Perhaps the right answer is to flip this around a bit and treat booleans > as a special case of enums and have a generic solution for enums. > Consider something like: > > ParseVariableEnum(valid_enums, str_value, name, curr_value); > > 'valid_enums' would be a simple list of what the valid values are for a > given variable and their corresponding value, str_value the string the > user typed, name the name of the variable, and curr_value the current > value of the variable. Firstly I'd like to insist that such a refactoring is not necessary for this patch and I feel like it would be out of place in it. That being said, if we wanted this, I think it would be successful only if we'd first change our internal variables pset.* from a struct of different types to a list of variables from some kind of common abstract type and an abstraction layer to access them. That would be an order of magnitude more sophisticated than what we have. Otherwise as I tried to explain in [1], I don't see how we could write a ParseVariableEnum() that would return different types and take variable inputs. Or if we say that ParseVariableEnum should not return the value but affect the variable directly, that would require refactoring all call sites, and what's the benefit that would justify such large changes? Plus we have two different non-mergeable concepts of variables that need this parser: psql variables from VariableSpace stored as strings, and C variables directly instantiated as native types. Also, the argument that bools are just another type of enums is legitimate in theory, but as in psql we accept any left-anchored match of true/false/on/off/0/1, it means that the enumeration of values is in fact: 0 1 t tr tru true f fa fal fals false on of off I don't see that it would help if the code treated the above like just a vanilla list of values, comparable to the other qualifiers like "auto", "expanded", "vertical", an so on, notwithstanding the fact that they don't share the same types. I think that the current code with ParseVariableBool() that only handles booleans is better in terms of separation of concerns and readability. [1] https://www.postgresql.org/message-id/fc879967-da93-43b6-aa5a-92f2d825e786@mm Best regards, -- Daniel Vérité PostgreSQL-powered mailer: http://www.manitou-mail.org Twitter: @DanielVerite
pgsql-hackers by date: