Skip to content
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

Closed
dylanbeaudette opened this issue Sep 29, 2020 · 3 comments · Fixed by #149
Closed

switch from RODBC to DBI/odbc for connections to the local NASIS database #146

dylanbeaudette opened this issue Sep 29, 2020 · 3 comments · Fixed by #149
Labels
NASIS-local This tag is used for pull requests, issues, discussions etc. for soilDB local NASIS functions

Comments

@dylanbeaudette
Copy link
Member

Tiny example.

library(DBI)
library(odbc)
con <- dbConnect(odbc(), ...)

This started as #145.

@brownag
Copy link
Member

brownag commented Nov 3, 2020

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 dbGetQuery would not error, regardless of long ccolumn order, but apparently not.

# 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)

@brownag
Copy link
Member

brownag commented Nov 3, 2020

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.

The redistributable installer for Microsoft ODBC Driver 17 for SQL Server installs the client components, which are required during run time to take advantage of newer SQL Server features. It optionally installs the header files needed to develop an application that uses the ODBC API. Starting with version 17.4.2, the installer also includes and installs the Microsoft Active Directory Authentication Library (ADAL.dll).

Version 17.6.1 is the latest general availability (GA) version. If you have a previous version of Microsoft ODBC Driver 17 for SQL Server installed, installing 17.6.1 upgrades it to 17.6.1.

https://docs.microsoft.com/en-us/sql/connect/odbc/download-odbc-driver-for-sql-server?view=sql-server-ver15

@dylanbeaudette
Copy link
Member Author

Thanks for looking into this--always more complicated than it would seem. I've been working towards adding explicit CASTing to ntext for all affected queries. There may be some remaining, but as far as I can tell most in soilDB have been fixed. I had to do similar work in the interpretation engine repo. This doesn't help folks who write ad hoc queries.

@brownag brownag linked a pull request Dec 30, 2020 that will close this issue
9 tasks
brownag added a commit that referenced this issue Jan 4, 2021
@brownag brownag added the NASIS-local This tag is used for pull requests, issues, discussions etc. for soilDB local NASIS functions label Jan 16, 2021
brownag added a commit that referenced this issue Jan 29, 2021
brownag added a commit that referenced this issue Mar 23, 2021
* 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
NASIS-local This tag is used for pull requests, issues, discussions etc. for soilDB local NASIS functions
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants