Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch from DataStreams to Tables #46

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/JDBC.jl
Original file line number Diff line number Diff line change
Expand Up @@ -640,7 +640,7 @@ export getTableMetaData, JDBCRowIterator


include("interface.jl")
include("datastreams.jl")
include("tables.jl")


end # module
39 changes: 16 additions & 23 deletions src/datastreams.jl → src/tables.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using DataStreams
using Tables

const column_types = Dict(
JDBC_COLTYPE_ARRAY=>Array,
Expand Down Expand Up @@ -57,37 +57,30 @@ colname(s::Source, col::Int) = getColumnName(s.md, col)
ncols(s::Source) = getColumnCount(s.md)

coltypes(s::Source) = Type[coltype(s, i) for i ∈ 1:ncols(s)]
colnames(s::Source) = String[colname(s, i) for i ∈ 1:ncols(s)]
colnames(s::Source) = Symbol[colname(s, i) for i ∈ 1:ncols(s)]

# WARNING: this does not seem to actually work
Data.reset!(s::Source) = beforeFirst!(s.rs)
Tables.istable(::Type{<:Source}) = true
Tables.rowaccess(::Type{<:Source}) = true
Tables.rows(s::Source) = s
Tables.schema(s::Source) = Tables.Schema(colnames(s), coltypes(s))

Data.isdone(s::Source, row::Int, col::Int) = isdone(s.rs)

Data.schema(s::Source) = Data.Schema(coltypes(s), colnames(s), missing)

Data.accesspattern(s::Source) = Data.Sequential

Data.streamtype(::Type{Source}, ::Type{Data.Field}) = true
Data.streamtype(::Type{Source}, ::Type{Data.Column}) = false
Base.IteratorSize(::Type{<:Source}) = Base.SizeUnknown()
Base.eltype(s::Source) = namedtupletype(Tables.schema(s))
namedtupletype(::Tables.Schema{names, types}) where {names, types} = NamedTuple{names, types}

# TODO currently jdbc_get_method is very inefficient
pullfield(s::Source, col::Int) = jdbc_get_method(getColumnType(s.md, col))(s.rs, col)

# does not store current row number as a persistent state
function Data.streamfrom(s::Source, ::Type{Data.Field}, ::Type{T}, row::Int, col::Int) where T
convert(T, pullfield(s, col))::T
end
function Data.streamfrom(s::Source, ::Type{Data.Field}, ::Type{Union{T, Missing}},
row::Int, col::Int) where T
o = pullfield(s, col)
if wasNull(s.rs)
return missing
end
convert(T, o)::T
jdbcconvert(::Type{T}, s, x) where {T} = convert(T, x)
jdbcconvert(::Type{Union{T, Missing}}, s, x) where {T} = wasNull(s.rs) ? missing : convert(T, x)

function Base.iterate(s::Source, NT::NamedTuple{names, types}=namedtupletype(s)) where {names, types}
isdone(s.rs) && return nothing
return NT(convert(fieldtype(types, i), s, pullfield(s, i)) for i = 1:fieldcount(types)), NT
end

load(::Type{T}, s::Source) where {T} = Data.close!(Data.stream!(s, T))
load(::Type{T}, s::Source) where {T} = Tables.materializer(T)(s)
load(::Type{T}, rs::JResultSet) where {T} = load(T, Source(rs))
load(::Type{T}, stmt::JStatement, query::AbstractString) where {T} = load(T, Source(stmt, query))
load(::Type{T}, csr::Union{JDBC.Cursor,JDBCRowIterator}) where {T} = load(T, Source(csr))
Expand Down