Skip to content

Commit

Permalink
use dbCreateTable() and dbAppendTable()
Browse files Browse the repository at this point in the history
- Using `dbCreateTable()` and `dbAppendTable()` internally (r-dbi/DBI#74).
  • Loading branch information
krlmlr committed Apr 25, 2018
1 parent 4caf449 commit 9087470
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 22 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Depends:
R (>= 2.8.0)
Imports:
bit64,
DBI (>= 0.8),
DBI (>= 0.8.0.9001),
hms,
methods,
Rcpp (>= 0.12.4)
Expand Down
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,15 @@ export(mariadbHasDefault)
exportClasses(MariaDBConnection)
exportClasses(MariaDBDriver)
exportClasses(MariaDBResult)
exportMethods(dbAppendTable)
exportMethods(dbBegin)
exportMethods(dbBind)
exportMethods(dbCanConnect)
exportMethods(dbClearResult)
exportMethods(dbColumnInfo)
exportMethods(dbCommit)
exportMethods(dbConnect)
exportMethods(dbCreateTable)
exportMethods(dbDataType)
exportMethods(dbDisconnect)
exportMethods(dbDriver)
Expand Down
6 changes: 6 additions & 0 deletions R/export.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
# paste0("#' @exportMethod ", ., "\nNULL\n", collapse = "\n") %>%
# cat(file = "R/export.R")

#' @exportMethod dbAppendTable
NULL

#' @exportMethod dbBegin
NULL

Expand All @@ -26,6 +29,9 @@ NULL
#' @exportMethod dbConnect
NULL

#' @exportMethod dbCreateTable
NULL

#' @exportMethod dbDataType
NULL

Expand Down
47 changes: 26 additions & 21 deletions R/table.R
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,11 @@ setMethod("dbWriteTable", c("MariaDBConnection", "character", "data.frame"),
if (overwrite && append) {
stopc("overwrite and append cannot both be TRUE")
}
if (!is.null(field.types) && !(is.character(field.types) && !is.null(names(field.types)) && !anyDuplicated(names(field.types)))) {
stopc("`field.types` must be a named character vector with unique names, or NULL")
}
if (append && !is.null(field.types)) {
stopc("Cannot specify field.types with append = TRUE")
stopc("Cannot specify `field.types` with `append = TRUE`")
}

need_transaction <- !connection_is_transacting(conn@ptr)
Expand All @@ -121,32 +124,34 @@ setMethod("dbWriteTable", c("MariaDBConnection", "character", "data.frame"),
dbRemoveTable(conn, name, temporary = temporary, fail_if_missing = FALSE)
}

value <- sql_data(value[, , drop = FALSE], row.names)

if (!found || overwrite) {
sql <- sqlCreateTable(
conn,
name,
if (is.null(field.types)) value else field.types,
row.names = row.names,
if (is.null(field.types)) {
combined_field_types <- lapply(value, dbDataType, dbObj = conn)
} else {
combined_field_types <- rep("", length(value))
names(combined_field_types) <- names(value)
field_types_idx <- match(names(field.types), names(combined_field_types))
stopifnot(!any(is.na(field_types_idx)))
combined_field_types[field_types_idx] <- field.types
values_idx <- setdiff(seq_along(value), field_types_idx)
combined_field_types[values_idx] <- lapply(value[values_idx], dbDataType, dbObj = conn)
}

dbCreateTable(
conn = conn,
name = name,
fields = combined_field_types,
temporary = temporary
)
dbExecute(conn, sql)
}

if (nrow(value) > 0) {
values <- sql_data(value[, , drop = FALSE], row.names)

name <- dbQuoteIdentifier(conn, name)
fields <- dbQuoteIdentifier(conn, names(values))
params <- rep("?", length(fields))

sql <- paste0(
"INSERT INTO ", name, " (", paste0(fields, collapse = ", "), ")\n",
"VALUES (", paste0(params, collapse = ", "), ")"
)
rs <- dbSendStatement(conn, sql)
tryCatch(
result_bind(rs@ptr, values),
finally = dbClearResult(rs)
dbAppendTable(
conn = conn,
name = name,
values = value
)
}

Expand Down

0 comments on commit 9087470

Please sign in to comment.