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

Swap readr for data.table as creator of TSV #271

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ Imports:
methods,
rlang
Suggests:
data.table,
DBItest (>= 1.7.2.9001),
decor,
readr,
Expand Down
4 changes: 2 additions & 2 deletions R/dbConnect_MariaDBDriver.R
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
}
}

Expand Down
24 changes: 11 additions & 13 deletions R/table.R
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Copy link
Member

Choose a reason for hiding this comment

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

Perhaps:

Suggested change
"FIELDS TERMINATED BY ", dbQuoteString(conn, "\t"), "\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) {
Expand All @@ -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)
Expand Down Expand Up @@ -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")) {
Expand Down
Loading