generated from r-dbi/RKazam
/
AdbiDriver.R
101 lines (80 loc) · 2.2 KB
/
AdbiDriver.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
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
#' @include adbi-package.R
NULL
#' Adbi driver
#'
#' In order to open a database connection, [DBI::dbConnect()] dispatches on a
#' driver object, which can be instantiated by calling `adbi()`.
#'
#' @details
#' To specify the type of adbc driver, `adbi` accepts as `driver` argument
#'
#' * an object inheriting from `adbc_driver`,
#' * a function that can be evaluated with no arguments and returns an object
#' inheriting from `adbc_driver`,
#' * a string of the form `pkg::fun` (where `pkg::` is optional and defaults
#' to `fun`), which can be used to look up such a function.
#'
#' As default, an [adbcdrivermanager::adbc_driver_monkey()] object is created.
#'
#' @param driver A driver specification that can be evaluated (with no
#' arguments) to give an [adbcdrivermanager::adbc_driver()]. See Details for
#' more information.
#'
#' @export
#' @rdname dbConnect
#' @examples
#' adbi()
#' if (requireNamespace("adbcsqlite")) {
#' adbi("adbcsqlite")
#' }
adbi <- function(driver = NA_character_) {
if (inherits(driver, "adbc_driver")) {
return(new("AdbiDriver", driver = driver))
}
if (is.function(driver)) {
drv_obj <- driver()
} else {
if (is.na(driver)) {
pkg <- "adbcdrivermanager"
fun <- "adbc_driver_monkey"
} else {
driver <- strsplit(driver, "::", fixed = TRUE)[[1L]]
if (length(driver) == 1L) {
pkg <- fun <- driver
} else {
stopifnot(length(driver) == 2L)
pkg <- driver[1L]
fun <- driver[2L]
}
}
drv_fun <- get(
fun,
envir = asNamespace(pkg),
mode = "function",
inherits = FALSE
)
drv_obj <- drv_fun()
}
stopifnot(inherits(drv_obj, "adbc_driver"))
new("AdbiDriver", driver = drv_obj)
}
#' Class AdbiDriver (and methods)
#'
#' AdbiDriver objects are created by [adbi()], and used to select the
#' correct method in [dbConnect()]. They are a superclass of the
#' [DBIDriver-class] class, and used purely for dispatch.
#' The "Usage" section lists the class methods overridden by \pkg{adbi}.
#'
#' @keywords internal
#' @export
setClass(
"AdbiDriver",
slots = list(
driver = "ANY"
),
contains = "DBIDriver"
)
#' @export
DBI::dbCanConnect
#' @export
DBI::Id