编写自定义延迟评估函数(如 dbplyr)来获取 SQL

问题描述 投票:0回答:1

我该如何编写这个函数的逻辑,它应该能够做两件事。

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
)
r dplyr tidyverse dbplyr
1个回答
0
投票

我认为这已经足够好了。

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"
。)

© www.soinside.com 2019 - 2024. All rights reserved.