Skip to content

Commit

Permalink
default implementations of dbQuoteIdentifier() and dbQuoteLiteral() p…
Browse files Browse the repository at this point in the history
…reserve names, closes #173

- Default implementations of `dbQuoteIdentifier()` and `dbQuoteLiteral()` preserve names, default implementation of `dbQuoteString()` strips names (#173).
  • Loading branch information
krlmlr committed Jan 27, 2018
1 parent 75778a9 commit a46249e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
8 changes: 4 additions & 4 deletions R/quote.R
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,10 @@ quote_identifier <-
# Avoid fixed = TRUE due to https://github.com/r-dbi/DBItest/issues/156
x <- gsub('"', '""', enc2utf8(x))
if (length(x) == 0L) {
SQL(character())
SQL(character(), names = names(x))
} else {
# Not calling encodeString() here to keep things simple
SQL(paste('"', x, '"', sep = ""))
SQL(paste('"', x, '"', sep = ""), names = names(x))
}
}

Expand Down Expand Up @@ -285,12 +285,12 @@ setMethod("dbQuoteLiteral", signature("DBIConnection"),
},
character(1)
)
return(SQL(blob_data))
return(SQL(blob_data, names = names(x)))
}

if (is.logical(x)) x <- as.numeric(x)
x <- as.character(x)
x[is.na(x)] <- "NULL"
SQL(x)
SQL(x, names = names(x))
}
)
9 changes: 9 additions & 0 deletions tests/testthat/test-quote.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@ test_that("identifier", {
expect_equal(dbQuoteIdentifier(ANSI(), SQL('"a b"')), SQL('"a b"'))
})

test_that("identifier names", {
expect_equal(dbQuoteIdentifier(ANSI(), setNames(character(), character())), SQL(character(), names = character()))
expect_equal(dbQuoteIdentifier(ANSI(), c(a = "a")), SQL('"a"', names = "a"))
expect_equal(dbQuoteIdentifier(ANSI(), c(ab = "a b")), SQL('"a b"', names = "ab"))

expect_equal(dbQuoteIdentifier(ANSI(), SQL('"a"', names = "a")), SQL('"a"', names = "a"))
expect_equal(dbQuoteIdentifier(ANSI(), SQL('"a b"', names = "ab")), SQL('"a b"', names = "ab"))
})

test_that("SQL names", {
expect_null(names(SQL(letters)))
expect_equal(names(SQL(letters, names = LETTERS)), LETTERS)
Expand Down

0 comments on commit a46249e

Please sign in to comment.