Skip to content
Open
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
12 changes: 1 addition & 11 deletions R/db-helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ find_name_clashes <- function(old, new) {
}

#' @autoglobal
get_src_tbl_names <- function(src, schema = NULL, dbname = NULL, names = NULL) {
get_src_tbl_names <- function(src, schema = NULL, dbname = NULL, names_pattern = "{.table}") {
if (!is_mssql(src) && !is_postgres(src) && !is_redshift(src) && !is_mariadb(src)) {
warn_if_arg_not(schema, only_on = c("MSSQL", "Postgres", "Redshift", "MariaDB"))
warn_if_arg_not(dbname, only_on = "MSSQL")
Expand Down Expand Up @@ -166,16 +166,6 @@ get_src_tbl_names <- function(src, schema = NULL, dbname = NULL, names = NULL) {
names_table <- get_names_table_mariadb(con)
}

# Use smart default for `.names`, if it wasn't provided
if (!is.null(names)) {
names_pattern <- names
} else if (length(schema) == 1) {
names_pattern <- "{.table}"
} else {
names_pattern <- "{.schema}.{.table}"
cli::cli_inform('Using {.code .names = "{names_pattern}"}')
}

names_table <- names_table %>%
filter(schema_name %in% !!(if (inherits(schema, "sql")) glue_sql_collapse(schema) else schema)) %>%
collect() %>%
Expand Down
15 changes: 13 additions & 2 deletions R/dm_from_con.R
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,22 @@ dm_from_con <- function(

src <- src_from_src_or_con(con)

# Use smart default for `.names`, if it wasn't provided
dots <- list2(...)
if (!is.null(.names)) {
names_pattern <- .names
} else if (is.null(dots$schema) || length(dots$schema) == 1) {
names_pattern <- "{.table}"
} else {
names_pattern <- "{.schema}.{.table}"
cli::cli_inform('Using {.code .names = "{names_pattern}"}')
}

if (is.null(learn_keys) || isTRUE(learn_keys)) {
# FIXME: Try to make it work everywhere
tryCatch(
{
dm_learned <- dm_learn_from_db(con, ...)
dm_learned <- dm_learn_from_db(con, ..., names_pattern = names_pattern)
if (is_null(learn_keys)) {
inform(c(
"Keys queried successfully.",
Expand Down Expand Up @@ -105,7 +116,7 @@ dm_from_con <- function(
}

if (is_null(table_names)) {
src_tbl_names <- get_src_tbl_names(src, ..., names = .names)
src_tbl_names <- get_src_tbl_names(src, ..., names_pattern = names_pattern)
} else {
src_tbl_names <- table_names
if (is.null(names(src_tbl_names))) {
Expand Down
6 changes: 3 additions & 3 deletions R/learn.R
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
#' iris_dm_learned <- dm_learn_from_db(src_sqlite)
#' }
#' @autoglobal
dm_learn_from_db <- function(dest, dbname = NA, schema = NULL, name_format = "{table}") {
dm_learn_from_db <- function(dest, dbname = NA, schema = NULL, names_pattern = "{.table}") {
Copy link

Copilot AI Aug 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function documentation needs to be updated to reflect the parameter name change from name_format to names_pattern. The @param documentation likely still references the old parameter name.

Copilot uses AI. Check for mistakes.
# assuming that we will not try to learn from (globally) temporary tables, which do not appear in sys.table
con <- con_from_src_or_con(dest)
src <- src_from_src_or_con(dest)
Expand All @@ -51,8 +51,8 @@ dm_learn_from_db <- function(dest, dbname = NA, schema = NULL, name_format = "{t

dm_name <-
df_info$tables %>%
select(catalog = table_catalog, schema = table_schema, table = table_name) %>%
mutate(name = glue(!!name_format)) %>%
select(catalog = table_catalog, .schema = table_schema, .table = table_name) %>%
mutate(name = glue(!!names_pattern)) %>%
pull() %>%
unclass() %>%
vec_as_names(repair = "unique")
Expand Down
16 changes: 8 additions & 8 deletions tests/testthat/test-db-helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,15 @@ test_that("DB helpers work for MSSQL", {
DBI::Id(catalog = "db_helpers_db", schema = "schema_db_helpers_2", table = "test_db_helpers_4")
)
expect_identical(
get_src_tbl_names(my_test_src(), schema = c("dbo", "schema_db_helpers"))[["dbo.test_db_helpers_2"]],
get_src_tbl_names(my_test_src(), schema = c("dbo", "schema_db_helpers"), names_pattern = "{.schema}.{.table}")[["dbo.test_db_helpers_2"]],
DBI::Id(schema = "dbo", table = "test_db_helpers_2")
)
expect_identical(
get_src_tbl_names(my_test_src(), schema = c("dbo", "schema_db_helpers"))[["schema_db_helpers.test_db_helpers_2"]],
get_src_tbl_names(my_test_src(), schema = c("dbo", "schema_db_helpers"), names_pattern = "{.schema}.{.table}")[["schema_db_helpers.test_db_helpers_2"]],
DBI::Id(schema = "schema_db_helpers", table = "test_db_helpers_2")
)
expect_warning(
out <- get_src_tbl_names(my_test_src(), schema = c("dbo", "schema_db_helpers"), names = "{.table}")["test_db_helpers_2"],
out <- get_src_tbl_names(my_test_src(), schema = c("dbo", "schema_db_helpers"), names_pattern = "{.table}")["test_db_helpers_2"],
'Local name test_db_helpers_2 will refer to <"dbo"."test_db_helpers_2">, rather than to <"schema_db_helpers"."test_db_helpers_2">',
fixed = TRUE
)
Expand All @@ -90,7 +90,7 @@ test_that("DB helpers work for MSSQL", {
))
)
expect_warning(
out <- get_src_tbl_names(my_test_src(), schema = c("schema_db_helpers", "dbo"), names = "{.table}")["test_db_helpers_2"],
out <- get_src_tbl_names(my_test_src(), schema = c("schema_db_helpers", "dbo"), names_pattern = "{.table}")["test_db_helpers_2"],
'Local name test_db_helpers_2 will refer to <"schema_db_helpers"."test_db_helpers_2">, rather than to <"dbo"."test_db_helpers_2">',
fixed = TRUE
)
Expand Down Expand Up @@ -145,15 +145,15 @@ test_that("DB helpers work for Postgres", {
DBI::Id(schema = "schema_db_helpers", table = "test_db_helpers_2")
)
expect_identical(
get_src_tbl_names(my_test_src(), schema = c("public", "schema_db_helpers"))["public.test_db_helpers_2"][[1]],
get_src_tbl_names(my_test_src(), schema = c("public", "schema_db_helpers"), names_pattern = "{.schema}.{.table}")["public.test_db_helpers_2"][[1]],
DBI::Id(schema = "public", table = "test_db_helpers_2")
)
expect_identical(
get_src_tbl_names(my_test_src(), schema = c("public", "schema_db_helpers"))["schema_db_helpers.test_db_helpers_2"][[1]],
get_src_tbl_names(my_test_src(), schema = c("public", "schema_db_helpers"), names_pattern = "{.schema}.{.table}")["schema_db_helpers.test_db_helpers_2"][[1]],
DBI::Id(schema = "schema_db_helpers", table = "test_db_helpers_2")
)
expect_warning(
out <- get_src_tbl_names(my_test_src(), schema = c("public", "schema_db_helpers"), names = "{.table}")["test_db_helpers_2"],
out <- get_src_tbl_names(my_test_src(), schema = c("public", "schema_db_helpers"), names_pattern = "{.table}")["test_db_helpers_2"],
'Local name test_db_helpers_2 will refer to <"public"."test_db_helpers_2">, rather than to <"schema_db_helpers"."test_db_helpers_2">',
fixed = TRUE
)
Expand All @@ -165,7 +165,7 @@ test_that("DB helpers work for Postgres", {
))
)
expect_warning(
out <- get_src_tbl_names(my_test_src(), schema = c("schema_db_helpers", "public"), names = "{.table}")["test_db_helpers_2"],
out <- get_src_tbl_names(my_test_src(), schema = c("schema_db_helpers", "public"), names_pattern = "{.table}")["test_db_helpers_2"],
'Local name test_db_helpers_2 will refer to <"schema_db_helpers"."test_db_helpers_2">, rather than to <"public"."test_db_helpers_2">',
fixed = TRUE
)
Expand Down
Loading