@@ -124,7 +124,7 @@ By default, this uses any existing `parse` method for parsing a value of type `T
124
124
You can implement default PostgreSQL-specific parsing for a given type by overriding
125
125
`pqparse`.
126
126
"""
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))
128
128
129
129
"""
130
130
LibPQ.pqparse(::Type{T}, str::AbstractString) -> T
@@ -135,9 +135,9 @@ This is used to parse PostgreSQL's output format.
135
135
function pqparse end
136
136
137
137
# Fallback method
138
- pqparse (:: Type{T} , str:: AbstractString ) where T = parse (T, str)
138
+ pqparse (:: Type{T} , str:: AbstractString ) where {T} = parse (T, str)
139
139
140
- function pqparse (:: Type{T} , ptr:: Ptr{UInt8} ) where T<: Number
140
+ function pqparse (:: Type{T} , ptr:: Ptr{UInt8} ) where { T<: Number }
141
141
return ntoh (unsafe_load (Ptr {T} (ptr)))
142
142
end
143
143
@@ -147,7 +147,7 @@ pqparse(::Type{Symbol}, str::AbstractString) = Symbol(str)
147
147
function generate_binary_parser (symbol)
148
148
@eval function Base. parse (
149
149
:: Type{T} , pqv:: PQBinaryValue{$(oid(symbol))}
150
- ) where T<: Number
150
+ ) where { T<: Number }
151
151
return convert (T, pqparse ($ (_DEFAULT_TYPE_MAP[symbol]), data_pointer (pqv)))
152
152
end
153
153
end
@@ -295,19 +295,27 @@ end
295
295
296
296
_DEFAULT_TYPE_MAP[:time ] = Time
297
297
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"
300
299
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))
304
313
end
305
314
end
306
- return isnothing (result) ? parse (Time, _trunc_seconds (str)) : result
307
315
end
308
316
309
317
# 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 }
311
319
if str == " infinity"
312
320
return InfExtendedTime {T} (∞)
313
321
elseif str == " -infinity"
372
380
373
381
function pqparse (
374
382
:: Type{InfExtendedTime{T}} , ptr:: Ptr{UInt8}
375
- ) where T<: Dates.AbstractDateTime
383
+ ) where { T<: Dates.AbstractDateTime }
376
384
microseconds = ntoh (unsafe_load (Ptr {Int64} (ptr)))
377
385
if microseconds == typemax (Int64)
378
386
return InfExtendedTime {T} (∞)
@@ -383,7 +391,7 @@ function pqparse(
383
391
return InfExtendedTime {T} (pqparse (T, ptr))
384
392
end
385
393
386
- function pqparse (:: Type{InfExtendedTime{T}} , ptr:: Ptr{UInt8} ) where T<: Date
394
+ function pqparse (:: Type{InfExtendedTime{T}} , ptr:: Ptr{UInt8} ) where { T<: Date }
387
395
microseconds = ntoh (unsafe_load (Ptr {Int32} (ptr)))
388
396
if microseconds == typemax (Int32)
389
397
return InfExtendedTime {T} (∞)
397
405
function generate_binary_date_parser (symbol)
398
406
@eval function Base. parse (
399
407
:: Type{T} , pqv:: PQBinaryValue{$(oid(symbol))}
400
- ) where T<: TimeType
408
+ ) where { T<: TimeType }
401
409
return pqparse (T, data_pointer (pqv))
402
410
end
403
411
end
@@ -533,25 +541,25 @@ _DEFAULT_TYPE_MAP[:tsrange] = Interval{DateTime}
533
541
_DEFAULT_TYPE_MAP[:tstzrange ] = Interval{ZonedDateTime}
534
542
_DEFAULT_TYPE_MAP[:daterange ] = Interval{Date}
535
543
536
- function pqparse (:: Type{Interval{T}} , str:: AbstractString ) where T
544
+ function pqparse (:: Type{Interval{T}} , str:: AbstractString ) where {T}
537
545
str == " empty" && return Interval {T} ()
538
546
return parse (Interval{T}, str; element_parser= pqparse)
539
547
end
540
548
541
549
# How to parse range binary fetch is shown here
542
550
# https://github.com/postgres/postgres/blob/31079a4a8e66e56e48bad94d380fa6224e9ffa0d/src/backend/utils/adt/rangetypes.c#L162
543
- const RANGE_EMPTY = 0b00000001
551
+ const RANGE_EMPTY = 0b00000001
544
552
const RANGE_LOWER_BOUND_INCLUSIVE = 0b00000010
545
553
const RANGE_UPPER_BOUND_INCLUSIVE = 0b00000100
546
554
const RANGE_LOWER_BOUND_INFINITIY = 0b00001000
547
555
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
550
558
551
559
function generate_range_binary_parser (symbol)
552
560
@eval function Base. parse (
553
561
:: Type{Interval{T}} , pqv:: PQBinaryValue{$(oid(symbol))}
554
- ) where T
562
+ ) where {T}
555
563
current_pointer = data_pointer (pqv)
556
564
flags = ntoh (unsafe_load (Ptr {UInt8} (current_pointer)))
557
565
current_pointer += sizeof (UInt8)
@@ -590,13 +598,13 @@ foreach(
590
598
591
599
# # arrays
592
600
# 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)
594
602
595
- function parse_numeric_element (:: Type{Union{T,Missing}} , str) where T
603
+ function parse_numeric_element (:: Type{Union{T,Missing}} , str) where {T}
596
604
return str == " NULL" ? missing : parse (T, str)
597
605
end
598
606
599
- function parse_numeric_array (eltype:: Type{T} , str:: AbstractString ) where T
607
+ function parse_numeric_array (eltype:: Type{T} , str:: AbstractString ) where {T}
600
608
eq_ind = findfirst (isequal (' =' ), str)
601
609
602
610
if eq_ind != = nothing
@@ -670,7 +678,7 @@ for pq_eltype in ("int2", "int4", "int8", "float4", "float8", "oid", "numeric")
670
678
for jl_eltype in (jl_type, jl_missingtype)
671
679
@eval function pqparse (
672
680
:: Type{A} , str:: AbstractString
673
- ) where A<: AbstractArray{$jl_eltype}
681
+ ) where { A<: AbstractArray{$jl_eltype} }
674
682
return parse_numeric_array ($ jl_eltype, str):: A
675
683
end
676
684
end
0 commit comments