From e79f720fc2401d346b0d3835dce74420119373da Mon Sep 17 00:00:00 2001 From: Edwin de Jonge Date: Fri, 11 Mar 2022 11:27:52 +0100 Subject: [PATCH] removed COLUMN for ALTER TABLE statements. Fix for issue #5 --- R/alter_stmt.R | 8 ++++++-- R/modifier_to_sql.R | 40 ---------------------------------------- tests/testthat/dump.sql | 4 ++-- 3 files changed, 8 insertions(+), 44 deletions(-) diff --git a/R/alter_stmt.R b/R/alter_stmt.R index 7e11f42..caee5d4 100644 --- a/R/alter_stmt.R +++ b/R/alter_stmt.R @@ -28,11 +28,15 @@ alter_stmt <- function(x, table, table_ident, con){ # DBI::dbClearResult(rs) new_vars <- types[!names(types) %in% org_vars] + add_column <- FALSE + + add_ <- if (add_column) sql("\nADD COLUMN ") else sql("\nADD ") lapply(names(new_vars), function(n){ build_sql( - "ALTER TABLE ", table_ident, - "\nADD COLUMN ", ident(n), " ", unname(sql(new_vars[n])), ";" + "ALTER TABLE ", table_ident + , add_ + , ident(n), " ", unname(sql(new_vars[n])), ";" , con = con ) }) diff --git a/R/modifier_to_sql.R b/R/modifier_to_sql.R index aaf5479..89d514a 100644 --- a/R/modifier_to_sql.R +++ b/R/modifier_to_sql.R @@ -21,43 +21,3 @@ modifier_to_sql <- function(x, table, con = NULL){ , con = tc$con ) } - -#' @importFrom dbplyr ident ident_q -alter_stmt <- function(x, table, table_ident, con){ - org_vars <- dplyr::tbl_vars(table) - - # just for querying meta data - tab <- utils::head(table, 2) - con <- dbplyr::remote_con(table) - - # collect all assignments - as <- get_assignments(x) - - vars <- sapply(as, function(a) a[[2]], USE.NAMES = FALSE) - asgns <- sapply(as, function(a) a[[3]]) - - names(asgns) <- vars - mut <- bquote(dplyr::mutate(tab, ..(asgns)), splice = TRUE) - tab <- utils::head(eval(mut)) - - # getting datatypes - df <- as.data.frame(tab) - types <- DBI::dbDataType(con, df) - #print(types) - - # qry <- dbplyr::remote_query(tab) - # - # rs <- DBI::dbSendQuery(con, qry) - # ci <- DBI::dbColumnInfo(rs) - # DBI::dbClearResult(rs) - - new_vars <- types[!names(types) %in% org_vars] - - lapply(names(new_vars), function(n){ - build_sql( - "ALTER TABLE ", table_ident, - "\nADD COLUMN ", ident(n), " ", unname(sql(new_vars[n])), ";" - , con = con - ) - }) -} diff --git a/tests/testthat/dump.sql b/tests/testthat/dump.sql index dc67200..935267a 100644 --- a/tests/testthat/dump.sql +++ b/tests/testthat/dump.sql @@ -1,10 +1,10 @@ ALTER TABLE `ds` -ADD COLUMN `child` TEXT; +ADD `child` TEXT; ALTER TABLE `ds` -ADD COLUMN `workstatus` TEXT; +ADD `workstatus` TEXT; -- M1: maximum age -- Age is limited.