Skip to content

Commit

Permalink
add the disconnect feature.
Browse files Browse the repository at this point in the history
  • Loading branch information
HuangRicky committed Jun 16, 2020
1 parent 0a28f1d commit 47c8d5e
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 2 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
Package: DBFactory
Type: Package
Title: Database Connection Factory
Version: 0.1.7
Version: 0.1.8
Author: Ruokun Huang <hruokun.2008@gmail.com>
Maintainer: Ruokun Huang <hruokun.2008@gmail.com>
Description: Convenient package for you to organize database connections
Use a nickname for your own server
License: GPL-3
Imports: jsonlite, DBI
Suggests: odbc, pool
Suggests: odbc, pool, ROracle
Encoding: UTF-8
LazyData: true
RoxygenNote: 7.1.0
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Generated by roxygen2: do not edit by hand

export(DBFactory_closepool)
export(DBFactory_disconnect)
export(DBFactory_getsetpool)
export(DBFactory_getsetpool_oracle)
export(get_connstr_template)
Expand Down
23 changes: 23 additions & 0 deletions R/run_query.R
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,29 @@ run_query <- function(stmt, dbobj=NULL, keepcnxn=F){
run(dbobj)
}

#' disconnect the connection saved in env
#' @param type type of connection. sqlserver, mysql, sybaseiq, oracle
#' @export
DBFactory_disconnect <- function(type='sqlserver', returnerr=F){
cnxnname <- paste0(type, '_cnxn')
if(exists(cnxnname,.DBFactoryEnv) && !is.null(get(cnxnname,.DBFactoryEnv))){
channel <- get(cnxnname,.DBFactoryEnv)
r <- tryCatch(DBI::dbDisconnect(channel),error=function(e){
if(returnerr){
message(e)
}
as.character(e)
})
return(r)
} else {
# doesn't need to return anything.
if(returnerr){
warning(paste0("No connection available: ", cnxnname))
}
}
invisible()
}

#' Get or Set Pool connection
#' @param constr connection string
#' @return a pool driver
Expand Down
14 changes: 14 additions & 0 deletions man/DBFactory_disconnect.Rd

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

0 comments on commit 47c8d5e

Please sign in to comment.