Skip to content

Commit 7c55805

Browse files
committed
feat: add is_view helper fn
1 parent 7076a34 commit 7c55805

3 files changed

Lines changed: 53 additions & 11 deletions

File tree

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export(dbList)
2020
export(dbLoad)
2121
export(dbProject)
2222
export(dbReconnect)
23+
export(is_view)
2324
export(read_pin_conn)
2425
export(to_view)
2526
export(unique_table_name)

R/input-validation.R

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -73,17 +73,7 @@
7373
}
7474

7575
if (overwrite && object_exists) {
76-
# Check if it's a view
77-
is_view <- DBI::dbGetQuery(
78-
conn,
79-
glue::glue(
80-
"SELECT COUNT(*) > 0 AS is_view
81-
FROM duckdb_views()
82-
WHERE view_name = '{name}'"
83-
)
84-
)$is_view
85-
86-
if (is_view) {
76+
if (is_view(conn, name)) {
8777
DBI::dbExecute(conn, glue::glue("DROP VIEW IF EXISTS {name}"))
8878
} else {
8979
DBI::dbRemoveTable(conn, name)
@@ -93,6 +83,37 @@
9383
invisible(NULL)
9484
}
9585

86+
#' Check if a database object is a VIEW
87+
#' @param conn DBI connection (must be duckdb)
88+
#' @param name Character, object name to check
89+
#' @return Logical, TRUE if the object is a VIEW
90+
#' @keywords internal
91+
#' @export
92+
is_view <- function(conn, name) {
93+
if (!inherits(conn, "duckdb_connection")) {
94+
stop("conn must be a duckdb connection")
95+
}
96+
if (!is.character(name) || length(name) != 1L || is.na(name)) {
97+
stop("name must be a single non-NA character string")
98+
}
99+
100+
res <- DBI::dbGetQuery(
101+
conn,
102+
glue::glue(
103+
"SELECT COUNT(*) > 0 AS is_view
104+
FROM duckdb_views()
105+
WHERE view_name = '{name}'"
106+
)
107+
)
108+
109+
if (is.null(res) || nrow(res) == 0 || !("is_view" %in% names(res))) {
110+
return(FALSE)
111+
}
112+
113+
val <- res$is_view[[1]]
114+
isTRUE(val) && !is.na(val)
115+
}
116+
96117
#' Validate dplyr tbl object
97118
#' @param tbl A tbl_duckdb_connection object
98119
#' @keywords internal

man/is_view.Rd

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)