从 tbl_dbi 中提取 SQL 作为字符串的首选方法是什么?
What is the preferred way to extract SQL from a tbl_dbi as a string?
我想从 tbl_dbi 中提取 SQL 并将其保存为字符向量。
我试过了
library(dplyr)
my_tbl_dbi <- tbl(conn, "myTable")
my_query <- capture.output(show_query(my_tbl_dbi), type = message) %>%
paste0(collapse = " ")
这在过去对我有用,但现在不起作用,可能是因为我在玩 sink 函数。我的问题是 "Is there a more robust/standard way to pull out the SQL query from a tbl_dbi or is what I am doing the only way to do it?"
as.character(db_sql_render(my_tbl_dbi$src$con, my_tbl_dbi))
注:
methods("show_query")
## [1] show_query.tbl_lazy* show_query.tbl_sql*
dbplyr:::show_query.tbl_sql
## function (x, ...)
## {
## message("<SQL>\n", db_sql_render(x$src$con, x))
## invisible(x)
## }
## <environment: namespace:dbplyr>
db_sql_render.DBIConnection()
的来源:
db_sql_render.DBIConnection <- function(con, sql, ...) {
qry <- sql_build(sql, con = con, ...)
sql_render(qry, con = con, ...)
}
我想从 tbl_dbi 中提取 SQL 并将其保存为字符向量。
我试过了
library(dplyr)
my_tbl_dbi <- tbl(conn, "myTable")
my_query <- capture.output(show_query(my_tbl_dbi), type = message) %>%
paste0(collapse = " ")
这在过去对我有用,但现在不起作用,可能是因为我在玩 sink 函数。我的问题是 "Is there a more robust/standard way to pull out the SQL query from a tbl_dbi or is what I am doing the only way to do it?"
as.character(db_sql_render(my_tbl_dbi$src$con, my_tbl_dbi))
注:
methods("show_query")
## [1] show_query.tbl_lazy* show_query.tbl_sql*
dbplyr:::show_query.tbl_sql
## function (x, ...)
## {
## message("<SQL>\n", db_sql_render(x$src$con, x))
## invisible(x)
## }
## <environment: namespace:dbplyr>
db_sql_render.DBIConnection()
的来源:
db_sql_render.DBIConnection <- function(con, sql, ...) {
qry <- sql_build(sql, con = con, ...)
sql_render(qry, con = con, ...)
}