Skip to content

Commit b1620f8

Browse files
wookayquinnj
authored andcommitted
avoid NamedTuple{names, types}, fix #130 (#131)
* avoid NamedTuple{names, types}, fix #130 * fix 130 for julia nightly * added test for issue 130
1 parent 70472ff commit b1620f8

File tree

3 files changed

+17
-12
lines changed

3 files changed

+17
-12
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ os:
99

1010
julia:
1111
- 1.1
12+
- 1.2
1213
- nightly
1314

1415
notifications:

src/types.jl

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ function Query(conn::Connection, sql::String; streaming::Bool=false, kwargs...)
8484
result = MySQL.Result(MySQL.API.mysql_use_result(conn.ptr))
8585
else
8686
resulttype = :default
87-
result = result = MySQL.Result(MySQL.API.mysql_store_result(conn.ptr))
87+
result = MySQL.Result(MySQL.API.mysql_store_result(conn.ptr))
8888
end
8989

9090
if result.ptr != C_NULL
@@ -115,7 +115,7 @@ Tables.rows(q::Query) = q
115115
Tables.schema(q::Query{hasresult, names, T}) where {hasresult, names, T} = Tables.Schema(names, T)
116116

117117
Base.length(q::Query) = q.ptr == C_NULL ? 0 : q.nrows
118-
Base.eltype(q::Query{hasresult, names, types}) where {hasresult, names, types} = NamedTuple{names, types}
118+
Base.eltype(q::Query{hasresult, names, types}) where {hasresult, names, types} = NamedTuple{names}
119119

120120
cast(str, ::Type{Union{Missing, T}}) where {T} = cast(str, T)
121121
cast(str, ::Type{API.Bit}) = API.Bit(isempty(str) ? 0 : UInt64(str[1]))
@@ -131,19 +131,10 @@ function getvalue(ptr, col, ::Type{T}) where {T}
131131
return deref == C_NULL ? missing : cast(unsafe_string(deref), T)
132132
end
133133

134-
function generate_namedtuple(::Type{NamedTuple{names, types}}, q) where {names, types}
135-
if @generated
136-
vals = Tuple(:(getvalue(q.ptr, $i, $(fieldtype(types, i)))) for i = 1:fieldcount(types))
137-
return :(NamedTuple{names, types}(($(vals...),)))
138-
else
139-
return NamedTuple{names, types}(Tuple(getvalue(q.ptr, i, fieldtype(types, i)) for i = 1:fieldcount(types)))
140-
end
141-
end
142-
143134
function Base.iterate(q::Query{resulttype, names, types}, st=1) where {resulttype, names, types}
144135
st == 1 && resulttype == :none && return (num_rows_affected=Int(q.result.ptr),), 2
145136
q.ptr == C_NULL && return nothing
146-
nt = generate_namedtuple(NamedTuple{names, types}, q)
137+
nt = NamedTuple{names}([getvalue(q.ptr, i, fieldtype(types, i)) for i in 1:fieldcount(types)])
147138
q.ptr = API.mysql_fetch_row(q.result.ptr)
148139
return nt, st + 1
149140
end

test/runtests.jl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,3 +122,16 @@ res = MySQL.Query(conn, """select * from test;""") |> columntable
122122

123123
@test res.a[1] == "test"
124124
@test res.b[1] == 123
125+
126+
# issue #130
127+
MySQL.execute!(conn, """
128+
CREATE TABLE issue130 (
129+
f1 Int, f2 Int, f3 Int, f4 Int, f5 Int, f6 Int, f7 Int, f8 Int, f9 Int, f10 Int,
130+
f11 Int, f12 Int, f13 Int, f14 Int, f15 Int, f16 Int, f17 Int, f18 Int, f19 Int, f20 Int,
131+
f21 Int, f22 Int, f23 Int, f24 Int, f25 Int, f26 Int, f27 Int, f28 Int, f29 Int, f30 Int,
132+
PRIMARY KEY (f1)
133+
);""")
134+
res = MySQL.Query(conn, "select count(*) from issue130;") |> columntable
135+
@test res[1][1] == 0
136+
res = MySQL.Query(conn, "select * from issue130;") |> columntable
137+
@test isempty(res[1])

0 commit comments

Comments
 (0)