diff --git a/DESCRIPTION b/DESCRIPTION index 888b8539..0d96abf8 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -31,6 +31,7 @@ Imports: methods, rlang Suggests: + data.table, DBItest (>= 1.7.2.9001), decor, readr, diff --git a/R/dbConnect_MariaDBDriver.R b/R/dbConnect_MariaDBDriver.R index 595fb2dd..f3f814d2 100644 --- a/R/dbConnect_MariaDBDriver.R +++ b/R/dbConnect_MariaDBDriver.R @@ -168,8 +168,8 @@ dbConnect_MariaDBDriver <- function( } if (isTRUE(load_data_local_infile)) { - if (!rlang::is_installed("readr")) { - stopc("`load_data_local_infile = TRUE` requires the readr package.") + if (!rlang::is_installed("data.table")) { + stopc("`load_data_local_infile = TRUE` requires the data.table package.") } } diff --git a/R/table.R b/R/table.R index 8c960edc..195b573b 100644 --- a/R/table.R +++ b/R/table.R @@ -61,23 +61,23 @@ db_append_table <- function(conn, name, value, warn_factor = TRUE, safe = TRUE, sql <- paste0( "LOAD DATA LOCAL INFILE ", dbQuoteString(conn, path), "\n", "IGNORE\n", + "FIELDS TERMINATED BY ", dbQuoteString(conn, "\t"), "\n", + "OPTIONALLY ENCLOSED BY ", dbQuoteString(conn, '"'), "\n", + "LINES TERMINATED BY ", dbQuoteString(conn, if (.Platform$OS.type == "windows") "\r\n" else "\n"), "\n", "INTO TABLE ", quoted_name, "\n", "CHARACTER SET utf8 \n", "(", paste0(colnames, collapse = ", "), ")", set ) - file <- file(path, "wb") - on.exit(close(file)) - - readr::write_delim( - csv_quote(value, warn_factor, conn), file, quote = "none", delim = "\t", na = "\\N", - col_names = FALSE + data.table::fwrite( + csv_quote(value, warn_factor, conn), path, + sep = "\t", na = "\\N", logical01 = TRUE, + col.names = FALSE ) # Close connection manually, unlink when done to save disk space on.exit(unlink(path), add = FALSE) - close(file) if (safe) { if (transact) { @@ -104,10 +104,6 @@ db_append_table <- function(conn, name, value, warn_factor = TRUE, safe = TRUE, dbCommit(conn) } - # Manual cleanup - unlink(path) - on.exit(NULL, add = FALSE) - out } else { dbExecute(conn, sql) @@ -152,8 +148,10 @@ csv_quote_one <- function(x, conn) { x <- formatC(x, digits = 17, format = "E") } x[is.na(x_orig) | is.infinite(x_orig)] <- NA_character_ - } else if (is.logical(x)) { - x <- as.character(as.integer(x)) + # No need to quote logical values manually as data.table::fwrite provides + # options for handling that + # } else if (is.logical(x)) { + # x <- as.character(as.integer(x)) } else if (inherits(x, "Date")) { x <- as.character(x) } else if (inherits(x, "difftime")) {