diff --git a/R/ODBCResult.R b/R/ODBCResult.R index e22aa3c..81ac5d1 100644 --- a/R/ODBCResult.R +++ b/R/ODBCResult.R @@ -25,7 +25,7 @@ is_done <- function(x) { #' @export #' @rdname odbc-query setMethod("dbFetch", "ODBCResult", function(res, n = -1, ...) { - result <- sqlQuery(res@connection@odbc, res@sql) + result <- sqlQuery(res@connection@odbc, res@sql, max=ifelse(n==-1, 0, n)) is_done(res) <- TRUE result }) diff --git a/tests/testthat/test-ODBCResult.R b/tests/testthat/test-ODBCResult.R index 66e23d4..56c716d 100755 --- a/tests/testthat/test-ODBCResult.R +++ b/tests/testthat/test-ODBCResult.R @@ -46,4 +46,17 @@ test_that("dbGetRowCount function should give the ", { expect_true(size == nrow(iris)) dbRemoveTable(con, "iris") dbDisconnect(con) -}) \ No newline at end of file +}) + +test_that("dbFetch should return the result depending on n argument", { + con <- make_test_connection() + dbWriteTable(con, "iris", iris, overwrite=TRUE, rownames=FALSE) + res <- dbSendQuery(con, "SELECT * FROM iris") + df <- dbFetch(res, n=3) + expect_true(nrow(df)==3) + # -1 means all row + df <- dbFetch(res, n=-1) + expect_true(nrow(df)==nrow(iris)) + dbRemoveTable(con, "iris") + dbDisconnect(con) +})