Skip to content

Commit

Permalink
change the way dplyr sql is rendered
Browse files Browse the repository at this point in the history
  • Loading branch information
ablack3 committed Dec 7, 2023
1 parent be58698 commit e7aacbf
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 13 deletions.
33 changes: 25 additions & 8 deletions R/Connect.R
Original file line number Diff line number Diff line change
Expand Up @@ -778,7 +778,7 @@ connectUsingJdbcDriver <- function(jdbcDriver,
abort(paste0("Unable to connect JDBC to ", url, " (", rJava::.jcall(x, "S", "getMessage"), ")"))
}
}
ensureDatabaseConnectorConnectionClassExists()
ensureDatabaseConnectorConnectionClassExists(dbms)
class <- getClassDef("DatabaseConnectorJdbcConnection", where = class_cache, inherits = FALSE)
if (is.null(class) || methods::isVirtualClass(class)) {
setClass("DatabaseConnectorJdbcConnection",
Expand All @@ -798,16 +798,30 @@ connectUsingJdbcDriver <- function(jdbcDriver,
return(connection)
}

ensureDatabaseConnectorConnectionClassExists <- function() {
class <- getClassDef("Microsoft SQL Server", where = class_cache, inherits = FALSE)
ensureDatabaseConnectorConnectionClassExists <- function(dbms) {
checkIfDbmsIsSupported(dbms)
dbmsClass <- switch(dbms,
"oracle" = "Oracle",
"postgresql" = "PqConnection",
"redshift" = "RedshiftConnection",
"sql server" = "Microsoft SQL Server",
"bigquery" = "BigQueryConnection",
"sqlite" = "SQLiteConnection",
"sqlite extended" = "SQLiteConnection",
"spark" = "Spark SQL",
"snowflake" = "Snowflake",
"synapse" = "Microsoft SQL Server",
"duckdb" = "duckdb_connection",
"blah")

class <- getClassDef(dbmsClass, where = class_cache, inherits = FALSE)
if (is.null(class) || methods::isVirtualClass(class)) {
setClass("Microsoft SQL Server",
where = class_cache)
setClass(dbmsClass, where = class_cache)
}
class <- getClassDef("DatabaseConnectorConnection", where = class_cache, inherits = FALSE)
if (is.null(class) || methods::isVirtualClass(class)) {
setClass("DatabaseConnectorConnection",
contains = c("Microsoft SQL Server", "DBIConnection"),
contains = c(dbmsClass, "DBIConnection"),
slots = list(
identifierQuote = "character",
stringQuote = "character",
Expand All @@ -822,7 +836,7 @@ connectUsingDbi <- function(dbiConnectionDetails) {
dbms <- dbiConnectionDetails$dbms
dbiConnectionDetails$dbms <- NULL
dbiConnection <- do.call(DBI::dbConnect, dbiConnectionDetails)
ensureDatabaseConnectorConnectionClassExists()
ensureDatabaseConnectorConnectionClassExists(dbms)
class <- getClassDef("DatabaseConnectorDbiConnection", where = class_cache, inherits = FALSE)
if (is.null(class) || methods::isVirtualClass(class)) {
setClass("DatabaseConnectorDbiConnection",
Expand Down Expand Up @@ -956,7 +970,10 @@ dbms <- function(connection) {
"RedshiftConnection" = "redshift",
"BigQueryConnection" = "bigquery",
"SQLiteConnection" = "sqlite",
"duckdb_connection" = "duckdb"
"duckdb_connection" = "duckdb",
"Spark Sql" = "spark",
"Snowflake" = "snowflake",
"Oracle" = "oracle"
# add mappings from various DBI connection classes to SqlRender dbms here
)
}
10 changes: 5 additions & 5 deletions R/DBI.R
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ setClass("DatabaseConnectorDbiResult",
setMethod(
"dbSendQuery",
signature("DatabaseConnectorJdbcConnection", "character"),
function(conn, statement, translate = TRUE, ...) {
function(conn, statement, translate = FALSE, ...) {
if (rJava::is.jnull(conn@jConnection)) {
abort("Connection is closed")
}
Expand Down Expand Up @@ -258,7 +258,7 @@ setMethod(
setMethod(
"dbSendQuery",
signature("DatabaseConnectorDbiConnection", "character"),
function(conn, statement, translate = TRUE, ...) {
function(conn, statement, translate = FALSE, ...) {
if (translate) {
statement <- translateStatement(
sql = statement,
Expand Down Expand Up @@ -426,7 +426,7 @@ setMethod("dbGetRowsAffected", "DatabaseConnectorDbiResult", function(res, ...)
setMethod(
"dbGetQuery",
signature("DatabaseConnectorConnection", "character"),
function(conn, statement, translate = TRUE, ...) {
function(conn, statement, translate = FALSE, ...) {
if (translate) {
statement <- translateStatement(
sql = statement,
Expand All @@ -447,7 +447,7 @@ setMethod(
setMethod(
"dbSendStatement",
signature("DatabaseConnectorConnection", "character"),
function(conn, statement, translate = TRUE, ...) {
function(conn, statement, translate = FALSE, ...) {
if (translate) {
statement <- translateStatement(
sql = statement,
Expand All @@ -472,7 +472,7 @@ setMethod(
setMethod(
"dbExecute",
signature("DatabaseConnectorConnection", "character"),
function(conn, statement, translate = TRUE, ...) {
function(conn, statement, translate = FALSE, ...) {
if (isDbplyrSql(statement) && dbms(conn) %in% c("oracle", "bigquery", "spark", "hive") && grepl("^UPDATE STATISTICS", statement)) {
# These platforms don't support this, so SqlRender translates to an empty string, which causes errors down the line.
return(0)
Expand Down

0 comments on commit e7aacbf

Please sign in to comment.