1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
|
test_select_bind_expr_one <- list()
#' spec_meta_bind
#' @family meta specifications
#' @name spec_meta_bind
#' @usage NULL
#' @format NULL
#' @keywords NULL
#' @section Specification:
#' \pkg{DBI} clients execute parametrized statements as follows:
#'
test_select_bind_expr_one$fun <- function(
bind_values,
...,
arrow,
bind,
query = TRUE,
has_cast_fun = FALSE,
check_return_value = NULL,
patch_bind_values = NULL,
bind_error = NA,
warn = FALSE,
is_repeated = FALSE,
is_premature_clear = FALSE,
is_untouched = FALSE) {
check_dots_empty()
force(bind_values)
force(arrow)
force(bind)
force(query)
force(check_return_value)
force(patch_bind_values)
force(bind_error)
force(is_repeated)
force(is_premature_clear)
force(is_untouched)
bind_values_expr <- if (bind == "stream") expr({
bind_values <- !!construct_expr(fix_params(bind_values))
}) else expr({
bind_values <- !!construct_expr(bind_values)
})
set_bind_values_patched_expr <- if (!is.null(patch_bind_values)) expr({
bind_values_patched <- !!body(patch_bind_values)
})
bind_values_patched_expr_base <- if (is.null(patch_bind_values)) expr({
bind_values
}) else expr({
bind_values_patched
})
cast_fun_placeholder_expr <- if (has_cast_fun) expr({
cast_fun(placeholder)
}) else expr({
placeholder
})
is_na <- which(map_lgl(bind_values, is_na_or_null))
result_names <- letters[seq_along(bind_values)]
#' 1. Call [dbSendQuery()], [dbSendQueryArrow()] or [dbSendStatement()]
#' with a query or statement that contains placeholders,
#' store the returned [DBIResult-class] object in a variable.
#' Mixing placeholders (in particular, named and unnamed ones) is not
#' recommended.
send_expr <- if (query) expr({
placeholder_values <- map_chr(bind_values, function(x) DBI::dbQuoteLiteral(con, x[1]))
result_check <- paste0("(", (!!cast_fun_placeholder_expr), " = ", placeholder_values, ")")
!!if (length(is_na) > 0) expr({
result_check[!!construct_expr(is_na)] <-
paste0("(", is_null_check((!!cast_fun_placeholder_expr)[!!construct_expr(is_na)]), ")")
})
sql <- "SELECT "
!!!map2(
seq_along(result_names), result_names, ~ expr({
sql <- paste0(
sql,
"CASE WHEN ",
result_check[[!!.x]],
!!paste0(
" THEN ",
trivial_values(2)[[1]],
" ELSE ",
trivial_values(2)[[2]],
" END AS ",
.y,
if (.x < length(result_names)) ", "
)
)
})
)
res <- (!!if (arrow == "none") expr(dbSendQuery) else expr(dbSendQueryArrow))(con, sql)
}) else expr({
data <- data.frame(a = rep(1:5, 1:5), b = 1:15)
table_name <- random_table_name()
dbWriteTable(con, table_name, data, temporary = TRUE)
sql <- paste0("UPDATE ", dbQuoteIdentifier(con, table_name), " SET b = b + 1 WHERE ")
!!!map2(result_names, seq_along(result_names), ~ expr({
sql <- paste0(
sql,
!!paste0(.x, " = "),
placeholder[[!!.y]],
!!!if (.y < length(result_names)) " AND "
)
}))
res <- dbSendStatement(con, sql)
})
#' It is good practice to register a call to [dbClearResult()] via
#' [on.exit()] right after calling `dbSendQuery()` or `dbSendStatement()`
#' (see the last enumeration item).
clear_expr <- if (is_premature_clear) expr({
dbClearResult(res)
}) else expr({
on.exit(if (!is.null(res)) expect_error(dbClearResult(res), NA))
!!if (!is.null(check_return_value)) expr({
#' Until [dbBind()] or [dbBindArrow()] have been called,
#' the returned result set object has the following behavior:
!!if (query) expr({
#' - [dbFetch()] raises an error (for `dbSendQuery()` and `dbSendQueryArrow()`)
expect_error(dbFetch(res))
#' - [dbGetRowCount()] returns zero (for `dbSendQuery()` and `dbSendQueryArrow()`)
expect_equal(dbGetRowCount(res), 0)
}) else expr({
#' - [dbGetRowsAffected()] returns an integer `NA` (for `dbSendStatement()`)
expect_identical(dbGetRowsAffected(res), NA_integer_)
})
#' - [dbIsValid()] returns `TRUE`
expect_true(dbIsValid(res))
#' - [dbHasCompleted()] returns `FALSE`
expect_false(dbHasCompleted(res))
})
})
#' 1. Call [dbBind()] or [dbBindArrow()]:
bind_values_patched_expr <- if (bind == "df") expr({
#' - For [dbBind()], the `params` argument must be a list where all elements
#' have the same lengths and contain values supported by the backend.
#' A [data.frame] is internally stored as such a list.
dbBind(res, !!bind_values_patched_expr_base)
}) else expr({
#' - For [dbBindArrow()], the `params` argument must be a
#' nanoarrow array stream, with one column per query parameter.
dbBindArrow(res, nanoarrow::as_nanoarrow_array_stream(!!bind_values_patched_expr_base))
})
name_values_expr <- expr({
placeholder <- placeholder_fun(!!length(bind_values))
names(bind_values) <- names(placeholder)
})
bind_expr <- if (!is.null(check_return_value)) expr({
bind_res <- withVisible(!!bind_values_patched_expr)
!!body(check_return_value)
}) else if (isTRUE(warn)) expr({
suppressWarnings(expect_warning(!!bind_values_patched_expr))
}) else if (is.na(bind_error)) expr({
!!bind_values_patched_expr
}) else expr({
expect_error(!!bind_values_patched_expr, !!bind_error)
})
#' 1. Retrieve the data or the number of affected rows from the `DBIResult` object.
#' - For queries issued by `dbSendQuery()` or `dbSendQueryArrow()`, call [dbFetch()].
retrieve_expr <- if (query) expr({
rows <- check_df(dbFetch(res))
expect_equal(nrow(rows), !!length(bind_values[[1]]))
# Not checking more specifically in the case of zero rows because of RSQLite
!!if (length(bind_values[[1]]) > 0) expr({
result <- !!construct_expr({
result_names <- letters[seq_along(bind_values)]
expected <- c(trivial_values(1), rep(trivial_values(2)[[2]], length(bind_values[[1]]) - 1))
all_expected <- rep(list(expected), length(bind_values))
as.data.frame(setNames(all_expected, result_names))
})
expect_equal(rows, result)
})
}) else expr({
#' - For statements issued by `dbSendStatements()`,
#' call [dbGetRowsAffected()].
#' (Execution begins immediately after the [dbBind()] call,
#' the statement is processed entirely before the function returns.)
rows_affected <- dbGetRowsAffected(res)
# Allow NA value for dbGetRowsAffected(), #297
if (!isTRUE(allow_na_rows_affected) || !is.na(rows_affected)) {
expect_equal(rows_affected, !!sum(bind_values[[1]]))
}
})
not_untouched_expr <- if (!is_untouched) expr({
!!retrieve_expr
})
#' 1. Repeat 2. and 3. as necessary.
repeated_expr <- if (is_repeated) expr({
!!bind_expr
!!retrieve_expr
})
early_exit <-
is_premature_clear ||
!is.na(bind_error) ||
(!is.null(patch_bind_values) && !identical(bind_values, patch_bind_values(bind_values)))
post_bind_expr <- if (!early_exit) expr({
!!not_untouched_expr
!!repeated_expr
})
#' 1. Close the result set via [dbClearResult()].
clear_now_expr <- if (!is_premature_clear) expr({
expect_error(dbClearResult(res), NA)
res <- NULL
})
test_expr <- expr({
!!bind_values_expr
!!name_values_expr
!!set_bind_values_patched_expr
!!send_expr
!!clear_expr
!!bind_expr
!!post_bind_expr
!!clear_now_expr
})
test_expr
}
construct_expr <- function(x) {
xc <- tryCatch(
constructive::construct(x, check = TRUE),
error = function(e) {
print(e)
constructive::construct(x, check = FALSE)
}
)
parse_expr(format(xc$code))
}
fix_params <- function(params) {
if (is.atomic(params)) {
params <- as.list(params)
}
as.data.frame(params, fix.empty.names = FALSE)
}
|