Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use default schema in dm_learn_from_db() #1448

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
44 changes: 33 additions & 11 deletions R/db-helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Weird, we're using a filter with DATABASE() elsewhere. Do we still need that?

}
}

# `schema_*()` : default schema if NULL, otherwise unchanged
schema_mssql <- function(con, schema) {
if (is_null(schema)) {
Expand All @@ -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"))
}

Comment on lines +237 to +241
Copy link
Collaborator

Choose a reason for hiding this comment

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

We're using dbplyr::ident_q() elsewhere.

schema_mariadb <- function(con, schema) {
if (is_null(schema)) {
schema <- sql("database()")
schema <- ident_q("database()")
Copy link
Collaborator

Choose a reason for hiding this comment

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

What's the advantage of using ident_q() here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It works better with %in% !!...:

library(dplyr, warn.conflicts = FALSE)
library(dbplyr, warn.conflicts = FALSE)
lf <- lazy_frame(x = 1)

lf %>% filter(x %in% !!sql("1"))
#> <SQL>
#> SELECT *
#> FROM `df`
#> WHERE (`x` IN 1)
lf %>% filter(x %in% !!ident_q("1"))
#> <SQL>
#> SELECT *
#> FROM `df`
#> WHERE (`x` IN (1))

Created on 2022-09-05 with reprex v2.0.2

}
schema
}
Expand Down
1 change: 1 addition & 0 deletions R/learn.R
Original file line number Diff line number Diff line change
Expand Up @@ -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 <-
Expand Down