-
Notifications
You must be signed in to change notification settings - Fork 19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
switch from RODBC to DBI/odbc for connections to the local NASIS database #146
Comments
Reprex for dealing with issues related to long (text) columns, truncation to 255 characters, and possible stumbling blocks in conversion to DBI. I thought that with the most recent {odbc} that # reprex for issues with column ordering and ODBC drivers
# RODBC truncates long columns to 255 characters
library(RODBC)
con2 <- soilDB:::.openNASISchannel()
res1 <- sqlQuery(con2, "SELECT * FROM calculation")
max(nchar(res1$calc)) # truncated to 255 characters
#> [1] 255
# manual casting of columns to ntext works, but not for simple queries like SELECT *
odbcClose(con2)
# compare DBI
library(DBI)
library(odbc) # driver to read MSSQL
#> Warning: package 'odbc' was built under R version 4.0.3
con <- dbConnect(odbc::odbc(),
DSN = "nasis_local",
UID = "NasisSqlRO",
PWD = "nasisRe@d0n1y365")
# this produces an error; may be fixed with new odbc?
# see https://github.com/r-dbi/odbc/issues/309
dbGetQuery(con, "SELECT * FROM calculation")
#> Error in result_fetch(res@ptr, n): nanodbc/nanodbc.cpp:3011: 07009: [Microsoft][SQL Server Native Client 11.0]Invalid Descriptor Index
#> Warning in dbClearResult(rs): Result already cleared
# this produces same error
library(dplyr, warn.conflicts = FALSE)
library(dbplyr, warn.conflicts = FALSE)
tbl(con, "calculation")
#> Error in result_fetch(res@ptr, n): nanodbc/nanodbc.cpp:3011: 07009: [Microsoft][SQL Server Native Client 11.0]Invalid Descriptor Index
#> Warning in dbClearResult(res): Result already cleared
# this works without error (manually identify and reorder long columns to end)
long_cols <- odbcConnectionColumns(con, "calculation") %>%
filter(field.type == "varchar" & column_size == 0) %>%
pull(name)
res2 <- tbl(con, "calculation") %>%
select(-all_of(long_cols), everything(), all_of(long_cols)) %>%
collect()
max(nchar(res2$calc)) # proper length
#> [1] 3167991
dbDisconnect(con) Created on 2020-11-03 by the reprex package (v0.3.0) |
This may be an interaction of the new {odbc}/nanodbc with the SQL Server Native Client 11.0. We should consider requesting the latest MS ODBC driver from IT for long-term stability of ODBC connections. Microsoft is several versions ahead of the version 11 we had to switch over because of changes to TLS policy.
|
Thanks for looking into this--always more complicated than it would seem. I've been working towards adding explicit CASTing to |
* Convert NASIS-related queries from RODBC->DBI #146 * remove require RODBC * roxygen for fetchNASIS * Color data NA moist state handling * use_sqlite = FALSE is the default for .openNASISchannel * Move VARCHAR(MAX) fields to end of query, per MSSQL specs * fix getHzErrorsNASIS * Validations and tests RE: #149 #146 * Move to fetchNASIS tests (skip on remote) * Add skip() and better handling of missing DB/no data * Use local_NASIS_defined everywhere w/ odbc::odbcListDataSources * cherry-pick: make a proper interface to sqlite NASIS queries cherry-pick: 🧬 Stitch SQLite data source API up to fetchNASIS cherry-pick: Update demo cherry-pick: Default code use 25cm, as proposed Validate/fix NASIS methods * Change name of path argument * fetchNASIS: Remove RIGHT JOIN in geomorphic features query and turn off the MSSQL specific syntax used for `d.rf.data.v2` * Method for cloning local NASIS tables into static SQLite file (#154) * Method for cloning local NASIS tables into static SQLite file #149 * local_NASIS_defined: add static_path arg and use RSQLite::dbCanConnect * Add selected set and static path to getHzErrorsNASIS * get_cosoilmoist / get_vegplot should use selected set argument * Special handling for local_NASIS_defined (does not take SS arg) * Add static_path to local_NASIS_defined * Roxygen: markdown = TRUE Broken docs md-doc: Fix single bracket semantics * Update docs * forgot to commit this one * NEWS / version 2.6.x * Rd2roxygen initial conversion; old in ./manbak * fix \href{} * passing check_man * Rd2roxygen (#162) * Rd2roxygen initial conversion; old in ./manbak * fix \href{} * passing check_man * fixes for R CMD check * deprecate old manpages * Revert existing roxygen back to human-made * get_extended_data_from_NASIS_db: artifact data query should respect SS=FALSE * get extended NASIS photo text notes: check for paths >260 chars * proper sequence to get SQLite pedon snapshot fetchNASIS-ready * Use checkHzDepthLogic (fast=TRUE) in fetchNASIS_pedons * .fetchNASIS_pedons: Use data.table for extended data processing * get_extended_data_from_NASIS_db: Replace plyr::join * dbQueryNASIS: vectorize/test; dbConnectNASIS: add NASIS() alias * docs * Add test for local NASIS DBI issues * Fix for get_cosoilmoist_from_NASISWebReport example * Fix for list output of createStaticNASIS * Close RODBC connection used in tests * Testing some pedon_table_column checks @jskovlin * aqp::union has been removed from namespace * test-fetchKSSL: hide txtProgressBar when running tests * Refactoring utils.R for data.table in fetchNASIS flattening * Docs * Oops DBI/odbc/RSQLite back in Imports * Missing comma * Docs * Mopping up * Move driver packages (odbc, RSQLite) to Suggests * get_cotext_from_NASIS_db: Add support for static_path argument and use dbQueryNASIS * get_cotext_from_NASIS_db: check for try-error * .formatParentMaterialString: Return NA_character_ for conformal data.frame with NULL data * Remove dangling require RODBC * fetchNASIS: extended data flattening handle NULL table contents * createStaticNASIS: better default arguments * Fixes for selected set argument (found by drop all _View_1 tables in a static DB) * Updates to nasisDBI "demo" that runs all the NASIS methods by all the methods * Remove requireNamespace("RODBC")--merge artifact? * demo createStaticNASIS workflow * Add WCS/SDA viz to demo * Fix bug in decoding of horizon data; thanks @dylanbeaudette * Pass through static_path argument to uncode() * Fix get_comonth_from_NASIS_db fill=TRUE * Update demos * Small adjustments to default args for demo/comparisons * Rename static_path arg to dsn * Rename static_path arg to dsn (docs) * Version bump + update README * Update NEWS.md
Tiny example.
This started as #145.
The text was updated successfully, but these errors were encountered: