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

remove build_insert_query, use dbAppendTable directly #2552

Merged
merged 2 commits into from
Mar 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ For more information about this file see also [Keep a Changelog](http://keepacha
- When building sipnet model would not set correct model version

### Changed
- `PEcAn.DB::insert_table` now uses `DBI::dbAppendTable` internally instead of manually constructed SQL (#2552).
- Rebuilt documentation using Roxygen 7. Readers get nicer formatting of usage sections, writers get more flexible behavior when inheriting parameters and less hassle when maintaining namespaces (#2524).
- Renamed functions that looked like S3 methods but were not:
* PEcAn.priors: `plot.posterior.density`->`plot_posterior.density`, `plot.prior.density`->`plot_prior.density`, `plot.trait`->`plot_trait` (#2439).
Expand Down
23 changes: 2 additions & 21 deletions base/db/R/insert_table.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
#' dplyr::tbl(irisdb, "iris")
insert_table <- function(values, table, con, coerce_col_class = TRUE, drop = TRUE) {
values_fixed <- match_dbcols(values, table, con, coerce_col_class, drop = TRUE)
insert_query <- build_insert_query(values_fixed, table, .con = con)
db.query(insert_query, con)

DBI::dbAppendTable(con, table, values_fixed)
}

#' Match column names and classes between local and SQL table
Expand Down Expand Up @@ -91,22 +91,3 @@ match_colnames <- function(values, table, con) {
values_cols <- colnames(values)
intersect(values_cols, table_cols)
}

#' Build query to insert R data frame into SQL table
#'
#' @inheritParams insert_table
#' @inheritParams glue::glue_sql
build_insert_query <- function(values, table, .con) {
value_list <- purrr::map(seq_len(nrow(values)), ~as.list(values[.x, ]))

insert_list <- value_list %>%
purrr::map(unname) %>%
purrr::map(dbplyr::escape, con = .con) %>%
purrr::map(dbplyr::sql_vector, con = .con)

glue::glue_sql(
"INSERT INTO {`table`} ({`colnames(values)`*}) ",
"VALUES {insert_list*}",
.con = .con
)
}
18 changes: 0 additions & 18 deletions base/db/man/build_insert_query.Rd

This file was deleted.