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

Support and default to UUID for PostgreSQL uuid #262

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ OffsetArrays = "6fe1bfb0-de20-5000-8ca7-80f57d26f881"
SQLStrings = "af517c2e-c243-48fa-aab8-efac3db270f5"
Tables = "bd369af6-aec1-5ad0-b16a-f7cc5008161c"
TimeZones = "f269a46b-ccf7-5d73-abea-4c690281aa53"
UUIDs = "cf7118a7-6976-5b1a-9a39-7adc72f591a4"

[compat]
CEnum = "0.2, 0.3, 0.4"
Expand Down
1 change: 1 addition & 0 deletions src/LibPQ.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ using Memento: Memento, getlogger, warn, info, error, debug
using OffsetArrays
using SQLStrings
using TimeZones
using UUIDs: UUID

const Parameter = Union{String,Missing}
const LOGGER = getlogger(@__MODULE__)
Expand Down
8 changes: 7 additions & 1 deletion src/parsing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,12 @@ function pqparse(::Type{Vector{UInt8}}, bytes::Array{UInt8,1})
return unescaped_vec
end

## uuid
_DEFAULT_TYPE_MAP[:uuid] = UUID
function Base.parse(::Type{UUID}, pqv::PQBinaryValue{PQ_SYSTEM_TYPES[:uuid]})
return UUID(pqparse(UInt128, data_pointer(pqv)))
end

## bool
# TODO: check whether we ever need this or if PostgreSQL always gives t or f
_DEFAULT_TYPE_MAP[:bool] = Bool
Expand Down Expand Up @@ -664,7 +670,7 @@ function array_size(str)
return dims
end

for pq_eltype in ("int2", "int4", "int8", "float4", "float8", "oid", "numeric")
for pq_eltype in ("int2", "int4", "int8", "float4", "float8", "oid", "numeric", "uuid")
array_oid = PQ_SYSTEM_TYPES[Symbol("_$pq_eltype")]
jl_type = _DEFAULT_TYPE_MAP[Symbol(pq_eltype)]
jl_missingtype = Union{jl_type,Missing}
Expand Down
18 changes: 17 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ using OffsetArrays
using SQLStrings
using TimeZones
using Tables
using UUIDs: UUID

Memento.config!("critical")

Expand Down Expand Up @@ -1191,6 +1192,7 @@ end
("'hello '::char(10)", "hello"),
("'hello '::varchar(10)", "hello "),
("'3'::\"char\"", LibPQ.PQChar('3')),
("'6dc2b682-a411-a51f-ce9e-af63d1ef7c1a'::uuid", UUID("6dc2b682-a411-a51f-ce9e-af63d1ef7c1a")),
("'t'::bool", true),
("'T'::bool", true),
("'true'::bool", true),
Expand Down Expand Up @@ -1256,6 +1258,7 @@ end
("'{{{NULL,2,3},{4,NULL,6}}}'::float8[]", Array{Union{Float64, Missing}}(reshape(Union{Float64, Missing}[missing 2 3; 4 missing 6], 1, 2, 3))),
("'{{{1,2,3},{4,5,6}}}'::oid[]", Array{Union{LibPQ.Oid, Missing}}(reshape(LibPQ.Oid[1 2 3; 4 5 6], 1, 2, 3))),
("'{{{1,2,3},{4,5,6}}}'::numeric[]", Array{Union{Decimal, Missing}}(reshape(Decimal[1 2 3; 4 5 6], 1, 2, 3))),
("'{6dc2b682-a411-a51f-ce9e-af63d1ef7c1a}'::uuid[]", Union{UUID, Missing}[UUID("6dc2b682-a411-a51f-ce9e-af63d1ef7c1a")]),
("'[1:1][-2:-1][3:5]={{{1,2,3},{4,5,6}}}'::int2[]", copyto!(OffsetArray{Union{Missing, Int16}}(undef, 1:1, -2:-1, 3:5), [1 2 3; 4 5 6])),
("'[1:1][-2:-1][3:5]={{{1,2,3},{4,5,6}}}'::int4[]", copyto!(OffsetArray{Union{Missing, Int32}}(undef, 1:1, -2:-1, 3:5), [1 2 3; 4 5 6])),
("'[1:1][-2:-1][3:5]={{{1,2,3},{4,5,6}}}'::int8[]", copyto!(OffsetArray{Union{Missing, Int64}}(undef, 1:1, -2:-1, 3:5), [1 2 3; 4 5 6])),
Expand Down Expand Up @@ -1416,6 +1419,18 @@ end
@testset "Parameters" begin
conn = LibPQ.Connection("dbname=postgres user=$DATABASE_USER"; throw_error=true)

@testset "UUID" begin
tests = (
("'6dc2b682-a411-a51f-ce9e-af63d1ef7c1a'::uuid", UUID("6dc2b682-a411-a51f-ce9e-af63d1ef7c1a")),
Comment on lines +1422 to +1424
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[JuliaFormatter] reported by reviewdog 🐶

Suggested change
@testset "UUID" begin
tests = (
("'6dc2b682-a411-a51f-ce9e-af63d1ef7c1a'::uuid", UUID("6dc2b682-a411-a51f-ce9e-af63d1ef7c1a")),
@testset "Parameters" begin
conn = LibPQ.Connection(
"dbname=postgres user=$DATABASE_USER"; throw_error=true

)

@testset for (pg_str, obj) in tests
result = execute(conn, "SELECT $pg_str = \$1", [obj])
@test first(first(result))
close(result)
Comment on lines +1427 to +1430
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[JuliaFormatter] reported by reviewdog 🐶

Suggested change
@testset for (pg_str, obj) in tests
result = execute(conn, "SELECT $pg_str = \$1", [obj])
@test first(first(result))
close(result)
@testset "UUID" begin
tests = ((
"'6dc2b682-a411-a51f-ce9e-af63d1ef7c1a'::uuid",
UUID("6dc2b682-a411-a51f-ce9e-af63d1ef7c1a"),
),)
@testset for (pg_str, obj) in tests
result = execute(conn, "SELECT $pg_str = \$1", [obj])
@test first(first(result))
close(result)
end

end
end
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[JuliaFormatter] reported by reviewdog 🐶

Suggested change
end


@testset "Arrays" begin
tests = (
("SELECT 'foo' = ANY(\$1)", [["bar", "foo"]]),
Expand All @@ -1428,7 +1443,8 @@ end
("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]])
("SELECT ARRAY[1, 2] = \$1", Any[Any[1, 2]]),
("SELECT '{6dc2b682-a411-a51f-ce9e-af63d1ef7c1a}'::uuid[] = \$1", [[UUID("6dc2b682-a411-a51f-ce9e-af63d1ef7c1a")]]),
)

@testset for (query, arr) in tests
Expand Down