diff --git a/Project.toml b/Project.toml index 89ef4d80..880ab127 100644 --- a/Project.toml +++ b/Project.toml @@ -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" diff --git a/src/reflect.jl b/src/reflect.jl index d90a7570..8aa30477 100644 --- a/src/reflect.jl +++ b/src/reflect.jl @@ -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 = @@ -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 @@ -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 = @@ -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 = [ @@ -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)), @@ -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] diff --git a/test/Project.toml b/test/Project.toml deleted file mode 100644 index d8de25a1..00000000 --- a/test/Project.toml +++ /dev/null @@ -1,12 +0,0 @@ -[deps] -DataAPI = "9a962f9c-6df0-11e9-0e5d-c546b8b5ee8a" -DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0" -Dates = "ade2ca70-3891-5945-98fb-dc099432e06a" -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" -Tables = "bd369af6-aec1-5ad0-b16a-f7cc5008161c" -Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"