1
"""
2
    get_table_names(source)::Tuple{Symbol}
3

4
Get the names of the tables in `source`.
5

6
```jldoctest
7
julia> using QuerySQLite
8

9
julia> database = Database(joinpath(pathof(QuerySQLite) |> dirname |> dirname, "test", "Chinook_Sqlite.sqlite"));
10

11
julia> get_table_names(getfield(database, :source))
12
(:Album, :Artist, :Customer, :Employee, :Genre, :Invoice, :InvoiceLine, :MediaType, :Playlist, :PlaylistTrack, :Track)
13
```
14
"""
15
function get_table_names(source::DB)
16 1
    as_symbols(tables(source).name)
17
end
18
export get_table_names
19

20
"""
21
    get_column_names(source, table_name)::Tuple{Symbol}
22

23
Get the names of the columns in `table_name` in `source`.
24

25
```jldoctest example
26
julia> using QuerySQLite
27

28
julia> database = Database(joinpath(pathof(QuerySQLite) |> dirname |> dirname, "test", "Chinook_Sqlite.sqlite"));
29

30
julia> get_column_names(getfield(database, :source), :Album)
31
(:AlbumId, :Title, :ArtistId)
32
```
33
"""
34
function get_column_names(source::DB, table_name)
35 1
    as_symbols(columns(source, String(table_name)).name)
36
end
37
export get_column_names
38

39
"""
40
    struct Database{Source}
41

42
A wrapper for an external database. `source` need only support
43
[`get_table_names`](@ref) and [`get_column_names`](@ref).
44
"""
45
struct Database{Source}
46 1
    source::Source
47
end
48

49
"""
50
    Database(filename::AbstractString)
51

52
Guess the database software from the filename.
53

54

55
```jldoctest
56
julia> using QuerySQLite
57

58
julia> database = Database(joinpath(pathof(QuerySQLite) |> dirname |> dirname, "test", "Chinook_Sqlite.sqlite"));
59

60
julia> database.Track
61
?x9 SQLite query result
62
TrackId │ Name                                      │ AlbumId │ MediaTypeId
63
────────┼───────────────────────────────────────────┼─────────┼────────────
64
1       │ "For Those About To Rock (We Salute You)" │ 1       │ 1
65
2       │ "Balls to the Wall"                       │ 2       │ 2
66
3       │ "Fast As a Shark"                         │ 3       │ 2
67
4       │ "Restless and Wild"                       │ 3       │ 2
68
5       │ "Princess of the Dawn"                    │ 3       │ 2
69
6       │ "Put The Finger On You"                   │ 1       │ 1
70
7       │ "Let's Get It Up"                         │ 1       │ 1
71
8       │ "Inject The Venom"                        │ 1       │ 1
72
9       │ "Snowballed"                              │ 1       │ 1
73
10      │ "Evil Walks"                              │ 1       │ 1
74
... with more rows, and 5 more columns: GenreId, Composer, Milliseconds, Bytes, UnitPrice
75
```
76
"""
77
function Database(filename::AbstractString)
78 1
    if endswith(filename, ".sqlite")
79 1
        Database(SQLite.DB(filename))
80
    else
81 1
        throw(ArgumentError("Unsupported database type for $filename"))
82
    end
83
end
84
export Database
85

86 1
get_source(source_tables::Database) = getfield(source_tables, :source)
87

88
# getproperty overloading allows direct access to each table in the database
89
function getproperty(source_tables::Database, table_name::Symbol)
90 1
    SourceCode(get_source(source_tables),
91
        Expr(:call, getproperty, source_tables, table_name)
92
    )
93
end

Read our documentation on viewing source code .

Loading