File: sqlCreateTable_DBIConnection.R

package info (click to toggle)
dbi 1.2.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,004 kB
  • sloc: makefile: 2
file content (28 lines) | stat: -rw-r--r-- 944 bytes parent folder | download | duplicates (2)
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
#' @rdname hidden_aliases
#' @usage NULL
sqlCreateTable_DBIConnection <- function(con, table, fields, row.names = NA, temporary = FALSE, ...) {
  if (missing(row.names)) {
    warning("Do not rely on the default value of the row.names argument for sqlCreateTable(), it will change in the future.",
      call. = FALSE
    )
  }

  table <- dbQuoteIdentifier(con, table)

  if (is.data.frame(fields)) {
    fields <- sqlRownamesToColumn(fields, row.names)
    fields <- vapply(fields, function(x) DBI::dbDataType(con, x), character(1))
  }

  field_names <- dbQuoteIdentifier(con, names(fields))
  field_types <- unname(fields)
  fields <- paste0(field_names, " ", field_types)

  SQL(paste0(
    "CREATE ", if (temporary) "TEMPORARY ", "TABLE ", table, " (\n",
    "  ", paste(fields, collapse = ",\n  "), "\n)\n"
  ))
}
#' @rdname hidden_aliases
#' @export
setMethod("sqlCreateTable", signature("DBIConnection"), sqlCreateTable_DBIConnection)