-
Notifications
You must be signed in to change notification settings - Fork 110
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
transition odbcDataType()
to S4
#756
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,10 @@ | ||
#' Return the corresponding ODBC data type for an R object | ||
#' | ||
#' @description | ||
#' | ||
#' This is used when creating a new table with `dbWriteTable()`. | ||
#' Databases with default methods defined are | ||
#' Databases with default methods defined are: | ||
#' | ||
#' - MySQL | ||
#' - PostgreSQL | ||
#' - SQL Server | ||
|
@@ -15,61 +18,51 @@ | |
#' - BigQuery | ||
#' - Teradata | ||
#' - Access | ||
#' - Snowflake | ||
#' | ||
#' @details | ||
#' | ||
#' If you are using a different database and `dbWriteTable()` fails with a SQL | ||
#' parsing error the default method is not appropriate, you will need to write | ||
#' a new method. | ||
#' | ||
#' @section Defining a new dbDataType method: | ||
#' | ||
#' The object type for your connection will be the database name retrieved by | ||
#' `dbGetInfo(con)$dbms.name`. Use the documentation provided with your | ||
#' database to determine appropriate values for each R data type. An example | ||
#' method definition of a fictional `foo` database follows. | ||
#' ``` | ||
#' con <- dbConnect(odbc::odbc(), "FooConnection") | ||
#' dbGetInfo(con)$dbms.name | ||
#' #> [1] "foo" | ||
#' a new method. The object type for your method will be the database name | ||
#' retrieved by `dbGetInfo(con)$dbms.name`. Use the documentation provided with | ||
#' your database to determine appropriate values for each R data type. | ||
#' | ||
#' `odbcDataType.foo <- function(con, obj, ...) { | ||
#' switch_type(obj, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This example used this unexported There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, I don't think there's a compelling reason to keep including this example. |
||
#' factor = "VARCHAR(255)", | ||
#' datetime = "TIMESTAMP", | ||
#' date = "DATE", | ||
#' binary = "BINARY", | ||
#' integer = "INTEGER", | ||
#' double = "DOUBLE", | ||
#' character = "VARCHAR(255)", | ||
#' logical = "BIT", | ||
#' list = "VARCHAR(255)", | ||
#' stop("Unsupported type", call. = FALSE) | ||
#' ) | ||
#' } | ||
#' ``` | ||
#' @param con A driver connection object, as returned by `dbConnect()`. | ||
#' @param obj An R object. | ||
#' @param ... Additional arguments passed to methods. | ||
#' @return Corresponding SQL type for the `obj`. | ||
#' @export | ||
odbcDataType <- function(con, obj, ...) UseMethod("odbcDataType") | ||
setGeneric( | ||
"odbcDataType", | ||
valueClass = "character", | ||
function(con, obj, ...) { | ||
standardGeneric("odbcDataType") | ||
} | ||
) | ||
|
||
#' @export | ||
odbcDataType.default <- function(con, obj, ...) { | ||
switch_type(obj, | ||
factor = "VARCHAR(255)", | ||
datetime = "TIMESTAMP", | ||
date = "DATE", | ||
time = "TIME", | ||
binary = "VARBINARY(255)", | ||
integer = "INTEGER", | ||
int64 = "INTEGER", | ||
double = "DOUBLE PRECISION", | ||
character = "VARCHAR(255)", | ||
logical = "BIT", # only valid if DB supports Null fields | ||
list = "VARCHAR(255)", | ||
stop("Unsupported type", call. = FALSE) | ||
) | ||
} | ||
#' @rdname odbcDataType | ||
#' @usage NULL | ||
setMethod("odbcDataType", "ANY", | ||
function(con, obj, ...) { | ||
switch_type( | ||
obj, | ||
factor = "VARCHAR(255)", | ||
datetime = "TIMESTAMP", | ||
date = "DATE", | ||
time = "TIME", | ||
binary = "VARBINARY(255)", | ||
integer = "INTEGER", | ||
int64 = "INTEGER", | ||
double = "DOUBLE PRECISION", | ||
character = "VARCHAR(255)", | ||
logical = "BIT", # only valid if DB supports Null fields | ||
list = "VARCHAR(255)", | ||
stop("Unsupported type", call. = FALSE) | ||
) | ||
} | ||
) | ||
|
||
switch_type <- function(obj, ...) { | ||
switch(object_type(obj), | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,29 @@ | ||
#' @include dbi-connection.R | ||
NULL | ||
|
||
#' @export | ||
#' @rdname DBI-classes | ||
setClass("Impala", contains = "OdbcConnection") | ||
|
||
# TODO: Revisit binary type (Impala) | ||
#' @export | ||
`odbcDataType.Impala` <- function(con, obj, ...) { | ||
switch_type(obj, | ||
factor = "STRING", | ||
datetime = "STRING", | ||
date = "VARCHAR(10)", | ||
integer = "INT", | ||
int64 = "INT", | ||
double = "DOUBLE", | ||
character = "STRING", | ||
logical = "BOOLEAN", | ||
list = "STRING", | ||
time = , | ||
stop("Unsupported type", call. = FALSE) | ||
) | ||
} | ||
#' @rdname odbcDataType | ||
#' @usage NULL | ||
setMethod("odbcDataType", "Impala", | ||
function(con, obj, ...) { | ||
switch_type( | ||
obj, | ||
factor = "STRING", | ||
datetime = "STRING", | ||
date = "VARCHAR(10)", | ||
integer = "INT", | ||
int64 = "INT", | ||
double = "DOUBLE", | ||
character = "STRING", | ||
logical = "BOOLEAN", | ||
list = "STRING", | ||
time = , | ||
stop("Unsupported type", call. = FALSE) | ||
) | ||
} | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
😞
I wasn't able to find any other places where we needed a workaround to define generics before methods. We could also just delete this file and place its contents in
odbc-connection.R
?