File: sqlAppendTable_DBIConnection.R

package info (click to toggle)
dbi 1.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,740 kB
  • sloc: makefile: 2
file content (42 lines) | stat: -rw-r--r-- 920 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#' @rdname hidden_aliases
#' @usage NULL
sqlAppendTable_DBIConnection <- function(
  con,
  table,
  values,
  row.names = NA,
  ...
) {
  stopifnot(is.list(values))

  if (missing(row.names)) {
    warning(
      "Do not rely on the default value of the row.names argument for sqlAppendTable(), it will change in the future.",
      call. = FALSE
    )
  }

  sql_values <- sqlData(con, values, row.names)
  table <- dbQuoteIdentifier(con, table)
  fields <- dbQuoteIdentifier(con, names(sql_values))

  # Convert fields into a character matrix
  rows <- do.call(paste, c(unname(sql_values), sep = ", "))
  SQL(paste0(
    "INSERT INTO ",
    table,
    "\n",
    "  (",
    paste(fields, collapse = ", "),
    ")\n",
    "VALUES\n",
    paste0("  (", rows, ")", collapse = ",\n")
  ))
}
#' @rdname hidden_aliases
#' @export
setMethod(
  "sqlAppendTable",
  signature("DBIConnection"),
  sqlAppendTable_DBIConnection
)