Skip to content

Commit

Permalink
Always use DBI::dbQuoteLiteral() (#293)
Browse files Browse the repository at this point in the history
Fixes #279

Co-authored-by: Jennifer (Jenny) Bryan <jenny.f.bryan@gmail.com>
  • Loading branch information
hadley and jennybc authored Mar 13, 2023
1 parent d668dd3 commit 3a784a3
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 24 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Imports:
methods
Suggests:
crayon,
DBI,
DBI (>= 1.1.3),
dplyr,
knitr,
magrittr,
Expand Down
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# glue (development version)

* `glue_sql()` now uses `DBI::dbQuoteLiteral()` for all object types. This
should increase fidelity of escaping for different object types (#279).

* The "Speed of glue" vignette has been converted to an article, which allows several package to be removed from `Suggests` (and re-located to `Config/Needs/website`). The code got a light refresh, including a switch from microbenchmark to bench and more modern use of ggplot2.

* Add `$(C_VISIBILITY)` to compiler flags to hide internal symbols from the dll (#284 @lionel-).
Expand Down
20 changes: 6 additions & 14 deletions R/sql.R
Original file line number Diff line number Diff line change
Expand Up @@ -232,22 +232,14 @@ sql_quote_transformer <- function(connection, .na) {
}
return(res)
}
}

# convert objects to characters
is_object <- is.object(res)
if (is_object) {
res <- as.character(res)
}

is_na <- is.na(res)
if (any(is_na)) {
res[is_na] <- rep(list(.na), sum(is_na))
}

is_char <- vapply(res, function(x) !is.na(x) && is.character(x), logical(1))
res[is_char] <- lapply(res[is_char], function(x) DBI::dbQuoteLiteral(conn = connection, x))
res[!is_char] <- lapply(res[!is_char], function(x) DBI::SQL(conn = connection, x))
if (is.list(res)) {
res <- unlist(lapply(res, DBI::dbQuoteLiteral, conn = connection))
} else {
res <- DBI::dbQuoteLiteral(connection, res)
}

if (should_collapse) {
res <- glue_collapse(res, ", ")
}
Expand Down
19 changes: 10 additions & 9 deletions tests/testthat/test-sql.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ describe("glue_sql", {
DBI::Id(schema = "foo", table = "bar", column = "baz"),
DBI::Id(schema = "foo", table = "bar", column = "baz2")
)
expect_identical(glue_sql("{`var`*}", .con = con), DBI::SQL("`foo`.`bar`.`baz`, `foo`.`bar`.`baz2`"))
expect_identical(
glue_sql("{`var`*}", .con = con),
DBI::SQL("`foo`.`bar`.`baz`, `foo`.`bar`.`baz2`")
)
})
it("Does not quote numbers", {
var <- 1
Expand All @@ -55,16 +58,11 @@ describe("glue_sql", {
expect_identical(glue_sql("x = {var}", .con = con), rep(DBI::SQL("x = NULL"), 4))
})

it("should return NA for missing values and .na = NULL", {
var <- list(NA, NA_character_, NA_real_, NA_integer_)
expect_identical(glue_sql("x = {var}", .con = con, .na = NULL), rep(DBI::SQL(NA), 4))
})

it("should preserve the type of the even with missing values (#130)", {
expect_identical(glue_sql("x = {c(1L, NA)}", .con = con), DBI::SQL(c(paste0("x = ", c(1, "NULL")))))
expect_identical(glue_sql("x = {c(1, NA)}", .con = con), DBI::SQL(c(paste0("x = ", c(1, "NULL")))))
expect_identical(glue_sql("x = {c('1', NA)}", .con = con), DBI::SQL(c(paste0("x = ", c("'1'", "NULL")))))
expect_identical(glue_sql("x = {c(TRUE, NA)}", .con = con), DBI::SQL(c(paste0("x = ", c("TRUE", "NULL")))))
expect_identical(glue_sql("x = {c(TRUE, NA)}", .con = con), DBI::SQL(c(paste0("x = ", c("1", "NULL")))))
})

it("should return NA for missing values quote strings", {
Expand All @@ -79,14 +77,17 @@ describe("glue_sql", {

it("should quote values from lists properly", {
var <- list(1, 2, "three")
expect_identical(glue_sql("x = {var}", .con = con), DBI::SQL(c("x = 1", "x = 2", "x = 'three'")))
expect_identical(
glue_sql("x = {var}", .con = con),
DBI::SQL(c("x = 1", "x = 2", "x = 'three'"))
)
})

it("should handle NA when collapsing (#185)", {
expect_identical(glue_sql("x IN ({c(NA, 'A')*})", .con = con), DBI::SQL(paste0("x IN (NULL, 'A')")))
expect_identical(glue_sql("x IN ({c(NA, 1)*})", .con = con), DBI::SQL(paste0("x IN (NULL, 1)")))
expect_identical(glue_sql("x IN ({c(NA, 1L)*})", .con = con), DBI::SQL(paste0("x IN (NULL, 1)")))
expect_identical(glue_sql("x IN ({c(NA, TRUE)*})", .con = con), DBI::SQL(paste0("x IN (NULL, TRUE)")))
expect_identical(glue_sql("x IN ({c(NA, TRUE)*})", .con = con), DBI::SQL(paste0("x IN (NULL, 1)")))
})

it("should handle DBI::SQL() elements correctly when collapsing (#191)", {
Expand Down

0 comments on commit 3a784a3

Please sign in to comment.