Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for primary keys in dbCreateTable() and sqlCreateTable() #351

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 20 additions & 6 deletions R/table-create.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ NULL
#' @param temporary If `TRUE`, will generate a temporary table statement.
#' @inheritParams rownames
#' @param ... Other arguments used by individual methods.
#' @param pk Primary key columns.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems too short compared to other argument names

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

primary.key? I'm not sure this package already uses underscores for argument names :-\

krlmlr marked this conversation as resolved.
Show resolved Hide resolved
#' For some DBMS this must be specified at creation time.
#' Even if not strictly necessary, it may save substantial processing time later on.
#' This argument is processed with [dbQuoteIdentifier()].
#' @export
#' @examples
#' sqlCreateTable(ANSI(), "my-table", c(a = "integer", b = "text"))
Expand All @@ -33,13 +37,13 @@ NULL
#' sqlCreateTable(ANSI(), "mtcars", mtcars[, 1:5])
#' sqlCreateTable(ANSI(), "mtcars", mtcars[, 1:5], row.names = FALSE)
setGeneric("sqlCreateTable",
def = function(con, table, fields, row.names = NA, temporary = FALSE, ...) standardGeneric("sqlCreateTable")
def = function(con, table, fields, row.names = NA, temporary = FALSE, ..., pk = NULL) standardGeneric("sqlCreateTable")
)

#' @rdname hidden_aliases
#' @export
setMethod("sqlCreateTable", signature("DBIConnection"),
function(con, table, fields, row.names = NA, temporary = FALSE, ...) {
function(con, table, fields, row.names = NA, temporary = FALSE, ..., pk = NULL) {
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
Expand All @@ -53,13 +57,22 @@ setMethod("sqlCreateTable", signature("DBIConnection"),
fields <- vapply(fields, function(x) DBI::dbDataType(con, x), character(1))
}

if (!is.null(pk)) {
stopifnot(all(pk %in% names(fields)))
nullable <- ifelse(names(fields) %in% pk, " NOT NULL", "")
} else {
nullable <- ""
}

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"
" ", paste0(fields, nullable, collapse = ",\n "),
if (!is.null(pk)) paste0(",\n PRIMARY KEY (", paste(dbQuoteIdentifier(con, pk), collapse = ", "), ")"),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How standard is this syntax?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pretty much, I think, but I don't know. We'll find out soon enough, we can override in backends -- this is a method.

"\n)\n"
))
}
)
Expand Down Expand Up @@ -91,13 +104,13 @@ setMethod("sqlCreateTable", signature("DBIConnection"),
#' dbReadTable(con, "iris")
#' dbDisconnect(con)
setGeneric("dbCreateTable",
def = function(conn, name, fields, ..., row.names = NULL, temporary = FALSE) standardGeneric("dbCreateTable")
def = function(conn, name, fields, ..., row.names = NULL, temporary = FALSE, pk = NULL) standardGeneric("dbCreateTable")
)

#' @rdname hidden_aliases
#' @export
setMethod("dbCreateTable", signature("DBIConnection"),
function(conn, name, fields, ..., row.names = NULL, temporary = FALSE) {
function(conn, name, fields, ..., row.names = NULL, temporary = FALSE, pk = NULL) {
stopifnot(is.null(row.names))
stopifnot(is.logical(temporary), length(temporary) == 1L)

Expand All @@ -107,7 +120,8 @@ setMethod("dbCreateTable", signature("DBIConnection"),
fields = fields,
row.names = row.names,
temporary = temporary,
...
...,
pk = pk
)
dbExecute(conn, query)
invisible(TRUE)
Expand Down
15 changes: 14 additions & 1 deletion man/dbCreateTable.Rd

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

20 changes: 18 additions & 2 deletions man/hidden_aliases.Rd

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

15 changes: 14 additions & 1 deletion man/sqlCreateTable.Rd

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