Tech debt: migrate ClickHouse query builder to use native query parameters
As brought up in !2473 (merged) and !2473 (comment 1799261008) we should start using the native query parameters approach to building queries and eventually migrate existing queries.
This allows you to provide named arguments to queries, rather than use positional arg binding as we do currently. See e.g. https://github.com/ClickHouse/clickhouse-go/blob/main/examples/clickhouse_api/query_parameters.go
This would also deprecate the query builder abstraction we are using.
Implementation
If each query method has a well defined query struct, it can easily be marshalled into the clickhouse parameter map using something like mapstructure
, rather than having to create a map literal.
Then, more complex query builder scenarios look something like:
if param.ServiceNames != nil {
b.build(" AND ( ServiceName IN {serviceNames:Array(String)} ) ")
}
if param.NotServiceNames != nil {
b.build(" AND ( ServiceName NOT IN {notServiceNames:Array(String)} ")
}
The builder is really just doing string concatenation here.
Resulting queries are no longer filled with many numeric parameters, but named parameters that can be more easily traced.
Known limitations
Currently the clickhouse-go library does not support marshalling data types to their query parameter types automatically. One must provide a string representation of the type that ClickHouse can convert into the desired data type.
See also https://github.com/ClickHouse/clickhouse-go/pull/854