R/obj-df_db.R

Defines functions get_df_db_tbl_db assert_df_db get_tbl_db.df_db print.df_db df_db

Documented in df_db

#' @title Database-connected Data Frame
#'
#' @description
#' A `df_db` class is a regular data frame with a `tbl_db` connection
#' attached as an attribute so that it can still use this to reference the
#' database connection. This allows you to `collect()` a `tbl_db()` and still
#' be able to use [`dml`] statements with the resulting data frame.
#'
#' @param df     (tbl_df) data frame linked to the database data
#' @param tbl_db (tbl_db) instance linked to the data frame
#'
#' @family Database Objects
#'
#' @export
df_db <- function(df, tbl_db) {

  assert_class(df, "data.frame")
  assert_tbl_db(tbl_db)
  attr(df, "tbl_db") <- tbl_db
  add_class(df, "df_db", pkg_name)

}

#' @export
print.df_db <- function(x, ...) {

  cli_rule("Data Frame with Database")
  NextMethod()
  print(attr(x, "tbl_db"))

}

#' @export
get_tbl_db.df_db <- function(object) {

  attr(object, "tbl_db")

}

assert_df_db <- function(df_db) {

  assert_class(df_db, "df_db")

}

get_df_db_tbl_db <- function(df_db) {

  assert_df_db(df_db)
  attr(df_db, "tbl_db")

}
tjpalanca/dbtools documentation built on Oct. 7, 2021, 6:43 a.m.