From b7885158410503506309b8651f7bbb3a899aa2ef Mon Sep 17 00:00:00 2001 From: eblondel Date: Mon, 28 Oct 2019 09:50:55 +0100 Subject: [PATCH] #33 add dbi entity/contact handlers --- NAMESPACE | 2 ++ R/geoflow_handler.R | 10 ++++++++++ R/geoflow_handler_contact.R | 31 +++++++++++++++++++++++++++++++ R/geoflow_handler_entity.R | 29 +++++++++++++++++++++++++++++ man/handle_contacts_dbi.Rd | 11 +++++++++++ man/handle_entities_dbi.Rd | 11 +++++++++++ 6 files changed, 94 insertions(+) create mode 100644 man/handle_contacts_dbi.Rd create mode 100644 man/handle_entities_dbi.Rd diff --git a/NAMESPACE b/NAMESPACE index 063c30eb..69b57ac3 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -23,10 +23,12 @@ export(geoflow_subject) export(get_line_separator) export(get_temp_directory) export(handle_contacts_csv) +export(handle_contacts_dbi) export(handle_contacts_df) export(handle_contacts_excel) export(handle_contacts_gsheet) export(handle_entities_csv) +export(handle_entities_dbi) export(handle_entities_df) export(handle_entities_excel) export(handle_entities_gsheet) diff --git a/R/geoflow_handler.R b/R/geoflow_handler.R index 139650f2..fb8dd857 100644 --- a/R/geoflow_handler.R +++ b/R/geoflow_handler.R @@ -33,6 +33,11 @@ register_contact_handlers <- function(){ id = "gsheet", def = "Handle metadata contacts from a Google spreadsheet", fun = handle_contacts_gsheet + ), + geoflow_handler$new( + id = "dbi", + def = "Handle metadata contacts from a DB source", + fun = handle_contacts_dbi ) ) .geoflow$contact_handlers <- handlers @@ -75,6 +80,11 @@ register_entity_handlers <- function(){ id = "gsheet", def = "Handle metadata entities from a Google spreadsheet", fun = handle_entities_gsheet + ), + geoflow_handler$new( + id = "dbi", + def = "Handle metadata entities from a DB source", + fun = handle_entities_dbi ) ) .geoflow$entity_handlers <- handlers diff --git a/R/geoflow_handler_contact.R b/R/geoflow_handler_contact.R index ced77a95..c5d24773 100644 --- a/R/geoflow_handler_contact.R +++ b/R/geoflow_handler_contact.R @@ -81,3 +81,34 @@ handle_contacts_excel <- function(config, source){ return(contacts) } +#' handle_contacts_dbi +#' @export +handle_contacts_dbi <- function(config, source){ + dbi <- config$software$input$dbi + if(is.null(dbi)){ + stop("There is no database input software configured to handle contacts from DB") + } + + #db source + is_query <- startsWith(tolower(source), "select ") + if(is_query){ + source <- try(DBI::dbGetQuery(dbi, source)) + if(class(source)="try-error"){ + errMsg <- sprintf("Error while trying to execute DB query '%s'.", source) + config$logger.error(errMsg) + stop(errMsg) + } + }else{ + source <- try(DBI::dbReadTable(dbi, source)) + if(class(source)="try-error"){ + errMsg <- sprintf("Error while trying to read DB table/view '%s'. Check if it exists in DB.", source) + config$logger.error(errMsg) + stop(errMsg) + } + } + + #apply generic handler + contacts <- handle_contacts_df(config, source) + return(contacts) + +} diff --git a/R/geoflow_handler_entity.R b/R/geoflow_handler_entity.R index 67b980da..2ca03b9d 100644 --- a/R/geoflow_handler_entity.R +++ b/R/geoflow_handler_entity.R @@ -214,4 +214,33 @@ handle_entities_excel <- function(config, source){ return(entities) } +#' handle_entities_dbi +#' @export +handle_entities_dbi <- function(config, source){ + dbi <- config$software$input$dbi + if(is.null(dbi)){ + stop("There is no database input software configured to handle entities from DB") + } + + #db source + is_query <- startsWith(tolower(source), "select ") + if(is_query){ + source <- try(DBI::dbGetQuery(dbi, source)) + if(class(source)="try-error"){ + errMsg <- sprintf("Error while trying to execute DB query '%s'.", source) + config$logger.error(errMsg) + stop(errMsg) + } + }else{ + source <- try(DBI::dbReadTable(dbi, source)) + if(class(source)="try-error"){ + errMsg <- sprintf("Error while trying to read DB table/view '%s'. Check if it exists in DB.", source) + config$logger.error(errMsg) + stop(errMsg) + } + } + #apply generic handler + entities <- handle_entities_df(config, source) + return(entities) +} diff --git a/man/handle_contacts_dbi.Rd b/man/handle_contacts_dbi.Rd new file mode 100644 index 00000000..f5325ad9 --- /dev/null +++ b/man/handle_contacts_dbi.Rd @@ -0,0 +1,11 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/geoflow_handler_contact.R +\name{handle_contacts_dbi} +\alias{handle_contacts_dbi} +\title{handle_contacts_dbi} +\usage{ +handle_contacts_dbi(config, source) +} +\description{ +handle_contacts_dbi +} diff --git a/man/handle_entities_dbi.Rd b/man/handle_entities_dbi.Rd new file mode 100644 index 00000000..9a1b3962 --- /dev/null +++ b/man/handle_entities_dbi.Rd @@ -0,0 +1,11 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/geoflow_handler_entity.R +\name{handle_entities_dbi} +\alias{handle_entities_dbi} +\title{handle_entities_dbi} +\usage{ +handle_entities_dbi(config, source) +} +\description{ +handle_entities_dbi +}