diff --git a/Project.toml b/Project.toml index 3bffa27..a75be60 100644 --- a/Project.toml +++ b/Project.toml @@ -1,6 +1,6 @@ name = "DBFTables" uuid = "75c7ada1-017a-5fb6-b8c7-2125ff2d6c93" -version = "0.2.2" +version = "0.2.3" [deps] Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7" diff --git a/README.md b/README.md index 8096ec5..75497de 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # DBFTables -[![CI](https://github.com/JuliaData/WeakRefStrings.jl/workflows/CI/badge.svg)](https://github.com/JuliaData/WeakRefStrings.jl/actions?query=workflow%3ACI) -[![codecov](https://codecov.io/gh/JuliaData/WeakRefStrings.jl/branch/master/graph/badge.svg)](https://codecov.io/gh/JuliaData/WeakRefStrings.jl) +[![CI](https://github.com/JuliaData/DBFTables.jl/workflows/CI/badge.svg)](https://github.com/JuliaData/DBFTables.jl/actions?query=workflow%3ACI) +[![codecov](https://codecov.io/gh/JuliaData/DBFTables.jl/branch/main/graph/badge.svg)](https://codecov.io/gh/JuliaData/DBFTables.jl) [![deps](https://juliahub.com/docs/DBFTables/deps.svg)](https://juliahub.com/ui/Packages/DBFTables/P7Qdk?t=2) [![version](https://juliahub.com/docs/DBFTables/version.svg)](https://juliahub.com/ui/Packages/DBFTables/P7Qdk) [![pkgeval](https://juliahub.com/docs/DBFTables/pkgeval.svg)](https://juliahub.com/ui/Packages/DBFTables/P7Qdk) diff --git a/src/DBFTables.jl b/src/DBFTables.jl index e28c363..b13afd5 100644 --- a/src/DBFTables.jl +++ b/src/DBFTables.jl @@ -33,7 +33,7 @@ struct Table end "Struct representing a single row or record of the DBF Table" -struct Row +struct Row <: Tables.AbstractRow table::Table row::Int end @@ -172,6 +172,8 @@ gettable(row::Row) = getfield(row, :table) Base.length(dbf::Table) = Int(getheader(dbf).records) Base.size(dbf::Table) = (length(dbf), length(getfields(dbf))) Base.size(dbf::Table, i) = size(dbf)[i] +Base.size(row::Row) = (length(row),) +Base.size(row::Row, i) = i == 1 ? length(row) : throw(BoundsError(row, i)) """ DBFTables.Table(source) => DBFTables.Table @@ -232,8 +234,7 @@ function Base.show(io::IO, row::Row) end function Base.show(io::IO, dbf::Table) - nr = length(dbf) - nc = length(getfields(dbf)) + nr, nc = size(dbf) println(io, "DBFTables.Table with $nr rows and $nc columns") println(io, Tables.schema(dbf)) end @@ -262,7 +263,7 @@ function Base.iterate(dbf::Table, st = 1) return Row(dbf, st), st + 1 end -function Base.getproperty(row::Row, name::Symbol) +function Tables.getcolumn(row::Row, name::Symbol) dbf = gettable(row) header = getheader(dbf) str = getstrings(dbf) @@ -273,6 +274,14 @@ function Base.getproperty(row::Row, name::Symbol) return @inbounds dbf_value(type, str[colidx, rowidx]) end +function Tables.getcolumn(row::Row, i::Int) + dbf = gettable(row) + str = getstrings(dbf) + type = getfields(dbf)[i].type + rowidx = getrow(row) + return @inbounds dbf_value(type, str[i, rowidx]) +end + Tables.istable(::Type{Table}) = true Tables.rowaccess(::Type{Table}) = true Tables.columnaccess(::Type{Table}) = true @@ -289,7 +298,7 @@ end "List all available DBF column names" Base.propertynames(dbf::Table) = getfield.(getfield(dbf, :header).fields, :name) -Base.propertynames(row::Row) = propertynames(gettable(row)) +Tables.columnnames(row::Row) = propertynames(gettable(row)) "Create a copy of an entire DBF column as a Vector. Usage: `dbf.myfield`" function Base.getproperty(dbf::Table, name::Symbol) diff --git a/test/runtests.jl b/test/runtests.jl index 52a698e..d2b8c4a 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -46,7 +46,9 @@ row, st = iterate(dbf) @testset "iterate" begin @test st === 2 + @test haskey(row, :CHAR) @test row.CHAR === "Bob" + @test row[2] === "19900102" @test_throws ArgumentError row.nonexistent_field firstrow = ( CHAR = "Bob", @@ -58,9 +60,15 @@ row, st = iterate(dbf) ) @test NamedTuple(row) === firstrow @test row isa DBFTables.Row + @test row isa Tables.AbstractRow + @test length(row) === 6 + @test size(row) === (6,) + @test size(row, 1) === 6 + @test_throws BoundsError size(row, 2) @test DBFTables.getrow(row) === 1 @test DBFTables.gettable(row) === dbf @test sum(1 for row in dbf) === 7 + @test sum(1 for cell in row) === 6 @test propertynames(dbf) == [:CHAR, :DATE, :BOOL, :FLOAT, :NUMERIC, :INTEGER] @test propertynames(row) == [:CHAR, :DATE, :BOOL, :FLOAT, :NUMERIC, :INTEGER] end