From 6bd835ff22f309d8aa6397df2f53c2dd5c506665 Mon Sep 17 00:00:00 2001 From: Daniel Possenriede Date: Thu, 19 Nov 2020 14:00:13 +0100 Subject: [PATCH 1/8] dbQuoteIdentifier: allow columns --- R/quote.R | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/R/quote.R b/R/quote.R index 07ad22e8..1a6ee314 100644 --- a/R/quote.R +++ b/R/quote.R @@ -55,20 +55,12 @@ setMethod("dbQuoteIdentifier", c("PqConnection", "SQL"), function(conn, x, ...) #' @export #' @rdname quote setMethod("dbQuoteIdentifier", c("PqConnection", "Id"), function(conn, x, ...) { - stopifnot(all(names(x@name) %in% c("catalog", "schema", "table"))) + elements <- c("catalog", "schema", "table", "column") + stopifnot(all(names(x@name) %in% elements)) stopifnot(!anyDuplicated(names(x@name))) - ret <- "" - if ("catalog" %in% names(x@name)) { - ret <- paste0(ret, dbQuoteIdentifier(conn, x@name[["catalog"]]), ".") - } - if ("schema" %in% names(x@name)) { - ret <- paste0(ret, dbQuoteIdentifier(conn, x@name[["schema"]]), ".") - } - if ("table" %in% names(x@name)) { - ret <- paste0(ret, dbQuoteIdentifier(conn, x@name[["table"]])) - } - SQL(ret) + ret <- na.omit(x@name[match(elements, names(x@name))]) + SQL(paste0(dbQuoteIdentifier(conn, ret), collapse = ".")) }) #' @export From ecaa6cf4b3cae458e0d5ee2eed0a6a573a113b82 Mon Sep 17 00:00:00 2001 From: Daniel Possenriede Date: Mon, 27 Dec 2021 10:30:38 +0100 Subject: [PATCH 2/8] Revert "dbQuoteIdentifier: allow columns" This reverts commit 6bd835ff22f309d8aa6397df2f53c2dd5c506665. --- R/quote.R | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/R/quote.R b/R/quote.R index 1a6ee314..07ad22e8 100644 --- a/R/quote.R +++ b/R/quote.R @@ -55,12 +55,20 @@ setMethod("dbQuoteIdentifier", c("PqConnection", "SQL"), function(conn, x, ...) #' @export #' @rdname quote setMethod("dbQuoteIdentifier", c("PqConnection", "Id"), function(conn, x, ...) { - elements <- c("catalog", "schema", "table", "column") - stopifnot(all(names(x@name) %in% elements)) + stopifnot(all(names(x@name) %in% c("catalog", "schema", "table"))) stopifnot(!anyDuplicated(names(x@name))) - ret <- na.omit(x@name[match(elements, names(x@name))]) - SQL(paste0(dbQuoteIdentifier(conn, ret), collapse = ".")) + ret <- "" + if ("catalog" %in% names(x@name)) { + ret <- paste0(ret, dbQuoteIdentifier(conn, x@name[["catalog"]]), ".") + } + if ("schema" %in% names(x@name)) { + ret <- paste0(ret, dbQuoteIdentifier(conn, x@name[["schema"]]), ".") + } + if ("table" %in% names(x@name)) { + ret <- paste0(ret, dbQuoteIdentifier(conn, x@name[["table"]])) + } + SQL(ret) }) #' @export From c6bb65145348b1d02c328c6670bed5624ee50e74 Mon Sep 17 00:00:00 2001 From: Daniel Possenriede Date: Tue, 28 Dec 2021 14:42:49 +0100 Subject: [PATCH 3/8] allow columns in dbQuoteIdentifier() --- R/dbQuoteIdentifier_PqConnection_Id.R | 16 ++++----------- tests/testthat/test-dbQuoteIdentifier.R | 27 +++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 12 deletions(-) diff --git a/R/dbQuoteIdentifier_PqConnection_Id.R b/R/dbQuoteIdentifier_PqConnection_Id.R index 86573b4c..aad5e41d 100644 --- a/R/dbQuoteIdentifier_PqConnection_Id.R +++ b/R/dbQuoteIdentifier_PqConnection_Id.R @@ -1,20 +1,12 @@ #' @name quote #' @usage NULL dbQuoteIdentifier_PqConnection_Id <- function(conn, x, ...) { - stopifnot(all(names(x@name) %in% c("catalog", "schema", "table"))) + components <- c("catalog", "schema", "table", "column") + stopifnot(all(names(x@name) %in% components)) stopifnot(!anyDuplicated(names(x@name))) - ret <- "" - if ("catalog" %in% names(x@name)) { - ret <- paste0(ret, dbQuoteIdentifier(conn, x@name[["catalog"]]), ".") - } - if ("schema" %in% names(x@name)) { - ret <- paste0(ret, dbQuoteIdentifier(conn, x@name[["schema"]]), ".") - } - if ("table" %in% names(x@name)) { - ret <- paste0(ret, dbQuoteIdentifier(conn, x@name[["table"]])) - } - SQL(ret) + ret <- na.omit(x@name[match(components, names(x@name))]) + SQL(paste0(dbQuoteIdentifier(conn, ret), collapse = ".")) } #' @rdname quote diff --git a/tests/testthat/test-dbQuoteIdentifier.R b/tests/testthat/test-dbQuoteIdentifier.R index c18f1503..8bdde1f9 100644 --- a/tests/testthat/test-dbQuoteIdentifier.R +++ b/tests/testthat/test-dbQuoteIdentifier.R @@ -27,6 +27,33 @@ test_that("quoting Id", { '"Robert"."Students;--"') }) +test_that("quoting Id with column, #263", { + con <- postgresDefault() + + quoted <- dbQuoteIdentifier(con, Id(schema = 'Robert', table = 'Students;--', column = "dormitory")) + expect_s4_class(quoted, 'SQL') + expect_equal(as.character(quoted), + '"Robert"."Students;--"."dormitory"') +}) + +test_that("quoting Id with column, unordered", { + con <- postgresDefault() + + quoted <- dbQuoteIdentifier(con, Id(column = "dormitory", table = 'Students;--')) + expect_s4_class(quoted, 'SQL') + expect_equal(as.character(quoted), + '"Students;--"."dormitory"') +}) + +test_that("quoting errors", { + con <- postgresDefault() + + expect_error(dbQuoteIdentifier(con, Id(tabel = 'Robert')), + "components") + expect_error(dbQuoteIdentifier(con, Id(table = 'Robert', table = 'Students;--')), + "Duplicated") +}) + test_that("unquoting identifier - SQL with quotes", { con <- postgresDefault() From c0aca74c0621bea663a3d7f4b20648ca5020a16e Mon Sep 17 00:00:00 2001 From: Daniel Possenriede Date: Tue, 28 Dec 2021 22:31:13 +0100 Subject: [PATCH 4/8] qualify na.omit() --- R/dbQuoteIdentifier_PqConnection_Id.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/dbQuoteIdentifier_PqConnection_Id.R b/R/dbQuoteIdentifier_PqConnection_Id.R index aad5e41d..69840435 100644 --- a/R/dbQuoteIdentifier_PqConnection_Id.R +++ b/R/dbQuoteIdentifier_PqConnection_Id.R @@ -5,7 +5,7 @@ dbQuoteIdentifier_PqConnection_Id <- function(conn, x, ...) { stopifnot(all(names(x@name) %in% components)) stopifnot(!anyDuplicated(names(x@name))) - ret <- na.omit(x@name[match(components, names(x@name))]) + ret <- stats::na.omit(x@name[match(components, names(x@name))]) SQL(paste0(dbQuoteIdentifier(conn, ret), collapse = ".")) } From 6daebc78a810d61a712f71ec6fdc9029bfe47cb8 Mon Sep 17 00:00:00 2001 From: Daniel Possenriede Date: Thu, 21 Dec 2023 11:27:22 +0100 Subject: [PATCH 5/8] allow quoting of unnamed `Id()` components re https://github.com/r-dbi/DBI/pull/417 --- R/dbQuoteIdentifier_PqConnection_Id.R | 7 +-- tests/testthat/test-dbQuoteIdentifier.R | 66 +++++++++++++++++++------ 2 files changed, 51 insertions(+), 22 deletions(-) diff --git a/R/dbQuoteIdentifier_PqConnection_Id.R b/R/dbQuoteIdentifier_PqConnection_Id.R index 69840435..b13851f4 100644 --- a/R/dbQuoteIdentifier_PqConnection_Id.R +++ b/R/dbQuoteIdentifier_PqConnection_Id.R @@ -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 diff --git a/tests/testthat/test-dbQuoteIdentifier.R b/tests/testthat/test-dbQuoteIdentifier.R index 82ac9aa8..fd64c9ab 100644 --- a/tests/testthat/test-dbQuoteIdentifier.R +++ b/tests/testthat/test-dbQuoteIdentifier.R @@ -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", { From 81eb4726d842d1ab117ee08e9255773031bbf1fc Mon Sep 17 00:00:00 2001 From: Daniel Possenriede Date: Thu, 21 Dec 2023 12:12:31 +0100 Subject: [PATCH 6/8] bump DBI and DBItest versions --- DESCRIPTION | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 075442ef..2803d4bb 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -22,7 +22,7 @@ Depends: Imports: bit64, blob (>= 1.2.0), - DBI (>= 1.1.0), + DBI (>= 1.2.0), hms (>= 1.0.0), lubridate, methods, @@ -30,7 +30,7 @@ Imports: Suggests: callr, covr, - DBItest (>= 1.7.2.9001), + DBItest (>= 1.7.3.9016), knitr, rlang, rmarkdown, From 26d9862ecb54bec5c09a780b54eb289dc7b62c21 Mon Sep 17 00:00:00 2001 From: Daniel Possenriede Date: Thu, 21 Dec 2023 12:44:50 +0100 Subject: [PATCH 7/8] add `Remotes:` for DBItest --- DESCRIPTION | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/DESCRIPTION b/DESCRIPTION index 2803d4bb..e52bbe1f 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -30,7 +30,7 @@ Imports: Suggests: callr, covr, - DBItest (>= 1.7.3.9016), + DBItest (>= 1.7.3), knitr, rlang, rmarkdown, @@ -106,3 +106,5 @@ Collate: 'tables.R' 'transactions.R' 'utils.R' +Remotes: + r-dbi/DBItest From 153ec24bca043a0efc5fc678305fd58c7a6353e0 Mon Sep 17 00:00:00 2001 From: Daniel Possenriede Date: Tue, 23 Jan 2024 14:38:18 +0100 Subject: [PATCH 8/8] remove `Remotes: r-dbi/DBItest` --- DESCRIPTION | 2 -- 1 file changed, 2 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index e52bbe1f..3307306b 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -106,5 +106,3 @@ Collate: 'tables.R' 'transactions.R' 'utils.R' -Remotes: - r-dbi/DBItest