Skip to content

Commit

Permalink
feat: Relax specification of dbUnquoteIdentifier(), character vecto…
Browse files Browse the repository at this point in the history
…rs are now allowed too
  • Loading branch information
krlmlr committed Dec 20, 2023
1 parent 37f2775 commit b810359
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 21 deletions.
40 changes: 27 additions & 13 deletions R/spec-sql-unquote-identifier.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,16 @@ spec_sql_unquote_identifier <- list(
letters_out <- dbUnquoteIdentifier(con, letters_in)
expect_equal(length(letters_out), length(letters_in))

#' For an empty character vector this function returns a length-0 object.
#' For an empty vector, this function returns a length-0 object.
empty <- character()
empty_in <- dbQuoteIdentifier(con, empty)
empty_out <- dbUnquoteIdentifier(con, empty_in)
expect_equal(length(empty_out), 0)

empty_in <- character()
empty_out <- dbUnquoteIdentifier(con, empty_in)
expect_equal(length(empty_out), 0)

#' The names of the input argument are preserved in the output.
unnamed_in <- dbQuoteIdentifier(con, letters)
unnamed_out <- dbUnquoteIdentifier(con, unnamed_in)
Expand All @@ -41,29 +45,39 @@ spec_sql_unquote_identifier <- list(
named_out <- dbUnquoteIdentifier(con, named_in)
expect_equal(names(named_out), letters[1:3])

#' When passing the first element of a returned object again to
#' `dbUnquoteIdentifier()` as `x`
#' argument, it is returned unchanged (but wrapped in a list).
#' If `x` is a value returned by `dbUnquoteIdentifier()`,
#' calling `dbUnquoteIdentifier(..., dbQuoteIdentifier(..., x))`
#' returns `list(x)`.
expect_identical(dbUnquoteIdentifier(con, simple_out[[1]]), simple_out)
expect_identical(dbUnquoteIdentifier(con, letters_out[[1]]), letters_out[1])
#' Passing objects of class [Id] should also return them unchanged (but wrapped in a list).
#' If `x` is an object of class [Id],
#' calling `dbUnquoteIdentifier(..., x)` returns `list(x)`.
expect_identical(dbUnquoteIdentifier(con, Id(table = "simple")), list(Id(table = "simple")))

#' (For backends it may be most convenient to return [Id] objects
#' to achieve this behavior, but this is not required.)
},
#'
unquote_identifier_error = function(ctx, con) {
unquote_identifier_plain = function(ctx, con) {
skip_if(ctx$tweaks$dbitest_version < "1.7.99.15")

#' Plain character vectors can also be passed to `dbUnquoteIdentifier()`.
expect_identical(dbUnquoteIdentifier(con, "a"), list(Id("a")))
expect_identical(dbUnquoteIdentifier(con, "a.b"), list(Id("a", "b")))
expect_identical(dbUnquoteIdentifier(con, "a.b.c"), list(Id("a", "b", "c")))
expect_identical(dbUnquoteIdentifier(con, "a.b.c.d"), list(Id("a", "b", "c", "d")))
},
#'
unquote_identifier_error = function(con) {
#' @section Failure modes:
#'
#' An error is raised if plain character vectors are passed as the `x`
#' argument.
#' An error is raised if a character vectors with a missing value is passed
#' as the `x` argument.
expect_error(dbUnquoteIdentifier(con, NA_character_))
expect_error(dbUnquoteIdentifier(con, c("a", NA_character_)))
expect_error(dbUnquoteIdentifier(con, character()))
},

unquote_identifier_roundtrip = function(ctx, con) {
unquote_identifier_roundtrip = function(con) {
#' @section Specification:
#' For any character vector of length one, quoting (with [dbQuoteIdentifier()])
#' then unquoting then quoting the first element is identical to just quoting.
Expand Down Expand Up @@ -124,7 +138,7 @@ spec_sql_unquote_identifier <- list(
},

#'
unquote_identifier_simple = function(ctx, con) {
unquote_identifier_simple = function(con) {
#' Unquoting simple strings (consisting of only letters) wrapped with [SQL()]
#' and then quoting via [dbQuoteIdentifier()] gives the same result as just
#' quoting the string.
Expand All @@ -138,10 +152,10 @@ spec_sql_unquote_identifier <- list(
unquote_identifier_table_schema = function(ctx, con) {
#' Similarly, unquoting expressions of the form `SQL("schema.table")`
#' and then quoting gives the same result as quoting the identifier
#' constructed by `Id(schema = "schema", table = "table")`.
#' constructed by `Id("schema", "table")`.
schema_in <- "schema"
table_in <- "table"
simple_quoted <- dbQuoteIdentifier(con, Id(schema = schema_in, table = table_in))
simple_quoted <- dbQuoteIdentifier(con, Id(schema_in, table_in))
simple_out <- dbUnquoteIdentifier(con, SQL(paste0(schema_in, ".", table_in)))
simple_roundtrip <- dbQuoteIdentifier(con, simple_out[[1]])
expect_identical(simple_roundtrip, simple_quoted)
Expand Down
19 changes: 11 additions & 8 deletions man/spec_sql_unquote_identifier.Rd

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

0 comments on commit b810359

Please sign in to comment.