Skip to content

Commit

Permalink
Restyle (#674)
Browse files Browse the repository at this point in the history
* Restyle with styleR

* Consistent setMethod formatting

* Tweak long function lines

* Fix long lines in roxygen blocks

* Update R/Connection.R
  • Loading branch information
hadley authored Dec 15, 2023
1 parent 69918cb commit ca68fff
Show file tree
Hide file tree
Showing 26 changed files with 1,031 additions and 837 deletions.
287 changes: 151 additions & 136 deletions R/Connection.R

Large diffs are not rendered by default.

67 changes: 41 additions & 26 deletions R/DataTypes.R
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ odbcDataType.default <- function(con, obj, ...) {
logical = "BOOLEAN",
list = "VARCHAR(255)",
time = ,
binary =,
binary = ,
stop("Unsupported type", call. = FALSE)
)
}
Expand Down Expand Up @@ -136,7 +136,7 @@ odbcDataType.default <- function(con, obj, ...) {
list = "STRING",
time = ,
stop("Unsupported type", call. = FALSE)
)
)
}

#' @export
Expand Down Expand Up @@ -229,21 +229,19 @@ odbcDataType.default <- function(con, obj, ...) {
#' @export
odbcDataType.Oracle <- function(con, obj, ...) {
switch_type(obj,
factor = "VARCHAR2(255)",

# No native oracle type for time
time = "VARCHAR2(255)",

date = "DATE",
datetime = "TIMESTAMP",
factor = "VARCHAR2(255)",

binary = "BLOB",
integer = "INTEGER",
double = "BINARY_DOUBLE",
character = "VARCHAR2(255)",
logical = "DECIMAL",
list = "VARCHAR2(255)",
stop("Unsupported type", call. = FALSE)
# No native oracle type for time
time = "VARCHAR2(255)",
date = "DATE",
datetime = "TIMESTAMP",
binary = "BLOB",
integer = "INTEGER",
double = "BINARY_DOUBLE",
character = "VARCHAR2(255)",
logical = "DECIMAL",
list = "VARCHAR2(255)",
stop("Unsupported type", call. = FALSE)
)
}

Expand Down Expand Up @@ -315,23 +313,41 @@ odbcDataType.Oracle <- function(con, obj, ...) {
}

switch_type <- function(obj, ...) {
switch(object_type(obj), ...)
switch(object_type(obj),
...
)
}

object_type <- function(obj) {
if (is.factor(obj)) return("factor")
if (is(obj, "POSIXct")) return("datetime")
if (is(obj, "Date")) return("date")
if (is_blob(obj)) return("binary")
if (is(obj, "difftime")) return("time")
if (is.factor(obj)) {
return("factor")
}
if (is(obj, "POSIXct")) {
return("datetime")
}
if (is(obj, "Date")) {
return("date")
}
if (is_blob(obj)) {
return("binary")
}
if (is(obj, "difftime")) {
return("time")
}

return(typeof(obj))
}

is_blob <- function(obj) {
if (is(obj, "blob")) return(TRUE)
if (is.object(obj) && any(class(obj) != "AsIs")) return(FALSE)
if (!is.list(obj)) return(FALSE)
if (is(obj, "blob")) {
return(TRUE)
}
if (is.object(obj) && any(class(obj) != "AsIs")) {
return(FALSE)
}
if (!is.list(obj)) {
return(FALSE)
}

# Assuming raw inside naked lists if the first non-NULL element is raw,
# not checking the other elements
Expand Down Expand Up @@ -366,4 +382,3 @@ varbinary <- function(x, type = "varbinary") {

paste0(type, "(", max_length, ")")
}

97 changes: 52 additions & 45 deletions R/Driver.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ setClass("OdbcDriver", contains = "DBIDriver")
#' @rdname OdbcDriver
#' @inheritParams methods::show
#' @export
setMethod(
"show", "OdbcDriver",
setMethod("show", "OdbcDriver",
function(object) {
cat("<OdbcDriver>\n")
# TODO: Print more details
})
}
)

#' Connect to a database via an ODBC driver
#'
Expand Down Expand Up @@ -106,7 +106,10 @@ setMethod(
#' The odbc package is one piece of the R interface to databases with support
#' for ODBC:
#'
#' ![A diagram containing four boxes with arrows linking each pointing left to right. The boxes read, in order, R interface, driver manager, ODBC driver, and database. The left-most box, R interface, contains three smaller components, labeled dbplyr, DBI, and odbc.](whole-game.png){options: width=95%}
#' ![A diagram containing four boxes with arrows linking each pointing left to
#' right. The boxes read, in order, R interface, driver manager, ODBC driver,
#' and database. The left-most box, R interface, contains three smaller
#' components, labeled dbplyr, DBI, and odbc.](whole-game.png){options: width=95%}
#'
#' The package supports any **Database Management System (DBMS)** with ODBC
#' support. Support for a given DBMS is provided by an **ODBC driver**, which
Expand Down Expand Up @@ -150,25 +153,24 @@ setMethod(
#'
#' @import rlang
#' @export
setMethod(
"dbConnect", "OdbcDriver",
function(drv,
dsn = NULL,
...,
timezone = "UTC",
timezone_out = "UTC",
encoding = "",
bigint = c("integer64", "integer", "numeric", "character"),
timeout = 10,
driver = NULL,
server = NULL,
database = NULL,
uid = NULL,
pwd = NULL,
dbms.name = NULL,
attributes = NULL,
.connection_string = NULL) {

setMethod("dbConnect", "OdbcDriver",
function(
drv,
dsn = NULL,
...,
timezone = "UTC",
timezone_out = "UTC",
encoding = "",
bigint = c("integer64", "integer", "numeric", "character"),
timeout = 10,
driver = NULL,
server = NULL,
database = NULL,
uid = NULL,
pwd = NULL,
dbms.name = NULL,
attributes = NULL,
.connection_string = NULL) {
con <- OdbcConnection(
dsn = dsn,
...,
Expand All @@ -184,24 +186,29 @@ setMethod(
pwd = pwd,
dbms.name = dbms.name,
attributes = attributes,
.connection_string = .connection_string)
.connection_string = .connection_string
)

# perform the connection notification at the top level, to ensure that it's had
# a chance to get its external pointer connected, and so we can capture the
# expression that created it
if (!is.null(getOption("connectionObserver"))) { # nocov start
addTaskCallback(function(expr, ...) {
tryCatch({
if (rlang::is_call(x = expr, name = c("<-", "=")) &&
tryCatch(
{
if (rlang::is_call(x = expr, name = c("<-", "=")) &&
"dbConnect" %in% as.character(expr[[3]][[1]])) {

# notify if this is an assignment we can replay
on_connection_opened(eval(expr[[2]]), paste(
c("library(DBI)", deparse(expr)), collapse = "\n"))
# notify if this is an assignment we can replay
on_connection_opened(eval(expr[[2]]), paste(
c("library(DBI)", deparse(expr)),
collapse = "\n"
))
}
},
error = function(e) {
warning("Could not notify connection observer. ", e$message, call. = FALSE)
}
}, error = function(e) {
warning("Could not notify connection observer. ", e$message, call. = FALSE)
})
)

# always return false so the task callback is run at most once
FALSE
Expand All @@ -215,20 +222,20 @@ setMethod(
#' @rdname OdbcDriver
#' @inheritParams DBI::dbDataType
#' @export
setMethod(
"dbDataType", "OdbcDriver",
setMethod("dbDataType", "OdbcDriver",
function(dbObj, obj, ...) {
odbcDataType(dbObj, obj, ...)
})
}
)

#' @rdname OdbcDriver
#' @inheritParams DBI::dbDataType
#' @export
setMethod(
"dbDataType", c("OdbcDriver", "list"),
setMethod("dbDataType", c("OdbcDriver", "list"),
function(dbObj, obj, ...) {
odbcDataType(dbObj, obj, ...)
})
}
)

odbc_data_type_df <- function(dbObj, obj, ...) {
res <- character(NCOL(obj))
Expand Down Expand Up @@ -257,20 +264,20 @@ setMethod("dbDataType", c("OdbcDriver", "data.frame"), odbc_data_type_df)
#' @rdname OdbcDriver
#' @inheritParams DBI::dbIsValid
#' @export
setMethod(
"dbIsValid", "OdbcDriver",
setMethod("dbIsValid", "OdbcDriver",
function(dbObj, ...) {
TRUE
})
}
)

#' @rdname OdbcDriver
#' @inheritParams DBI::dbGetInfo
#' @export
setMethod(
"dbGetInfo", "OdbcDriver",
setMethod("dbGetInfo", "OdbcDriver",
function(dbObj, ...) {
list(max.connections = NULL, driver.version = NULL, client.version = NULL)
})
}
)


#' Unimportant DBI methods
Expand Down
Loading

0 comments on commit ca68fff

Please sign in to comment.