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

Fix sql escape function for array of strings #206

Open
n-pn opened this issue Dec 10, 2020 · 2 comments
Open

Fix sql escape function for array of strings #206

n-pn opened this issue Dec 10, 2020 · 2 comments
Labels

Comments

@n-pn
Copy link

n-pn commented Dec 10, 2020

Searching for records with array field contain other array using @> (e.g Post.query("tags @> ?", tags) does not yield any result.

@n-pn n-pn changed the title Support postgreql array contains and is contained by operators Support postgreql array @> and <@ operators Dec 10, 2020
@n-pn
Copy link
Author

n-pn commented Dec 10, 2020

I think the problem lies in these two functions:

https://github.com/anykeyh/clear/blob/master/src/clear/expression/expression.cr#L127
https://github.com/anykeyh/clear/blob/master/src/clear/expression/expression.cr#L112

array ["a", "b", "c"] will be escaped as '{''a'', ''b'', ''c''}' instead of '{"a", "b", "c"}' (first one is two ' instead of a single ").

@n-pn n-pn changed the title Support postgreql array @> and <@ operators Fix sql escape function for array of string Dec 10, 2020
@n-pn
Copy link
Author

n-pn commented Dec 10, 2020

change method def self.safe_literal(x : Enumerable(AvailableLiteral)) body to

{"ARRAY[", x.map { |item| self.safe_literal(item) }.join(", "), "]"}.join

solve the problem for me.

@n-pn n-pn changed the title Fix sql escape function for array of string Fix sql escape function for array of strings Dec 10, 2020
@anykeyh anykeyh added the bug label Feb 13, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants