Skip to content

Commit b0fea57

Browse files
committed
Cache column names to avoid having to escape them
1 parent d6d5c38 commit b0fea57

File tree

2 files changed

+28
-7
lines changed

2 files changed

+28
-7
lines changed

src/results.jl

+10-7
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ mutable struct Result
1818
"Conversions from PostgreSQL data to Julia types for each column in the result"
1919
column_funcs::Vector{Base.Callable}
2020

21+
"Name of each column in the result"
22+
column_names::Vector{String}
23+
2124
# TODO: attach encoding per https://wiki.postgresql.org/wiki/Driver_development#Result_object_and_client_encoding
2225
function Result(
2326
result::Ptr{libpq_c.PGresult},
@@ -64,6 +67,10 @@ mutable struct Result
6467
func_lookup[(oid, typ)]
6568
end)
6669

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+
6774
# figure out which columns the user says may contain nulls
6875
if not_null isa Bool
6976
jl_result.not_null = fill(not_null, size(col_types))
@@ -423,27 +430,23 @@ end
423430
Return the name of the column at index `column_number` (1-based).
424431
"""
425432
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]
428434
end
429435

430436
"""
431437
column_names(jl_result::Result) -> Vector{String}
432438
433439
Return the names of all the columns in the query result.
434440
"""
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)
438442

439443
"""
440444
column_number(jl_result::Result, column_name::Union{AbstractString, Symbol}) -> Int
441445
442446
Return the index (1-based) of the column named `column_name`.
443447
"""
444448
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)
447450
end
448451

449452
"""

test/runtests.jl

+18
Original file line numberDiff line numberDiff line change
@@ -843,6 +843,24 @@ end
843843
close(conn)
844844
end
845845

846+
@testset "Uppercase Columns" begin
847+
conn = LibPQ.Connection("dbname=postgres user=$DATABASE_USER"; throw_error=true)
848+
849+
result = execute(conn, "SELECT 1 AS \"Column\";")
850+
@test num_columns(result) == 1
851+
@test LibPQ.column_name(result, 1) == "Column"
852+
@test LibPQ.column_number(result, :Column) == 1
853+
854+
table = Tables.columntable(result)
855+
@test hasproperty(table, :Column)
856+
857+
table = Tables.rowtable(result)
858+
@test hasproperty(table[1], :Column)
859+
860+
close(result)
861+
close(conn)
862+
end
863+
846864
@testset "PQResultError" begin
847865
conn = LibPQ.Connection("dbname=postgres user=$DATABASE_USER"; throw_error=true)
848866

0 commit comments

Comments
 (0)