-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcheck_dbtable_variable.R
41 lines (40 loc) · 1.08 KB
/
check_dbtable_variable.R
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
#' Check if a variable is available in a given table
#' @param error Indicates the behaviour when a variable is missing. Gives an error when error = TRUE (default). Return FALSE otherwise.
#' @inheritParams odbc_get_id
#' @export
#' @importFrom DBI dbListFields
#' @importFrom stats na.fail
#' @return TRUE when all variables are present in the table.
check_dbtable_variable <- function(
table,
variable,
schema = "public",
channel,
error = TRUE
){
table <- check_single_character(table, name = "table")
variable <- check_character(
x = variable,
name = "variable",
na.action = na.fail
)
if (length(variable) == 0) {
stop("'variable' must contain at least one value")
}
check_dbtable(table = table, schema = schema, channel = channel, error = TRUE)
# nocov start
check <- variable %in%
dbListFields(conn = channel$con, name = c(schema, table))
if (all(check)) {
return(TRUE)
}
if (error) {
stop(
"Variable(s) missing from '", table, "': ",
paste(variable[!check], collapse = ", ")
)
} else {
return(FALSE)
}
# nocov end
}