From f26411b6a1ec7487b43a38c3b7433f5ff5540343 Mon Sep 17 00:00:00 2001 From: SteveViss Date: Tue, 15 Oct 2024 02:41:43 -0400 Subject: [PATCH] Expose get_db function --- R/db_tbl.R | 24 ++++++++++++++++++++++++ man/get_db.Rd | 16 +++++++++++++--- 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/R/db_tbl.R b/R/db_tbl.R index d0d83fb..b42337d 100644 --- a/R/db_tbl.R +++ b/R/db_tbl.R @@ -176,3 +176,27 @@ delete_entry <- function(con = NULL, tbl = NULL, ...) { get_tbl <- function(con = NULL, tbl = NULL) { DBI::dbReadTable(con, tbl) } + +#' Retrieve joined data from the database +#' +#' This function retrieves data by performing a series of `INNER JOIN` operations across multiple tables in the database, +#' including `lab_measurement`, `analyte`, `lab_sample`, `lab_field_sample`, `field_sample`, `sites`, and `species`. +#' The result is a combined dataset containing related information from all these tables. +#' +#' @param con A DBI connection object. The database connection used to query the data. +#' +#' @return A lazy data frame (tibble) representing the joined data. The query is not executed until explicitly requested by the user (e.g., with `dplyr::collect()`). +#' +#' @export +get_db <- function(con = NULL) { + dplyr::tbl( + con, dplyr::sql(" + SELECT * FROM lab_measurement + INNER JOIN analyte USING (id_analyte) + INNER JOIN lab_sample USING (id_lab_sample) + INNER JOIN lab_field_sample USING (id_lab_sample) + INNER JOIN field_sample USING (id_field_sample) + INNER JOIN sites USING (id_site) + INNER JOIN species USING (id_species)") + ) +} diff --git a/man/get_db.Rd b/man/get_db.Rd index d8ef3be..3581749 100644 --- a/man/get_db.Rd +++ b/man/get_db.Rd @@ -1,11 +1,21 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/fct_search.R +% Please edit documentation in R/db_tbl.R, R/fct_search.R \name{get_db} \alias{get_db} -\title{Get full database to cascade filter} +\title{Retrieve joined data from the database} \usage{ get_db(con = NULL) + +get_db(con = NULL) +} +\arguments{ +\item{con}{A DBI connection object. The database connection used to query the data.} +} +\value{ +A lazy data frame (tibble) representing the joined data. The query is not executed until explicitly requested by the user (e.g., with \code{dplyr::collect()}). } \description{ -Get full database to cascade filter +This function retrieves data by performing a series of \verb{INNER JOIN} operations across multiple tables in the database, +including \code{lab_measurement}, \code{analyte}, \code{lab_sample}, \code{lab_field_sample}, \code{field_sample}, \code{sites}, and \code{species}. +The result is a combined dataset containing related information from all these tables. }