Skip to content

Commit 36a294f

Browse files
committed
fix: pass naming pattern through to dm_learn_from_db() (#2213)
1 parent e52ba91 commit 36a294f

File tree

3 files changed

+17
-16
lines changed

3 files changed

+17
-16
lines changed

R/db-helpers.R

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ find_name_clashes <- function(old, new) {
121121
}
122122

123123
#' @autoglobal
124-
get_src_tbl_names <- function(src, schema = NULL, dbname = NULL, names = NULL) {
124+
get_src_tbl_names <- function(src, schema = NULL, dbname = NULL, names_pattern = "{.table}") {
125125
if (!is_mssql(src) && !is_postgres(src) && !is_mariadb(src)) {
126126
warn_if_arg_not(schema, only_on = c("MSSQL", "Postgres", "MariaDB"))
127127
warn_if_arg_not(dbname, only_on = "MSSQL")
@@ -155,16 +155,6 @@ get_src_tbl_names <- function(src, schema = NULL, dbname = NULL, names = NULL) {
155155
names_table <- get_names_table_mariadb(con)
156156
}
157157

158-
# Use smart default for `.names`, if it wasn't provided
159-
if (!is.null(names)) {
160-
names_pattern <- names
161-
} else if (length(schema) == 1) {
162-
names_pattern <- "{.table}"
163-
} else {
164-
names_pattern <- "{.schema}.{.table}"
165-
cli::cli_inform('Using {.code .names = "{names_pattern}"}')
166-
}
167-
168158
names_table <- names_table %>%
169159
filter(schema_name %in% !!(if (inherits(schema, "sql")) glue_sql_collapse(schema) else schema)) %>%
170160
collect() %>%

R/dm_from_con.R

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,22 @@ dm_from_con <- function(
6464

6565
src <- src_from_src_or_con(con)
6666

67+
# Use smart default for `.names`, if it wasn't provided
68+
dots <- list2(...)
69+
if (!is.null(.names)) {
70+
names_pattern <- .names
71+
} else if (is.null(dots$schema) || length(dots$schema) == 1) {
72+
names_pattern <- "{.table}"
73+
} else {
74+
names_pattern <- "{.schema}.{.table}"
75+
cli::cli_inform('Using {.code .names = "{names_pattern}"}')
76+
}
77+
6778
if (is.null(learn_keys) || isTRUE(learn_keys)) {
6879
# FIXME: Try to make it work everywhere
6980
tryCatch(
7081
{
71-
dm_learned <- dm_learn_from_db(con, ...)
82+
dm_learned <- dm_learn_from_db(con, ..., names_pattern = names_pattern)
7283
if (is_null(learn_keys)) {
7384
inform(c(
7485
"Keys queried successfully.",
@@ -104,7 +115,7 @@ dm_from_con <- function(
104115
}
105116

106117
if (is_null(table_names)) {
107-
src_tbl_names <- get_src_tbl_names(src, ..., names = .names)
118+
src_tbl_names <- get_src_tbl_names(src, ..., names_pattern = names_pattern)
108119
} else {
109120
src_tbl_names <- table_names
110121
if (is.null(names(src_tbl_names))) {

R/learn.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
#' iris_dm_learned <- dm_learn_from_db(src_sqlite)
3434
#' }
3535
#' @autoglobal
36-
dm_learn_from_db <- function(dest, dbname = NA, schema = NULL, name_format = "{table}") {
36+
dm_learn_from_db <- function(dest, dbname = NA, schema = NULL, names_pattern = "{.table}") {
3737
# assuming that we will not try to learn from (globally) temporary tables, which do not appear in sys.table
3838
con <- con_from_src_or_con(dest)
3939
src <- src_from_src_or_con(dest)
@@ -51,8 +51,8 @@ dm_learn_from_db <- function(dest, dbname = NA, schema = NULL, name_format = "{t
5151

5252
dm_name <-
5353
df_info$tables %>%
54-
select(catalog = table_catalog, schema = table_schema, table = table_name) %>%
55-
mutate(name = glue(!!name_format)) %>%
54+
select(catalog = table_catalog, .schema = table_schema, .table = table_name) %>%
55+
mutate(name = glue(!!names_pattern)) %>%
5656
pull() %>%
5757
unclass() %>%
5858
vec_as_names(repair = "unique")

0 commit comments

Comments
 (0)