-
Notifications
You must be signed in to change notification settings - Fork 190
Expand file tree
/
Copy pathsrc_dbi.R
More file actions
127 lines (118 loc) · 4.02 KB
/
src_dbi.R
File metadata and controls
127 lines (118 loc) · 4.02 KB
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
#' Create a lazy query backed by a database
#'
#' @description
#' Use `tbl()` to create a SQL query backed by a database. Manipulating this
#' object with dplyr verbs then builds up a SQL query that will only be executed
#' when you explicitly ask for it, either by printing the object, calling
#' [collect()] to bring the data back to R or calling [compute()] to create a
#' new table in the database. You can see the query without executing it with
#' [show_query()].
#'
#' Learn more in `vignette("dbplyr")`.
#'
#' @param src A `DBIConnection` object produced by [DBI::dbConnect()].
#' @param from Either a table identifier or a literal [sql()] string.
#'
#' Use a string to identify a table in the current schema/catalog or
#' `I()` for a table elsewhere, e.g. `I("schema.table")` or
#' `I("catalog.schema.table")`. For backward compatibility, you can also use
#' [in_schema()]/[in_catalog()] or [DBI::Id()].
#' @param vars Optionally, provide a character vector of column names. If
#' not supplied, will be retrieved from the database by running a simple
#' query. This argument is mainly useful for better performance when creating
#' many `tbl`s with known variables.
#' @param ... Passed on to [tbl_sql()]
#' @export
#' @examples
#' library(dplyr)
#'
#' # Connect to a temporary in-memory SQLite database and add some data
#' con <- DBI::dbConnect(RSQLite::SQLite(), ":memory:")
#' copy_to(con, mtcars)
#'
#' # To retrieve a single table from a source, use `tbl()`
#' mtcars_db <- con |> tbl("mtcars")
#' mtcars_db
#'
#' # Use `I()` for qualified table names
#' con |> tbl(I("temp.mtcars")) |> head(1)
#'
#' # You can also pass raw SQL if you want a more sophisticated query
#' con |> tbl(sql("SELECT * FROM mtcars WHERE cyl = 8")) |> head(1)
#'
#' # But in most cases, you'll rely on dbplyr to construct the SQL:
#' mtcars_db |>
#' filter(vs == 1) |>
#' summarise(mpg = mean(mpg, na.rm = TRUE), .by = cyl) |>
#' show_query()
#'
#' @importFrom dplyr tbl
#' @aliases tbl_dbi
tbl.src_dbi <- function(src, from, vars = NULL, ...) {
check_dots_empty()
db_table(src$con, from, vars = vars)
}
db_table <- function(
con,
from,
vars = NULL,
subclass = NULL,
call = caller_env()
) {
check_con(con)
source <- as_table_source(from, con = con, error_call = call)
check_character(vars, allow_null = TRUE, call = call)
vars <- vars %||% find_variables(con, from, call = call)
new_tbl_sql(con = con, source = source, vars = vars, subclass = subclass)
}
#' Database src
#'
#' @description
#' `r lifecycle::badge("superseded")`
#'
#' Since can generate a `tbl()` directly from a DBI connection we no longer
#' recommend using `src_dbi()`.
#'
#' @param con An object that inherits from [DBI::DBIConnection-class],
#' typically generated by [DBI::dbConnect]
#' @param auto_disconnect Should the connection be automatically closed when
#' the src is deleted? Set to `TRUE` if you initialize the connection
#' the call to `src_dbi()`. Pass `NA` to auto-disconnect but print a message
#' when this happens.
#' @return An S3 object with class `src_dbi`, `src_sql`, `src`.
#' @keywords internal
#' @export
src_dbi <- function(con, auto_disconnect = FALSE) {
# Avoid registering disconnector if con can't be evaluated
force(con)
# stopifnot(is(con, "DBIConnection"))
if (is_false(auto_disconnect)) {
disco <- NULL
} else {
disco <- db_disconnector(con, quiet = is_true(auto_disconnect)) # nocov
}
structure(
list(
con = con,
disco = disco
),
class = connection_s3_class(con)
)
}
connection_s3_class <- function(con) {
subclass <- setdiff(methods::is(con), methods::extends("DBIConnection"))
c(paste0("src_", subclass), "src_dbi", "src_sql", "src")
}
methods::setOldClass(c("src_dbi", "src_sql", "src"))
# nocov start
# Creates an environment that disconnects the database when it's GC'd
db_disconnector <- function(con, quiet = FALSE) {
reg.finalizer(environment(), function(...) {
if (!quiet) {
message("Auto-disconnecting ", class(con)[[1]])
}
DBI::dbDisconnect(con)
})
environment()
}
# nocov end