A DataSource implementation for lazy SQL tibbles connected to databases via
dbplyr::tbl_sql() or dplyr::sql().
Super classes
querychat::DataSource -> querychat::DBISource -> TblSqlSource
Methods
Method new()
Create a new TblSqlSource
Usage
TblSqlSource$new(tbl, table_name = missing_arg())Arguments
tblA
dbplyr::tbl_sql()(or SQL tibble viadplyr::tbl()).table_nameName of the table in the database. Can be a character string, or will be inferred from the
tblargument, if possible.
Method get_schema()
Get schema information about the table
Method get_data()
Get the unfiltered data as a SQL tibble
Returns
A dbplyr::tbl_sql() containing the original, unfiltered data
Examples
if (FALSE) { # rlang::is_interactive() && rlang::is_installed("dbplyr") && rlang::is_installed("dplyr") && rlang::is_installed("duckdb")
con <- DBI::dbConnect(duckdb::duckdb())
DBI::dbWriteTable(con, "mtcars", mtcars)
mtcars_source <- TblSqlSource$new(dplyr::tbl(con, "mtcars"))
mtcars_source$get_db_type() # "DuckDB"
result <- mtcars_source$execute_query("SELECT * FROM mtcars WHERE cyl > 4")
# Note, the result is not the *full* data frame, but a lazy SQL tibble
result
# You can chain this result into a dplyr pipeline
dplyr::count(result, cyl, gear)
# Or collect the entire data frame into local memory
dplyr::collect(result)
# Finally, clean up when done with the database (closes the DB connection)
mtcars_source$cleanup()
}
if (FALSE) { # rlang::is_interactive() && rlang::is_installed("dbplyr") && rlang::is_installed("dplyr") && rlang::is_installed("RSQLite")
con <- DBI::dbConnect(RSQLite::SQLite(), ":memory:")
DBI::dbWriteTable(con, "mtcars", mtcars)
source <- TblSqlSource$new(dplyr::tbl(con, "mtcars"))
}
