-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add optional DuckDB query source
- Loading branch information
Showing
6 changed files
with
78 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
#' Create a new DuckDB query source | ||
#' @param srcname Path or URI to source | ||
#' @param ... Passed to [DBI::dbConnect]. | ||
#' @param .attach If `TRUE`, then `ATTACH <srcname>` is executed on the DuckDB connection | ||
#' @param .extensions Optional extensions to install and load on connection | ||
#' @returns An `hfsubset_query_source_duckdb` object | ||
#' @export | ||
query_source_duckdb <- function(srcname, ..., .attach = FALSE, .extensions = c("spatial")) { | ||
if (!requireNamespace("duckdb", quietly = TRUE)) { | ||
stop("Package `duckdb` is required for query_source_duckdb()") | ||
} | ||
|
||
conn <- DBI::dbConnect(duckdb::duckdb(), ...) | ||
|
||
for (ext in .extensions) { | ||
DBI::dbExecute(conn, paste("INSTALL", ext)) | ||
DBI::dbExecute(conn, paste("LOAD", ext)) | ||
} | ||
|
||
if (.attach) { | ||
DBI::dbExecute( | ||
conn, | ||
paste0("ATTACH '", srcname, "' AS hydrofabric (TYPE SQLITE)") | ||
) | ||
} | ||
|
||
structure( | ||
list(src = srcname, conn = conn), | ||
class = c("hfsubset_query_source_duckdb", "hfsubset_query_source") | ||
) | ||
} | ||
|
||
#' @method query_source_layer hfsubset_query_source_duckdb | ||
#' @keywords internal | ||
query_source_layer.hfsubset_query_source_duckdb <- function(query_source, layer, ..., query = NULL) { | ||
if (missing(layer) && !is.null(query)) { | ||
dplyr::tbl(query_source$conn, dbplyr::sql(query), ...) | ||
} else { | ||
dplyr::tbl(query_source$conn, paste0("hydrofabric.", layer), ...) | ||
} | ||
} | ||
|
||
#' @method query_source_layers hfsubset_query_source_duckdb | ||
#' @keywords internal | ||
query_source_layers.hfsubset_query_source_duckdb <- function(query_source, ...) { | ||
DBI::dbListTables(query_source$conn) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.