From 4f7b4d7ee0c08a57c57c233374d114def143eff3 Mon Sep 17 00:00:00 2001 From: Maximilian Girlich Date: Fri, 2 Sep 2022 08:01:54 +0000 Subject: [PATCH] Use default schema in `dm_learn_from_db()` --- R/db-helpers.R | 44 +++++++++++++++++++++++++++++++++----------- R/learn.R | 1 + 2 files changed, 34 insertions(+), 11 deletions(-) diff --git a/R/db-helpers.R b/R/db-helpers.R index 5e8998d27..ba7a6e897 100644 --- a/R/db-helpers.R +++ b/R/db-helpers.R @@ -115,33 +115,25 @@ repair_table_names_for_db <- function(table_names, temporary, con, schema = NULL } get_src_tbl_names <- function(src, schema = NULL, dbname = NULL) { + schema <- check_schema(src, schema) + if (!is_mssql(src) && !is_postgres(src) && !is_mariadb(src)) { - warn_if_arg_not(schema, only_on = c("MSSQL", "Postgres", "MariaDB")) warn_if_arg_not(dbname, only_on = "MSSQL") return(set_names(src_tbls(src))) } - con <- con_from_src_or_con(src) - if (!is.null(schema)) { - check_param_class(schema, "character") - check_param_length(schema) - } - if (is_mssql(src)) { # MSSQL - schema <- schema_mssql(con, schema) dbname_sql <- dbname_mssql(con, dbname) names_table <- get_names_table_mssql(con, dbname_sql) dbname <- names(dbname_sql) } else if (is_postgres(src)) { # Postgres - schema <- schema_postgres(con, schema) dbname <- warn_if_arg_not(dbname, only_on = "MSSQL") names_table <- get_names_table_postgres(con) } else if (is_mariadb(src)) { # MariaDB - schema <- schema_mariadb(con, schema) dbname <- warn_if_arg_not(dbname, only_on = "MSSQL") names_table <- get_names_table_mariadb(con) } @@ -155,6 +147,31 @@ get_src_tbl_names <- function(src, schema = NULL, dbname = NULL) { deframe() } +check_schema <- function(src, schema) { + if (!is_mssql(src) && !is_postgres(src) && !is_mariadb(src)) { + warn_if_arg_not(schema, only_on = c("MSSQL", "Postgres", "MariaDB")) + return(schema) + } + + con <- con_from_src_or_con(src) + + if (!is.null(schema)) { + check_param_class(schema, "character") + check_param_length(schema) + } + + if (is_mssql(src)) { + # MSSQL + schema_mssql(con, schema) + } else if (is_postgres(src)) { + # Postgres + schema_postgres(con, schema) + } else if (is_mariadb(src)) { + # MariaDB + schema_mariadb(con, schema) + } +} + # `schema_*()` : default schema if NULL, otherwise unchanged schema_mssql <- function(con, schema) { if (is_null(schema)) { @@ -170,9 +187,14 @@ schema_postgres <- function(con, schema) { schema } +ident_q <- function(...) { + # to avoid dependency on dbplyr define `ident_q()` here (not exported from dplyr) + structure(c(...), class = c("ident_q", "ident", "character")) +} + schema_mariadb <- function(con, schema) { if (is_null(schema)) { - schema <- sql("database()") + schema <- ident_q("database()") } schema } diff --git a/R/learn.R b/R/learn.R index e0f91baf5..abce87d4e 100644 --- a/R/learn.R +++ b/R/learn.R @@ -41,6 +41,7 @@ dm_learn_from_db <- function(dest, dbname = NA, schema = NULL, name_format = "{t return() } + schema <- check_schema(src, schema) info <- dm_meta(con, catalog = dbname, schema = schema) df_info <-