Skip to content

Commit

Permalink
allow quoting of unnamed Id() components
Browse files Browse the repository at this point in the history
  • Loading branch information
dpprdan committed Dec 21, 2023
1 parent 0f62fb0 commit 6daebc7
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 22 deletions.
7 changes: 1 addition & 6 deletions R/dbQuoteIdentifier_PqConnection_Id.R
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
#' @name quote
#' @usage NULL
dbQuoteIdentifier_PqConnection_Id <- function(conn, x, ...) {
components <- c("catalog", "schema", "table", "column")
stopifnot(all(names(x@name) %in% components))
stopifnot(!anyDuplicated(names(x@name)))

ret <- stats::na.omit(x@name[match(components, names(x@name))])
SQL(paste0(dbQuoteIdentifier(conn, ret), collapse = "."))
SQL(paste0(dbQuoteIdentifier(conn, x@name), collapse = "."))
}

#' @rdname quote
Expand Down
66 changes: 50 additions & 16 deletions tests/testthat/test-dbQuoteIdentifier.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,40 +16,74 @@ test_that("quoting SQL", {
"Robert'); DROP TABLE Students;--")
})

test_that("quoting Id", {
test_that("quoting Id, table", {
con <- postgresDefault()

quoted <- dbQuoteIdentifier(con, Id(table = 'Students;--'))
expect_s4_class(quoted, 'SQL')
expect_equal(as.character(quoted), '"Students;--"')
})

test_that("quoting Id, table, unnamed", {
con <- postgresDefault()

quoted <- dbQuoteIdentifier(con, Id('Students;--'))
expect_s4_class(quoted, 'SQL')
expect_equal(as.character(quoted), '"Students;--"')
})

test_that("quoting Id, schema", {
con <- postgresDefault()

quoted <- dbQuoteIdentifier(con, Id(schema = 'Robert'))
expect_s4_class(quoted, 'SQL')
expect_equal(as.character(quoted), '"Robert"')
})

test_that("quoting Id, schema, unnamed", {
con <- postgresDefault()

quoted <- dbQuoteIdentifier(con, Id('Robert'))
expect_s4_class(quoted, 'SQL')
expect_equal(as.character(quoted), '"Robert"')
})

test_that("quoting Id, fully-qualified table", {
con <- postgresDefault()

quoted <- dbQuoteIdentifier(con, Id(schema = 'Robert', table = 'Students;--'))
expect_s4_class(quoted, 'SQL')
expect_equal(as.character(quoted),
'"Robert"."Students;--"')
expect_equal(as.character(quoted), '"Robert"."Students;--"')
})

test_that("quoting Id with column, #263", {
test_that("quoting Id, fully-qualified column, #263", {
con <- postgresDefault()

quoted <- dbQuoteIdentifier(con, Id(schema = 'Robert', table = 'Students;--', column = "dormitory"))
quoted <-
dbQuoteIdentifier(
con,
Id(schema = "Robert", table = "Students;--", column = "dormitory")
)
expect_s4_class(quoted, 'SQL')
expect_equal(as.character(quoted),
'"Robert"."Students;--"."dormitory"')
expect_equal(as.character(quoted), '"Robert"."Students;--"."dormitory"')
})

test_that("quoting Id with column, unordered", {
test_that("quoting Id, column, unordered", {
con <- postgresDefault()

quoted <- dbQuoteIdentifier(con, Id(column = "dormitory", table = 'Students;--'))
quoted <-
dbQuoteIdentifier(con, Id(column = "dormitory", table = 'Students;--'))
expect_s4_class(quoted, 'SQL')
expect_equal(as.character(quoted),
'"Students;--"."dormitory"')
expect_equal(as.character(quoted), '"Students;--"."dormitory"')
})

test_that("quoting errors", {
test_that("quoting Id, fully-qualified column, unnamed", {
con <- postgresDefault()

expect_error(dbQuoteIdentifier(con, Id(tabel = 'Robert')),
"components")
expect_error(dbQuoteIdentifier(con, Id(table = 'Robert', table = 'Students;--')),
"Duplicated")
quoted <-
dbQuoteIdentifier(con, Id('Robert', 'Students;--', "dormitory"))
expect_s4_class(quoted, 'SQL')
expect_equal(as.character(quoted), '"Robert"."Students;--"."dormitory"')
})

test_that("unquoting identifier - SQL with quotes", {
Expand Down

0 comments on commit 6daebc7

Please sign in to comment.