Skip to content

Commit d99fdfb

Browse files
Add DBTables type (#269)
* Add DBTables type / code * Fix tests so they pass * Improve test coverage * Update src/tables.jl Co-authored-by: Jacob Quinn <[email protected]> * Update src/tables.jl Co-authored-by: Jacob Quinn <[email protected]> * Update src/tables.jl Co-authored-by: Jacob Quinn <[email protected]> Co-authored-by: Jeremiah Lewis <--get> Co-authored-by: Jacob Quinn <[email protected]>
1 parent f904f27 commit d99fdfb

File tree

3 files changed

+51
-6
lines changed

3 files changed

+51
-6
lines changed

src/SQLite.jl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -678,7 +678,10 @@ include("tables.jl")
678678
679679
returns a list of tables in `db`
680680
"""
681-
tables(db::DB, sink=columntable) = DBInterface.execute(sink, db, "SELECT name FROM sqlite_master WHERE type='table';")
681+
function tables(db::DB, sink=columntable)
682+
tblnames = DBInterface.execute(sink, db, "SELECT name FROM sqlite_master WHERE type='table';")
683+
return [DBTable(tbl, Tables.schema(DBInterface.execute(db,"SELECT * FROM $(tbl) LIMIT 0"))) for tbl in tblnames.name]
684+
end
682685

683686
"""
684687
SQLite.indices(db, sink=columntable)

src/tables.jl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,19 @@ getquery(r::Row) = getfield(r, :q)
2424
Tables.isrowtable(::Type{Query}) = true
2525
Tables.columnnames(q::Query) = q.names
2626

27+
struct DBTable
28+
name::String
29+
schema::Union{Tables.Schema, Nothing}
30+
end
31+
32+
DBTable(name::String) = DBTable(name, nothing)
33+
34+
const DBTables = AbstractVector{DBTable}
35+
36+
Tables.istable(::Type{<:DBTables}) = true
37+
Tables.rowaccess(::Type{<:DBTables}) = true
38+
Tables.rows(dbtbl::DBTables) = dbtbl
39+
2740
function Tables.schema(q::Query)
2841
if isempty(q)
2942
# when the query is empty, return the types provided by SQLite

test/runtests.jl

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,35 @@ end
7878
DBInterface.close!(con)
7979
end
8080

81+
@testset "SQLite.tables(db)" begin
82+
setup_clean_test_db() do db
83+
84+
results1 = SQLite.tables(db)
85+
86+
@test isa(results1, SQLite.DBTables)
87+
@test length(results1) == 11
88+
@test isa(results1[1], SQLite.DBTable)
89+
90+
@test Tables.istable(results1)
91+
@test Tables.rowaccess(results1)
92+
@test Tables.rows(results1) == results1
93+
94+
@test results1[1].name == "Album"
95+
@test results1[1].schema == Tables.schema(DBInterface.execute(db,"SELECT * FROM Album LIMIT 0"))
96+
97+
@test SQLite.DBTable("Album") == SQLite.DBTable("Album", nothing)
98+
99+
@test [t.name for t in results1] == ["Album", "Artist", "Customer", "Employee", "Genre", "Invoice", "InvoiceLine", "MediaType", "Playlist", "PlaylistTrack", "Track"]
100+
end
101+
102+
@testset "#209: SQLite.tables response when no tables in DB" begin
103+
db = SQLite.DB()
104+
tables_v = SQLite.tables(db)
105+
@test String[] == [t.name for t in tables_v]
106+
DBInterface.close!(db)
107+
end
108+
end
109+
81110
@testset "Issue #207: 32 bit integers" begin
82111
setup_clean_test_db() do db
83112
ds =
@@ -117,7 +146,7 @@ end
117146
@test length(ds.name) == 11
118147

119148
results1 = SQLite.tables(db)
120-
@test isequal(ds, results1)
149+
@test isequal(ds.name, [t.name for t in results1])
121150
end
122151
end
123152

@@ -223,7 +252,7 @@ end
223252

224253
r = DBInterface.execute(db, "select * from temp limit 10") |> columntable
225254
@test length(r) == 4 && length(r[1]) == 10
226-
@test typeof(r[4][1]) == Date
255+
@test isa(r[4][1], Date)
227256
@test all(Bool[x == Date(2014, 1, 1) for x in r[4]])
228257
DBInterface.execute(db, "drop table temp")
229258

@@ -233,7 +262,7 @@ end
233262
r = DBInterface.execute(db, "select * from $tablename") |> columntable
234263
@test length(r) == 2 && length(r[1]) == 5
235264
@test all([i for i in r[1]] .== collect(rng))
236-
@test all([typeof(i) for i in r[1]] .== Dates.Date)
265+
@test all([isa(i, Dates.Date) for i in r[1]])
237266
SQLite.drop!(db, "$tablename")
238267
end
239268
end
@@ -411,11 +440,11 @@ end
411440
SQLite.drop!(db2, "nonexistant", ifexists = true)
412441
# should drop "tab2"
413442
SQLite.drop!(db2, "tab2", ifexists = true)
414-
@test !in("tab2", SQLite.tables(db2)[1])
443+
@test filter(x -> x.name == "tab2", SQLite.tables(db2)) |> length == 0
415444

416445
SQLite.drop!(db, "sqlite_stat1", ifexists = true)
417446
tables = SQLite.tables(db)
418-
@test length(tables[1]) == 11
447+
@test length(tables) == 11
419448
end
420449
end
421450

0 commit comments

Comments
 (0)