Skip to content

Commit

Permalink
use dbCreateTable() and dbAppendTable()
Browse files Browse the repository at this point in the history
- The `field.types` argument to `dbWriteTable()` now must be named.
- Using `dbCreateTable()` and `dbAppendTable()` internally (r-dbi/DBI#74).
  • Loading branch information
krlmlr committed Apr 25, 2018
1 parent e022eb9 commit e0f8d3a
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 15 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Depends:
Imports:
bit64,
blob (>= 1.1.1),
DBI (>= 0.8),
DBI (>= 0.8.0.9001),
hms,
methods,
Rcpp (>= 0.11.4.2),
Expand Down
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ export(postgresHasDefault)
exportClasses(PqConnection)
exportClasses(PqDriver)
exportClasses(PqResult)
exportMethods(dbAppendTable)
exportMethods(dbBegin)
exportMethods(dbBind)
exportMethods(dbCanConnect)
exportMethods(dbClearResult)
exportMethods(dbColumnInfo)
exportMethods(dbCommit)
exportMethods(dbConnect)
exportMethods(dbCreateTable)
exportMethods(dbDataType)
exportMethods(dbDisconnect)
exportMethods(dbDriver)
Expand Down
6 changes: 6 additions & 0 deletions R/export.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
# paste0("#' @exportMethod ", ., "\nNULL\n", collapse = "\n") %>%
# cat(file = "R/export.R")

#' @exportMethod dbAppendTable
NULL

#' @exportMethod dbBegin
NULL

Expand All @@ -26,6 +29,9 @@ NULL
#' @exportMethod dbConnect
NULL

#' @exportMethod dbCreateTable
NULL

#' @exportMethod dbDataType
NULL

Expand Down
37 changes: 25 additions & 12 deletions R/tables.R
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,11 @@ setMethod("dbWriteTable", c("PqConnection", "character", "data.frame"),
if (overwrite && append) {
stopc("overwrite and append cannot both be TRUE")
}
if (!is.null(field.types) && !(is.character(field.types) && !is.null(names(field.types)) && !anyDuplicated(names(field.types)))) {
stopc("`field.types` must be a named character vector with unique names, or NULL")
}
if (append && !is.null(field.types)) {
stopc("Cannot specify field.types with append = TRUE")
stopc("Cannot specify `field.types` with `append = TRUE`")
}

found <- dbExistsTable(conn, name)
Expand All @@ -78,22 +81,31 @@ setMethod("dbWriteTable", c("PqConnection", "character", "data.frame"),
dbRemoveTable(conn, name)
}

value <- sqlRownamesToColumn(value, row.names)

if (!found || overwrite) {
if (!is.null(field.types)) {
if (is.null(names(field.types)))
types <- structure(field.types, .Names = colnames(value))
else
types <- field.types
if (is.null(field.types)) {
combined_field_types <- lapply(value, dbDataType, dbObj = conn)
} else {
types <- value
combined_field_types <- rep("", length(value))
names(combined_field_types) <- names(value)
field_types_idx <- match(names(field.types), names(combined_field_types))
stopifnot(!any(is.na(field_types_idx)))
combined_field_types[field_types_idx] <- field.types
values_idx <- setdiff(seq_along(value), field_types_idx)
combined_field_types[values_idx] <- lapply(value[values_idx], dbDataType, dbObj = conn)
}
sql <- sqlCreateTable(conn, name, if (is.null(field.types)) value else types,
row.names = row.names, temporary = temporary)
dbExecute(conn, sql)

dbCreateTable(
conn = conn,
name = name,
fields = combined_field_types,
temporary = temporary
)
}

if (nrow(value) > 0) {
value <- sqlData(conn, value, row.names = row.names, copy = copy)
value <- sqlData(conn, value, row.names = FALSE, copy = copy)
if (!copy) {
sql <- sqlAppendTable(conn, name, value)
dbExecute(conn, sql)
Expand All @@ -115,8 +127,9 @@ setMethod("dbWriteTable", c("PqConnection", "character", "data.frame"),

#' @export
#' @inheritParams DBI::sqlRownamesToColumn
#' @param ... Ignored.
#' @rdname postgres-tables
setMethod("sqlData", "PqConnection", function(con, value, row.names = FALSE, copy = TRUE) {
setMethod("sqlData", "PqConnection", function(con, value, row.names = FALSE, ...) {
if (is.null(row.names)) row.names <- FALSE
value <- sqlRownamesToColumn(value, row.names)

Expand Down
4 changes: 2 additions & 2 deletions man/postgres-tables.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions tests/testthat/test-dbWriteTable.R
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ with_database_connection({
Species = as.character(Species)
)
field.types <- c("real", "double precision", "numeric", "bigint", "text")
names(field.types) <- names(iris2)

dbWriteTable(con, "iris", iris2, field.types = field.types, temporary = TRUE)

Expand Down Expand Up @@ -75,6 +76,7 @@ with_database_connection({
iris2 <- transform(iris, Petal.Width = as.integer(Petal.Width),
Species = as.character(Species))
field.types <- c("real", "double precision", "numeric", "bigint", "text")
names(field.types) <- names(iris2)

dbWriteTable(con, "iris", iris2, field.types = field.types, temporary = TRUE)
expect_error(
Expand Down

0 comments on commit e0f8d3a

Please sign in to comment.