Skip to content

Commit 921b75e

Browse files
committed
createStaticNASIS: remove automatic rearranging of VARCHAR(MAX) columns
- dbListFields does not return necessary information and driver does not need this to be done manually anymore
1 parent e53e9f4 commit 921b75e

File tree

1 file changed

+44
-42
lines changed

1 file changed

+44
-42
lines changed

R/createStaticNASIS.R

+44-42
Original file line numberDiff line numberDiff line change
@@ -8,47 +8,49 @@
88
# structure; Default: \code{NULL}
99
# @return A data.frame or other result of \code{DBI::dbGetQuery}
1010
# @export .dump_NASIS_table
11-
.dump_NASIS_table <- function(table_name, dsn = NULL) {
12-
13-
# connect to NASIS, identify columns
14-
con <- dbConnectNASIS(dsn)
15-
allcols <- "*"
16-
17-
columns <- NULL
18-
19-
# handling for MSSQL/ODBC weirdness
20-
if (is.null(dsn) || inherits(con, 'OdbcConnection')) {
21-
22-
# assuming that default connection uses ODBC
23-
if (!requireNamespace("odbc"))
24-
stop("package `odbc` is required ", call. = FALSE)
25-
26-
columns <- odbc::dbListFields(con, table_name)
27-
28-
# re-arrange VARCHAR(MAX) columns
29-
longcols <- subset(columns, columns$field.type == "varchar" & columns$column_size == 0)$name
30-
allcols <- columns$name
31-
32-
if (length(longcols) > 0) {
33-
allcols[which(allcols %in% longcols)] <- NA
34-
allcols <- c(na.omit(allcols), longcols)
35-
}
36-
}
37-
38-
# construct query and return result
39-
q <- sprintf("SELECT %s FROM %s", paste(allcols, collapse = ", "), table_name)
40-
41-
q <- gsub('\\brule\\b', '\"rule\"', q)
42-
43-
res <- dbQueryNASIS(con, q)
44-
45-
if (is.null(columns)) {
46-
columns <- data.frame(name = colnames(res))
47-
}
48-
49-
# put back into original order from NASIS
50-
return(res[, match(colnames(res), columns$name)])
51-
}
11+
# .dump_NASIS_table <- function(table_name, dsn = NULL) {
12+
#
13+
# # connect to NASIS, identify columns
14+
# con <- dbConnectNASIS(dsn)
15+
# allcols <- "*"
16+
#
17+
# columns <- NULL
18+
#
19+
# # # handling for MSSQL/ODBC weirdness
20+
# # # previously required for issues with nanoodbc driver
21+
# # # see https://github.com/ncss-tech/soilDB/pull/149
22+
# # if (is.null(dsn) || inherits(con, 'OdbcConnection')) {
23+
# #
24+
# # # assuming that default connection uses ODBC
25+
# # if (!requireNamespace("odbc"))
26+
# # stop("package `odbc` is required ", call. = FALSE)
27+
# #
28+
# # columns <- odbc::dbListFields(con, table_name)
29+
# #
30+
# # # re-arrange VARCHAR(MAX) columns
31+
# # longcols <- subset(columns, columns$field.type == "varchar" & columns$column_size == 0)$name
32+
# # allcols <- columns$name
33+
# #
34+
# # if (length(longcols) > 0) {
35+
# # allcols[which(allcols %in% longcols)] <- NA
36+
# # allcols <- c(na.omit(allcols), longcols)
37+
# # }
38+
# # }
39+
#
40+
# # construct query and return result
41+
# q <- sprintf("SELECT %s FROM %s", paste(allcols, collapse = ", "), table_name)
42+
#
43+
# q <- gsub('\\brule\\b', '\"rule\"', q)
44+
#
45+
# res <- dbQueryNASIS(con, q)
46+
#
47+
# if (is.null(columns)) {
48+
# columns <- data.frame(name = colnames(res))
49+
# }
50+
#
51+
# # put back into original order from NASIS
52+
# return(res[, match(colnames(res), columns$name)])
53+
# }
5254

5355
#' Create a memory or file-based instance of NASIS database
5456
#'
@@ -157,7 +159,7 @@ createStaticNASIS <- function(tables = NULL,
157159
newname <- new_names[i]
158160
else newname <- n
159161

160-
newdata <- try(.dump_NASIS_table(n, dsn = dsn), silent = verbose)
162+
newdata <- try(DBI::dbReadTable(con, n), silent = verbose)
161163

162164
# previously processed Date/Times -> character for output
163165

0 commit comments

Comments
 (0)