Skip to content
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
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ Imports:
lifecycle,
methods,
Rcpp (>= 0.12.11),
rlang (>= 1.1.0)
rlang (>= 1.1.0),
vctrs
Suggests:
connectcreds,
covr,
Expand Down
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# odbc (development version)

* Fix R error/crash when the `params=` arguments are different lengths
and one is `POSIXt` (#491). This forces all bound-parameters to be
length 1 or the same length (i.e., tidyverse recycling rules).

# odbc 1.6.4

* Fix writing of [R] date/time values that have integer storage. (#952)
Expand Down
7 changes: 5 additions & 2 deletions R/dbi-result.R
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,11 @@ setMethod("dbBind", "OdbcResult",
cli::cli_abort("When mixing data.frame(s) with other parameter types,
all non-df parameters must be of length one")
}
} else if (is.na(batch_rows)) {
batch_rows <- length(params[[1]])
} else {
params[!paramDfs] <- vctrs::vec_recycle_common(!!!params[!paramDfs], .arg = "params")
if (is.na(batch_rows)) {
batch_rows <- length(params[[1]])
}
}

batch_rows <- parse_size(batch_rows)
Expand Down
27 changes: 27 additions & 0 deletions tests/testthat/test-driver-sql-server.R
Original file line number Diff line number Diff line change
Expand Up @@ -490,3 +490,30 @@ test_that("Variable date type storage", {
res <- dbReadTable(con, tbl_name)
expect_identical(data_real, res)
})

test_that("Recycling in dbBind works (#491)", {
dat_in <- data.frame(id = 1000:1004, timestamp = sprintf("2022-04-01 12:00:%02d -04:00", 1:5))
dat_expect <- subset(dat_in, id %in% 1003:1004) |>
transform(timestamp = as.POSIXct(timestamp, tz = "America/New_York"))
rownames(dat_expect) <- NULL

con <- test_con("SQLSERVER")
tbl_name <- "recyclingtemptable"
dbWriteTable(con, tbl_name, dat_in, field.types = c(id = "int", timestamp = "DATETIMEOFFSET"))
defer(dbExecute(con, paste("drop table", tbl_name)))

res <- dbSendStatement(con, paste("select * from ", tbl_name, " where id = ? and timestamp > ? order by id"))
expect_silent( dbBind(res, list(1003:1004, "2022-04-01 12:00:02.000000 +00:00")) )
expect_silent( ret <- dbFetch(res) )
dbClearResult(res)
attr(ret$timestamp, "tzone") <- attr(dat_expect$timestamp, "tzone")
expect_equal(ret, dat_expect)

res <- dbSendStatement(con, paste("select * from ", tbl_name, " where id = ? and timestamp > ? order by id"))
expect_error(
# different lengths: 3 and 2
dbBind(res, list(1003:1005, rep("2022-04-01 12:00:02.000000 +00:00", 2))),
"Can't recycle .*"
)
dbClearResult(res)
})
Loading