diff --git a/R/db.R b/R/db.R index 6e6d1886..8967822b 100644 --- a/R/db.R +++ b/R/db.R @@ -219,72 +219,9 @@ create_table <- function(data_list, db, table) { DBI::dbExecute(conn, paste0('ALTER TABLE "', table, '" ENABLE ROW LEVEL SECURITY;')) }) - message("Table created (or already exists) in your Supabase database.") + message(paste("Table", table, "created in the database.")) } -check_and_add_columns <- function(data_local, db, table) { - pool::poolWithTransaction(db, function(conn) { - existing_cols <- DBI::dbListFields(conn, table) - new_cols <- setdiff(names(data_local), existing_cols) - for (col in new_cols) { - r_type <- typeof(data_local[[col]]) - sql_type <- r_to_sql_type(r_type) - query <- paste0('ALTER TABLE "', table, '" ADD COLUMN "', col, '" ', sql_type, ';') - DBI::dbExecute(conn, query) - } - }) -} - -# Less secure approach - vulnerable to SQL injection -# database_uploading <- function(data_list, db, table) { -# if(is.null(db)) { -# return(warning("Databasing is not in use")) -# } -# -# tryCatch({ -# pool::poolWithTransaction(db, function(conn) { -# # Get the actual columns in the table -# existing_cols <- DBI::dbListFields(conn, table) -# -# # Filter data_list to only include existing columns -# data_list <- data_list[names(data_list) %in% existing_cols] -# -# # Prepare the update query -# cols <- names(data_list) -# update_cols <- setdiff(cols, "session_id") -# -# # Create value string, properly escaping and quoting values -# values <- sapply(data_list, function(x) { -# if (is.character(x)) { -# paste0("'", gsub("'", "''", x), "'") -# } else if (is.numeric(x)) { -# as.character(x) -# } else { -# "NULL" -# } -# }) -# values_str <- paste(values, collapse = ", ") -# -# update_set <- paste(sapply(update_cols, function(col) { -# paste0('"', col, '" = EXCLUDED."', col, '"') -# }), collapse = ", ") -# -# update_query <- paste0( -# 'INSERT INTO "', table, '" ("', paste(cols, collapse = '", "'), '") ', -# 'VALUES (', values_str, ') ', -# 'ON CONFLICT (session_id) DO UPDATE SET ', -# update_set -# ) -# -# # Execute the query -# DBI::dbExecute(conn, update_query) -# }) -# }, error = function(e) { -# warning("Error in database operation: ", e$message) -# print(e) # Print the full error for debugging -# }) -# } - # Solution found in this issue: # https://github.com/r-dbi/DBI/issues/193 sqlInterpolateList <- function(conn, sql, vars=list(), list_vars=list()) { @@ -303,56 +240,6 @@ sqlInterpolateList <- function(conn, sql, vars=list(), list_vars=list()) { DBI::sqlInterpolate(conn, sql, .dots=vars) } -# database_uploading <- function(data_list, db, table) { -# if(is.null(db)) { -# return(warning("Databasing is not in use")) -# } -# -# tryCatch({ -# pool::poolWithTransaction(db, function(conn) { -# # Get the actual columns in the table -# existing_cols <- DBI::dbListFields(conn, table) -# -# # Filter data_list to only include existing columns -# data_list <- data_list[names(data_list) %in% existing_cols] -# -# # Ensure session_id is the first column -# cols <- c("session_id", setdiff(names(data_list), "session_id")) -# data_list <- data_list[cols] -# -# # Prepare the placeholders -# placeholders <- paste0("?", names(data_list)) -# -# # Prepare the update set -# update_cols <- setdiff(cols, "session_id") -# update_set <- paste(sapply(update_cols, function(col) { -# sprintf('"%s" = EXCLUDED."%s"', col, col) -# }), collapse = ", ") -# -# # Prepare the SQL query template -# query_template <- sprintf( -# 'INSERT INTO "%s" ("%s") VALUES (%s) ON CONFLICT (session_id) DO UPDATE SET %s', -# table, -# paste(cols, collapse = '", "'), -# paste(placeholders, collapse = ", "), -# update_set -# ) -# -# # Use sqlInterpolateList to safely insert values -# query <- sqlInterpolateList( -# conn, -# query_template, -# list_vars = data_list -# ) -# -# # Execute the query -# DBI::dbExecute(conn, query) -# }) -# }, error = function(e) { -# warning("Error in database operation: ", e$message) -# print(e) # Print the full error for debugging -# }) - database_uploading <- function(data_list, db, table, changed_fields) { if(is.null(db)) { return(warning("Databasing is not in use")) diff --git a/R/server.R b/R/server.R index 4c14d627..59a61179 100644 --- a/R/server.R +++ b/R/server.R @@ -149,7 +149,7 @@ sd_server <- function( } update_data <- function(data_list, changed_fields = NULL, time_last = FALSE) { - if (is.null(changed_fields)) { + if (length(changed_fields) == 0) { changed_fields = names(data_list) } if (time_last) { @@ -200,7 +200,7 @@ sd_server <- function( ) all_data <- do.call(shiny::reactiveValues, initial_data) - # Database table initialization + # Initialize database table if (!ignore_mode) { table_exists <- pool::poolWithTransaction(db$db, function(conn) { DBI::dbExistsTable(conn, db$table) @@ -208,8 +208,6 @@ sd_server <- function( if (!table_exists) { create_table(initial_data, db$db, db$table) } - # Check if there are any new columns, update DB accordingly - check_and_add_columns(initial_data, db$db, db$table) } # Reactive expression that returns a list of the latest data