Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hstore lookup #342

Open
RJNY opened this issue Nov 14, 2022 · 2 comments
Open

hstore lookup #342

RJNY opened this issue Nov 14, 2022 · 2 comments

Comments

@RJNY
Copy link

RJNY commented Nov 14, 2022

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)

@lann
Copy link
Member

lann commented Nov 14, 2022

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

@RJNY
Copy link
Author

RJNY commented Nov 14, 2022

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants