Skip to content

Commit

Permalink
Merge pull request #324 from r-dbi/f-inline-bind-3
Browse files Browse the repository at this point in the history
chore: Inlining
  • Loading branch information
aviator-app[bot] authored Dec 17, 2023
2 parents ddcda2c + 5b6cfe2 commit 23d40fd
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 88 deletions.
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ Collate:
'spec-sql-remove-table.R'
'spec-sql-list-objects.R'
'spec-meta-bind-runner.R'
'spec-meta-bind-formals.R'
'spec-meta-bind.R'
'spec-meta-bind-.R'
'spec-meta-is-valid.R'
Expand Down
1 change: 1 addition & 0 deletions R/spec-.R
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
#' @include spec-meta-is-valid.R
#' @include spec-meta-bind-.R
#' @include spec-meta-bind.R
#' @include spec-meta-bind-formals.R
#' @include spec-meta-bind-runner.R
#' @include spec-sql-list-objects.R
#' @include spec-sql-remove-table.R
Expand Down
73 changes: 12 additions & 61 deletions R/spec-meta-bind-.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,20 @@ test_select_bind <- function(
...,
cast_fun = identity,
requires_names = NULL) {

force(bind_values)
test_expr <- run_bind_tester$fun(bind_values = bind_values, ...)

placeholder_funs <- get_placeholder_funs(ctx, requires_names)

lapply(
placeholder_funs,
test_select_bind_one,
con = con,
bind_values = bind_values,
is_null_check = ctx$tweaks$is_null_check,
cast_fun = cast_fun,
allow_na_rows_affected = ctx$tweaks$allow_na_rows_affected,
...
)
force(con)
is_null_check <- ctx$tweaks$is_null_check
force(cast_fun)
allow_na_rows_affected <- ctx$tweaks$allow_na_rows_affected

for (placeholder_fun in placeholder_funs) {
rlang::eval_bare(test_expr)
}
}

get_placeholder_funs <- function(ctx, requires_names = NULL) {
Expand Down Expand Up @@ -52,57 +54,6 @@ get_placeholder_funs <- function(ctx, requires_names = NULL) {
placeholder_funs
}

test_select_bind_one <- function(
# Run time
con,
placeholder_fun,
...,
is_null_check,
cast_fun = identity,
allow_na_rows_affected = FALSE,
# Spec time
bind_values,
query = TRUE,
skip_fun = NULL,
check_return_value = NULL,
patch_bind_values = NULL,
bind_error = NA,
is_repeated = FALSE,
is_premature_clear = FALSE,
is_untouched = FALSE) {

rlang::check_dots_empty()

test_expr <- run_bind_tester$fun(
bind_values = bind_values,
query = query,
skip_fun = skip_fun,
check_return_value = check_return_value,
patch_bind_values = patch_bind_values,
bind_error = bind_error,
is_repeated = is_repeated,
is_premature_clear = is_premature_clear,
is_untouched = is_untouched
)

rm(bind_values)
rm(query)
rm(skip_fun)
rm(check_return_value)
rm(patch_bind_values)
rm(bind_error)
rm(is_repeated)
rm(is_premature_clear)
rm(is_untouched)

force(placeholder_fun)
force(is_null_check)
force(cast_fun)
force(allow_na_rows_affected)

rlang::eval_bare(test_expr)
}


# make_placeholder_fun ----------------------------------------------------

Expand Down
22 changes: 22 additions & 0 deletions R/spec-meta-bind-formals.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#' spec_meta_bind
#' @name spec_meta_bind
#' @family meta specifications
#' @aliases NULL
#' @usage NULL
#' @format NULL
#' @keywords NULL
spec_meta_bind_formals <- list(
bind_formals = function() {
# <establish formals of described functions>
expect_equal(names(formals(dbBind)), c("res", "params", "..."))
},
#'
bind_empty = function(con) {
#' @section Failure modes:
#' Calling `dbBind()` for a query without parameters
res <- local_result(dbSendQuery(con, trivial_query()))
#' raises an error.
expect_error(dbBind(res, list()))
},
NULL
)
30 changes: 17 additions & 13 deletions R/spec-meta-bind-runner.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,16 @@ run_bind_tester <- list()
#' \pkg{DBI} clients execute parametrized statements as follows:
#'
run_bind_tester$fun <- function(
...,
# Spec time
bind_values,
query,
skip_fun,
check_return_value,
patch_bind_values,
bind_error,
is_repeated,
is_premature_clear,
is_untouched) {
...,
query = TRUE,
skip_fun = NULL,
check_return_value = NULL,
patch_bind_values = NULL,
bind_error = NA,
is_repeated = FALSE,
is_premature_clear = FALSE,
is_untouched = FALSE) {
rlang::check_dots_empty()
force(bind_values)
force(query)
Expand Down Expand Up @@ -98,7 +97,7 @@ run_bind_tester$fun <- function(
dbClearResult(res)
}) else {
on_exit_expr <- rlang::expr({
on.exit(expect_error(dbClearResult(res), NA))
on.exit(if (!is.null(res)) expect_error(dbClearResult(res), NA))
})

#' Until `dbBind()` has been called, the returned result set object has the
Expand Down Expand Up @@ -193,8 +192,6 @@ run_bind_tester$fun <- function(
!!retrieve_expr
})

#' 1. Close the result set via [dbClearResult()].

early_exit <-
is_premature_clear ||
!is.na(bind_error) ||
Expand All @@ -205,6 +202,12 @@ run_bind_tester$fun <- function(
!!repeated_expr
})

#' 1. Close the result set via [dbClearResult()].
clear_now_expr <- if (!is_premature_clear) rlang::expr({
expect_error(dbClearResult(res), NA)
res <- NULL
})

test_expr <- rlang::expr({
!!skip_expr
!!bind_values_expr
Expand All @@ -214,6 +217,7 @@ run_bind_tester$fun <- function(
!!clear_expr
!!bind_expr
!!post_bind_expr
!!clear_now_expr
})

test_expr
Expand Down
14 changes: 1 addition & 13 deletions R/spec-meta-bind.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,6 @@
#' @format NULL
#' @keywords NULL
spec_meta_bind <- list(
bind_formals = function() {
# <establish formals of described functions>
expect_equal(names(formals(dbBind)), c("res", "params", "..."))
},
#
bind_return_value = function(ctx, con) {
#' @return
check_return_value <- function(bind_res, res) {
Expand Down Expand Up @@ -43,16 +38,9 @@ spec_meta_bind <- list(
query = FALSE
)
},
#'
bind_empty = function(con) {
#' @section Failure modes:
#' Calling `dbBind()` for a query without parameters
res <- local_result(dbSendQuery(con, trivial_query()))
#' raises an error.
expect_error(dbBind(res, list()))
},
#
bind_too_many = function(ctx, con) {
#' @section Failure modes:
patch_bind_values <- function(bind_values) {
#' Binding too many
if (is.null(names(bind_values))) {
Expand Down
1 change: 1 addition & 0 deletions R/spec-meta.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#' @format NULL
spec_meta <- c(
spec_meta_bind_formals,
spec_meta_bind,
spec_meta_is_valid,
spec_meta_has_completed,
Expand Down
16 changes: 15 additions & 1 deletion man/spec_meta_bind.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 23d40fd

Please sign in to comment.