Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #422 from r-dbi/unquote
- Loading branch information
Showing
5 changed files
with
52 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,37 @@ | ||
#' @rdname hidden_aliases | ||
#' @usage NULL | ||
dbUnquoteIdentifier_DBIConnection <- function(conn, x, ...) { | ||
if (is(x, "SQL")) { | ||
id_rx <- '(?:"((?:[^"]|"")+)"|([^". ]+))' | ||
# Determine quoting character | ||
quote_char <- substr(dbQuoteIdentifier(conn, ""), 1, 1) | ||
|
||
rx <- paste0( | ||
"^", | ||
"(?:|(?:|", id_rx, "[.])", | ||
id_rx, "[.])", | ||
"(?:|", id_rx, ")", | ||
"$" | ||
) | ||
if (is(x, "SQL") || is.character(x)) { | ||
x <- lapply(x, unquote, quote_char = quote_char) | ||
lapply(x, Id) | ||
} else if (is(x, "Id")) { | ||
list(x) | ||
} else { | ||
stop("x must be SQL, Id, or character", call. = FALSE) | ||
} | ||
} | ||
|
||
bad <- grep(rx, x, invert = TRUE) | ||
if (length(bad) > 0) { | ||
stop("Can't unquote ", x[bad[[1]]], call. = FALSE) | ||
} | ||
catalog <- gsub(rx, "\\1\\2", x) | ||
catalog <- gsub('""', '"', catalog) | ||
schema <- gsub(rx, "\\3\\4", x) | ||
schema <- gsub('""', '"', schema) | ||
table <- gsub(rx, "\\5\\6", x) | ||
table <- gsub('""', '"', table) | ||
unquote <- function(x, quote_char) { | ||
# replace doubled quotes with escaped quote | ||
gsub <- gsub( | ||
pattern = paste0(quote_char, quote_char), | ||
replacement = paste0("\\", quote_char), | ||
x | ||
) | ||
|
||
ret <- Map(catalog, schema, table, f = as_table) | ||
names(ret) <- names(x) | ||
return(ret) | ||
} | ||
if (is(x, "Id")) { | ||
return(list(x)) | ||
} | ||
stop("x must be SQL or Id", call. = FALSE) | ||
scan( | ||
text = x, | ||
what = character(), | ||
quote = quote_char, | ||
quiet = TRUE, | ||
na.strings = character(), | ||
sep = "." | ||
) | ||
} | ||
|
||
#' @rdname hidden_aliases | ||
#' @export | ||
setMethod("dbUnquoteIdentifier", signature("DBIConnection"), dbUnquoteIdentifier_DBIConnection) | ||
|
||
as_table <- function(catalog, schema, table) { | ||
args <- c(catalog = catalog, schema = schema, table = table) | ||
# Also omits NA args | ||
args <- args[!is.na(args) & args != ""] | ||
do.call(Id, as.list(args)) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
test_that("can unquote any number of components", { | ||
expect_equal(dbUnquoteIdentifier(ANSI(), "a"), list(Id("a"))) | ||
expect_equal(dbUnquoteIdentifier(ANSI(), "a.b"), list(Id("a", "b"))) | ||
expect_equal(dbUnquoteIdentifier(ANSI(), "a.b.c"), list(Id("a", "b", "c"))) | ||
expect_equal(dbUnquoteIdentifier(ANSI(), "a.b.c.d"), list(Id("a", "b", "c", "d"))) | ||
}) | ||
|
||
test_that("can unquote any quoted components", { | ||
expect_equal(dbUnquoteIdentifier(ANSI(), '"a.b"'), list(Id("a.b"))) | ||
|
||
expect_equal(dbUnquoteIdentifier(ANSI(), 'a."b"'), list(Id("a", "b"))) | ||
expect_equal(dbUnquoteIdentifier(ANSI(), '"a".b'), list(Id("a", "b"))) | ||
expect_equal(dbUnquoteIdentifier(ANSI(), '"a"."b"'), list(Id("a", "b"))) | ||
|
||
expect_equal(dbUnquoteIdentifier(ANSI(), '"a"""."b"""'), list(Id('a"', 'b"'))) | ||
}) | ||
|
||
test_that("Id is unchanged and wrapped in list", { | ||
expect_equal(dbUnquoteIdentifier(ANSI(), Id("foo")), list(Id("foo"))) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters