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

Escape quotation marks in strings inside vectors #222

Merged
merged 5 commits into from
Jan 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "LibPQ"
uuid = "194296ae-ab2e-5f79-8cd4-7183a0a5a0d1"
license = "MIT"
version = "1.9.0"
version = "1.10.0"

[deps]
BinaryProvider = "b99e7846-7c00-51b0-8f62-c81ae34c0232"
Expand Down
6 changes: 5 additions & 1 deletion src/results.jl
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,11 @@ function string_parameter(parameter::AbstractVector)
return String(take!(io))
end

_array_element(el::AbstractString) = "\"$el\""
function _array_element(el::AbstractString)
el = replace(el, "\\" => "\\\\")
el = replace(el, "\"" => "\\\"")
return "\"$el\""
end
_array_element(el::Missing) = "NULL"
_array_element(el) = string_parameter(el)

Expand Down
51 changes: 28 additions & 23 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1410,33 +1410,38 @@ end
conn = LibPQ.Connection("dbname=postgres user=$DATABASE_USER"; throw_error=true)

@testset "Arrays" begin
result = execute(conn, "SELECT 'foo' = ANY(\$1)", [["bar", "foo"]])
@test first(first(result))
close(result)

result = execute(conn, "SELECT 'foo' = ANY(\$1)", (["bar", "foo"],))
@test first(first(result))
close(result)

result = execute(conn, "SELECT 'foo' = ANY(\$1)", [Any["bar", "foo"]])
@test first(first(result))
close(result)
tests = (
("SELECT 'foo' = ANY(\$1)", [["bar", "foo"]]),
("SELECT 'foo' = ANY(\$1)", (["bar", "foo"],)),
("SELECT 'foo' = ANY(\$1)", [Any["bar", "foo"]]),
("SELECT 'foo' = ANY(\$1)", Any[Any["bar", "foo"]]),
("SELECT 'f\"oo' = ANY(\$1)", [["b\"ar", "f\"oo"]]),
("SELECT 'f\\oo' = ANY(\$1)", [["b\\ar", "f\\oo"]]),
("SELECT 'f\\\\oo' = ANY(\$1)", [["b\\\\ar", "f\\\\oo"]]),
("SELECT 'f\\\"oo' = ANY(\$1)", [["b\\\"ar", "f\\\"oo"]]),
("SELECT 'f\"\\oo' = ANY(\$1)", [["b\"\\ar", "f\"\\oo"]]),
("SELECT ARRAY[1, 2] = \$1", [[1, 2]]),
("SELECT ARRAY[1, 2] = \$1", Any[Any[1, 2]])
)

result = execute(conn, "SELECT 'foo' = ANY(\$1)", Any[Any["bar", "foo"]])
@test first(first(result))
close(result)
@testset for (query, arr) in tests
result = execute(conn, query, arr)
@test first(first(result))
close(result)
end

result = execute(conn, "SELECT 'foo' = ANY(\$1)", [["bar", "foobar"]])
@test !first(first(result))
close(result)
tests = (
("SELECT 'foo' = ANY(\$1)", [["bar", "foobar"]]),
("SELECT 'f\\\\oo' = ANY(\$1)", [["b\\ar", "f\\oo"]]),
("SELECT 'f\\oo' = ANY(\$1)", [["b\\\\ar", "f\\\\oo"]])
)

result = execute(conn, "SELECT ARRAY[1, 2] = \$1", [[1, 2]])
@test first(first(result))
close(result)
@testset for (query, arr) in tests
result = execute(conn, query, arr)
@test !first(first(result))
close(result)
end

result = execute(conn, "SELECT ARRAY[1, 2] = \$1", Any[Any[1, 2]])
@test first(first(result))
close(result)
end

@testset "Intervals" begin
Expand Down