Fix sql escape function for array of strings
n-pn opened this issue · 2 comments
n-pn commented
Searching for records with array field contain other array using @>
(e.g Post.query("tags @> ?", tags)
does not yield any result.
n-pn commented
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 commented
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.