Skip to content

Commit

Permalink
Support dbplyr.trace option
Browse files Browse the repository at this point in the history
  • Loading branch information
krlmlr committed Jul 4, 2022
1 parent 6233132 commit cd1902e
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 3 deletions.
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ Collate:
'testthat.R'
'tidyeval-across.R'
'tidyeval.R'
'trace.R'
'translate-sql.R'
'utils-format.R'
'verb-arrange.R'
Expand Down
58 changes: 58 additions & 0 deletions R/trace.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
tracing_level <- function() {
trace <- getOption("dbplyr.trace")
if (is.null(trace)) {
return(0L)
}
if (is.logical(trace)) {
return(as.integer(trace))
}
if (!is_integerish(trace)) {
warn("Invalid value for dbplyr.trace option, resetting.")
options(dbplyr.trace = NULL)
return(0)
}
as.integer(trace)
}

tracing_id <- function() {
# Needs to use option to unique IDs across reloads while testing
i <- getOption("dbplyr.trace_id", 0) + 1
options(dbplyr.trace_id = i)
i
}

dbGetQuery <- function(conn, statement, ...) {
level <- tracing_level()
id <- tracing_id()

if (level >= 1) {
message_base <- paste0("[", id, "]: dbGetQuery()")
message_pre <- paste0(message_base, "\n", statement)
message_post <- paste0(message_base, " done")
class <- c("dplyr_message_trace_get_query", "dplyr_message_trace", "dplyr_message")
inform(message_pre, class = class)
on.exit({
inform(message_post, class = class)
})
}

DBI::dbGetQuery(conn, statement, ...)
}

dbExecute <- function(conn, statement, ...) {
level <- tracing_level()
id <- tracing_id()

if (level >= 1) {
message_base <- paste0("[", id, "]: dbExecute()")
message_pre <- paste0(message_base, "\n", statement)
message_post <- paste0(message_base, " done")
class <- c("dplyr_message_trace_execute", "dplyr_message_trace", "dplyr_message")
inform(message_pre, class = class)
on.exit({
inform(message_post, class = class)
})
}

DBI::dbExecute(conn, statement, ...)
}
6 changes: 3 additions & 3 deletions tests/testthat/test-tbl-sql.R
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ test_that("sql tbl can be printed", {
test_that("can refer to default schema explicitly", {
con <- sqlite_con_with_aux()
on.exit(DBI::dbDisconnect(con))
DBI::dbExecute(con, "CREATE TABLE t1 (x)")
dbExecute(con, "CREATE TABLE t1 (x)")

expect_equal(as.character(tbl_vars(tbl(con, "t1"))), "x")
expect_equal(as.character(tbl_vars(tbl(con, in_schema("main", "t1")))), "x")
Expand All @@ -48,8 +48,8 @@ test_that("can refer to default schema explicitly", {
test_that("can distinguish 'schema.table' from 'schema'.'table'", {
con <- sqlite_con_with_aux()
on.exit(DBI::dbDisconnect(con))
DBI::dbExecute(con, "CREATE TABLE aux.t1 (x, y, z)")
DBI::dbExecute(con, "CREATE TABLE 'aux.t1' (a, b, c)")
dbExecute(con, "CREATE TABLE aux.t1 (x, y, z)")
dbExecute(con, "CREATE TABLE 'aux.t1' (a, b, c)")

expect_equal(as.character(tbl_vars(tbl(con, in_schema("aux", "t1")))), c("x", "y", "z"))
expect_equal(as.character(tbl_vars(tbl(con, ident("aux.t1")))), c("a", "b", "c"))
Expand Down

0 comments on commit cd1902e

Please sign in to comment.