From 4a38372fc90a29d2d28286da678a7de0029beedd Mon Sep 17 00:00:00 2001 From: Craig Williams Date: Wed, 28 Jan 2026 12:47:12 +0000 Subject: [PATCH] Re-jigged the class implementation to get better support from roxygen2 and CRAN check(). Majority of public functions are documented (remaining are the CRUD functions) --- DESCRIPTION | 5 +- NAMESPACE | 4 + .../CRUDKeys.R => DBClient-CRUDKeys.R} | 14 +- .../CRUDRow.R => DBClient-CRUDRow.R} | 0 .../CRUDRows.R => DBClient-CRUDRows.R} | 11 +- R/DBClient-Initialize.R | 29 ++++ R/DBClient-dbAction.R | 14 ++ R/DBClient-dbCollect.R | 84 +++++++++++ R/DBClient-dbQuery.R | 17 +++ R/DBClient-dbSchema.R | 39 +++++ R/DBClient-dbTable.R | 14 ++ R/DBClient-dbTransaction.R | 15 ++ R/DBClient.R | 13 +- R/DBClientClass/Initialize.R | 8 - R/DBClientClass/dbAction.R | 6 - R/DBClientClass/dbCollect.R | 52 ------- R/DBClientClass/dbQuery.R | 9 -- R/DBClientClass/dbSchema.R | 21 --- R/DBClientClass/dbTable.R | 7 - R/DBClientClass/dbTransaction.R | 8 - R/DBConnection-Configure.R | 141 ++++++++++++++++++ .../Connect.R => DBConnection-Connect.R} | 57 +++++-- R/DBConnection-Initialize.R | 22 +++ R/DBConnection-Transaction.R | 69 +++++++++ R/DBConnection.R | 16 +- R/DBConnectionClass/Configure.R | 68 --------- R/DBConnectionClass/Transaction.R | 39 ----- R/PrintDebug.R | 23 ++- R/aaa.R | 8 + man/DBClient-cash-collectGeometries.Rd | 30 ++++ man/DBClient-cash-collectOrReturn.Rd | 18 +++ man/DBClient-cash-dbAction.Rd | 14 ++ man/DBClient-cash-dbQuery.Rd | 14 ++ man/DBClient-cash-dplyrWithSchema.Rd | 16 ++ man/DBClient-cash-getConnection.Rd | 8 + man/DBClient-cash-initialize.Rd | 11 ++ man/DBClient-cash-table.Rd | 14 ++ man/DBClient-cash-withSchema.Rd | 14 ++ man/DBClient-cash-withTransaction.Rd | 14 ++ man/DBConnection-cash-beginTransaction.Rd | 15 ++ man/DBConnection-cash-commitTransaction.Rd | 18 +++ man/DBConnection-cash-configure.Rd | 18 +++ man/DBConnection-cash-connect.Rd | 12 ++ man/DBConnection-cash-disconnect.Rd | 8 + ...BConnection-cash-disconnectOnSessionEnd.Rd | 15 ++ man/DBConnection-cash-getConfiguration.Rd | 11 ++ man/DBConnection-cash-getConnection.Rd | 14 ++ man/DBConnection-cash-getToken.Rd | 8 + man/DBConnection-cash-initialize.Rd | 8 + man/DBConnection-cash-isConnected.Rd | 11 ++ man/DBConnection-cash-loadConfiguration.Rd | 14 ++ man/DBConnection-cash-rollbackTransaction.Rd | 15 ++ man/DBConnection.Rd | 2 +- man/dot-configureDefault.Rd | 44 ++++++ 54 files changed, 900 insertions(+), 279 deletions(-) rename R/{DBClientClass/CRUDKeys.R => DBClient-CRUDKeys.R} (79%) rename R/{DBClientClass/CRUDRow.R => DBClient-CRUDRow.R} (100%) rename R/{DBClientClass/CRUDRows.R => DBClient-CRUDRows.R} (94%) create mode 100644 R/DBClient-Initialize.R create mode 100644 R/DBClient-dbAction.R create mode 100644 R/DBClient-dbCollect.R create mode 100644 R/DBClient-dbQuery.R create mode 100644 R/DBClient-dbSchema.R create mode 100644 R/DBClient-dbTable.R create mode 100644 R/DBClient-dbTransaction.R delete mode 100644 R/DBClientClass/Initialize.R delete mode 100644 R/DBClientClass/dbAction.R delete mode 100644 R/DBClientClass/dbCollect.R delete mode 100644 R/DBClientClass/dbQuery.R delete mode 100644 R/DBClientClass/dbSchema.R delete mode 100644 R/DBClientClass/dbTable.R delete mode 100644 R/DBClientClass/dbTransaction.R create mode 100644 R/DBConnection-Configure.R rename R/{DBConnectionClass/Connect.R => DBConnection-Connect.R} (51%) create mode 100644 R/DBConnection-Initialize.R create mode 100644 R/DBConnection-Transaction.R delete mode 100644 R/DBConnectionClass/Configure.R delete mode 100644 R/DBConnectionClass/Transaction.R create mode 100644 R/aaa.R create mode 100644 man/DBClient-cash-collectGeometries.Rd create mode 100644 man/DBClient-cash-collectOrReturn.Rd create mode 100644 man/DBClient-cash-dbAction.Rd create mode 100644 man/DBClient-cash-dbQuery.Rd create mode 100644 man/DBClient-cash-dplyrWithSchema.Rd create mode 100644 man/DBClient-cash-getConnection.Rd create mode 100644 man/DBClient-cash-initialize.Rd create mode 100644 man/DBClient-cash-table.Rd create mode 100644 man/DBClient-cash-withSchema.Rd create mode 100644 man/DBClient-cash-withTransaction.Rd create mode 100644 man/DBConnection-cash-beginTransaction.Rd create mode 100644 man/DBConnection-cash-commitTransaction.Rd create mode 100644 man/DBConnection-cash-configure.Rd create mode 100644 man/DBConnection-cash-connect.Rd create mode 100644 man/DBConnection-cash-disconnect.Rd create mode 100644 man/DBConnection-cash-disconnectOnSessionEnd.Rd create mode 100644 man/DBConnection-cash-getConfiguration.Rd create mode 100644 man/DBConnection-cash-getConnection.Rd create mode 100644 man/DBConnection-cash-getToken.Rd create mode 100644 man/DBConnection-cash-initialize.Rd create mode 100644 man/DBConnection-cash-isConnected.Rd create mode 100644 man/DBConnection-cash-loadConfiguration.Rd create mode 100644 man/DBConnection-cash-rollbackTransaction.Rd create mode 100644 man/dot-configureDefault.Rd diff --git a/DESCRIPTION b/DESCRIPTION index 57e6101..b73863e 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -14,6 +14,9 @@ Imports: dbplyr, dplyr, R6, + rlang, tibble -Suggests: +Suggests: + RMariaDB, + RPostgres, sf diff --git a/NAMESPACE b/NAMESPACE index 250d92d..ea1e206 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -2,3 +2,7 @@ export(DBClient) export(DBConnection) +import(R6) +importFrom(R6,R6Class) +importFrom(rlang,":=") +importFrom(rlang,.data) diff --git a/R/DBClientClass/CRUDKeys.R b/R/DBClient-CRUDKeys.R similarity index 79% rename from R/DBClientClass/CRUDKeys.R rename to R/DBClient-CRUDKeys.R index b7a9fa3..532e7f8 100644 --- a/R/DBClientClass/CRUDKeys.R +++ b/R/DBClient-CRUDKeys.R @@ -1,18 +1,14 @@ keyExists <- function(table_name, key_name, id) { key <- dbplyr::ident(key_name) - res <- self$table(table_name) %>% - dplyr::filter(key == !!id) %>% - dplyr::count() %>% - dplyr::pull(n) - return(res[[1]] > 0) + tbl <- self$table(table_name) + res <- dplyr::pull(dplyr::count(dplyr::filter(tbl, key == !!id)), .data$n) + res[[1]] > 0 } selectByKey <- function(table_name, key_name, id) { key <- dbplyr::ident(key_name) - res <- self$table(table_name) %>% - dplyr::filter(key %in% !!id) %>% - dplyr::collect() - return(res) + tbl <- self$table(table_name) + dplyr::collect(dplyr::filter(tbl, key %in% !!id)) } insertByKey <- function(table_name, key_name, id, ...) { diff --git a/R/DBClientClass/CRUDRow.R b/R/DBClient-CRUDRow.R similarity index 100% rename from R/DBClientClass/CRUDRow.R rename to R/DBClient-CRUDRow.R diff --git a/R/DBClientClass/CRUDRows.R b/R/DBClient-CRUDRows.R similarity index 94% rename from R/DBClientClass/CRUDRows.R rename to R/DBClient-CRUDRows.R index 96e8173..7cef5f4 100644 --- a/R/DBClientClass/CRUDRows.R +++ b/R/DBClient-CRUDRows.R @@ -75,13 +75,10 @@ upsertRows <- function(table_name, rows, key_name = NULL) { # NOTE: Temporary fix for issue: tidyverse/dbplyr#1279 if ("MariaDBConnection" %in% class(private$client$getConnection())) { - keys <- rows %>% - dplyr::pull(!!key_name) - n_rows <- tbl %>% - dplyr::select(!!key_name) %>% - dplyr::filter(!!as.symbol(key_name) %in% !!keys) %>% - dplyr::count() %>% - dplyr::pull(n) + keys <- dplyr::pull(rows, !!key_name) + tbl_keys <- dplyr::select(tbl, !!key_name) + filt_rows <- dplyr::filter(tbl_keys, !!as.symbol(key_name) %in% !!keys) + n_rows <- dplyr::pull(dplyr::count(filt_rows), .data$n) if (n_rows == 0) { return(insertRows(table_name, rows, key_name)) } else { diff --git a/R/DBClient-Initialize.R b/R/DBClient-Initialize.R new file mode 100644 index 0000000..623e4ee --- /dev/null +++ b/R/DBClient-Initialize.R @@ -0,0 +1,29 @@ +conn <- NULL + +#' @name DBClient$initialize +#' @title DBClient$initialize +#' @description +#' Initalises the DBClient instance with a DBConnection +#' @param conn An instance of DBConnection +NULL +initialize <- function(conn) { + if (is.null(conn) || !R6::is.R6(conn) || !inherits(conn, "DBConnection")) { + stop("DBConnection instance required!") + } + private$conn <- conn +} + + +#' @name DBClient$getConnection +#' @title DBClient$getConnection +#' @description +#' Returns the DBConnection instance for this client +NULL +getConnection <- function() { + private$conn +} + + +.getDBConn <- function() { + private$conn$getConnection() +} diff --git a/R/DBClient-dbAction.R b/R/DBClient-dbAction.R new file mode 100644 index 0000000..bb29157 --- /dev/null +++ b/R/DBClient-dbAction.R @@ -0,0 +1,14 @@ + +#' @name DBClient$dbAction +#' @title DBClient$dbAction +#' @description +#' Executes a query/statement on a database which has no result +#' @param statement Statement to run +#' @returns TRUE if the execution was successful, FALSE if not +NULL +dbAction <- function(statement) { + if (getOption("db.debug", FALSE)) { + cat("dbQuery:", statement, "\n") + } + DBI::dbExecute(private$getConn(), statement) +} diff --git a/R/DBClient-dbCollect.R b/R/DBClient-dbCollect.R new file mode 100644 index 0000000..b912afb --- /dev/null +++ b/R/DBClient-dbCollect.R @@ -0,0 +1,84 @@ + +#' @name DBClient$collectOrReturn +#' @title DBClient$collectOrReturn +#' @description +#' Utility method which returns the query and optionally runs dplyr::collect on +#' it first. +#' @param qry A dplyr query +#' @param collect Collect the query. Defaults to checking parent frame +#' for the parameter value +#' @returns The query, optionally collect'd +NULL +collectOrReturn <- function( + qry, collect = get("collect", pos = parent.frame()) +) { + res <- qry + if (length(collect) == 1 && collect[[1]] == TRUE) { + res <- dplyr::collect(res) + } + res +} + + +#' @name DBClient$collectGeometries +#' @title DBClient$collectGeometries +#' @description +#' Requires sf package. +#' +#' Runs dplyr::collect on a query, If there are gemoetry columns (specified by +#' gemoetry_cols or named geom or named proj) then they are translated into sf +#' geometry columns using sf::st_as_sfc with a CRS. +#' +#' The default CRS for a column named `geom` is 4326. +#' The default CRS for a column named `proj` is 3035. +#' The default CRS for other geometry columns is specified by `geometry_crs` +#' +#' @param qry The query to collect +#' @param geometry_cols Any geometry columns to translate to sf +#' @param geometry_crs The CRS to assign to the columns. Either +#' specified by the CRS number or an sf::st_crs() object. +#' @param check_ring_dir Check the direction of rings during the +#' translation. See `sf::st_as_sfc` for further details. +#' @returns The collected query with translated geometry columns +NULL +collectGeometries <- function( + qry, geometry_cols = c("geometry"), geometry_crs = 4326, check_ring_dir = TRUE +) { + requireNamespace("sf") + + res <- dplyr::collect(qry) + + if (sum(geometry_cols %in% colnames(res)) > 0) { + res <- dplyr::mutate( + res, + dplyr::across(dplyr::any_of(geometry_cols), function(.col) { + sf::st_as_sfc( + .col, + EWKB = TRUE, + crs = geometry_crs, + check_ring_dir = check_ring_dir + ) + }) + ) + } + + if ("geom" %in% colnames(res) && sum("geom" %in% geometry_cols) == 0) { + res <- dplyr::mutate( + res, + geom = sf::st_as_sfc( + .data$geom, EWKB = TRUE, crs = 4326, check_ring_dir = check_ring_dir + ) + ) + } + + if ("proj" %in% colnames(res) && sum("proj" %in% geometry_cols) == 0) { + res <- dplyr::mutate( + res, + proj = sf::st_as_sfc( + .data$proj, EWKB = TRUE, crs = 3035, check_ring_dir = check_ring_dir + ) + ) + } + + res +} diff --git a/R/DBClient-dbQuery.R b/R/DBClient-dbQuery.R new file mode 100644 index 0000000..aea7a51 --- /dev/null +++ b/R/DBClient-dbQuery.R @@ -0,0 +1,17 @@ + +#' @name DBClient$dbQuery +#' @title DBClient$dbQuery +#' @description +#' Runs a query (statement) on a database and returns the results +#' @param statement Query to run on the database +#' @returns A lazy data table with the results from the query +NULL +dbQuery <- function(statement) { + if (getOption("db.debug", FALSE)) { + cat("dbQuery:", statement, "\n") + } + query <- DBI::dbSendQuery(private$getConn(), statement) + res <- DBI::dbFetch(query) + DBI::dbClearResult(query) + res +} diff --git a/R/DBClient-dbSchema.R b/R/DBClient-dbSchema.R new file mode 100644 index 0000000..7d2f60e --- /dev/null +++ b/R/DBClient-dbSchema.R @@ -0,0 +1,39 @@ + +#' @name DBClient$withSchema +#' @title DBClient$withSchema +#' @description +#' Prefixes schema to the table name, if set in the configuration. +#' @param table_name Table name to prefix schema to +#' @returns Table name with prefixed schema +NULL +withSchema <- function(table_name) { + config <- private$conn$getConfiguration() + if (exists("schema", config)) { + return(DBI::dbQuoteIdentifier( + private$getConn(), DBI::Id(schema = config$schema, table = table_name) + )) + } else { + return(DBI::dbQuoteIdentifier( + private$getConn(), DBI::Id(table = table_name) + )) + } +} + + +#' @name DBClient$dplyrWithSchema +#' @title DBClient$dplyrWithSchema +#' @description +#' Prefixes schema to the table name, if set in the configuration. Specifically +#' uses the dbplyr::in_schema method rather than the direct +#' DBI::dbQuoteIdentifier method. +#' @param table_name Table name to prefix schema to +#' @returns Table name with prefixed schema +NULL +dplyrWithSchema <- function(table_name) { + config <- private$conn$getConfiguration() + if (exists("schema", config) && !is.null(config$schema)) { + return(dbplyr::in_schema(config$schema, table_name)) + } else { + return(table_name) + } +} diff --git a/R/DBClient-dbTable.R b/R/DBClient-dbTable.R new file mode 100644 index 0000000..e0872a3 --- /dev/null +++ b/R/DBClient-dbTable.R @@ -0,0 +1,14 @@ + +#' @name DBClient$table +#' @title DBClient$table +#' @description +#' Creates a `dplyr::tbl` instantiation with the current database connection. +#' @param table_name The database table name +#' @returns A dplyr::tbl instance +NULL +table <- function(table_name) { + if (getOption("db.debug", FALSE)) { + cat("dbTable:", table_name, "\n") + } + dplyr::tbl(private$getConn(), self$dplyrWithSchema(table_name)) +} diff --git a/R/DBClient-dbTransaction.R b/R/DBClient-dbTransaction.R new file mode 100644 index 0000000..12ba17b --- /dev/null +++ b/R/DBClient-dbTransaction.R @@ -0,0 +1,15 @@ + +#' @name DBClient$withTransaction +#' @title DBClient$withTransaction +#' @description +#' Runs a given expression within a database transaction. +#' @param expr Expression to run in a transaction +#' @returns TRUE if transaction was successful +NULL +withTransaction <- function(expr) { + private$conn$beginTransaction() + on.exit(private$conn$rollbackTransaction()) + expr + on.exit() + private$conn$commitTransaction() +} diff --git a/R/DBClient.R b/R/DBClient.R index a717c75..15f5b08 100644 --- a/R/DBClient.R +++ b/R/DBClient.R @@ -1,13 +1,12 @@ class_env <- new.env(parent = baseenv()) -res <- lapply(list.files("R/DBClientClass", pattern = ".R"), function(fn) { - eval( - parse(paste0("R/DBClientClass/", fn)), - envir = class_env, - enclos = emptyenv() - ) -}) +res <- lapply( + list.files("R", pattern = "DBClient-.*\\.R"), + function(fn) { + eval(parse(paste0("R/", fn)), envir = class_env, enclos = emptyenv()) + } +) fn_names <- names(which(vapply(class_env, is.function, logical(1)))) fn_list <- lapply(fn_names, function(fn_name) { diff --git a/R/DBClientClass/Initialize.R b/R/DBClientClass/Initialize.R deleted file mode 100644 index 1e54499..0000000 --- a/R/DBClientClass/Initialize.R +++ /dev/null @@ -1,8 +0,0 @@ -client <- NULL - -Initialize <- function(client) { - if (is.null(client) || !is.R6(client) || inherits(client, "DBClient")) { - stop("DBClient instance required!") - } - private$client <- client -} diff --git a/R/DBClientClass/dbAction.R b/R/DBClientClass/dbAction.R deleted file mode 100644 index 5673afb..0000000 --- a/R/DBClientClass/dbAction.R +++ /dev/null @@ -1,6 +0,0 @@ -dbAction <- function(statement) { - if (getOption("db.debug", FALSE)) { - cat("dbQuery:", statement, "\n") - } - DBI::dbExecute(private$client$getConnection(), statement) -} diff --git a/R/DBClientClass/dbCollect.R b/R/DBClientClass/dbCollect.R deleted file mode 100644 index 07b6edf..0000000 --- a/R/DBClientClass/dbCollect.R +++ /dev/null @@ -1,52 +0,0 @@ -collectOrReturn <- function( - qry, collect = get("collect", pos = parent.frame()) -) { - if (length(collect) == 1 && collect[[1]] == TRUE) { - return(dplyr::collect(qry)) - } else { - return(qry) - } -} - -collectGeometries <- function( - qry, geometry_cols = c("geometry"), geometry_crs = 4326, check_ring_dir = TRUE -) { - requireNamespace("sf") - - qry <- qry %>% - dplyr::collect() - - if (sum(geometry_cols %in% colnames(qry)) > 0) { - qry <- qry %>% - dplyr::mutate( - dplyr::across(dplyr::any_of(geometry_cols), function(.col) { - sf::st_as_sfc( - .col, - EWKB = TRUE, - crs = geometry_crs, - check_ring_dir = check_ring_dir - ) - }) - ) - } - - if ("geom" %in% colnames(qry) && sum("geom" %in% geometry_cols) == 0) { - qry <- qry %>% - dplyr::mutate( - geom = sf::st_as_sfc( - geom, EWKB = TRUE, crs = 4326, check_ring_dir = check_ring_dir - ) - ) - } - - if ("proj" %in% colnames(qry) && sum("proj" %in% geometry_cols) == 0) { - qry <- qry %>% - dplyr::mutate( - proj = sf::st_as_sfc( - proj, EWKB = TRUE, crs = 3035, check_ring_dir = check_ring_dir - ) - ) - } - - return(qry) -} diff --git a/R/DBClientClass/dbQuery.R b/R/DBClientClass/dbQuery.R deleted file mode 100644 index 24a3be7..0000000 --- a/R/DBClientClass/dbQuery.R +++ /dev/null @@ -1,9 +0,0 @@ -dbQuery <- function(statement) { - if (getOption("db.debug", FALSE)) { - cat("dbQuery:", statement, "\n") - } - query <- DBI::dbSendQuery(private$client$getConnection(), statement) - res <- DBI::dbFetch(query) - DBI::dbClearResult(query) - res -} diff --git a/R/DBClientClass/dbSchema.R b/R/DBClientClass/dbSchema.R deleted file mode 100644 index 7572774..0000000 --- a/R/DBClientClass/dbSchema.R +++ /dev/null @@ -1,21 +0,0 @@ -withSchema <- function(table_name) { - config <- private$client$getConfiguration() - if (exists("schema", config)) { - return(DBI::dbQuoteIdentifier( - getConnection(), DBI::Id(schema = config$schema, table = table_name) - )) - } else { - return(DBI::dbQuoteIdentifier( - getConnection(), DBI::Id(table = table_name) - )) - } -} - -dplyrWithSchema <- function(table_name) { - config <- private$client$getConfiguration() - if (exists("schema", config) && !is.null(config$schema)) { - return(dbplyr::in_schema(config$schema, table_name)) - } else { - return(table_name) - } -} diff --git a/R/DBClientClass/dbTable.R b/R/DBClientClass/dbTable.R deleted file mode 100644 index e33e54a..0000000 --- a/R/DBClientClass/dbTable.R +++ /dev/null @@ -1,7 +0,0 @@ - -table <- function(table_name) { - if (getOption("db.debug", FALSE)) { - cat("dbTable:", table_name, "\n") - } - dplyr::tbl(private$client$getConnection(), self$dplyrWithSchema(table_name)) -} diff --git a/R/DBClientClass/dbTransaction.R b/R/DBClientClass/dbTransaction.R deleted file mode 100644 index 9786cca..0000000 --- a/R/DBClientClass/dbTransaction.R +++ /dev/null @@ -1,8 +0,0 @@ - -withTransaction <- function(expr) { - private$client$beginTransaction() - on.exit(private$client$rollbackTransaction()) - expr - on.exit() - private$client$commitTransaction() -} diff --git a/R/DBConnection-Configure.R b/R/DBConnection-Configure.R new file mode 100644 index 0000000..add7ac4 --- /dev/null +++ b/R/DBConnection-Configure.R @@ -0,0 +1,141 @@ +config <- NULL + +.configureList <- function(db) { + was_connected <- FALSE + if (!is.null(private$conn)) { + was_connected <- TRUE + self$disconnect() + } + + private$config <- db + + if (was_connected) { + self$connect() + } + + invisible(private$config) +} + +#' Configures the DBClient instance. +#' +#' There are 2 options for configuration, either: pass in a named list; or use +#' named parameters. +#' +#' @param engine The database engine (mysql, postgres) as supported by DBI +#' @param host The database host +#' @param port The database port +#' @param user The database user to connect with +#' @param password The database password to connect with +#' @param dbname The database to use +#' @param schema The database schema to use (postgres) +#' @param autoconnect Autoconnect to the database if not already connected +#' @param name The database to use (alternative to dbname) +#' +#' @returns The configuration accepted +.configureDefault <- function( + host, port, user, password, dbname, + engine = NULL, name = NULL, schema = NULL, autoconnect = NULL +) { + if (is.null(autoconnect)) { + autoconnect <- getOption("db.autoconnect", FALSE) + } + private$configureList(list( + engine = engine, + host = host, + port = port, + user = user, + password = password, + name = ifelse(is.null(name), dbname, name), + schema = schema, + autoconnect = autoconnect + )) +} + +#' @name DBConnection$configure +#' @title DBConnection$configure +#' @description +#' Configures the DBClient instance. +#' +#' There are 2 options for configuration, either: pass in a named list; or use +#' named parameters. +#' +#' @param ... Named parameters for configuring the connection or a name list of +#' parameters +#' +#' @returns The configuration that has loaded +NULL +configure <- function(...) { + args <- list(...) + if (sum("list" %in% class(args[[1]])) > 0) { + private$configureList(args[[1]]) + } else { + do.call(private$configureDefault, args) + } +} + + +#' @name DBConnection$getConfiguration +#' @title DBConnection$getConfiguration +#' @description +#' Get the current configuration of the DBClient instance. +#' @returns Current configuration. If not configured, NULL is returned +NULL +getConfiguration <- function() { + private$config +} + + +.loadConfigurationGlobalR <- function() { + if (!file.exists("global.R")) { + return(FALSE) + } + e <- new.env(parent = baseenv()) + eval(parse("global.R"), envir = e) + if ("db" %in% ls(e)) { + self$configure(e$db) + } else if ("global" %in% ls(e)) { + self$configure(e$global$db) + } else { + return(FALSE) + } + TRUE +} + +.loadConfigurationEnvR <- function() { + if (!file.exists(".env.R")) { + return(FALSE) + } + e <- new.env(parent = baseenv()) + eval(parse(".env.R"), envir = e) + if ("db" %in% ls(e)) { + self$configure(e$db) + } else if ("global" %in% ls(e)) { + self$configure(e$global$db) + } else { + return(FALSE) + } + TRUE +} + +#' @name DBConnection$loadConfiguration +#' @title DBConnection$loadConfiguration +#' @description +#' Attempts to load the configuration either from global.R or .env.R. +#' @param warn Emit a warning with which file it attempting to be loaded +#' @returns TRUE if configuration was successful, FALSE otherwise +NULL +loadConfiguration <- function(warn = FALSE) { + if (file.exists("global.R")) { + if (warn != FALSE) { + warning("Attempting to load default configuration from global.R") + } + private$loadConfigurationGlobalR() + } else if (file.exists(".env.R")) { + if (warn != FALSE) { + warning("Attempting to load default configuration from .env.R") + } + private$loadConfigurationEnvR() + } else { + FALSE + } +} diff --git a/R/DBConnectionClass/Connect.R b/R/DBConnection-Connect.R similarity index 51% rename from R/DBConnectionClass/Connect.R rename to R/DBConnection-Connect.R index 644bd9e..16642ac 100644 --- a/R/DBConnectionClass/Connect.R +++ b/R/DBConnection-Connect.R @@ -1,18 +1,16 @@ conn <- NULL + +#' @name DBConnection$connect +#' @title DBConnection$connect +#' @description +#' Attempts to connect to the database. If not already configured, will first +#' attempt to load the configuration. +#' @returns TRUE if connection was success, FALSE if it failed +NULL connect <- function() { if (is.null(private$config)) { - if (!file.exists("global.R")) { - return(FALSE) - } - warning("Attempting to load default configuration from global.R") - e <- new.env(parent = baseenv()) - eval(parse("global.R"), envir = e) - if ("db" %in% ls(e)) { - self$configure(e$db) - } else if ("global" %in% ls(e)) { - self$configure(e$global$db) - } else { + if (!self$loadConfiguration(warn = TRUE)) { return(FALSE) } } @@ -54,9 +52,17 @@ connect <- function() { } ) - return(res) + invisible(res) } + +#' @name DBConnection$getConnection +#' @title DBConnection$getConnection +#' @param do_connect Forcibly connect if not already connected +#' @description +#' Returns the DBConnection instance for this client. +#' @returns DBConnection instance +NULL getConnection <- function(do_connect = NULL) { if (!self$isConnected()) { if ((!is.null(do_connect) && do_connect) @@ -66,20 +72,43 @@ getConnection <- function(do_connect = NULL) { warning("Database is not connected. Caller of getConnection() may error.") } } - return(private$conn) + private$conn } + +#' @name DBConnection$isConnected +#' @title DBConnection$isConnected +#' @description +#' Indicates if the instance has an active database connection. +#' @returns TRUE if connected to the database, FALSE otherwise +NULL isConnected <- function() { - return(!is.null(private$conn)) + !is.null(private$conn) } + +#' @name DBConnection$disconnect +#' @title DBConnection$disconnect +#' @description +#' Disconnects the current database connection, if connected. +NULL disconnect <- function() { if (!is.null(private$conn)) { DBI::dbDisconnect(private$conn) private$conn <- NULL } + invisible(NULL) } + +#' @name DBConnection$disconnectOnSessionEnd +#' @title DBConnection$disconnectOnSessionEnd +#' @param session The R Shiny session to register the callback with +#' @description +#' Registers a callback with an R Shiny session so that the connection is +#' disconnected once the session ends. +#' @returns A function to cancel the onSessionEnded callback +NULL disconnectOnSessionEnd <- function(session) { session$onSessionEnded(self$disconnect) } diff --git a/R/DBConnection-Initialize.R b/R/DBConnection-Initialize.R new file mode 100644 index 0000000..d174bfc --- /dev/null +++ b/R/DBConnection-Initialize.R @@ -0,0 +1,22 @@ +token <- NULL + + +#' @name DBConnection$initialize +#' @title DBConnection$initialize +#' @aliases DBConnection$initialize +#' @description +#' Initalises the DBConnection instance with an identity token. +NULL +initialize <- function() { + private$token <- paste0(collapse = "", sample(c(LETTERS, 0:9), 16, TRUE)) +} + + +#' @name DBConnection$getToken +#' @title DBConnection$getToken +#' @description +#' Returns the identity token for this DBConnection instance. +NULL +getToken <- function() { + private$token +} diff --git a/R/DBConnection-Transaction.R b/R/DBConnection-Transaction.R new file mode 100644 index 0000000..6abf408 --- /dev/null +++ b/R/DBConnection-Transaction.R @@ -0,0 +1,69 @@ +transactionLevel <- 0 + + +#' @name DBConnection$beginTransaction +#' @title DBConnection$beginTransaction +#' @param session The R Shiny session to register the callback with +#' @description +#' Registers a callback with an R Shiny session so that the connection is +#' disconnected once the session ends. +#' @returns A function to cancel the onSessionEnded callback +NULL +beginTransaction <- function(allowRecursive = FALSE, quietly = FALSE) { + if (private$transactionLevel > 0) { + if (allowRecursive == FALSE) { + if (quietly != FALSE) { + stop("Transaction already started. Aborting.") + } + return(FALSE) + } + } + private$transactionLevel <- private$transactionLevel + 1 + DBI::dbBegin(self$getConnection()) +} + + +#' @name DBConnection$rollbackTransaction +#' @title DBConnection$rollbackTransaction +#' @param session The R Shiny session to register the callback with +#' @description +#' Registers a callback with an R Shiny session so that the connection is +#' disconnected once the session ends. +#' @returns A function to cancel the onSessionEnded callback +NULL +rollbackTransaction <- function(quietly = FALSE) { + if (private$transactionLevel == 0) { + if (quietly != FALSE) { + stop("No transaction to rollback. Aborting.") + } + return(FALSE) + } + DBI::dbRollback(self$getConnection()) +} + + +#' @name DBConnection$commitTransaction +#' @title DBConnection$commitTransaction +#' @param quietly If there is no transaction, will cause a `stop()` +#' if TRUE +#' @description +#' Commits the current database transaction. If nested, will not actually commit +#' until the last level is committed. +#' +#' If `rollbackTransaction()` +#' +#' @returns A function to cancel the onSessionEnded callback +NULL +commitTransaction <- function(quietly = FALSE) { + if (private$transactionLevel == 0) { + if (quietly != FALSE) { + stop("No transaction to commit. Aborting.") + } + return(FALSE) + } + private$transactionLevel <- private$transactionLevel - 1 + if (transactionLevel > 0) { + return(TRUE) + } + DBI::dbRollback(self$getConnection()) +} diff --git a/R/DBConnection.R b/R/DBConnection.R index 1985258..3cc22ff 100644 --- a/R/DBConnection.R +++ b/R/DBConnection.R @@ -1,13 +1,12 @@ class_env <- new.env(parent = baseenv()) -res <- lapply(list.files("R/DBConnectionClass", pattern = ".R"), function(fn) { - eval( - parse(paste0("R/DBConnectionClass/", fn)), - envir = class_env, - enclos = emptyenv() - ) -}) +res <- lapply( + list.files("R", pattern = "DBConnection-.*\\.R"), + function(fn) { + eval(parse(paste0("R/", fn)), envir = class_env, enclos = emptyenv()) + } +) fn_names <- names(which(vapply(class_env, is.function, logical(1)))) fn_list <- lapply(fn_names, function(fn_name) { @@ -30,7 +29,8 @@ names(member_list) <- member_names #' @name DBConnection #' @title DBConnection -#' @description Database Connection class +#' @description +#' Database Connection class. NULL #' @export diff --git a/R/DBConnectionClass/Configure.R b/R/DBConnectionClass/Configure.R deleted file mode 100644 index 1df0141..0000000 --- a/R/DBConnectionClass/Configure.R +++ /dev/null @@ -1,68 +0,0 @@ -config <- NULL - -.configureList <- function(db) { - was_connected <- FALSE - if (!is.null(private$conn)) { - was_connected <- TRUE - disconnect() - } - - private$config <- db - - if (was_connected) { - connect() - } - - return(invisible(private$config)) -} - -.configureDefault <- function( - host, port, user, password, dbname, - engine = NULL, name = NULL, schema = NULL, autoconnect = NULL -) { - if (is.null(autoconnect)) { - autoconnect <- getOption("db.autoconnect", FALSE) - } - private$configureList(list( - engine = engine, - host = host, - port = port, - user = user, - password = password, - name = ifelse(is.null(name), dbname, name), - schema = schema, - autoconnect = autoconnect - )) -} - - -#' Configures the DBClient instance. -#' -#' There are 2 options for configuration, either: pass in a named list; or use -#' named parameters. The required elements are: -#' -#' @param ... Named parameters for configuring the connection or a name list of -#' parameters -#' @param engine The database engine (mysql, postgres) as supported by DBI -#' @param host The database host -#' @param port The database port -#' @param user The database user to connect with -#' @param password The database password to connect with -#' @param dbname The database to use -#' @param schema The database schema to use (postgres) -#' @param autoconnect Autoconnect to the database if not already connected -#' -#' @return The configuration accepted -configure <- function(...) { - args <- list(...) - if (sum("list" %in% class(args[[1]])) > 0) { - private$configureList(args[[1]]) - } else { - do.call(private$configureDefault, args) - } -} - -#' Get the current configuration of the DBClient instance. -getConfiguration <- function() { - private$config -} diff --git a/R/DBConnectionClass/Transaction.R b/R/DBConnectionClass/Transaction.R deleted file mode 100644 index af6a1de..0000000 --- a/R/DBConnectionClass/Transaction.R +++ /dev/null @@ -1,39 +0,0 @@ - -transactionLevel <- 0 - -beginTransaction <- function(allowRecursive = FALSE, quietly = FALSE) { - if (private$transactionLevel > 0) { - if (allowRecursive == FALSE) { - if (quietly != FALSE) { - stop("Transaction already started. Aborting.") - } - return(FALSE) - } - } - private$transactionLevel <- private$transactionLevel + 1 - DBI::dbBegin(self$getConnection()) -} - -rollbackTransaction <- function(quietly = FALSE) { - if (private$transactionLevel == 0) { - if (quietly != FALSE) { - stop("No transaction to rollback. Aborting.") - } - return(FALSE) - } - DB::dbRollback(self$getConnection()) -} - -commitTransaction <- function(quietly = FALSE) { - if (private$transactionLevel == 0) { - if (quietly != FALSE) { - stop("No transaction to commit. Aborting.") - } - return(FALSE) - } - private$transactionLevel <- private$transactionLevel - 1 - if (transactionLevel > 0) { - return(TRUE) - } - DB::dbRollback(self$getConnection()) -} diff --git a/R/PrintDebug.R b/R/PrintDebug.R index 4d4b32e..4b3ae8f 100644 --- a/R/PrintDebug.R +++ b/R/PrintDebug.R @@ -79,19 +79,14 @@ printDebug <- function(fn_name, table, key, tbl, rows, query_only = FALSE) { } return_keys <- NULL - if (key %in% colnames(rows)) { - return_keys <- rows %>% - dplyr::select(key) - } else { - return_keys <- rows %>% - dplyr::bind_rows(tibble::tibble(!!key := character(0))) %>% - dplyr::mutate( - !!key := dplyr::coalesce( - !!!dplyr::sym(key), - paste0("<>") - ) - ) %>% - dplyr::select(key) + if (!(key %in% colnames(rows))) { + rows <- dplyr::mutate( + dplyr::bind_rows(rows, tibble::tibble(!!key := character(0))), + !!key := dplyr::coalesce( + !!!dplyr::sym(key), + paste0("<>") + ) + ) } - return(invisible(return_keys)) + invisible(dplyr::select(rows, key)) } diff --git a/R/aaa.R b/R/aaa.R new file mode 100644 index 0000000..99875c5 --- /dev/null +++ b/R/aaa.R @@ -0,0 +1,8 @@ +if(getRversion() >= "2.15.1") utils::globalVariables(c("self")) +if(getRversion() >= "2.15.1") utils::globalVariables(c("private")) + +#' @importFrom rlang := +#' @importFrom rlang .data +#' @importFrom R6 R6Class +#' @import R6 +NULL diff --git a/man/DBClient-cash-collectGeometries.Rd b/man/DBClient-cash-collectGeometries.Rd new file mode 100644 index 0000000..9a7d965 --- /dev/null +++ b/man/DBClient-cash-collectGeometries.Rd @@ -0,0 +1,30 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/DBClient-dbCollect.R +\name{DBClient$collectGeometries} +\alias{DBClient$collectGeometries} +\title{DBClient$collectGeometries} +\arguments{ +\item{qry}{\if{html}{\out{}} The query to collect} + +\item{geometry_cols}{\if{html}{\out{}} Any geometry columns to translate to sf} + +\item{geometry_crs}{\if{html}{\out{}} The CRS to assign to the columns. Either +specified by the CRS number or an sf::st_crs() object.} + +\item{check_ring_dir}{\if{html}{\out{}} Check the direction of rings during the +translation. See \code{sf::st_as_sfc} for further details.} +} +\value{ +\if{html}{\out{}} The collected query with translated geometry columns +} +\description{ +Requires sf package. + +Runs dplyr::collect on a query, If there are gemoetry columns (specified by +gemoetry_cols or named geom or named proj) then they are translated into sf +geometry columns using sf::st_as_sfc with a CRS. + +The default CRS for a column named \code{geom} is 4326. +The default CRS for a column named \code{proj} is 3035. +The default CRS for other geometry columns is specified by \code{geometry_crs} +} diff --git a/man/DBClient-cash-collectOrReturn.Rd b/man/DBClient-cash-collectOrReturn.Rd new file mode 100644 index 0000000..1a6b7c5 --- /dev/null +++ b/man/DBClient-cash-collectOrReturn.Rd @@ -0,0 +1,18 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/DBClient-dbCollect.R +\name{DBClient$collectOrReturn} +\alias{DBClient$collectOrReturn} +\title{DBClient$collectOrReturn} +\arguments{ +\item{qry}{\if{html}{\out{}} A dplyr query} + +\item{collect}{\if{html}{\out{}} Collect the query. Defaults to checking parent frame +for the parameter value} +} +\value{ +\if{html}{\out{}} The query, optionally collect'd +} +\description{ +Utility method which returns the query and optionally runs dplyr::collect on +it first. +} diff --git a/man/DBClient-cash-dbAction.Rd b/man/DBClient-cash-dbAction.Rd new file mode 100644 index 0000000..39f8f02 --- /dev/null +++ b/man/DBClient-cash-dbAction.Rd @@ -0,0 +1,14 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/DBClient-dbAction.R +\name{DBClient$dbAction} +\alias{DBClient$dbAction} +\title{DBClient$dbAction} +\arguments{ +\item{statement}{\if{html}{\out{}} Statement to run} +} +\value{ +\if{html}{\out{}} TRUE if the execution was successful, FALSE if not +} +\description{ +Executes a query/statement on a database which has no result +} diff --git a/man/DBClient-cash-dbQuery.Rd b/man/DBClient-cash-dbQuery.Rd new file mode 100644 index 0000000..510d0eb --- /dev/null +++ b/man/DBClient-cash-dbQuery.Rd @@ -0,0 +1,14 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/DBClient-dbQuery.R +\name{DBClient$dbQuery} +\alias{DBClient$dbQuery} +\title{DBClient$dbQuery} +\arguments{ +\item{statement}{\if{html}{\out{}} Query to run on the database} +} +\value{ +\if{html}{\out{}} A lazy data table with the results from the query +} +\description{ +Runs a query (statement) on a database and returns the results +} diff --git a/man/DBClient-cash-dplyrWithSchema.Rd b/man/DBClient-cash-dplyrWithSchema.Rd new file mode 100644 index 0000000..c236446 --- /dev/null +++ b/man/DBClient-cash-dplyrWithSchema.Rd @@ -0,0 +1,16 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/DBClient-dbSchema.R +\name{DBClient$dplyrWithSchema} +\alias{DBClient$dplyrWithSchema} +\title{DBClient$dplyrWithSchema} +\arguments{ +\item{table_name}{\if{html}{\out{}} Table name to prefix schema to} +} +\value{ +\if{html}{\out{}} Table name with prefixed schema +} +\description{ +Prefixes schema to the table name, if set in the configuration. Specifically +uses the dbplyr::in_schema method rather than the direct +DBI::dbQuoteIdentifier method. +} diff --git a/man/DBClient-cash-getConnection.Rd b/man/DBClient-cash-getConnection.Rd new file mode 100644 index 0000000..8d53d75 --- /dev/null +++ b/man/DBClient-cash-getConnection.Rd @@ -0,0 +1,8 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/DBClient-Initialize.R +\name{DBClient$getConnection} +\alias{DBClient$getConnection} +\title{DBClient$getConnection} +\description{ +Returns the DBConnection instance for this client +} diff --git a/man/DBClient-cash-initialize.Rd b/man/DBClient-cash-initialize.Rd new file mode 100644 index 0000000..77ebbe4 --- /dev/null +++ b/man/DBClient-cash-initialize.Rd @@ -0,0 +1,11 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/DBClient-Initialize.R +\name{DBClient$initialize} +\alias{DBClient$initialize} +\title{DBClient$initialize} +\arguments{ +\item{conn}{\if{html}{\out{}} An instance of DBConnection} +} +\description{ +Initalises the DBClient instance with a DBConnection +} diff --git a/man/DBClient-cash-table.Rd b/man/DBClient-cash-table.Rd new file mode 100644 index 0000000..c215ee2 --- /dev/null +++ b/man/DBClient-cash-table.Rd @@ -0,0 +1,14 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/DBClient-dbTable.R +\name{DBClient$table} +\alias{DBClient$table} +\title{DBClient$table} +\arguments{ +\item{table_name}{\if{html}{\out{}} The database table name} +} +\value{ +\if{html}{\out{}} A dplyr::tbl instance +} +\description{ +Creates a \code{dplyr::tbl} instantiation with the current database connection. +} diff --git a/man/DBClient-cash-withSchema.Rd b/man/DBClient-cash-withSchema.Rd new file mode 100644 index 0000000..6be5c13 --- /dev/null +++ b/man/DBClient-cash-withSchema.Rd @@ -0,0 +1,14 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/DBClient-dbSchema.R +\name{DBClient$withSchema} +\alias{DBClient$withSchema} +\title{DBClient$withSchema} +\arguments{ +\item{table_name}{\if{html}{\out{}} Table name to prefix schema to} +} +\value{ +\if{html}{\out{}} Table name with prefixed schema +} +\description{ +Prefixes schema to the table name, if set in the configuration. +} diff --git a/man/DBClient-cash-withTransaction.Rd b/man/DBClient-cash-withTransaction.Rd new file mode 100644 index 0000000..e8bca89 --- /dev/null +++ b/man/DBClient-cash-withTransaction.Rd @@ -0,0 +1,14 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/DBClient-dbTransaction.R +\name{DBClient$withTransaction} +\alias{DBClient$withTransaction} +\title{DBClient$withTransaction} +\arguments{ +\item{expr}{\if{html}{\out{}} Expression to run in a transaction} +} +\value{ +\if{html}{\out{}} TRUE if transaction was successful +} +\description{ +Runs a given expression within a database transaction. +} diff --git a/man/DBConnection-cash-beginTransaction.Rd b/man/DBConnection-cash-beginTransaction.Rd new file mode 100644 index 0000000..fbfa92e --- /dev/null +++ b/man/DBConnection-cash-beginTransaction.Rd @@ -0,0 +1,15 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/DBConnection-Transaction.R +\name{DBConnection$beginTransaction} +\alias{DBConnection$beginTransaction} +\title{DBConnection$beginTransaction} +\arguments{ +\item{session}{\if{html}{\out{}} The R Shiny session to register the callback with} +} +\value{ +\if{html}{\out{}} A function to cancel the onSessionEnded callback +} +\description{ +Registers a callback with an R Shiny session so that the connection is +disconnected once the session ends. +} diff --git a/man/DBConnection-cash-commitTransaction.Rd b/man/DBConnection-cash-commitTransaction.Rd new file mode 100644 index 0000000..94446b7 --- /dev/null +++ b/man/DBConnection-cash-commitTransaction.Rd @@ -0,0 +1,18 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/DBConnection-Transaction.R +\name{DBConnection$commitTransaction} +\alias{DBConnection$commitTransaction} +\title{DBConnection$commitTransaction} +\arguments{ +\item{quietly}{\if{html}{\out{}} If there is no transaction, will cause a \code{stop()} +if TRUE} +} +\value{ +\if{html}{\out{}} A function to cancel the onSessionEnded callback +} +\description{ +Commits the current database transaction. If nested, will not actually commit +until the last level is committed. + +If \code{rollbackTransaction()} +} diff --git a/man/DBConnection-cash-configure.Rd b/man/DBConnection-cash-configure.Rd new file mode 100644 index 0000000..65c8fc3 --- /dev/null +++ b/man/DBConnection-cash-configure.Rd @@ -0,0 +1,18 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/DBConnection-Configure.R +\name{DBConnection$configure} +\alias{DBConnection$configure} +\title{DBConnection$configure} +\arguments{ +\item{...}{Named parameters for configuring the connection or a name list of +parameters} +} +\value{ +\if{html}{\out{}} The configuration that has loaded +} +\description{ +Configures the DBClient instance. + +There are 2 options for configuration, either: pass in a named list; or use +named parameters. +} diff --git a/man/DBConnection-cash-connect.Rd b/man/DBConnection-cash-connect.Rd new file mode 100644 index 0000000..9c6dd12 --- /dev/null +++ b/man/DBConnection-cash-connect.Rd @@ -0,0 +1,12 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/DBConnection-Connect.R +\name{DBConnection$connect} +\alias{DBConnection$connect} +\title{DBConnection$connect} +\value{ +\if{html}{\out{}} TRUE if connection was success, FALSE if it failed +} +\description{ +Attempts to connect to the database. If not already configured, will first +attempt to load the configuration. +} diff --git a/man/DBConnection-cash-disconnect.Rd b/man/DBConnection-cash-disconnect.Rd new file mode 100644 index 0000000..8ef6221 --- /dev/null +++ b/man/DBConnection-cash-disconnect.Rd @@ -0,0 +1,8 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/DBConnection-Connect.R +\name{DBConnection$disconnect} +\alias{DBConnection$disconnect} +\title{DBConnection$disconnect} +\description{ +Disconnects the current database connection, if connected. +} diff --git a/man/DBConnection-cash-disconnectOnSessionEnd.Rd b/man/DBConnection-cash-disconnectOnSessionEnd.Rd new file mode 100644 index 0000000..0db44e0 --- /dev/null +++ b/man/DBConnection-cash-disconnectOnSessionEnd.Rd @@ -0,0 +1,15 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/DBConnection-Connect.R +\name{DBConnection$disconnectOnSessionEnd} +\alias{DBConnection$disconnectOnSessionEnd} +\title{DBConnection$disconnectOnSessionEnd} +\arguments{ +\item{session}{\if{html}{\out{}} The R Shiny session to register the callback with} +} +\value{ +\if{html}{\out{}} A function to cancel the onSessionEnded callback +} +\description{ +Registers a callback with an R Shiny session so that the connection is +disconnected once the session ends. +} diff --git a/man/DBConnection-cash-getConfiguration.Rd b/man/DBConnection-cash-getConfiguration.Rd new file mode 100644 index 0000000..ab338ae --- /dev/null +++ b/man/DBConnection-cash-getConfiguration.Rd @@ -0,0 +1,11 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/DBConnection-Configure.R +\name{DBConnection$getConfiguration} +\alias{DBConnection$getConfiguration} +\title{DBConnection$getConfiguration} +\value{ +\if{html}{\out{}} Current configuration. If not configured, NULL is returned +} +\description{ +Get the current configuration of the DBClient instance. +} diff --git a/man/DBConnection-cash-getConnection.Rd b/man/DBConnection-cash-getConnection.Rd new file mode 100644 index 0000000..4bd05ad --- /dev/null +++ b/man/DBConnection-cash-getConnection.Rd @@ -0,0 +1,14 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/DBConnection-Connect.R +\name{DBConnection$getConnection} +\alias{DBConnection$getConnection} +\title{DBConnection$getConnection} +\arguments{ +\item{do_connect}{\if{html}{\out{}} Forcibly connect if not already connected} +} +\value{ +\if{html}{\out{}} DBConnection instance +} +\description{ +Returns the DBConnection instance for this client. +} diff --git a/man/DBConnection-cash-getToken.Rd b/man/DBConnection-cash-getToken.Rd new file mode 100644 index 0000000..9aff9de --- /dev/null +++ b/man/DBConnection-cash-getToken.Rd @@ -0,0 +1,8 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/DBConnection-Initialize.R +\name{DBConnection$getToken} +\alias{DBConnection$getToken} +\title{DBConnection$getToken} +\description{ +Returns the identity token for this DBConnection instance. +} diff --git a/man/DBConnection-cash-initialize.Rd b/man/DBConnection-cash-initialize.Rd new file mode 100644 index 0000000..03ecf7e --- /dev/null +++ b/man/DBConnection-cash-initialize.Rd @@ -0,0 +1,8 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/DBConnection-Initialize.R +\name{DBConnection$initialize} +\alias{DBConnection$initialize} +\title{DBConnection$initialize} +\description{ +Initalises the DBConnection instance with an identity token. +} diff --git a/man/DBConnection-cash-isConnected.Rd b/man/DBConnection-cash-isConnected.Rd new file mode 100644 index 0000000..0af95a2 --- /dev/null +++ b/man/DBConnection-cash-isConnected.Rd @@ -0,0 +1,11 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/DBConnection-Connect.R +\name{DBConnection$isConnected} +\alias{DBConnection$isConnected} +\title{DBConnection$isConnected} +\value{ +\if{html}{\out{}} TRUE if connected to the database, FALSE otherwise +} +\description{ +Indicates if the instance has an active database connection. +} diff --git a/man/DBConnection-cash-loadConfiguration.Rd b/man/DBConnection-cash-loadConfiguration.Rd new file mode 100644 index 0000000..85f4730 --- /dev/null +++ b/man/DBConnection-cash-loadConfiguration.Rd @@ -0,0 +1,14 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/DBConnection-Configure.R +\name{DBConnection$loadConfiguration} +\alias{DBConnection$loadConfiguration} +\title{DBConnection$loadConfiguration} +\arguments{ +\item{warn}{Emit a warning with which file it attempting to be loaded} +} +\value{ +\if{html}{\out{}} TRUE if configuration was successful, FALSE otherwise +} +\description{ +Attempts to load the configuration either from global.R or .env.R. +} diff --git a/man/DBConnection-cash-rollbackTransaction.Rd b/man/DBConnection-cash-rollbackTransaction.Rd new file mode 100644 index 0000000..40e86f0 --- /dev/null +++ b/man/DBConnection-cash-rollbackTransaction.Rd @@ -0,0 +1,15 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/DBConnection-Transaction.R +\name{DBConnection$rollbackTransaction} +\alias{DBConnection$rollbackTransaction} +\title{DBConnection$rollbackTransaction} +\arguments{ +\item{session}{\if{html}{\out{}} The R Shiny session to register the callback with} +} +\value{ +\if{html}{\out{}} A function to cancel the onSessionEnded callback +} +\description{ +Registers a callback with an R Shiny session so that the connection is +disconnected once the session ends. +} diff --git a/man/DBConnection.Rd b/man/DBConnection.Rd index c226954..3d3683d 100644 --- a/man/DBConnection.Rd +++ b/man/DBConnection.Rd @@ -4,5 +4,5 @@ \alias{DBConnection} \title{DBConnection} \description{ -Database Connection class +Database Connection class. } diff --git a/man/dot-configureDefault.Rd b/man/dot-configureDefault.Rd new file mode 100644 index 0000000..bb7d208 --- /dev/null +++ b/man/dot-configureDefault.Rd @@ -0,0 +1,44 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/DBConnection-Configure.R +\name{.configureDefault} +\alias{.configureDefault} +\title{Configures the DBClient instance.} +\usage{ +.configureDefault( + host, + port, + user, + password, + dbname, + engine = NULL, + name = NULL, + schema = NULL, + autoconnect = NULL +) +} +\arguments{ +\item{host}{The database host} + +\item{port}{The database port} + +\item{user}{The database user to connect with} + +\item{password}{The database password to connect with} + +\item{dbname}{The database to use} + +\item{engine}{The database engine (mysql, postgres) as supported by DBI} + +\item{name}{The database to use (alternative to dbname)} + +\item{schema}{The database schema to use (postgres)} + +\item{autoconnect}{Autoconnect to the database if not already connected} +} +\value{ +The configuration accepted +} +\description{ +There are 2 options for configuration, either: pass in a named list; or use +named parameters. +}