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 #288

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 DBInterface
using TimeZones
using Tables
using UUIDs: UUID
using UTCDateTimes

Memento.config!("critical")
Expand Down Expand Up @@ -1166,7 +1167,7 @@
OffsetArray,
]

@testset for binary_format in (LibPQ.TEXT, LibPQ.BINARY)

Check warning on line 1170 in test/runtests.jl

View workflow job for this annotation

GitHub Actions / format

[JuliaFormatter] reported by reviewdog 🐶 Raw Output: test/runtests.jl:1170:- @testset for binary_format in (LibPQ.TEXT, LibPQ.BINARY) test/runtests.jl:1171:- @testset "Default Types" begin test/runtests.jl:1172:- conn = LibPQ.Connection("dbname=postgres user=$DATABASE_USER"; throw_error=true) test/runtests.jl:1173:- test/runtests.jl:1174:- test_data = [ test/runtests.jl:1175:- ("3", Cint(3)), test/runtests.jl:1176:- ("3::int8", Int64(3)), test/runtests.jl:1177:- ("3::int4", Int32(3)), test/runtests.jl:1178:- ("3::int2", Int16(3)), test/runtests.jl:1179:- ("3::float8", Float64(3)), test/runtests.jl:1180:- ("3::float4", Float32(3)), test/runtests.jl:1181:- ("3::oid", LibPQ.Oid(3)), test/runtests.jl:1182:- ("3::numeric", decimal("3")), test/runtests.jl:1183:- ("$(BigFloat(pi))::numeric", decimal(BigFloat(pi))), test/runtests.jl:1184:- ("$(big"4608230166434464229556241992703")::numeric", parse(Decimal, "4608230166434464229556241992703")), test/runtests.jl:1185:- ("E'\\\\xDEADBEEF'::bytea", hex2bytes("DEADBEEF")), test/runtests.jl:1186:- ("E'\\\\000'::bytea", UInt8[0o000]), test/runtests.jl:1187:- ("E'\\\\047'::bytea", UInt8[0o047]), test/runtests.jl:1188:- ("E'\\''::bytea", UInt8[0o047]), test/runtests.jl:1189:- ("E'\\\\134'::bytea", UInt8[0o134]), test/runtests.jl:1190:- ("E'\\\\\\\\'::bytea", UInt8[0o134]), test/runtests.jl:1191:- ("E'\\\\001'::bytea", UInt8[0o001]), test/runtests.jl:1192:- ("E'\\\\176'::bytea", UInt8[0o176]), test/runtests.jl:1193:- ("'hello'::char(10)", "hello"), test/runtests.jl:1194:- ("'hello '::char(10)", "hello"), test/runtests.jl:1195:- ("'hello '::varchar(10)", "hello "), test/runtests.jl:1196:- ("'3'::\"char\"", LibPQ.PQChar('3')), test/runtests.jl:1197:- ("'6dc2b682-a411-a51f-ce9e-af63d1ef7c1a'::uuid", UUID("6dc2b682-a411-a51f-ce9e-af63d1ef7c1a")), test/runtests.jl:1198:- ("'t'::bool", true), test/runtests.jl:1199:- ("'T'::bool", true), test/runtests.jl:1200:- ("'true'::bool", true), test/runtests.jl:1201:- ("'TRUE'::bool", true), test/runtests.jl:1202:- ("'tRuE'::bool", true), test/runtests.jl:1203:- ("'y'::bool", true), test/runtests.jl:1204:- ("'YEs'::bool", true), test/runtests.jl:1205:- ("'on'::bool", true), test/runtests.jl:1206:- ("1::bool", true), test/runtests.jl:1207:- ("true", true), test/runtests.jl:1208:- ("'f'::bool", false), test/runtests.jl:1209:- ("'F'::bool", false), test/runtests.jl:1210:- ("'false'::bool", false), test/runtests.jl:1211:- ("'FALSE'::bool", false), test/runtests.jl:1212:- ("'fAlsE'::bool", false), test/runtests.jl:1213:- ("'n'::bool", false), test/runtests.jl:1214:- ("'nO'::bool", false), test/runtests.jl:1215:- ("'off'::bool", false), test/runtests.jl:1216:- ("0::bool", false), test/runtests.jl:1217:- ("false", false), test/runtests.jl:1218:- ("TIMESTAMP '2004-10-19 10:23:54'", DateTime(2004, 10, 19, 10, 23, 54
@testset "Default Types" begin
conn = LibPQ.Connection("dbname=postgres user=$DATABASE_USER"; throw_error=true)

Expand All @@ -1193,6 +1194,7 @@
("'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 @@
("'{{{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 @@ -1424,12 +1427,24 @@
end
end
end
end

Check warning on line 1430 in test/runtests.jl

View workflow job for this annotation

GitHub Actions / format

[JuliaFormatter] reported by reviewdog 🐶 Raw Output: test/runtests.jl:1430:- end test/runtests.jl:1431:- test/runtests.jl:1432:- @testset "Parameters" begin test/runtests.jl:1433:- conn = LibPQ.Connection("dbname=postgres user=$DATABASE_USER"; throw_error=true)

@testset "Parameters" begin
conn = LibPQ.Connection("dbname=postgres user=$DATABASE_USER"; throw_error=true)

@testset "UUID" begin

Check warning on line 1435 in test/runtests.jl

View workflow job for this annotation

GitHub Actions / format

[JuliaFormatter] reported by reviewdog 🐶 Raw Output: test/runtests.jl:1435:- @testset "UUID" begin test/runtests.jl:1436:- tests = ( test/runtests.jl:1437:- ("'6dc2b682-a411-a51f-ce9e-af63d1ef7c1a'::uuid", UUID("6dc2b682-a411-a51f-ce9e-af63d1ef7c1a")), test/runtests.jl:1913:+ @testset "Parameters" begin test/runtests.jl:1914:+ conn = LibPQ.Connection( test/runtests.jl:1915:+ "dbname=postgres user=$DATABASE_USER"; throw_error=true
tests = (
("'6dc2b682-a411-a51f-ce9e-af63d1ef7c1a'::uuid", UUID("6dc2b682-a411-a51f-ce9e-af63d1ef7c1a")),
)

@testset for (pg_str, obj) in tests

Check warning on line 1440 in test/runtests.jl

View workflow job for this annotation

GitHub Actions / format

[JuliaFormatter] reported by reviewdog 🐶 Raw Output: test/runtests.jl:1440:- @testset for (pg_str, obj) in tests test/runtests.jl:1441:- result = execute(conn, "SELECT $pg_str = \$1", [obj]) test/runtests.jl:1442:- @test first(first(result)) test/runtests.jl:1443:- close(result) test/runtests.jl:1918:+ @testset "UUID" begin test/runtests.jl:1919:+ tests = (( test/runtests.jl:1920:+ "'6dc2b682-a411-a51f-ce9e-af63d1ef7c1a'::uuid", test/runtests.jl:1921:+ UUID("6dc2b682-a411-a51f-ce9e-af63d1ef7c1a"), test/runtests.jl:1922:+ ),) test/runtests.jl:1923:+ test/runtests.jl:1924:+ @testset for (pg_str, obj) in tests test/runtests.jl:1925:+ result = execute(conn, "SELECT $pg_str = \$1", [obj]) test/runtests.jl:1926:+ @test first(first(result)) test/runtests.jl:1927:+ close(result) test/runtests.jl:1928:+ end
result = execute(conn, "SELECT $pg_str = \$1", [obj])
@test first(first(result))
close(result)
end
end

Check warning on line 1445 in test/runtests.jl

View workflow job for this annotation

GitHub Actions / format

[JuliaFormatter] reported by reviewdog 🐶 Raw Output: test/runtests.jl:1445:- end

@testset "Arrays" begin

Check warning on line 1447 in test/runtests.jl

View workflow job for this annotation

GitHub Actions / format

[JuliaFormatter] reported by reviewdog 🐶 Raw Output: test/runtests.jl:1447:- @testset "Arrays" begin test/runtests.jl:1448:- tests = ( test/runtests.jl:1449:- ("SELECT 'foo' = ANY(\$1)", [["bar", "foo"]]), test/runtests.jl:1450:- ("SELECT 'foo' = ANY(\$1)", (["bar", "foo"],)), test/runtests.jl:1451:- ("SELECT 'foo' = ANY(\$1)", [Any["bar", "foo"]]), test/runtests.jl:1452:- ("SELECT 'foo' = ANY(\$1)", Any[Any["bar", "foo"]]), test/runtests.jl:1453:- ("SELECT 'f\"oo' = ANY(\$1)", [["b\"ar", "f\"oo"]]), test/runtests.jl:1454:- ("SELECT 'f\\oo' = ANY(\$1)", [["b\\ar", "f\\oo"]]), test/runtests.jl:1455:- ("SELECT 'f\\\\oo' = ANY(\$1)", [["b\\\\ar", "f\\\\oo"]]), test/runtests.jl:1456:- ("SELECT 'f\\\"oo' = ANY(\$1)", [["b\\\"ar", "f\\\"oo"]]), test/runtests.jl:1457:- ("SELECT 'f\"\\oo' = ANY(\$1)", [["b\"\\ar", "f\"\\oo"]]), test/runtests.jl:1458:- ("SELECT ARRAY[1, 2] = \$1", [[1, 2]]), test/runtests.jl:1459:- ("SELECT ARRAY[1, 2] = \$1", Any[Any[1, 2]]), test/runtests.jl:1460:- ("SELECT '{6dc2b682-a411-a51f-ce9e-af63d1ef7c1a}'::uuid[] = \$1", [[UUID("6dc2b682-a411-a51f-ce9e-af63d1ef7c1a")]]), test/runtests.jl:1461:- ) test/runtests.jl:1931:+ @testset "Arrays" begin test/runtests.jl:1932:+ tests = ( test/runtests.jl:1933:+ ("SELECT 'foo' = ANY(\$1)", [["bar", "foo"]]), test/runtests.jl:1934:+ ("SELECT 'foo' = ANY(\$1)", (["bar", "foo"],)), test/runtests.jl:1935:+ ("SELECT 'foo' = ANY(\$1)", [Any["bar", "foo"]]), test/runtests.jl:1936:+ ("SELECT 'foo' = ANY(\$1)", Any[Any["bar", "foo"]]), test/runtests.jl:1937:+ ("SELECT 'f\"oo' = ANY(\$1)", [["b\"ar", "f\"oo"]]), test/runtests.jl:1938:+ ("SELECT 'f\\oo' = ANY(\$1)", [["b\\ar", "f\\oo"]]), test/runtests.jl:1939:+ ("SELECT 'f\\\\oo' = ANY(\$1)", [["b\\\\ar", "f\\\\oo"]]), test/runtests.jl:1940:+ ("SELECT 'f\\\"oo' = ANY(\$1)", [["b\\\"ar", "f\\\"oo"]]), test/runtests.jl:1941:+ ("SELECT 'f\"\\oo' = ANY(\$1)", [["b\"\\ar", "f\"\\oo"]]), test/runtests.jl:1942:+ ("SELECT ARRAY[1, 2] = \$1", [[1, 2]]), test/runtests.jl:1943:+ ("SELECT ARRAY[1, 2] = \$1", Any[Any[1, 2]]), test/runtests.jl:1944:+ ( test/runtests.jl:1945:+ "SELECT '{6dc2b682-a411-a51f-ce9e-af63d1ef7c1a}'::uuid[] = \$1", test/runtests.jl:1946:+ [[UUID("6dc2b682-a411-a51f-ce9e-af63d1ef7c1a")]], test/runtests.jl:1947:+ ), test/runtests.jl:1948:+ )
tests = (
("SELECT 'foo' = ANY(\$1)", [["bar", "foo"]]),
("SELECT 'foo' = ANY(\$1)", (["bar", "foo"],)),
Expand All @@ -1441,10 +1456,11 @@
("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

Check warning on line 1463 in test/runtests.jl

View workflow job for this annotation

GitHub Actions / format

[JuliaFormatter] reported by reviewdog 🐶 Raw Output: test/runtests.jl:1463:- @testset for (query, arr) in tests test/runtests.jl:1464:- result = execute(conn, query, arr) test/runtests.jl:1465:- @test first(first(result)) test/runtests.jl:1466:- close(result) test/runtests.jl:1467:- end test/runtests.jl:1950:+ @testset for (query, arr) in tests test/runtests.jl:1951:+ result = execute(conn, query, arr) test/runtests.jl:1952:+ @test first(first(result)) test/runtests.jl:1953:+ close(result) test/runtests.jl:1954:+ end
result = execute(conn, query, arr)
@test first(first(result))
close(result)
Expand Down
Loading