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

transition odbcDataType() to S4 #756

Merged
merged 3 commits into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ RoxygenNote: 7.3.0
SystemRequirements: GNU make, An ODBC3 driver manager and drivers.
Collate:
'RcppExports.R'
'aaa-odbc-data-type.R'
Copy link
Collaborator Author

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?

'connection-pane.R'
'dbi-connection.R'
'odbc-connection.R'
Expand Down Expand Up @@ -76,7 +77,6 @@ Collate:
'driver-vertica.R'
'odbc-config.R'
'odbc-data-sources.R'
'odbc-data-type.R'
'odbc-drivers.R'
'odbc-package.R'
'odbc.R'
Expand Down
25 changes: 10 additions & 15 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,20 +1,5 @@
# Generated by roxygen2: do not edit by hand

S3method(odbcDataType,"Microsoft SQL Server")
S3method(odbcDataType,"Spark SQL")
S3method(odbcDataType,"Vertica Database")
S3method(odbcDataType,ACCESS)
S3method(odbcDataType,BigQuery)
S3method(odbcDataType,Hive)
S3method(odbcDataType,Impala)
S3method(odbcDataType,MySQL)
S3method(odbcDataType,Oracle)
S3method(odbcDataType,PostgreSQL)
S3method(odbcDataType,Redshift)
S3method(odbcDataType,SQLite)
S3method(odbcDataType,Snowflake)
S3method(odbcDataType,Teradata)
S3method(odbcDataType,default)
S3method(odbcListColumns,OdbcConnection)
S3method(odbcListObjectTypes,default)
S3method(odbcListObjects,OdbcConnection)
Expand All @@ -38,13 +23,22 @@ export(quote_value)
exportClasses("DB2/AIX64")
exportClasses("Microsoft SQL Server")
exportClasses("Spark SQL")
exportClasses("Vertica Database")
exportClasses(ACCESS)
exportClasses(BigQuery)
exportClasses(DatabricksOdbcDriver)
exportClasses(HDB)
exportClasses(Hive)
exportClasses(Impala)
exportClasses(MySQL)
exportClasses(OdbcConnection)
exportClasses(OdbcDriver)
exportClasses(OdbcResult)
exportClasses(Oracle)
exportClasses(PostgreSQL)
exportClasses(Redshift)
exportClasses(SQLite)
exportClasses(Snowflake)
exportClasses(Teradata)
exportMethods(dbAppendTable)
exportMethods(dbBegin)
Expand Down Expand Up @@ -74,6 +68,7 @@ exportMethods(dbRollback)
exportMethods(dbSendQuery)
exportMethods(dbSendStatement)
exportMethods(dbWriteTable)
exportMethods(odbcDataType)
exportMethods(show)
exportMethods(sqlCreateTable)
exportMethods(sqlData)
Expand Down
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# odbc (development version)

* Transitioned `odbcDataType()` to use S4 for consistency. S3 methods defined
locally will need to be rewritten (#701).

# odbc 1.4.2

* `dbAppendTable()` Improve performance by checking existence once (#691).
Expand Down
83 changes: 38 additions & 45 deletions R/odbc-data-type.R → R/aaa-odbc-data-type.R
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
Expand All @@ -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,
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This example used this unexported switch_type() helper. If we wanted to keep this example, we could export switch_type(), but us never having received an issue related to this makes me wonder if folks ever used this code.

Copy link
Member

Choose a reason for hiding this comment

The 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),
Expand Down
42 changes: 25 additions & 17 deletions R/driver-access.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,28 @@
NULL

#' @export
`odbcDataType.ACCESS` <- function(con, obj, ...) {
switch_type(
obj,
factor = varchar(obj),
datetime = "DATETIME",
date = "DATE",
time = "TIME",
binary = "BINARY",
integer = "INTEGER",
int64 = "INTEGER",
double = "DOUBLE",
character = varchar(obj),
logical = "BIT",
list = varchar(obj),
stop("Unsupported type", call. = FALSE)
)
}
#' @rdname DBI-classes
setClass("ACCESS", contains = "OdbcConnection")

#' @export
#' @rdname odbcDataType
#' @usage NULL
setMethod("odbcDataType", "ACCESS",
function(con, obj, ...) {
switch_type(
obj,
factor = varchar(obj),
datetime = "DATETIME",
date = "DATE",
time = "TIME",
binary = "BINARY",
integer = "INTEGER",
int64 = "INTEGER",
double = "DOUBLE",
character = varchar(obj),
logical = "BIT",
list = varchar(obj),
stop("Unsupported type", call. = FALSE)
)
}
)
39 changes: 24 additions & 15 deletions R/driver-bigquery.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,27 @@
NULL

#' @export
`odbcDataType.BigQuery` <- function(con, obj, ...) {
switch_type(obj,
factor = "STRING",
datetime = "TIMESTAMP",
time = "TIME",
date = "DATE",
binary = "BYTES",
integer = "INT64",
int64 = "INT64",
double = "FLOAT64",
character = "STRING",
logical = "BOOL",
stop("Unsupported type", call. = FALSE)
)
}
#' @rdname DBI-classes
setClass("BigQuery", contains = "OdbcConnection")

#' @export
#' @rdname odbcDataType
#' @usage NULL
setMethod("odbcDataType", "BigQuery",
function(con, obj, ...) {
switch_type(
obj,
factor = "STRING",
datetime = "TIMESTAMP",
time = "TIME",
date = "DATE",
binary = "BYTES",
integer = "INT64",
int64 = "INT64",
double = "FLOAT64",
character = "STRING",
logical = "BOOL",
stop("Unsupported type", call. = FALSE)
)
}
)
37 changes: 21 additions & 16 deletions R/driver-hive.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,24 @@ setMethod("dbQuoteString", c("Hive", "character"),


#' @export
`odbcDataType.Hive` <- function(con, obj, ...) {
switch_type(obj,
factor = "STRING",
datetime = "TIMESTAMP",
date = "DATE",
binary = "BINARY",
integer = "INT",
int64 = "INT",
double = "DOUBLE",
character = "STRING",
logical = "BOOLEAN",
list = "STRING",
time = ,
stop("Unsupported type", call. = FALSE)
)
}
#' @rdname odbcDataType
#' @usage NULL
setMethod("odbcDataType", "Hive",
function(con, obj, ...) {
switch_type(
obj,
factor = "STRING",
datetime = "TIMESTAMP",
date = "DATE",
binary = "BINARY",
integer = "INT",
int64 = "INT",
double = "DOUBLE",
character = "STRING",
logical = "BOOLEAN",
list = "STRING",
time = ,
stop("Unsupported type", call. = FALSE)
)
}
)
39 changes: 24 additions & 15 deletions R/driver-impala.R
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)
)
}
)
Loading