Skip to content
Closed
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
13 changes: 13 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,19 @@ OrderedCollections = "bac558e1-5e72-5ebc-8fee-abe8a469f55d"
PrettyPrinting = "54e16d92-306c-5ea0-a30b-337be88ac337"
Tables = "bd369af6-aec1-5ad0-b16a-f7cc5008161c"

[extras]
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
LazyArtifacts = "4af54fe1-eca0-43a8-85a7-787d91b784e3"
Logging = "56ddb016-857b-54e1-b83d-db4d58db5568"
NarrativeTest = "9563631e-acd4-5dd8-bebb-4cf36518960d"
Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"
SQLite = "0aa819cd-b072-5ff4-a722-6bc24af294d9"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[targets]
test = ["DataFrames", "Documenter", "LazyArtifacts", "Logging", "NarrativeTest", "Pkg", "SQLite", "Test"]

[compat]
DBInterface = "2.5"
DataAPI = "1.13"
Expand Down
51 changes: 34 additions & 17 deletions src/reflect.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,31 @@

const default_reflect_clause =
FROM(:c => (:information_schema, :columns)) |>
WHERE(FUN("=", (:c, :table_schema), VAR(:schema))) |>
ORDER((:c, :table_schema), (:c, :table_name), (:c, :ordinal_position)) |>
SELECT(:schema => (:c, :table_schema),
WHERE(FUN(:and, FUN("=", (:c, :table_catalog), VAR(:catalog)),
FUN("=", (:c, :table_schema), VAR(:schema))
)) |>
ORDER((:c, :table_catalog), (:c, :table_schema), (:c, :table_name), (:c, :ordinal_position)) |>
SELECT(:catalog => (:c, :table_catalog),
:schema => (:c, :table_schema),
:name => (:c, :table_name),
:column => (:c, :column_name))
const duckdb_reflect_clause =
FROM(:c => (:information_schema, :columns)) |>
WHERE(FUN(:and, FUN("=", (:c, :table_schema), FUN(:coalesce, VAR(:schema), "main")),
WHERE(FUN(:and, FUN("=", (:c, :table_catalog), FUN(:coalesce, VAR(:catalog), FUN(:current_catalog))),
FUN("=", (:c, :table_schema), FUN(:coalesce, VAR(:schema), FUN(:current_schema))),
FUN(:not_like, (:c, :table_name), "sqlite_%"),
FUN(:not_like, (:c, :table_name), "pragma_database_list"))) |>
ORDER((:c, :table_schema), (:c, :table_name), (:c, :ordinal_position)) |>
SELECT(:schema => (:c, :table_schema),
ORDER((:c, :table_catalog), (:c, :table_schema), (:c, :table_name), (:c, :ordinal_position)) |>
SELECT(:catalog => (:c, :table_catalog),
:schema => (:c, :table_schema),
:name => (:c, :table_name),
:column => (:c, :column_name))
const mysql_reflect_clause =
FROM(:c => (:information_schema, :columns)) |>
WHERE(FUN("=", (:c, :table_schema), FUN(:coalesce, VAR(:schema), FUN("DATABASE")))) |>
ORDER((:c, :table_schema), (:c, :table_name), (:c, :ordinal_position)) |>
SELECT(:schema => (:c, :table_schema),
SELECT(:catalog => missing,
:schema => (:c, :table_schema),
:name => (:c, :table_name),
:column => (:c, :column_name))
const postgresql_reflect_clause =
Expand All @@ -32,7 +38,8 @@ const postgresql_reflect_clause =
FUN(">", (:a, :attnum), 0),
FUN(:not, (:a, :attisdropped)))) |>
ORDER((:n, :nspname), (:c, :relname), (:a, :attnum)) |>
SELECT(:schema => (:n, :nspname),
SELECT(:catalog => missing,
:schema => (:n, :nspname),
:name => (:c, :relname),
:column => (:a, :attname))
const redshift_reflect_clause = postgresql_reflect_clause
Expand All @@ -42,7 +49,8 @@ const sqlite_reflect_clause =
WHERE(FUN(:and, FUN(:in, (:sm, :type), "table", "view"),
FUN(:not_like, (:sm, :name), "sqlite_%"))) |>
ORDER((:sm, :name), (:pti, :cid)) |>
SELECT(:schema => missing,
SELECT(:catalog => missing,
:schema => missing,
:name => (:sm, :name),
:column => (:pti, :name))
const sqlserver_reflect_clause =
Expand All @@ -52,7 +60,8 @@ const sqlserver_reflect_clause =
WHERE(FUN(:and, FUN("=", (:s, :name), FUN(:coalesce, VAR(:schema), "dbo")),
FUN(:in, (:o, :type), "U", "V"))) |>
ORDER((:s, :name), (:o, :name), (:c, :column_id)) |>
SELECT(:schema => (:s, :name),
SELECT(:catalog => missing,
:schema => (:s, :name),
:name => (:o, :name),
:column => (:c, :name))
const standard_reflect_clauses = [
Expand Down Expand Up @@ -92,10 +101,10 @@ Parameter `dialect` specifies the target [`SQLDialect`](@ref). If not set,
`dialect` will be inferred from the type of the connection object.

"""
function reflect(conn; schema = nothing, dialect = nothing, cache = default_cache_maxsize)
function reflect(conn; catalog = nothing, schema = nothing, dialect = nothing, cache = default_cache_maxsize)
dialect = dialect === nothing ? SQLDialect(typeof(conn)) : convert(SQLDialect, dialect)
sql = reflect_sql(dialect)
params = pack(sql, (; schema = something(schema, missing)))
params = pack(sql, (; catalog = something(catalog, missing), schema = something(schema, missing)))
stmt = DBInterface.prepare(conn, String(sql))
cr = DBInterface.execute(stmt, params)
SQLCatalog(tables = tables_from_column_list(Tables.rows(cr)),
Expand All @@ -106,22 +115,30 @@ end
function tables_from_column_list(rows)
tables = SQLTable[]
qualifiers = Symbol[]
schema = name = nothing
catalog = schema = name = nothing
columns = Symbol[]
for (s, n, c) in rows
for (g, s, n, c) in rows
g = g !== missing ? Symbol(g) : nothing
s = s !== missing ? Symbol(s) : nothing
n = Symbol(n)
c = Symbol(c)
if s === schema && n === name
if g === catalog && s === schema && n === name
push!(columns, c)
else
if !isempty(columns)
t = SQLTable(qualifiers = qualifiers, name = name, columns = columns)
push!(tables, t)
end
if s !== schema
qualifiers = s !== nothing ? [s] : Symbol[]
if s !== schema || g !== catalog
qualifiers = Symbol[]
if !isnothing(g)
push!(qualifiers, g)
end
if !isnothing(s)
push!(qualifiers, s)
end
end
catalog = g
schema = s
name = n
columns = [c]
Expand Down
12 changes: 0 additions & 12 deletions test/Project.toml

This file was deleted.

Loading