-
Notifications
You must be signed in to change notification settings - Fork 74
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
base: main
Are you sure you want to change the base?
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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. | ||
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")) | ||
|
@@ -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 | ||
|
@@ -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 = ", "), ")"), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How standard is this syntax? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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" | ||
)) | ||
} | ||
) | ||
|
@@ -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) | ||
|
||
|
@@ -107,7 +120,8 @@ setMethod("dbCreateTable", signature("DBIConnection"), | |
fields = fields, | ||
row.names = row.names, | ||
temporary = temporary, | ||
... | ||
..., | ||
pk = pk | ||
) | ||
dbExecute(conn, query) | ||
invisible(TRUE) | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 :-\