Named columns and convenience SQL query interface #6543
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR offers a more modern interface to SQL query results based on named columns, as well as convenience methods for casting result values into popular types. Some breakdown:
Convenience methods in
Value
Previously, after
select
ing an integer column, the process to convert it toint64
was:New convenience methods support the following:
There is no longer a need to call an external
evalengine
function.supported convenience methods are:
ToInt64()
ToUint64()
ToBool()
ToString()
pre-existed.A new type:
NamedResult
Result
type now has aNamed()
method that returns aNamedResult
type. It has same fields, insert ID, rows affected asResult
. It has same Rows data, but the data is an array of maps, where value is found by column name as opposed to ordinal position.Again iterating on the above example:
Note:
true
inconn.Exec()
to get field namestm.Rows[0]["ts"]
means "columnts
in first row", and now the ordinal of the column is unimportant. In this particular example we return a single column, so maybe this example is underwhelming, but in a result set with 10 columns, names are a safer approach to access columns than ordinals.Convenience methods in
NamedResult
:We further make access more convenient like so:
or
.ToInt64("ts")
is a syntactic sugar shortcut.AsInt64("ts", 0)
provides default value (0
in this case) and returns with no error. If column does not exist, or if there's a casting error, the function returns the default value silently.We likewise have
ToString
,AsString
,ToUint64
,AsUint64
,ToBool
,AsBool
functions.Single
Row()
Single row queries are common. Examples:
SELECT UNIX_TIMESTAMP()
SELECT @@global.read_only, @@global.gtid_mode
SELECT COUNT(*) FROM my_table WHERE status=1
The
Row()
function returns the first and single row in a result set, ornil
if this isn't a single row result set.Again iterating on our example:
Optimistic example
When we
SELECT UNIX_TIMESTAMP()
it is safe to assume that the query either returns with an error, or returns with a single row that has a single integer column. Therefore, we can cleanup the above code to read: