refactor: some code tweaking
This commit is contained in:
22
modules/db.R
22
modules/db.R
@@ -2,7 +2,7 @@
|
||||
#' @export
|
||||
#' @description Function to open connection to db, disigned to easy dubugging.
|
||||
#' @param where text mark to distingiush calss
|
||||
make_db_connection <- function(where = "") {
|
||||
make_db_connection = function(where = "") {
|
||||
if (getOption("APP.DEBUG", FALSE)) message("=== DB CONNECT ", where)
|
||||
DBI::dbConnect(RSQLite::SQLite(), getOption("APP.FILE_DB", FALSE))
|
||||
}
|
||||
@@ -11,7 +11,7 @@ make_db_connection <- function(where = "") {
|
||||
#' @description
|
||||
#' Function to close connection to db, disigned to easy dubugging and
|
||||
#' hide warnings.
|
||||
close_db_connection <- function(con, where = "") {
|
||||
close_db_connection = function(con, where = "") {
|
||||
tryCatch(
|
||||
expr = DBI::dbDisconnect(con),
|
||||
error = function(e) print(e),
|
||||
@@ -23,7 +23,7 @@ close_db_connection <- function(con, where = "") {
|
||||
#' @export
|
||||
#' @description
|
||||
#' Проверить если таблица есть в базе данных и инициировать ее, если от
|
||||
check_if_table_is_exist_and_init_if_not <- function(
|
||||
check_if_table_is_exist_and_init_if_not = function(
|
||||
schm,
|
||||
con = rlang::env_get(rlang::caller_env(), nm = "con")
|
||||
) {
|
||||
@@ -79,7 +79,7 @@ check_if_table_is_exist_and_init_if_not <- function(
|
||||
|
||||
#' @description
|
||||
#' Возращает пустое значение для каждого типа формы
|
||||
get_dummy_data <- function(type) {
|
||||
get_dummy_data = function(type) {
|
||||
|
||||
if (type %in% c("text", "select_one", "select_multiple")) return("dummy")
|
||||
if (type %in% c("radio", "checkbox")) return("dummy")
|
||||
@@ -92,7 +92,7 @@ get_dummy_data <- function(type) {
|
||||
#' @description
|
||||
#' Генерация пустого датасета с пустыми значениями соответствующие
|
||||
#' типу данных
|
||||
get_dummy_df <- function(forms_id_type_list) {
|
||||
get_dummy_df = function(forms_id_type_list) {
|
||||
|
||||
options(box.path = here::here())
|
||||
box::use(modules/utils)
|
||||
@@ -108,7 +108,7 @@ get_dummy_df <- function(forms_id_type_list) {
|
||||
#' @description
|
||||
#' Сравнение полей в существующей в базе данных таблице и попытка
|
||||
#' коррекции таблицы
|
||||
compare_existing_table_with_schema <- function(
|
||||
compare_existing_table_with_schema = function(
|
||||
table_name,
|
||||
schm,
|
||||
con = rlang::env_get(rlang::caller_env(), nm = "con")
|
||||
@@ -176,7 +176,7 @@ compare_existing_table_with_schema <- function(
|
||||
}
|
||||
|
||||
#' @export
|
||||
write_df_to_db <- function(
|
||||
write_df_to_db = function(
|
||||
df,
|
||||
table_name,
|
||||
schm,
|
||||
@@ -225,7 +225,7 @@ write_df_to_db <- function(
|
||||
|
||||
#' @export
|
||||
#' reading tables from db by name and id ========
|
||||
read_df_from_db_by_id <- function(
|
||||
read_df_from_db_by_id = function(
|
||||
table_name,
|
||||
schm,
|
||||
main_key_value,
|
||||
@@ -264,7 +264,7 @@ read_df_from_db_by_id <- function(
|
||||
}
|
||||
|
||||
#' @export
|
||||
get_keys_from_table <- function(table_name, schm, con) {
|
||||
get_keys_from_table = function(table_name, schm, con) {
|
||||
|
||||
main_key_id <- schm$get_main_key_id
|
||||
DBI::dbGetQuery(con, glue::glue("SELECT DISTINCT {main_key_id} FROM {table_name}")) |>
|
||||
@@ -273,7 +273,7 @@ get_keys_from_table <- function(table_name, schm, con) {
|
||||
}
|
||||
|
||||
#' @export
|
||||
get_nested_keys_from_table <- function(table_name, schm, main_key_value, con) {
|
||||
get_nested_keys_from_table = function(table_name, schm, main_key_value, con) {
|
||||
|
||||
main_key_id <- schm$get_main_key_id
|
||||
key_id <- schm$get_key_id(table_name)
|
||||
@@ -286,7 +286,7 @@ get_nested_keys_from_table <- function(table_name, schm, main_key_value, con) {
|
||||
|
||||
### HELPERS ---------
|
||||
#' @export
|
||||
excel_to_db_dates_converter <- function(date) {
|
||||
excel_to_db_dates_converter = function(date) {
|
||||
|
||||
if (is.na(date)) return(NA)
|
||||
# cli::cli_inform("date: {date} | nchar: {nchar(date)} | typeof: {typeof(date)}")
|
||||
|
||||
Reference in New Issue
Block a user