@@ -18,6 +18,9 @@ mutable struct Result
18
18
" Conversions from PostgreSQL data to Julia types for each column in the result"
19
19
column_funcs:: Vector{Base.Callable}
20
20
21
+ " Name of each column in the result"
22
+ column_names:: Vector{String}
23
+
21
24
# TODO : attach encoding per https://wiki.postgresql.org/wiki/Driver_development#Result_object_and_client_encoding
22
25
function Result (
23
26
result:: Ptr{libpq_c.PGresult} ,
@@ -64,6 +67,10 @@ mutable struct Result
64
67
func_lookup[(oid, typ)]
65
68
end )
66
69
70
+ jl_result. column_names = map (1 : num_columns (jl_result)) do col_num
71
+ unsafe_string (libpq_c. PQfname (jl_result. result, col_num - 1 ))
72
+ end
73
+
67
74
# figure out which columns the user says may contain nulls
68
75
if not_null isa Bool
69
76
jl_result. not_null = fill (not_null, size (col_types))
@@ -423,27 +430,23 @@ end
423
430
Return the name of the column at index `column_number` (1-based).
424
431
"""
425
432
function column_name (jl_result:: Result , column_number:: Integer )
426
- # todo: check cleared?
427
- unsafe_string (libpq_c. PQfname (jl_result. result, column_number - 1 ))
433
+ return jl_result. column_names[column_number]
428
434
end
429
435
430
436
"""
431
437
column_names(jl_result::Result) -> Vector{String}
432
438
433
439
Return the names of all the columns in the query result.
434
440
"""
435
- function column_names (jl_result:: Result )
436
- return [column_name (jl_result, i) for i in 1 : num_columns (jl_result)]
437
- end
441
+ column_names (jl_result:: Result ) = copy (jl_result. column_names)
438
442
439
443
"""
440
444
column_number(jl_result::Result, column_name::Union{AbstractString, Symbol}) -> Int
441
445
442
446
Return the index (1-based) of the column named `column_name`.
443
447
"""
444
448
function column_number (jl_result:: Result , column_name:: Union{AbstractString, Symbol} ):: Int
445
- # todo: check cleared?
446
- return libpq_c. PQfnumber (jl_result. result, String (column_name)) + 1
449
+ return something (findfirst (isequal (String (column_name)), jl_result. column_names), 0 )
447
450
end
448
451
449
452
"""
0 commit comments