squirrel icon indicating copy to clipboard operation
squirrel copied to clipboard

hstore lookup

Open RJNY opened this issue 2 years ago • 2 comments

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)

RJNY avatar Nov 14 '22 21:11 RJNY

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

lann avatar Nov 14 '22 21:11 lann

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

RJNY avatar Nov 14 '22 22:11 RJNY