R/dbpf_table_columns.R

Defines functions dbpf_table_columns

Documented in dbpf_table_columns

# =============================================================================
#'
#' @title Return all column names from a table
#'
#' @description Return all columns from a table within the
#' permafrost database @ Carleton University
#'
#' @details These simple functions return all data as data frames. When
#'          making a query many times, optimise the SQL statement to only
#'          request the data you actually need.
#'
#' @param tablename character, name of a table within the database. For a
#' complete list, use the function \link{dbpf_tables}.
#'
#' @param detailed logical, whether or not to return a list of all column
#'  information or just names. Default to false.
#'
#'
#' @return List of columns in the DB table
#'
#' @export
#' @examples
#' \dontrun{
#' location_cols <- dbpf_table_columns('locations')
#' }
#' @param con Database connection object, as returned by \code{\link{dbpf_con}}
#' 
#' @param tablename Name of the table for which to retrieve field names
#' 
#' @author Nick Brown <nick.brown@@carleton.ca>
# =============================================================================
dbpf_table_columns <- function(con, tablename, detailed=F){

  if (missing(con)){
    con <- dbpf_con()
  }

  query <- sprintf("SELECT *
                   FROM information_schema.columns
                   WHERE table_name ='%s'
                   AND table_schema = 'public'", tablename)
  result <- dbGetQuery(con, query)

  if (!detailed){
    result <- result$column_name
  }
  return(result)
}
geocryology/PermafrostDB documentation built on April 17, 2025, 11:54 a.m.