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 #51

Merged
merged 5 commits into from
Oct 8, 2019
Merged
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
28 changes: 22 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,28 @@

sudo: required

language: julia

services:
- docker

os:
- osx
- linux

julia:
- 0.7
- 1.0
- 1.0
- 1.3
- nightly

matrix:
allow_failures:
- julia: nightly

notifications:
email: false
# uncomment the following lines to override the default test script
#script:
# - if [[ -a .git/shallow ]]; then git fetch --unshallow; fi
# - julia --check-bounds=yes -e 'Pkg.clone(pwd()); Pkg.build("JDBC"); Pkg.test("JDBC"; coverage=true)'

after_success:
- julia -e 'import Pkg, JDBC; cd(joinpath(dirname(pathof(JDBC)),"..")); Pkg.add("Coverage"); using Coverage; Codecov.submit(process_folder())'
- julia -e 'import Pkg; Pkg.add("Documenter")'
- julia -e 'import JDBC; cd(joinpath(dirname(pathof(JDBC)),"..")); include(joinpath("docs", "make.jl"))'
5 changes: 2 additions & 3 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
name = "JDBC"
uuid = "6042db11-3c3d-5e84-8dba-9cbf74c9ba48"
version = "0.4.1"
version = "0.4.2"

[deps]
DataStreams = "9a8bc11e-79be-5b39-94d7-1ccc349a1a85"
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
JavaCall = "494afd89-becb-516b-aafa-70d2670c0337"
Tables = "bd369af6-aec1-5ad0-b16a-f7cc5008161c"

[compat]
DataFrames = "< 0.18.0"
JavaCall = "≥ 0.7.0"
julia = "≥ 0.7.0"

Expand Down
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
This package enables the use of Java JDBC drivers to access databases from within Julia. It uses the [JavaCall.jl](https://github.com/aviks/JavaCall.jl) package to call into Java in order to use the JDBC drivers.

The API provided by this package consists essentially of two components: a "direct" (i.e. minimally wrapped) interface directly to Java JDBC and a minimal
Julian interface with support for [DataStreams.jl](https://github.com/JuliaData/DataStreams.jl).
Julian interface with support for [Tables.jl](https://github.com/JuliaData/Tables.jl).

This package currently supports only Julia v0.6 and later.

Expand Down Expand Up @@ -121,13 +121,13 @@ end
close(csr) # closes Connection, can be called on Connection or Cursor
```

#### `DataStreams` Interface and Creating `DataFrame`s
#### `Tables` Interface and Creating `DataFrame`s

JDBC includes a [DataStreams](https://github.com/JuliaData/DataStreams.jl) interface. A DataStreams `Source` object can be created from a `JDBC.Cursor` or a
`JDBCRowIterator` simply by doing e.g. `JDBC.Source(csr)`. This object implements the DataStreams `Data.Source` interface. It can be useful for retrieving metadata
with `Data.schema`.
JDBC includes a [Tables](https://github.com/JuliaData/Tables.jl) interface. A Tables
`Source` object can be created from a `JDBC.Cursor` or a `JDBCRowIterator` simply by doing
e.g. `JDBC.Source(csr)`. It can be useful for retrieving metadata with `Tables.schema`.

This is also useful for loading data from a database into an object that implements the DataStreams `Data.Sink` interface such as a `DataFrame`. For this we
This is also useful for loading data from a database into another object that implements the Tables interface. For this we
provide also the convenient `JDBC.load` function.

For example, you can do
Expand Down
3 changes: 0 additions & 3 deletions REQUIRE

This file was deleted.

7 changes: 3 additions & 4 deletions appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
environment:
matrix:
- julia_version: 0.7
- julia_version: 1
- julia_version: 1.0
- julia_version: 1.3
- julia_version: nightly

platform:
Expand All @@ -12,8 +12,7 @@ platform:
# # (tests will run but not make your overall status red)
matrix:
allow_failures:
- julia_version: nightly
- platform: x86
- julia_version: nightly

branches:
only:
Expand Down
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
43 changes: 18 additions & 25 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,41 +57,34 @@ 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[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}
namedtupletype(s::Source) = namedtupletype(Tables.schema(s))

# 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::Type{NamedTuple{names, types}}=namedtupletype(s)) where {names, types}
isdone(s.rs) && return nothing
NT(jdbcconvert(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} = 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))
function load(::Type{T}, csr::Cursor, q::AbstractString) where T
function load(::Type{T}, csr::Cursor, q::AbstractString) where {T}
execute!(csr, q)
load(T, csr)
end
Expand Down
1 change: 0 additions & 1 deletion test/REQUIRE

This file was deleted.

1 change: 0 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using JavaCall
using JDBC
using DataFrames
using DataStreams
using Test
using Dates
import Pkg
Expand Down