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

Synapse Support #23

Merged
merged 13 commits into from
Apr 3, 2024
Merged
1 change: 1 addition & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ _pkgdown.yml
^inst/tests$
TODO.md
^.lintr$
^CRAN-SUBMISSION$
4 changes: 2 additions & 2 deletions .github/workflows/R-CMD-check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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: |
Expand Down
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -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 = "[email protected]", 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
Expand Down
7 changes: 7 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
78 changes: 48 additions & 30 deletions R/bcp.R
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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
Expand All @@ -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 = ' '))
}
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -456,30 +458,23 @@ 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 `<table>` and `<schema>.<table>` are supported for table
argument.')
}
if (length(quotedTable) < 2) {
quotedTable <- append(quotedTable, '[dbo]', after = 0)
}
query <- sprintf('EXECUTE sp_tables @table_name = %s, @table_owner = %s',
quotedTable[2], quotedTable[1])
sqlcmdArgs <- append(mapConnectArgs(connectargs = connectargs,
utility = 'sqlcmd'), values = list('-Q', shQuote(query)))
system2(command = sqlcmd, args = sqlcmdArgs, stdout = TRUE)[[3]] != ''
query <- sprintf("
IF OBJECT_ID('%s', 'U') IS NOT NULL
BEGIN PRINT 1 END
ELSE
BEGIN PRINT 0 END", unQuoteTable(table))
sqlcmdArgs <- mapConnectArgs(connectargs = connectargs, utility = 'sqlcmd')
sqlcmdArgs <- append(sqlcmdArgs, values = list('-Q', shQuote(query)))
identical(system2(command = sqlcmd, args = sqlcmdArgs, stdout = TRUE)[[1]], '1')
}
readTable <- function(connectargs, table, ...) {
sqlcmd <- findUtility('sqlcmd')
quotedTable <- quoteTable(table)
query <- sprintf('SET NOCOUNT ON; SELECT * FROM %s;', quotedTable)
queryHeaders <- sprintf('SET NOCOUNT ON; SELECT TOP 0 * FROM %s;',
quotedTable)
sqlcmdArgs <- append(mapConnectArgs(connectargs = connectargs,
utility = 'sqlcmd'),
sqlcmdArgs <- mapConnectArgs(connectargs = connectargs, utility = 'sqlcmd')
sqlcmdArgs <- append(sqlcmdArgs,
values = list(
'-s', shQuote(','),
'-W',
Expand Down Expand Up @@ -526,13 +521,19 @@ readTable <- function(connectargs, table, ...) {
#' use Azure Active Directory authentication, does not work with integrated
#' authentication.
#'
#' @param quotedidentifiers
#'
#' set QUOTED_IDENTIFIERS option to 'ON' for the connection between bcp/sqlcmd
#' and SQL Server.
#'
#' @return
#'
#' a list with connection arguments
#'
#' @export
makeConnectArgs <- function(server, database, username, password,
trustedconnection = TRUE, trustservercert = FALSE, azure = FALSE) {
trustedconnection = TRUE, trustservercert = FALSE, azure = FALSE,
quotedidentifiers = FALSE) {
if (isTRUE(trustedconnection) && isTRUE(azure)) {
stop('trustedconnection and azure cannot both be TRUE')
}
Expand All @@ -551,6 +552,10 @@ makeConnectArgs <- function(server, database, username, password,
if (isTRUE(trustservercert)) {
connectArgs <- append(x = connectArgs,
values = list(trustservercert = trustservercert))
}
if (isTRUE(quotedidentifiers)) {
connectArgs <- append(x = connectArgs,
values = list(quotedidentifiers = quotedidentifiers))
}
connectArgs
}
Expand All @@ -567,6 +572,17 @@ quoteTable <- function(table) {
sprintf('[%s]', x)
}), collapse = '.')
}
unQuoteTable <- function(table) {
paste(lapply(strsplit(table, split = '\\.')[[1]], function(x) {
if (substring(text = x, first = 1, last = 1) == '[' &&
substring(text = x,
first = nchar(x),
last = nchar(x)) == ']') {
return(substring(text = x, first = 2, last = nchar(x) - 1))
}
x
}), collapse = '.')
}
convertGeoCol <- function(connectargs, table, geometrycol, binarycol,
spatialtype, srid, ...) {
sqlcmd <- findUtility(utility = 'sqlcmd')
Expand Down Expand Up @@ -600,14 +616,16 @@ mapConnectArgs <- function(connectargs, utility = c('sqlcmd', 'bcp')
username = '-U',
password = '-P',
azure = '-G',
trustservercert = '-C'),
trustservercert = '-C',
quotedidentifiers = '-I'),
bcp = list(server = '-S',
database = '-d',
trustedconnection = '-T',
username = '-U',
password = '-P',
azure = '-G',
trustservercert = '-u'),
trustservercert = '-u',
quotedidentifiers = '-q'),
stop('Unsupported utility')
)
argSyntax <- argSyntax[names(connectargs)]
Expand Down
Binary file modified R/sysdata.rda
Binary file not shown.
2 changes: 1 addition & 1 deletion docs/404.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/LICENSE-text.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/LICENSE.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions docs/authors.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading