diff --git a/.Rbuildignore b/.Rbuildignore index 6c1e194..67b498d 100644 --- a/.Rbuildignore +++ b/.Rbuildignore @@ -11,3 +11,4 @@ _pkgdown.yml ^inst/tests$ TODO.md ^.lintr$ +^CRAN-SUBMISSION$ diff --git a/.github/workflows/R-CMD-check.yaml b/.github/workflows/R-CMD-check.yaml index 6dc366a..3e91f34 100644 --- a/.github/workflows/R-CMD-check.yaml +++ b/.github/workflows/R-CMD-check.yaml @@ -33,11 +33,11 @@ jobs: steps: - uses: actions/checkout@v2 - - uses: r-lib/actions/setup-r@v1 + - uses: r-lib/actions/setup-r@v2 with: r-version: ${{ matrix.config.r }} - - uses: r-lib/actions/setup-pandoc@v1 + - uses: r-lib/actions/setup-pandoc@v2 - name: Query dependencies run: | diff --git a/DESCRIPTION b/DESCRIPTION index 105320e..b9d7567 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,12 +1,12 @@ Package: bcputility Type: Package Title: Wrapper for SQL Server bcp Utility -Version: 0.4.0 +Version: 0.4.2 Authors@R: person("Thomas", "Roh", email = "thomas@roh.engineering", role = c("aut", "cre")) Description: Provides functions to utilize a command line utility that does bulk inserts and exports from SQL Server databases. License: MIT + file LICENSE Encoding: UTF-8 -RoxygenNote: 7.2.2 +RoxygenNote: 7.2.3 SystemRequirements: bcp Utility Depends: R (>= 3.5.0) Imports: data.table, sf, methods diff --git a/NEWS.md b/NEWS.md index 4d69d84..a1b0f1c 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,10 @@ +# bcputility 0.4.2 + +* Added ability to turn on QUOTED_IDENTIFIERS. This is required for some +Microsoft database products. Use the bcp "-q" option. + +* Column names are quoted so that reserved words can be used. + # bcputility 0.4.0 * Override/set path to sqlcmd with diff --git a/R/bcp.R b/R/bcp.R index 54d02ad..31a27c3 100644 --- a/R/bcp.R +++ b/R/bcp.R @@ -105,6 +105,7 @@ bcpImport <- function( bcp <- findUtility('bcp') # syntax differs for the two utilities bcpArgs <- mapConnectArgs(connectargs = connectargs, utility = 'bcp') + quotedIdentifiers <- connectargs[['quotedidentifiers']] isSpatial <- methods::is(x, 'sf') if (methods::is(x, 'data.frame')) { tmp <- tempfile(fileext = '.dat') @@ -141,10 +142,10 @@ bcpImport <- function( bcpArgs <- append(bcpArgs, list('-t', shQuote(fieldterminator), '-r', shQuote(rowterminator), '-c')) - bcpArgs <- append(bcpArgs, list(quoteTable(table = table), + bcpArgs <- append(bcpArgs, list( + table = table, 'in', shQuote(fileName)), after = 0) - tableExists <- checkTableExists(connectargs = connectargs, - table = table) + tableExists <- checkTableExists(connectargs = connectargs, table = table) append <- tableExists && isFALSE(overwrite) if (isFALSE(append)) { # guess sql server data types @@ -163,8 +164,8 @@ bcpImport <- function( } } # create empty table - createOutput <- createTable(connectargs = connectargs, - table = table, coltypes = dbTypes, stderr = TRUE) + createOutput <- createTable(connectargs = connectargs, table = table, + coltypes = dbTypes, stderr = TRUE) if (length(createOutput) != 0) { stop(paste(createOutput, collapse = ' ')) } @@ -432,10 +433,11 @@ createTable <- function(connectargs, table, coltypes, ...) { query <- sprintf( 'CREATE TABLE %s (%s);', quotedTable, - paste(names(coltypes), coltypes, sep = ' ', collapse = ', ') + paste(sprintf('[%s]', names(coltypes)), coltypes, sep = ' ', + collapse = ', ') ) - sqlcmdArgs <- append(mapConnectArgs(connectargs = connectargs, - utility = 'sqlcmd'), values = list('-Q', shQuote(query))) + sqlcmdArgs <- mapConnectArgs(connectargs = connectargs, utility = 'sqlcmd') + sqlcmdArgs <- append(sqlcmdArgs, values = list('-Q', shQuote(query))) system2(command = sqlcmd, args = sqlcmdArgs, ...) } #' @rdname createTable @@ -446,8 +448,8 @@ dropTable <- function(connectargs, table, ...) { sqlcmd <- findUtility('sqlcmd') quotedTable <- quoteTable(table) query <- sprintf('DROP TABLE %s;', quotedTable) - sqlcmdArgs <- append(mapConnectArgs(connectargs = connectargs, - utility = 'sqlcmd'), values = list('-Q', shQuote(query))) + sqlcmdArgs <- mapConnectArgs(connectargs = connectargs, utility = 'sqlcmd') + sqlcmdArgs <- append(sqlcmdArgs, values = list('-Q', shQuote(query))) system2(command = sqlcmd, args = sqlcmdArgs, ...) } #' @rdname createTable @@ -456,21 +458,14 @@ dropTable <- function(connectargs, table, ...) { #' checkTableExists <- function(connectargs, table) { sqlcmd <- findUtility('sqlcmd') - # IF OBJECT_ID('*objectName*', 'U') IS NOT NULL - quotedTable <- quoteTable(table) - quotedTable <- strsplit(x = quotedTable, split = '\\.')[[1]] - if (length(quotedTable) > 2) { - stop('Only `