Skip to content

Commit

Permalink
removed COLUMN for ALTER TABLE statements. Fix for issue #5
Browse files Browse the repository at this point in the history
  • Loading branch information
edwindj committed Mar 11, 2022
1 parent b60e5ab commit e79f720
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 44 deletions.
8 changes: 6 additions & 2 deletions R/alter_stmt.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
})
Expand Down
40 changes: 0 additions & 40 deletions R/modifier_to_sql.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
})
}
4 changes: 2 additions & 2 deletions tests/testthat/dump.sql
Original file line number Diff line number Diff line change
@@ -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.
Expand Down

0 comments on commit e79f720

Please sign in to comment.