fixes to db work: properly closing connection
This commit is contained in:
@@ -16,9 +16,10 @@
|
|||||||
- fixed not erasing inputs while loading empty values (with checkboxes, radiobuttons);
|
- fixed not erasing inputs while loading empty values (with checkboxes, radiobuttons);
|
||||||
- +number input validation
|
- +number input validation
|
||||||
- fix validation errors (2025-03-18);
|
- fix validation errors (2025-03-18);
|
||||||
|
- fixes to db work: properly closing connection (2025-03-18);
|
||||||
|
|
||||||
##### changes
|
##### changes
|
||||||
- redesign work with db (open connection only when action performed) (2024-03-03);
|
- redesign work with db (open connection only when action performed) (2025-03-03);
|
||||||
- some code refactoring;
|
- some code refactoring;
|
||||||
- replacing NumberImput to TextInput due to correct implement validation;
|
- replacing NumberImput to TextInput due to correct implement validation;
|
||||||
- added options to enable/disable auth module (disabled on default) (2025-03-17);
|
- added options to enable/disable auth module (disabled on default) (2025-03-17);
|
||||||
|
|||||||
21
app.R
21
app.R
@@ -63,7 +63,7 @@ make_db_connection <- function(where = "") {
|
|||||||
|
|
||||||
#' @description Function to close connection to db, disigned to easy dubugging and
|
#' @description Function to close connection to db, disigned to easy dubugging and
|
||||||
#' hide warnings.
|
#' hide warnings.
|
||||||
close_db_connection <- function(where = "") {
|
close_db_connection <- function(con, where = "") {
|
||||||
tryCatch(
|
tryCatch(
|
||||||
expr = DBI::dbDisconnect(con),
|
expr = DBI::dbDisconnect(con),
|
||||||
error = function(e) print(e),
|
error = function(e) print(e),
|
||||||
@@ -133,7 +133,7 @@ if (identical(colnames(DBI::dbReadTable(con, "main")), names(inputs_simple_list)
|
|||||||
}
|
}
|
||||||
|
|
||||||
# close connection to prevent data loss
|
# close connection to prevent data loss
|
||||||
close_db_connection()
|
close_db_connection(con)
|
||||||
|
|
||||||
|
|
||||||
# INLINE TABLES =====================
|
# INLINE TABLES =====================
|
||||||
@@ -417,8 +417,8 @@ server <- function(input, output) {
|
|||||||
.x = names(inputs_simple_list),
|
.x = names(inputs_simple_list),
|
||||||
.f = \(x_input_id) {
|
.f = \(x_input_id) {
|
||||||
form_type <- inputs_simple_list[[x_input_id]]
|
form_type <- inputs_simple_list[[x_input_id]]
|
||||||
choices <- filter(SCHEME_MAIN, form_id == {{x_input_id}}) %>% pull(choices)
|
choices <- filter(SCHEME_MAIN, form_id == {{ x_input_id }}) %>% pull(choices)
|
||||||
val_required <- filter(SCHEME_MAIN, form_id == {{x_input_id}}) %>%
|
val_required <- filter(SCHEME_MAIN, form_id == {{ x_input_id }}) %>%
|
||||||
distinct(required) %>%
|
distinct(required) %>%
|
||||||
pull(required)
|
pull(required)
|
||||||
|
|
||||||
@@ -597,19 +597,18 @@ server <- function(input, output) {
|
|||||||
observeEvent(input$save_data_button, {
|
observeEvent(input$save_data_button, {
|
||||||
req(input$id)
|
req(input$id)
|
||||||
con <- make_db_connection("save_data_button")
|
con <- make_db_connection("save_data_button")
|
||||||
on.exit(close_db_connection("save_data_button"), add = TRUE)
|
on.exit(close_db_connection(con, "save_data_button"), add = TRUE)
|
||||||
|
|
||||||
## MAIN
|
## MAIN
|
||||||
# собрать все значения по введенным данным;
|
# собрать все значения по введенным данным;
|
||||||
result_df <- purrr::map(
|
result_df <- purrr::map(
|
||||||
.x = names(inputs_simple_list),
|
.x = names(inputs_simple_list),
|
||||||
.f = \(x) {
|
.f = \(x) {
|
||||||
type <- inputs_simple_list[[x]]
|
|
||||||
input_d <- input[[x]]
|
input_d <- input[[x]]
|
||||||
|
|
||||||
# return empty if 0 element
|
# return empty if 0 element
|
||||||
if (length(input_d) == 0) {
|
if (length(input_d) == 0) {
|
||||||
return(get_empty_data(type))
|
return(get_empty_data(inputs_simple_list[[x]]))
|
||||||
}
|
}
|
||||||
# return element if there one
|
# return element if there one
|
||||||
if (length(input_d) == 1) {
|
if (length(input_d) == 1) {
|
||||||
@@ -655,7 +654,7 @@ server <- function(input, output) {
|
|||||||
## get list of id's from db =====================
|
## get list of id's from db =====================
|
||||||
observeEvent(input$load_data_button, {
|
observeEvent(input$load_data_button, {
|
||||||
con <- make_db_connection("load_data_button")
|
con <- make_db_connection("load_data_button")
|
||||||
on.exit(close_db_connection("load_data_button"))
|
on.exit(close_db_connection(con, "load_data_button"))
|
||||||
|
|
||||||
if (length(dbListTables(con)) != 0 && "main" %in% DBI::dbListTables(con)) {
|
if (length(dbListTables(con)) != 0 && "main" %in% DBI::dbListTables(con)) {
|
||||||
# GET DATA files
|
# GET DATA files
|
||||||
@@ -686,7 +685,7 @@ server <- function(input, output) {
|
|||||||
## load data to input forms ==================================
|
## load data to input forms ==================================
|
||||||
observeEvent(input$read_data, {
|
observeEvent(input$read_data, {
|
||||||
con <- make_db_connection("read_data")
|
con <- make_db_connection("read_data")
|
||||||
on.exit(close_db_connection("read_data"), add = TRUE)
|
on.exit(close_db_connection(con, "read_data"), add = TRUE)
|
||||||
|
|
||||||
# main df read
|
# main df read
|
||||||
test_read_df <- read_df_from_db_by_id("main", con)
|
test_read_df <- read_df_from_db_by_id("main", con)
|
||||||
@@ -739,7 +738,7 @@ server <- function(input, output) {
|
|||||||
filename = paste0("d2tra_", format(Sys.time(), "%Y%m%d_%H%M%S"), ".xlsx"),
|
filename = paste0("d2tra_", format(Sys.time(), "%Y%m%d_%H%M%S"), ".xlsx"),
|
||||||
content = function(file) {
|
content = function(file) {
|
||||||
con <- make_db_connection("downloadData")
|
con <- make_db_connection("downloadData")
|
||||||
on.exit(close_db_connection("downloadData"), add = TRUE)
|
on.exit(close_db_connection(con, "downloadData"), add = TRUE)
|
||||||
|
|
||||||
# get all data
|
# get all data
|
||||||
list_of_df <- purrr::map(
|
list_of_df <- purrr::map(
|
||||||
@@ -870,7 +869,7 @@ server <- function(input, output) {
|
|||||||
## trigger saving function =============
|
## trigger saving function =============
|
||||||
observeEvent(input$data_save, {
|
observeEvent(input$data_save, {
|
||||||
con <- make_db_connection("saving data (from modal conf)")
|
con <- make_db_connection("saving data (from modal conf)")
|
||||||
on.exit(close_db_connection("saving data (from modal conf)"), add = TRUE)
|
on.exit(close_db_connection(con, "saving data (from modal conf)"), add = TRUE)
|
||||||
|
|
||||||
# убираем плашку
|
# убираем плашку
|
||||||
removeModal()
|
removeModal()
|
||||||
|
|||||||
Reference in New Issue
Block a user