Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# dbplyr (development version)

* Oracle: Added support for `str_replace()` and `str_replace_all()` via
`REGEXP_REPLACE()` (@thomashulst, #1402).

* Allow additional arguments to be passed from `compute()` all the way to
`sql_query_save()`-method (@rsund).

Expand Down
10 changes: 10 additions & 0 deletions R/backend-oracle.R
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,16 @@ sql_translation.Oracle <- function(con) {
paste0 = sql_paste_infix("", "||", function(x) sql_expr(cast(!!x %as% text))),
str_c = sql_paste_infix("", "||", function(x) sql_expr(cast(!!x %as% text))),

# https://docs.oracle.com/en/database/oracle/oracle-database/19/sqlrf/REGEXP_REPLACE.html
# 4th argument is starting position (default: 1 => first char of string)
# 5th argument is occurrence (default: 0 => match all occurrences)
str_replace = function(string, pattern, replacement){
sql_expr(regexp_replace(!!string, !!pattern, !!replacement, 1L, 1L))
},
str_replace_all = function(string, pattern, replacement){
sql_expr(regexp_replace(!!string, !!pattern, !!replacement))
},

# lubridate --------------------------------------------------------------
today = function() sql_expr(TRUNC(CURRENT_TIMESTAMP)),
now = function() sql_expr(CURRENT_TIMESTAMP)
Expand Down
11 changes: 11 additions & 0 deletions tests/testthat/_snaps/backend-oracle.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
# string functions translate correctly

Code
test_translate_sql(str_replace(col, "pattern", "replacement"))
Output
<SQL> REGEXP_REPLACE(`col`, 'pattern', 'replacement', 1, 1)
Code
test_translate_sql(str_replace_all(col, "pattern", "replacement"))
Output
<SQL> REGEXP_REPLACE(`col`, 'pattern', 'replacement')

# queries translate correctly

Code
Expand Down
10 changes: 10 additions & 0 deletions tests/testthat/test-backend-oracle.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@ test_that("paste and paste0 translate correctly", {
expect_equal(test_translate_sql(str_c(x, y)), sql("`x` || `y`"))
})


test_that("string functions translate correctly", {
local_con(simulate_oracle())

expect_snapshot({
test_translate_sql(str_replace(col, "pattern", "replacement"))
test_translate_sql(str_replace_all(col, "pattern", "replacement"))
})
})

test_that("queries translate correctly", {
mf <- lazy_frame(x = 1, con = simulate_oracle())
expect_snapshot(mf %>% head())
Expand Down