From 4873c84975d3a01a6325c57c62f951e6c0a0464a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kirill=20M=C3=BCller?= Date: Sun, 12 Sep 2021 17:37:41 +0200 Subject: [PATCH] copy = NULL default for dbWriteTable() and dbAppendTable() --- R/tables.R | 10 ++++++++-- man/postgres-tables.Rd | 8 +++++--- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/R/tables.R b/R/tables.R index 5e0d3ac3..7352c35f 100644 --- a/R/tables.R +++ b/R/tables.R @@ -24,6 +24,8 @@ #' and uses `COPY name FROM stdin`. This is fast, but not supported by #' all postgres servers (e.g. Amazon's Redshift). If `FALSE`, generates #' a single SQL string. This is slower, but always supported. +#' The default maps to `TRUE` on connections established via [Postgres()] +#' and to `FALSE` on connections established via [Redshift()]. #' #' @examplesIf postgresHasDefault() #' library(DBI) @@ -47,7 +49,7 @@ NULL #' @rdname postgres-tables setMethod("dbWriteTable", c("PqConnection", "character", "data.frame"), function(conn, name, value, ..., row.names = FALSE, overwrite = FALSE, append = FALSE, - field.types = NULL, temporary = FALSE, copy = TRUE) { + field.types = NULL, temporary = FALSE, copy = NULL) { if (is.null(row.names)) row.names <- FALSE if ((!is.logical(row.names) && !is.character(row.names)) || length(row.names) != 1L) { @@ -181,7 +183,7 @@ format_keep_na <- function(x, ...) { #' @rdname postgres-tables #' @export setMethod("dbAppendTable", c("PqConnection"), - function(conn, name, value, copy = TRUE, ..., row.names = NULL) { + function(conn, name, value, copy = NULL, ..., row.names = NULL) { stopifnot(is.null(row.names)) stopifnot(is.data.frame(value)) db_append_table(conn, name, value, copy = copy, warn = TRUE) @@ -191,6 +193,10 @@ setMethod("dbAppendTable", c("PqConnection"), db_append_table <- function(conn, name, value, copy, warn) { value <- factor_to_string(value, warn = warn) + if (is.null(copy)) { + copy <- !is(conn, "RedshiftConnection") + } + if (copy) { value <- sql_data_copy(value, row.names = FALSE) diff --git a/man/postgres-tables.Rd b/man/postgres-tables.Rd index 1fd96999..6a374199 100644 --- a/man/postgres-tables.Rd +++ b/man/postgres-tables.Rd @@ -25,12 +25,12 @@ append = FALSE, field.types = NULL, temporary = FALSE, - copy = TRUE + copy = NULL ) \S4method{sqlData}{PqConnection}(con, value, row.names = FALSE, ...) -\S4method{dbAppendTable}{PqConnection}(conn, name, value, copy = TRUE, ..., row.names = NULL) +\S4method{dbAppendTable}{PqConnection}(conn, name, value, copy = NULL, ..., row.names = NULL) \S4method{dbReadTable}{PqConnection,character}(conn, name, ..., check.names = TRUE, row.names = FALSE) @@ -86,7 +86,9 @@ with \code{\link[DBI:dbDataType]{DBI::dbDataType()}}).} \item{copy}{If \code{TRUE}, serializes the data frame to a single string and uses \verb{COPY name FROM stdin}. This is fast, but not supported by all postgres servers (e.g. Amazon's Redshift). If \code{FALSE}, generates -a single SQL string. This is slower, but always supported.} +a single SQL string. This is slower, but always supported. +The default maps to \code{TRUE} on connections established via \code{\link[=Postgres]{Postgres()}} +and to \code{FALSE} on connections established via \code{\link[=Redshift]{Redshift()}}.} \item{con}{A database connection.}