diff --git a/NAMESPACE b/NAMESPACE index 10abc7d..330390d 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -10,4 +10,5 @@ export(drop_database_tables) export(get_doc) export(get_download_ranges) export(insert_location_info) +export(list_doc) export(remove_doc) diff --git a/NEWS.md b/NEWS.md index ee596bf..b648db6 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,2 @@ # ATdatabase 0.0.1 -* Start of development diff --git a/R/list_doc.R b/R/list_doc.R new file mode 100644 index 0000000..8d3159e --- /dev/null +++ b/R/list_doc.R @@ -0,0 +1,36 @@ +#' Lists all references for a meta-document type +#' +#' This function lists the references (ref) of all meta-data documents +#' in the database for a given type. +#' +#' @param type character string with type +#' @param conn database connection object +#' +#' @return character vector of references (ref) for the given type, or +#' NA if no documents of that type exist +#' +#' A meta-data document is an R object which is stored in the database. +#' Each object has a type and reference (ref) so they are findable. +#' This function returns all references belonging to a certain type. +#' +#' @export +#' + + +list_doc <- function(type, conn) { + + if (!is.character(type)) { + stop("ERROR list_doc: type is not character") + } + + qry <- glue::glue_sql("SELECT ref FROM meta WHERE type={type};", + .con = conn) + + res <- pool::dbGetQuery(conn, qry) + if (length(res$ref) == 0) { + ref <- NA + } else { + ref <- res$ref + } + return(ref) +} diff --git a/man/list_doc.Rd b/man/list_doc.Rd new file mode 100644 index 0000000..a2a83ff --- /dev/null +++ b/man/list_doc.Rd @@ -0,0 +1,25 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/list_doc.R +\name{list_doc} +\alias{list_doc} +\title{Lists all references for a meta-document type} +\usage{ +list_doc(type, conn) +} +\arguments{ +\item{type}{character string with type} + +\item{conn}{database connection object} +} +\value{ +character vector of references (ref) for the given type, or +NA if no documents of that type exist + +A meta-data document is an R object which is stored in the database. +Each object has a type and reference (ref) so they are findable. +This function returns all references belonging to a certain type. +} +\description{ +This function lists the references (ref) of all meta-data documents +in the database for a given type. +} diff --git a/tests/testthat/test-list_doc.R b/tests/testthat/test-list_doc.R new file mode 100644 index 0000000..d10c49d --- /dev/null +++ b/tests/testthat/test-list_doc.R @@ -0,0 +1,30 @@ +test_that("list_doc input", { + + create_database_tables(dbconn) + expect_error(list_doc(type = 1, conn = dbconn)) + drop_database_tables(dbconn) + +}) + + + +test_that("list_doc output", { + + create_database_tables(dbconn) + x <- data.frame(x = rnorm(5), y = rnorm(5)) + + add_doc(type = "test", ref = "a", doc = x, conn = dbconn) + add_doc(type = "test", ref = "b", doc = x, conn = dbconn) + add_doc(type = "other", ref = "c", doc = x, conn = dbconn) + + refs <- list_doc(type = "test", conn = dbconn) + expect_equal(sort(refs), c("a", "b")) + + refs2 <- list_doc(type = "other", conn = dbconn) + expect_equal(refs2, "c") + + refs3 <- list_doc(type = "nonexistent", conn = dbconn) + expect_true(is.na(refs3)) + + drop_database_tables(dbconn) +})