Skip to content

Commit 3a1992c

Browse files
committed
switch to version based logic
1 parent 4f774dc commit 3a1992c

File tree

1 file changed

+31
-23
lines changed

1 file changed

+31
-23
lines changed

src/parsing.jl

+31-23
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ By default, this uses any existing `parse` method for parsing a value of type `T
124124
You can implement default PostgreSQL-specific parsing for a given type by overriding
125125
`pqparse`.
126126
"""
127-
Base.parse(::Type{T}, pqv::PQValue) where T = pqparse(T, string_view(pqv))
127+
Base.parse(::Type{T}, pqv::PQValue) where {T} = pqparse(T, string_view(pqv))
128128

129129
"""
130130
LibPQ.pqparse(::Type{T}, str::AbstractString) -> T
@@ -135,9 +135,9 @@ This is used to parse PostgreSQL's output format.
135135
function pqparse end
136136

137137
# Fallback method
138-
pqparse(::Type{T}, str::AbstractString) where T = parse(T, str)
138+
pqparse(::Type{T}, str::AbstractString) where {T} = parse(T, str)
139139

140-
function pqparse(::Type{T}, ptr::Ptr{UInt8}) where T<:Number
140+
function pqparse(::Type{T}, ptr::Ptr{UInt8}) where {T<:Number}
141141
return ntoh(unsafe_load(Ptr{T}(ptr)))
142142
end
143143

@@ -147,7 +147,7 @@ pqparse(::Type{Symbol}, str::AbstractString) = Symbol(str)
147147
function generate_binary_parser(symbol)
148148
@eval function Base.parse(
149149
::Type{T}, pqv::PQBinaryValue{$(oid(symbol))}
150-
) where T<:Number
150+
) where {T<:Number}
151151
return convert(T, pqparse($(_DEFAULT_TYPE_MAP[symbol]), data_pointer(pqv)))
152152
end
153153
end
@@ -295,19 +295,27 @@ end
295295

296296
_DEFAULT_TYPE_MAP[:time] = Time
297297
function pqparse(::Type{Time}, str::AbstractString)
298-
result = nothing
299-
try
298+
@static if v"1.6.6" <= VERSION < v"1.7.0" || VERSION >= "1.7.2"
300299
result = tryparse(Time, str)
301-
catch err
302-
if !(err isa InexactError)
303-
rethrow(err)
300+
if isnothing(result)
301+
# If there's an error we want to see it here
302+
result = parse(Time, _trunc_seconds(str))
303+
end
304+
return result
305+
else
306+
try
307+
return parse(Time, str)
308+
catch err
309+
if !(err isa InexactError)
310+
rethrow(err)
311+
end
312+
return parse(Time, _trunc_seconds(str))
304313
end
305314
end
306-
return isnothing(result) ? parse(Time, _trunc_seconds(str)) : result
307315
end
308316

309317
# InfExtendedTime support for Dates.TimeType
310-
function pqparse(::Type{InfExtendedTime{T}}, str::AbstractString) where T<:Dates.TimeType
318+
function pqparse(::Type{InfExtendedTime{T}}, str::AbstractString) where {T<:Dates.TimeType}
311319
if str == "infinity"
312320
return InfExtendedTime{T}(∞)
313321
elseif str == "-infinity"
@@ -372,7 +380,7 @@ end
372380

373381
function pqparse(
374382
::Type{InfExtendedTime{T}}, ptr::Ptr{UInt8}
375-
) where T<:Dates.AbstractDateTime
383+
) where {T<:Dates.AbstractDateTime}
376384
microseconds = ntoh(unsafe_load(Ptr{Int64}(ptr)))
377385
if microseconds == typemax(Int64)
378386
return InfExtendedTime{T}(∞)
@@ -383,7 +391,7 @@ function pqparse(
383391
return InfExtendedTime{T}(pqparse(T, ptr))
384392
end
385393

386-
function pqparse(::Type{InfExtendedTime{T}}, ptr::Ptr{UInt8}) where T<:Date
394+
function pqparse(::Type{InfExtendedTime{T}}, ptr::Ptr{UInt8}) where {T<:Date}
387395
microseconds = ntoh(unsafe_load(Ptr{Int32}(ptr)))
388396
if microseconds == typemax(Int32)
389397
return InfExtendedTime{T}(∞)
@@ -397,7 +405,7 @@ end
397405
function generate_binary_date_parser(symbol)
398406
@eval function Base.parse(
399407
::Type{T}, pqv::PQBinaryValue{$(oid(symbol))}
400-
) where T<:TimeType
408+
) where {T<:TimeType}
401409
return pqparse(T, data_pointer(pqv))
402410
end
403411
end
@@ -533,25 +541,25 @@ _DEFAULT_TYPE_MAP[:tsrange] = Interval{DateTime}
533541
_DEFAULT_TYPE_MAP[:tstzrange] = Interval{ZonedDateTime}
534542
_DEFAULT_TYPE_MAP[:daterange] = Interval{Date}
535543

536-
function pqparse(::Type{Interval{T}}, str::AbstractString) where T
544+
function pqparse(::Type{Interval{T}}, str::AbstractString) where {T}
537545
str == "empty" && return Interval{T}()
538546
return parse(Interval{T}, str; element_parser=pqparse)
539547
end
540548

541549
# How to parse range binary fetch is shown here
542550
# https://github.com/postgres/postgres/blob/31079a4a8e66e56e48bad94d380fa6224e9ffa0d/src/backend/utils/adt/rangetypes.c#L162
543-
const RANGE_EMPTY = 0b00000001
551+
const RANGE_EMPTY = 0b00000001
544552
const RANGE_LOWER_BOUND_INCLUSIVE = 0b00000010
545553
const RANGE_UPPER_BOUND_INCLUSIVE = 0b00000100
546554
const RANGE_LOWER_BOUND_INFINITIY = 0b00001000
547555
const RANGE_UPPER_BOUND_INFINITIY = 0b00010000
548-
const RANGE_LOWER_BOUND_NULL = 0b00100000
549-
const RANGE_UPPER_BOUND_NULL = 0b01000000
556+
const RANGE_LOWER_BOUND_NULL = 0b00100000
557+
const RANGE_UPPER_BOUND_NULL = 0b01000000
550558

551559
function generate_range_binary_parser(symbol)
552560
@eval function Base.parse(
553561
::Type{Interval{T}}, pqv::PQBinaryValue{$(oid(symbol))}
554-
) where T
562+
) where {T}
555563
current_pointer = data_pointer(pqv)
556564
flags = ntoh(unsafe_load(Ptr{UInt8}(current_pointer)))
557565
current_pointer += sizeof(UInt8)
@@ -590,13 +598,13 @@ foreach(
590598

591599
## arrays
592600
# numeric arrays never have double quotes and always use ',' as a separator
593-
parse_numeric_element(::Type{T}, str) where T = parse(T, str)
601+
parse_numeric_element(::Type{T}, str) where {T} = parse(T, str)
594602

595-
function parse_numeric_element(::Type{Union{T,Missing}}, str) where T
603+
function parse_numeric_element(::Type{Union{T,Missing}}, str) where {T}
596604
return str == "NULL" ? missing : parse(T, str)
597605
end
598606

599-
function parse_numeric_array(eltype::Type{T}, str::AbstractString) where T
607+
function parse_numeric_array(eltype::Type{T}, str::AbstractString) where {T}
600608
eq_ind = findfirst(isequal('='), str)
601609

602610
if eq_ind !== nothing
@@ -670,7 +678,7 @@ for pq_eltype in ("int2", "int4", "int8", "float4", "float8", "oid", "numeric")
670678
for jl_eltype in (jl_type, jl_missingtype)
671679
@eval function pqparse(
672680
::Type{A}, str::AbstractString
673-
) where A<:AbstractArray{$jl_eltype}
681+
) where {A<:AbstractArray{$jl_eltype}}
674682
return parse_numeric_array($jl_eltype, str)::A
675683
end
676684
end

0 commit comments

Comments
 (0)