我该如何编写这个函数的逻辑,它应该能够做两件事。
get_data <- function(database, table=NULL, query=NULL){
rlang::check_required(
x = database
)
if(is.null(query) & !is.null(table)){
# check whether code is piped %>% | |>
query <- dbplyr::sql_render("piped code")
}
return(query)
}
接受SQL查询
get_data(
database = "uspto",
query = "select * from applications where id = 1"
)
认识到 Query 为 NULL 并且 Table 不为 NULL,因此执行一些检查或传递到管道代码。
get_data(
database = "uspto",
table = "applications"
) %>%
filter(id == 1) %>%
collect()
为了了解更多上下文,此函数将与接受 SQL 作为输入的 API 进行交互。客户端和 API 之间将有一个网络/微服务层。因此,我可能需要执行如下操作来创建一个虚拟连接,然后我可以使用
dbplyr::sql_render
来获取查询字符串。
con <- memdb_frame(
database,
.name = table
)
我认为这已经足够好了。
get_data <- function(database, table = NULL, query = NULL) {
if (is.null(query)) {
tbl(database, table)
} else {
tbl(database sql(query))
}
有了这个,你就可以做到
con <- DBI::dbConnect(...)
get_data(con, table = "MyTable") |>
head() |>
collect()
get_data(con, query = "select * from MyTable limit 5") |>
collect()
(如果您使用的是 SQL Server,显然该查询应该类似于
"select top 5 * from MyTable"
。)