Skip to content

Commit e5abba9

Browse files
authored
transition odbcDataType() to S4 (#756)
1 parent 8e08b44 commit e5abba9

20 files changed

+421
-319
lines changed

DESCRIPTION

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ RoxygenNote: 7.3.0
4949
SystemRequirements: GNU make, An ODBC3 driver manager and drivers.
5050
Collate:
5151
'RcppExports.R'
52+
'aaa-odbc-data-type.R'
5253
'connection-pane.R'
5354
'dbi-connection.R'
5455
'odbc-connection.R'
@@ -76,7 +77,6 @@ Collate:
7677
'driver-vertica.R'
7778
'odbc-config.R'
7879
'odbc-data-sources.R'
79-
'odbc-data-type.R'
8080
'odbc-drivers.R'
8181
'odbc-package.R'
8282
'odbc.R'

NAMESPACE

+10-15
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,5 @@
11
# Generated by roxygen2: do not edit by hand
22

3-
S3method(odbcDataType,"Microsoft SQL Server")
4-
S3method(odbcDataType,"Spark SQL")
5-
S3method(odbcDataType,"Vertica Database")
6-
S3method(odbcDataType,ACCESS)
7-
S3method(odbcDataType,BigQuery)
8-
S3method(odbcDataType,Hive)
9-
S3method(odbcDataType,Impala)
10-
S3method(odbcDataType,MySQL)
11-
S3method(odbcDataType,Oracle)
12-
S3method(odbcDataType,PostgreSQL)
13-
S3method(odbcDataType,Redshift)
14-
S3method(odbcDataType,SQLite)
15-
S3method(odbcDataType,Snowflake)
16-
S3method(odbcDataType,Teradata)
17-
S3method(odbcDataType,default)
183
S3method(odbcListColumns,OdbcConnection)
194
S3method(odbcListObjectTypes,default)
205
S3method(odbcListObjects,OdbcConnection)
@@ -38,13 +23,22 @@ export(quote_value)
3823
exportClasses("DB2/AIX64")
3924
exportClasses("Microsoft SQL Server")
4025
exportClasses("Spark SQL")
26+
exportClasses("Vertica Database")
27+
exportClasses(ACCESS)
28+
exportClasses(BigQuery)
4129
exportClasses(DatabricksOdbcDriver)
4230
exportClasses(HDB)
4331
exportClasses(Hive)
32+
exportClasses(Impala)
33+
exportClasses(MySQL)
4434
exportClasses(OdbcConnection)
4535
exportClasses(OdbcDriver)
4636
exportClasses(OdbcResult)
4737
exportClasses(Oracle)
38+
exportClasses(PostgreSQL)
39+
exportClasses(Redshift)
40+
exportClasses(SQLite)
41+
exportClasses(Snowflake)
4842
exportClasses(Teradata)
4943
exportMethods(dbAppendTable)
5044
exportMethods(dbBegin)
@@ -74,6 +68,7 @@ exportMethods(dbRollback)
7468
exportMethods(dbSendQuery)
7569
exportMethods(dbSendStatement)
7670
exportMethods(dbWriteTable)
71+
exportMethods(odbcDataType)
7772
exportMethods(show)
7873
exportMethods(sqlCreateTable)
7974
exportMethods(sqlData)

NEWS.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
# odbc (development version)
22

3+
* Transitioned `odbcDataType()` to use S4 for consistency. S3 methods defined
4+
locally will need to be rewritten (#701).
5+
36
* Teradata: Resolved issue when previewing tables using the Connections pane.
47

8+
59
# odbc 1.4.2
610

711
* `dbAppendTable()` Improve performance by checking existence once (#691).

R/odbc-data-type.R R/aaa-odbc-data-type.R

+38-45
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
#' Return the corresponding ODBC data type for an R object
22
#'
3+
#' @description
4+
#'
35
#' This is used when creating a new table with `dbWriteTable()`.
4-
#' Databases with default methods defined are
6+
#' Databases with default methods defined are:
7+
#'
58
#' - MySQL
69
#' - PostgreSQL
710
#' - SQL Server
@@ -15,61 +18,51 @@
1518
#' - BigQuery
1619
#' - Teradata
1720
#' - Access
21+
#' - Snowflake
22+
#'
23+
#' @details
1824
#'
1925
#' If you are using a different database and `dbWriteTable()` fails with a SQL
2026
#' parsing error the default method is not appropriate, you will need to write
21-
#' a new method.
22-
#'
23-
#' @section Defining a new dbDataType method:
24-
#'
25-
#' The object type for your connection will be the database name retrieved by
26-
#' `dbGetInfo(con)$dbms.name`. Use the documentation provided with your
27-
#' database to determine appropriate values for each R data type. An example
28-
#' method definition of a fictional `foo` database follows.
29-
#' ```
30-
#' con <- dbConnect(odbc::odbc(), "FooConnection")
31-
#' dbGetInfo(con)$dbms.name
32-
#' #> [1] "foo"
27+
#' a new method. The object type for your method will be the database name
28+
#' retrieved by `dbGetInfo(con)$dbms.name`. Use the documentation provided with
29+
#' your database to determine appropriate values for each R data type.
3330
#'
34-
#' `odbcDataType.foo <- function(con, obj, ...) {
35-
#' switch_type(obj,
36-
#' factor = "VARCHAR(255)",
37-
#' datetime = "TIMESTAMP",
38-
#' date = "DATE",
39-
#' binary = "BINARY",
40-
#' integer = "INTEGER",
41-
#' double = "DOUBLE",
42-
#' character = "VARCHAR(255)",
43-
#' logical = "BIT",
44-
#' list = "VARCHAR(255)",
45-
#' stop("Unsupported type", call. = FALSE)
46-
#' )
47-
#' }
48-
#' ```
4931
#' @param con A driver connection object, as returned by `dbConnect()`.
5032
#' @param obj An R object.
5133
#' @param ... Additional arguments passed to methods.
5234
#' @return Corresponding SQL type for the `obj`.
5335
#' @export
54-
odbcDataType <- function(con, obj, ...) UseMethod("odbcDataType")
36+
setGeneric(
37+
"odbcDataType",
38+
valueClass = "character",
39+
function(con, obj, ...) {
40+
standardGeneric("odbcDataType")
41+
}
42+
)
5543

5644
#' @export
57-
odbcDataType.default <- function(con, obj, ...) {
58-
switch_type(obj,
59-
factor = "VARCHAR(255)",
60-
datetime = "TIMESTAMP",
61-
date = "DATE",
62-
time = "TIME",
63-
binary = "VARBINARY(255)",
64-
integer = "INTEGER",
65-
int64 = "INTEGER",
66-
double = "DOUBLE PRECISION",
67-
character = "VARCHAR(255)",
68-
logical = "BIT", # only valid if DB supports Null fields
69-
list = "VARCHAR(255)",
70-
stop("Unsupported type", call. = FALSE)
71-
)
72-
}
45+
#' @rdname odbcDataType
46+
#' @usage NULL
47+
setMethod("odbcDataType", "ANY",
48+
function(con, obj, ...) {
49+
switch_type(
50+
obj,
51+
factor = "VARCHAR(255)",
52+
datetime = "TIMESTAMP",
53+
date = "DATE",
54+
time = "TIME",
55+
binary = "VARBINARY(255)",
56+
integer = "INTEGER",
57+
int64 = "INTEGER",
58+
double = "DOUBLE PRECISION",
59+
character = "VARCHAR(255)",
60+
logical = "BIT", # only valid if DB supports Null fields
61+
list = "VARCHAR(255)",
62+
stop("Unsupported type", call. = FALSE)
63+
)
64+
}
65+
)
7366

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

R/driver-access.R

+25-17
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,28 @@
22
NULL
33

44
#' @export
5-
`odbcDataType.ACCESS` <- function(con, obj, ...) {
6-
switch_type(
7-
obj,
8-
factor = varchar(obj),
9-
datetime = "DATETIME",
10-
date = "DATE",
11-
time = "TIME",
12-
binary = "BINARY",
13-
integer = "INTEGER",
14-
int64 = "INTEGER",
15-
double = "DOUBLE",
16-
character = varchar(obj),
17-
logical = "BIT",
18-
list = varchar(obj),
19-
stop("Unsupported type", call. = FALSE)
20-
)
21-
}
5+
#' @rdname DBI-classes
6+
setClass("ACCESS", contains = "OdbcConnection")
7+
8+
#' @export
9+
#' @rdname odbcDataType
10+
#' @usage NULL
11+
setMethod("odbcDataType", "ACCESS",
12+
function(con, obj, ...) {
13+
switch_type(
14+
obj,
15+
factor = varchar(obj),
16+
datetime = "DATETIME",
17+
date = "DATE",
18+
time = "TIME",
19+
binary = "BINARY",
20+
integer = "INTEGER",
21+
int64 = "INTEGER",
22+
double = "DOUBLE",
23+
character = varchar(obj),
24+
logical = "BIT",
25+
list = varchar(obj),
26+
stop("Unsupported type", call. = FALSE)
27+
)
28+
}
29+
)

R/driver-bigquery.R

+24-15
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,27 @@
22
NULL
33

44
#' @export
5-
`odbcDataType.BigQuery` <- function(con, obj, ...) {
6-
switch_type(obj,
7-
factor = "STRING",
8-
datetime = "TIMESTAMP",
9-
time = "TIME",
10-
date = "DATE",
11-
binary = "BYTES",
12-
integer = "INT64",
13-
int64 = "INT64",
14-
double = "FLOAT64",
15-
character = "STRING",
16-
logical = "BOOL",
17-
stop("Unsupported type", call. = FALSE)
18-
)
19-
}
5+
#' @rdname DBI-classes
6+
setClass("BigQuery", contains = "OdbcConnection")
7+
8+
#' @export
9+
#' @rdname odbcDataType
10+
#' @usage NULL
11+
setMethod("odbcDataType", "BigQuery",
12+
function(con, obj, ...) {
13+
switch_type(
14+
obj,
15+
factor = "STRING",
16+
datetime = "TIMESTAMP",
17+
time = "TIME",
18+
date = "DATE",
19+
binary = "BYTES",
20+
integer = "INT64",
21+
int64 = "INT64",
22+
double = "FLOAT64",
23+
character = "STRING",
24+
logical = "BOOL",
25+
stop("Unsupported type", call. = FALSE)
26+
)
27+
}
28+
)

R/driver-hive.R

+21-16
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,24 @@ setMethod("dbQuoteString", c("Hive", "character"),
2323

2424

2525
#' @export
26-
`odbcDataType.Hive` <- function(con, obj, ...) {
27-
switch_type(obj,
28-
factor = "STRING",
29-
datetime = "TIMESTAMP",
30-
date = "DATE",
31-
binary = "BINARY",
32-
integer = "INT",
33-
int64 = "INT",
34-
double = "DOUBLE",
35-
character = "STRING",
36-
logical = "BOOLEAN",
37-
list = "STRING",
38-
time = ,
39-
stop("Unsupported type", call. = FALSE)
40-
)
41-
}
26+
#' @rdname odbcDataType
27+
#' @usage NULL
28+
setMethod("odbcDataType", "Hive",
29+
function(con, obj, ...) {
30+
switch_type(
31+
obj,
32+
factor = "STRING",
33+
datetime = "TIMESTAMP",
34+
date = "DATE",
35+
binary = "BINARY",
36+
integer = "INT",
37+
int64 = "INT",
38+
double = "DOUBLE",
39+
character = "STRING",
40+
logical = "BOOLEAN",
41+
list = "STRING",
42+
time = ,
43+
stop("Unsupported type", call. = FALSE)
44+
)
45+
}
46+
)

R/driver-impala.R

+24-15
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,29 @@
11
#' @include dbi-connection.R
22
NULL
33

4+
#' @export
5+
#' @rdname DBI-classes
6+
setClass("Impala", contains = "OdbcConnection")
7+
48
# TODO: Revisit binary type (Impala)
59
#' @export
6-
`odbcDataType.Impala` <- function(con, obj, ...) {
7-
switch_type(obj,
8-
factor = "STRING",
9-
datetime = "STRING",
10-
date = "VARCHAR(10)",
11-
integer = "INT",
12-
int64 = "INT",
13-
double = "DOUBLE",
14-
character = "STRING",
15-
logical = "BOOLEAN",
16-
list = "STRING",
17-
time = ,
18-
stop("Unsupported type", call. = FALSE)
19-
)
20-
}
10+
#' @rdname odbcDataType
11+
#' @usage NULL
12+
setMethod("odbcDataType", "Impala",
13+
function(con, obj, ...) {
14+
switch_type(
15+
obj,
16+
factor = "STRING",
17+
datetime = "STRING",
18+
date = "VARCHAR(10)",
19+
integer = "INT",
20+
int64 = "INT",
21+
double = "DOUBLE",
22+
character = "STRING",
23+
logical = "BOOLEAN",
24+
list = "STRING",
25+
time = ,
26+
stop("Unsupported type", call. = FALSE)
27+
)
28+
}
29+
)

0 commit comments

Comments
 (0)