squirrel
squirrel copied to clipboard
hstore lookup
I have a GET endpoint that searches a resource by converting a struct into a map and passing it to Squirrel
whereClauseMap := map[string]interface{}{
user_first: "Joe",
user_last: "Shmo",
}
And this typically works very well when passing the map literal into there where:
selectBuilder := sq.StatementBuilder.PlaceholderFormat(sq.Dollar).
Select(selectRows).
From(table).
Where(whereClauseMap)
However, I also have an hstore on the table that makes this Where clause less elegant
whereMetaClauseMap := map[string]interface{}{
meta_data_1: "foo",
meta_data_2: "bar",
}
for k, v := range whereMetaClauseMap {
selectBuilder = selectBuilder.Where("meta @> '?=>?'", k, v)
}
For now, I have the following workaround:
selectBuilder := psql.
Select(SelectAbuseRows).
From(abuseTable).
Where(paramsWhereClauseMap)
for k, v := range whereMetaClauseMap {
selectBuilder = selectBuilder.Where("meta @> '?=>?'", k, v)
}
My Question Is there a more elegant solution to passing in a kv map to where it outputs for an hstore lookup? (similar to how a map will generate a basic where clause)
I don't plan to add support for dialect-specific features like that into Squirrel itself, but the Sqlizer
(ToSql
) interface is public so you could create your own wrapper. There are many examples of various complexity in https://github.com/Masterminds/squirrel/blob/master/expr.go
Thanks for the quick reply, I'll poke around and see what I can do with what you've given me and report back for others that may have run into the same issue. Thank you