Skip to content

Commit

Permalink
fix to put max at end then reorder
Browse files Browse the repository at this point in the history
  • Loading branch information
tomwilsonsco committed Oct 8, 2024
1 parent ff016fd commit ad0004a
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion R/read_table_from_db.R
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,18 @@ create_read_sql <- function(schema,
}
}

max_to_end <- function(metadata){

Check warning on line 97 in R/read_table_from_db.R

View workflow job for this annotation

GitHub Actions / lint

file=R/read_table_from_db.R,line=97,col=33,[paren_body_linter] There should be a space between a right parenthesis and a body expression.
long_types <- c("varchar(max)",
"nvarchar(max)",
"image",
"text",
"ntext",
"varbinary(max)")
not_max <- metadata[! metadata$data_type %in% long_types, ]$column_name
is_max <- metadata[metadata$data_type %in% long_types, ]$column_name
c(not_max, is_max)
}


#' Read a SQL Server table into an R dataframe
#'
Expand Down Expand Up @@ -179,6 +191,16 @@ read_table_from_db <- function(server,
include_pk
)


table_metadata <- table_metadata[table_metadata$column_name
%in% select_list, ]

# To deal with https://github.com/r-dbi/odbc/issues/309
# related with older odbc drivers and long column datatypes
select_list <- max_to_end(table_metadata)

original_positions <- match(table_metadata$column_name, select_list)

read_sql <- create_read_sql(
schema,
select_list,
Expand All @@ -193,5 +215,5 @@ read_table_from_db <- function(server,
server, sql = read_sql, output = TRUE
)
message(glue::glue("returned {nrow(df)} rows to data frame"))
df
df[, original_positions, drop = FALSE]
}

0 comments on commit ad0004a

Please sign in to comment.