Skip to content

Support and default to UUID for PostgreSQL uuid #288

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

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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,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"
UTCDateTimes = "0f7cfa37-7abf-4834-b969-a8aa512401c2"

[compat]
Expand Down
3 changes: 2 additions & 1 deletion src/LibPQ.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ using Base.Iterators: zip, product
using Base.Threads

using Dates
using DBInterface
using DocStringExtensions
using Decimals
using FileWatching
Expand All @@ -28,8 +29,8 @@ using Memento: Memento, getlogger, warn, info, error, debug
using OffsetArrays
using SQLStrings
using TimeZones
using UUIDs: UUID
using UTCDateTimes
using DBInterface

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 @@ -692,7 +698,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 @@ -14,6 +14,7 @@ using SQLStrings
using DBInterface
using TimeZones
using Tables
using UUIDs: UUID
using UTCDateTimes

Memento.config!("critical")
Expand Down Expand Up @@ -1193,6 +1194,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 @@ -1258,6 +1260,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 @@ -1429,6 +1432,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")),
)

@testset for (pg_str, obj) in tests
result = execute(conn, "SELECT $pg_str = \$1", [obj])
@test first(first(result))
close(result)
end
end

@testset "Arrays" begin
tests = (
("SELECT 'foo' = ANY(\$1)", [["bar", "foo"]]),
Expand All @@ -1441,7 +1456,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