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

fix: dbQuoteLiteral() uses exponential notation for numeric values #471

Merged
merged 1 commit into from
May 1, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion R/dbQuoteLiteral_DBIConnection.R
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,14 @@ dbQuoteLiteral_DBIConnection <- function(conn, x, ...) {
return(SQL(blob_data, names = names(x)))
}

if (is.double(x)) {
out <- sprintf("%.17e", x)
out[is.na(x)] <- "NULL"
return(SQL(out, names = names(x)))
}

if (is.logical(x)) {
x <- as.numeric(x)
x <- as.integer(x)
}

x <- as.character(x)
Expand Down
20 changes: 10 additions & 10 deletions tests/testthat/test-interpolate.R
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
test_that("parameter names matched", {
expect_equal(
sqlInterpolate(ANSI(), "?a ?b", a = 1, b = 2),
sqlInterpolate(ANSI(), "?a ?b", a = 1L, b = 2L),
SQL("1 2")
)

expect_equal(
sqlInterpolate(ANSI(), "?a ?b", b = 2, a = 1),
sqlInterpolate(ANSI(), "?a ?b", b = 2L, a = 1L),
SQL("1 2")
)

expect_equal(
sqlInterpolate(ANSI(), "?a ?b", b = 2, .dots = list(a = 1)),
sqlInterpolate(ANSI(), "?a ?b", b = 2L, .dots = list(a = 1L)),
SQL("1 2")
)

expect_equal(
sqlInterpolate(ANSI(), "?a ?b", .dots = list(a = 1, b = 2)),
sqlInterpolate(ANSI(), "?a ?b", .dots = list(a = 1L, b = 2L)),
SQL("1 2")
)
})
Expand All @@ -29,21 +29,21 @@ test_that("parameters in strings are ignored", {

test_that("named parameters check matches", {
expect_error(
sqlInterpolate(ANSI(), "?a ?b", a = 1, d = 2),
sqlInterpolate(ANSI(), "?a ?b", a = 1L, d = 2L),
"Supplied values don't match named vars to interpolate"
)
})

test_that("positional parameters work", {
expect_equal(
sqlInterpolate(ANSI(), "a ? c ? d ", 1, 2),
sqlInterpolate(ANSI(), "a ? c ? d ", 1L, 2L),
SQL("a 1 c 2 d ")
)
})

test_that("positional parameters can't have names", {
expect_error(
sqlInterpolate(ANSI(), "? ?", a = 1, 2),
sqlInterpolate(ANSI(), "? ?", a = 1L, 2),
"Positional variables don't take named arguments"
)
})
Expand Down Expand Up @@ -78,7 +78,7 @@ test_that("some more complex case works as well", {

test_that("escaping quotes with doubling works", {
expect_equal(
sqlInterpolate(ANSI(), "'this is a single '' one ?quoted string' ?bar ", bar = 42),
sqlInterpolate(ANSI(), "'this is a single '' one ?quoted string' ?bar ", bar = 42L),
SQL("'this is a single '' one ?quoted string' 42 ")
)
})
Expand All @@ -93,7 +93,7 @@ test_that("corner cases work", {
"Supplied values don't match positional vars to interpolate"
)
expect_equal(
sqlInterpolate(ANSI(), "?a", a = 1),
sqlInterpolate(ANSI(), "?a", a = 1L),
SQL("1")
)
expect_equal(
Expand All @@ -105,7 +105,7 @@ test_that("corner cases work", {
"Unterminated literal"
)
expect_equal(
sqlInterpolate(ANSI(), "?a\"\"?b", a = 1, b = 2),
sqlInterpolate(ANSI(), "?a\"\"?b", a = 1L, b = 2L),
SQL("1\"\"2")
)
expect_equal(
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-sql-df.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ test_that("NAs turn in NULLs", {
)
sql_df <- sqlData(ANSI(), df)

expect_equal(sql_df$x, SQL(c("1", "NULL")))
expect_equal(sql_df$x, SQL(c("1.00000000000000000e+00", "NULL")))
expect_equal(sql_df$y, SQL(c("'a'", "NULL")))
})
Loading