Skip to content

Commit

Permalink
refact(db): harmonize database connection
Browse files Browse the repository at this point in the history
  • Loading branch information
juliendiot42 committed Jun 25, 2024
1 parent 40a1b50 commit 440028a
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/fun/func_dbRequests.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,13 @@

## Functions related to data file / data-base requests

connect_to_db <- function(dbname = dbname) {
dbConnect(SQLite(), dbname = dbname)
}

db_get_request <- function(query, dbname = DATA_DB) {
# for SELECT query only
conn <- dbConnect(SQLite(), dbname = dbname)
conn <- connect_to_db(dbname = dbname)
tryCatch({
out <- dbGetQuery(conn = conn, query)
}, finally = {
Expand All @@ -31,7 +35,7 @@ db_get_request <- function(query, dbname = DATA_DB) {
}

db_execute_request <- function(query, dbname = DATA_DB) {
conn <- dbConnect(SQLite(), dbname = dbname)
conn <- connect_to_db(dbname = dbname)
tryCatch({
dbExecute(conn = conn, query)
}, finally = {
Expand All @@ -40,7 +44,7 @@ db_execute_request <- function(query, dbname = DATA_DB) {
return(TRUE)
}
db_execute_request_safe <- function(query, dbname = DATA_DB, ...) {
conn <- dbConnect(SQLite(), dbname = dbname)
conn <- connect_to_db(dbname = dbname)
tryCatch({
safe_query <- DBI::sqlInterpolate(conn, query, ...)
dbExecute(conn = conn, safe_query)
Expand All @@ -56,7 +60,7 @@ db_execute_request_safe <- function(query, dbname = DATA_DB, ...) {


db_list_tables <- function(dbname = DATA_DB) {
conn <- dbConnect(SQLite(), dbname = dbname)
conn <- connect_to_db(dbname = dbname)
tryCatch({
allTbls <- dbListTables(conn = conn)
}, finally = {
Expand Down

0 comments on commit 440028a

Please sign in to comment.