diff --git a/R/create_connection.R b/R/create_connection.R index fe02879..ff4fb6f 100644 --- a/R/create_connection.R +++ b/R/create_connection.R @@ -14,51 +14,32 @@ #' #' @export rs_create_conn <- function( - driver = NULL, - param_list, - pool = FALSE - ){ - - if(is.null(param_list$drv)){ - - if(missing(driver)){ - + driver = NULL, + param_list, + pool = FALSE) { + if (is.null(param_list$drv)) { + if (missing(driver)) { stop("Please provide a valid Driver") - } else { - param_list <- append(param_list, c(drv = driver)) - } - - } else if( is.character(param_list$drv)) { - - if(missing(driver)){ - + } else if (is.character(param_list$drv)) { + if (missing(driver)) { stop("Please provide a valid Driver") - } else { - param_list$drv <- NULL param_list <- append(param_list, c(drv = driver)) - } - } - if( pool ) { - + if (pool) { conn <- do.call(pool::dbPool, param_list) - } else { - conn <- do.call(DBI::dbConnect, param_list) - } return(conn) - } diff --git a/R/migration.R b/R/migration.R index 239947a..66196a3 100644 --- a/R/migration.R +++ b/R/migration.R @@ -15,30 +15,21 @@ #' @export rs_migrate <- function( - sql_conn, - up = TRUE, - file_names = NULL, - default_method = "post" -){ - - if( length(file_names) == 0 ) { - + sql_conn, + up = TRUE, + file_names = NULL, + default_method = "post") { + if (length(file_names) == 0) { folder_path <- Sys.getenv("rs_migrate_folder") - if( folder_path == "" ){ - + if (folder_path == "") { folder_path <- "sql/migrate/" - } - if(up){ - + if (up) { folder_path <- paste0(folder_path, "up") - } else { - folder_path <- paste0(folder_path, "down") - } file_names <- sort( @@ -47,18 +38,16 @@ rs_migrate <- function( full.names = TRUE ) ) - } - if(length(file_names) == 0 ){ + if (length(file_names) == 0) { print(folder_path, "-- is an empty folder") } value <- character(length(file_names)) - for( i in seq_along(file_names)){ - + for (i in seq_along(file_names)) { file_name <- file_names[[i]] sql_query <- rs_read_query( @@ -73,7 +62,7 @@ rs_migrate <- function( sql_conn = sql_conn ) }, - error = function(e){ + error = function(e) { cat( sprintf( fmt = "There occurred an error in %s \n Error is ----- \n %s", @@ -83,10 +72,8 @@ rs_migrate <- function( ) return(as.character(e)) - } ) - } return_value <- data.frame( @@ -95,5 +82,4 @@ rs_migrate <- function( ) return(return_value) - } diff --git a/R/read_sql.R b/R/read_sql.R index c806700..978269e 100644 --- a/R/read_sql.R +++ b/R/read_sql.R @@ -8,16 +8,14 @@ #' #' @return character string #' -#'@export +#' @export rs_read_query <- function( - filepath, - method = "get" -){ - + filepath, + method = "get") { method <- tolower(method) - if(! tolower(method) %in% c("get","post")){ + if (!tolower(method) %in% c("get", "post")) { stop("method can only be get or post") } @@ -36,7 +34,6 @@ rs_read_query <- function( ) return(sql_query) - } # print sql query ---------------------------------------------------------- @@ -52,13 +49,12 @@ rs_read_query <- function( #' #' @export print.sql_query <- function( - sql_query -){ -cat( - sprintf( - fmt = " %s ==> \n--------------------- \n%s--------------------- ", - sql_query$method, - sql_query$sql_query + sql_query) { + cat( + sprintf( + fmt = " %s ==> \n--------------------- \n%s--------------------- ", + sql_query$method, + sql_query$sql_query ) ) } @@ -79,11 +75,9 @@ cat( #' #' @export rs_interpolate <- function( - sql_query, - sql_conn, - query_params = list() -) { - + sql_query, + sql_conn, + query_params = list()) { # set Variables ------------------------------------------------------------ sql_query$sql_query <- SQL( @@ -95,7 +89,6 @@ rs_interpolate <- function( ) return(sql_query) - } # send query to DB interpolated ------------------------------------------- @@ -112,32 +105,23 @@ rs_interpolate <- function( #' #' @export rs_execute <- function( - sql_query, - sql_conn -){ - - if ( sql_query$method == "get" ) { - + sql_query, + sql_conn) { + if (sql_query$method == "get") { value <- DBI::dbGetQuery( conn = sql_conn, statement = sql_query$sql_query ) return(value) - - } else if ( sql_query$method == "post" ) { - + } else if (sql_query$method == "post") { value <- DBI::dbExecute( conn = sql_conn, statement = sql_query$sql_query ) return(value) - } else { - stop("Please choose between 'get' and 'post' methods only") - } - }