Skip to content

Commit

Permalink
#33 add dbi entity/contact handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
eblondel committed Oct 28, 2019
1 parent f41be76 commit b788515
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 0 deletions.
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 10 additions & 0 deletions R/geoflow_handler.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
31 changes: 31 additions & 0 deletions R/geoflow_handler_contact.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)

}
29 changes: 29 additions & 0 deletions R/geoflow_handler_entity.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
11 changes: 11 additions & 0 deletions man/handle_contacts_dbi.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions man/handle_entities_dbi.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit b788515

Please sign in to comment.