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
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,7 @@ export(lahman_mysql)
export(lahman_postgres)
export(lahman_sqlite)
export(lahman_srcs)
export(last_sql)
export(lazy_base_query)
export(lazy_frame)
export(lazy_multi_join_query)
Expand Down
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# dbplyr (development version)

* New `last_sql()` retrieves the most recent SQL query generated by dbplyr, which is useful for debugging (#1471).
* Custom translations of functions starting with `.` work (@MichaelChirico, #1529).
* SQL Server 2025 (version 17.0) now supports stringr regex functions: `str_detect()`, `str_starts()`, `str_ends()`, `str_replace()`, `str_replace_all()`, `str_remove()`, `str_remove_all()`, `str_extract()`, and `str_count()`. Fixed pattern versions of `str_detect()`, `str_starts()`, and `str_ends()` work on all SQL Server versions (#1671).
* MS Access now correctly generates SQL for multiple joins by adding required parentheses (#1576).
Expand Down
16 changes: 9 additions & 7 deletions R/db.R
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,19 @@ db_sql_render <- function(con, sql, ..., cte = FALSE, sql_options = NULL) {
with = I("db_sql_render(sql_options = sql_options(cte = TRUE))")
)
sql_options <- sql_options %||% sql_options(cte = TRUE)
out <- db_sql_render(con, sql, ..., sql_options = sql_options)
return(out)
}

if (is.null(sql_options)) {
sql_options <- sql_options()
sql_options <- sql_options %||% sql_options()

out <- db_sql_render(con, sql, ..., sql_options = sql_options)
return(out)
}
out <- db_sql_render_dispatch(con, sql, ..., sql_options = sql_options)
the$last_sql <- out
return(out)

# Unusued declaration for roxygen2
UseMethod("db_sql_render")
}

db_sql_render_dispatch <- function(con, sql, ..., sql_options) {
UseMethod("db_sql_render")
}
#' @export
Expand Down
18 changes: 18 additions & 0 deletions R/remote.R
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,21 @@ remote_query <- function(x, cte = FALSE, sql_options = NULL) {
remote_query_plan <- function(x, ...) {
dbplyr_explain(remote_con(x), db_sql_render(remote_con(x), x$lazy_query), ...)
}

#' Retrieve the last SQL query generated
#'
#' This is a helper function that retrieves the most recent SQL query generated
#' by dbplyr, which can be useful for debugging.
#'
#' @return A SQL string, or `NULL` if no query has been generated yet.
#' @export
#' @examples
#' library(dplyr, warn.conflicts = FALSE)
#'
#' df <- lazy_frame(x = 1:3)
#' df |> filter(x > 1)
#'
#' last_sql()
last_sql <- function() {
the$last_sql
}
1 change: 1 addition & 0 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ reference:
- copy_inline
- pull.tbl_sql
- show_query
- last_sql
- .sql

- subtitle: Verbs that affect rows
Expand Down
23 changes: 23 additions & 0 deletions man/last_sql.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions tests/testthat/test-remote.R
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,13 @@ test_that("can retrieve query, src and con metadata", {
expect_s3_class(remote_query(mf), "sql")
expect_type(remote_query_plan(mf), "character")
})

test_that("last_sql() retrieves the most recent query", {
lf <- lazy_frame(x = 1:3, y = c("a", "b", "c"))

lf |> filter(x > 1) |> show_query()
expect_match(last_sql(), "WHERE")

lf |> mutate(z = x + 1) |> show_query()
expect_match(last_sql(), "\\+ 1")
})