#' S4 Connection Class
#' @import DBI
#' @import RMySQL
#' @export
setClass("FecalConnection", contains = c("DBIConnection", "MySQLConnection"))
#' Connect to the KRSP Fecals Database
#' @description These methods are extensions of the RMySQL and DBI drivers and are therefore compatible with \code{tbl} functions.
#'
#' For additional information on underlying arguments that can be passed to this call, see documentation for \link[DBI]{dbConnect} or \link[RMySQL]{dbConnect}
#'
#' @param username,password Username and password. If username omittedd,
#' defaults to the current user. If password is omitted, only users
#' without a password can login. By defualt this will throw and error if no parameters are entered.
#' This will be changed in future version to default to local instance without a password.
#' @param dbname krsp fecal database name. Defualts to "krspfecals"
#' @param host host endpoint for database instance. Defulats to remote AWS instance but can be changed to localhost.
#' @examples
#' \dontrun{
#' #connect5 to the MySQL database running remotely
#' con = fecalConnect(user = "user", password = "password")
#'
#' #connect to local instance of the database
#' con = fecalConnect(user = "user", password = "password", host = "127.0.0.1")
#'
#' #Always clean up by disconnecting from the database
#' dbDisconnect(con)
#'
#' #Alternatively, there is a handy function built in here
#' fecalDisconnect(con)
#' }
#'
#' @export
setGeneric("fecalConnect",
def = function(...)
standardGeneric("fecalConnect"),
valueClass = "MySQLConnection")
#' @export
setMethod("fecalConnect", NULL, function(drv=RMySQL::MySQL(),username=NULL, group=NULL, password=NULL, dbname=NULL, host=NULL,
unix.socket=NULL, port = 0L, client.flag = 0,
groups = 'rs-dbi', default.file = NULL, ...){
if(is.null(group)){
con = dbConnect(drv,
username = username,
password = password,
dbname = "krspfecals",
host = "hormonedb.cfvasuoihvfm.ca-central-1.rds.amazonaws.com",
port = 3306,...)
} else {
con = dbConnect(drv,
group = group,...)
}
return(con)
})
#' Show Available tables in the database
#' @examples
#' \dontrun{
#' con = fecalConnect(user = "user", password = "pw")
#' fecalTables(con)
#'
#' #always cleanup by disconnecting
#'
#' fecalDisconnect(con)
#' }
#' @description ListTables() returns a character vector that enumerate all tables and views in the database. Tables added with \link[DBI]{dbWriteTable} are part of this list.
#' As soon as a table is removed from the database, it is alsio removed from the list of database tables.
#' \cr
#' @export
setGeneric("fecalTables",
def = function(con,...) standardGeneric("fecalTables"),
valueClass = "character")
setMethod("fecalTables", "MySQLConnection",
function(con,...){
dbGetQuery(con, "SHOW TABLES")[[1]]
})
#' Disconnect from the fecal database
#' @examples
#' \dontrun{
#' #connect to the MySQL database running remotely
#' con = fecalConnect(user = "user", password = "password")
#'
#' #connect to local instance of the database
#' con = fecalConnect(user = "user", password = "password", host = "127.0.0.1")
#'
#' #Always clean up by disconnecting from the database
#' dbDisconnect(con)
#'
#' #Alternatively, there is a handy function built in here
#' fecalDisconnect(con)
#' }
#'
#' @export
setGeneric("fecalDisconnect",
def = function(con,...) standardGeneric("fecalDisconnect"),
valueClass = "logical")
setMethod("fecalDisconnect", "MySQLConnection",
function(con,...){
dbDisconnect(con)
})
#TODO: Currently returns class MySQL connection. Would be nice to convert to formal class fecals
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.