Skip to content

Commit

Permalink
oracle: tables: do not escape when using exact query (#712)
Browse files Browse the repository at this point in the history
* oracle: tables: do not escape when using exact query

* code-review: tests and news

---------

Co-authored-by: Hadley Wickham <h.wickham@gmail.com>
  • Loading branch information
detule and hadley authored Jan 15, 2024
1 parent 181e120 commit 3740969
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
6 changes: 6 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# odbc (development version)

* Oracle: Fix checking for existence when identifier components
contain underscores (@detule, #712).

* databricks: Fix schema enumeration in connections pane
(@detule, #715).

* SQL Server Fix `dbExists` for temporary table (@meztez, #724).

# odbc 1.4.1
Expand Down
10 changes: 7 additions & 3 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,13 @@ convertWildCards <- function(val) {
gsub("_", "(.)", val)
}

# Will return " AND key [= | LIKE] value"
# where "=" or "LIKE" is chosen depending
# on the the `exact` argument, and whether
# the `value` contains any wild card characters.
#
# Note, wild cards are not escaped in `value`
# as part of this call.
getSelector <- function(key, value, exact) {
if (is.null(value)) {
return("")
Expand All @@ -112,9 +119,6 @@ getSelector <- function(key, value, exact) {
isPatternValue(value)) {
comp <- " LIKE "
}
if (exact && (value != "%")) {
value <- escapePattern(value)
}
value <- paste0("'", value, "'")

paste0(" AND ", key, comp, value)
Expand Down
17 changes: 17 additions & 0 deletions tests/testthat/test-utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,21 @@ test_that("id_field checks inputs", {
id_field(DBI::Id("a"), "foo")
id_field(DBI::Id("a", "b", "c", "d"))
})

test_that("getSelector", {
# If no wild cards are detected always use exact comparison / ignore `exact` argument
expect_equal(getSelector("mykey", "myvalue", exact = TRUE), " AND mykey = 'myvalue'")
expect_equal(getSelector("mykey", "myvalue", exact = FALSE), " AND mykey = 'myvalue'")

# If `value` contains wild cards, respect `exact`argument
expect_equal(getSelector("mykey", "myvalu_", exact = TRUE), " AND mykey = 'myvalu_'")
expect_equal(getSelector("mykey", "myvalu_", exact = FALSE), " AND mykey LIKE 'myvalu_'")

expect_equal(getSelector("mykey", "myvalu%", exact = TRUE), " AND mykey = 'myvalu%'")
expect_equal(getSelector("mykey", "myvalu%", exact = FALSE), " AND mykey LIKE 'myvalu%'")

# ... unless argument is '%' - always use 'LIKE' since this is most likely the
# desired comparison / ignore `exact` argument
expect_equal(getSelector("mykey", "%", exact = TRUE), " AND mykey LIKE '%'")
expect_equal(getSelector("mykey", "%", exact = FALSE), " AND mykey LIKE '%'")
})

0 comments on commit 3740969

Please sign in to comment.