Skip to content

Commit fac7045

Browse files
authored
Escape quotation marks in strings inside vectors (#222)
* Escape quotation marks in strings inside vectors * Escape backslashes in strings inside vectors * Add several tests for escaping strings in vectors * Bump version to 1.10.0 * Refactor array tests for format check
1 parent 953fb6c commit fac7045

File tree

3 files changed

+34
-25
lines changed

3 files changed

+34
-25
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "LibPQ"
22
uuid = "194296ae-ab2e-5f79-8cd4-7183a0a5a0d1"
33
license = "MIT"
4-
version = "1.9.0"
4+
version = "1.10.0"
55

66
[deps]
77
BinaryProvider = "b99e7846-7c00-51b0-8f62-c81ae34c0232"

src/results.jl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,11 @@ function string_parameter(parameter::AbstractVector)
378378
return String(take!(io))
379379
end
380380

381-
_array_element(el::AbstractString) = "\"$el\""
381+
function _array_element(el::AbstractString)
382+
el = replace(el, "\\" => "\\\\")
383+
el = replace(el, "\"" => "\\\"")
384+
return "\"$el\""
385+
end
382386
_array_element(el::Missing) = "NULL"
383387
_array_element(el) = string_parameter(el)
384388

test/runtests.jl

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1410,33 +1410,38 @@ end
14101410
conn = LibPQ.Connection("dbname=postgres user=$DATABASE_USER"; throw_error=true)
14111411

14121412
@testset "Arrays" begin
1413-
result = execute(conn, "SELECT 'foo' = ANY(\$1)", [["bar", "foo"]])
1414-
@test first(first(result))
1415-
close(result)
1416-
1417-
result = execute(conn, "SELECT 'foo' = ANY(\$1)", (["bar", "foo"],))
1418-
@test first(first(result))
1419-
close(result)
1420-
1421-
result = execute(conn, "SELECT 'foo' = ANY(\$1)", [Any["bar", "foo"]])
1422-
@test first(first(result))
1423-
close(result)
1413+
tests = (
1414+
("SELECT 'foo' = ANY(\$1)", [["bar", "foo"]]),
1415+
("SELECT 'foo' = ANY(\$1)", (["bar", "foo"],)),
1416+
("SELECT 'foo' = ANY(\$1)", [Any["bar", "foo"]]),
1417+
("SELECT 'foo' = ANY(\$1)", Any[Any["bar", "foo"]]),
1418+
("SELECT 'f\"oo' = ANY(\$1)", [["b\"ar", "f\"oo"]]),
1419+
("SELECT 'f\\oo' = ANY(\$1)", [["b\\ar", "f\\oo"]]),
1420+
("SELECT 'f\\\\oo' = ANY(\$1)", [["b\\\\ar", "f\\\\oo"]]),
1421+
("SELECT 'f\\\"oo' = ANY(\$1)", [["b\\\"ar", "f\\\"oo"]]),
1422+
("SELECT 'f\"\\oo' = ANY(\$1)", [["b\"\\ar", "f\"\\oo"]]),
1423+
("SELECT ARRAY[1, 2] = \$1", [[1, 2]]),
1424+
("SELECT ARRAY[1, 2] = \$1", Any[Any[1, 2]])
1425+
)
14241426

1425-
result = execute(conn, "SELECT 'foo' = ANY(\$1)", Any[Any["bar", "foo"]])
1426-
@test first(first(result))
1427-
close(result)
1427+
@testset for (query, arr) in tests
1428+
result = execute(conn, query, arr)
1429+
@test first(first(result))
1430+
close(result)
1431+
end
14281432

1429-
result = execute(conn, "SELECT 'foo' = ANY(\$1)", [["bar", "foobar"]])
1430-
@test !first(first(result))
1431-
close(result)
1433+
tests = (
1434+
("SELECT 'foo' = ANY(\$1)", [["bar", "foobar"]]),
1435+
("SELECT 'f\\\\oo' = ANY(\$1)", [["b\\ar", "f\\oo"]]),
1436+
("SELECT 'f\\oo' = ANY(\$1)", [["b\\\\ar", "f\\\\oo"]])
1437+
)
14321438

1433-
result = execute(conn, "SELECT ARRAY[1, 2] = \$1", [[1, 2]])
1434-
@test first(first(result))
1435-
close(result)
1439+
@testset for (query, arr) in tests
1440+
result = execute(conn, query, arr)
1441+
@test !first(first(result))
1442+
close(result)
1443+
end
14361444

1437-
result = execute(conn, "SELECT ARRAY[1, 2] = \$1", Any[Any[1, 2]])
1438-
@test first(first(result))
1439-
close(result)
14401445
end
14411446

14421447
@testset "Intervals" begin

0 commit comments

Comments
 (0)