|
73 | 73 | } |
74 | 74 |
|
75 | 75 | 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)) { |
87 | 77 | DBI::dbExecute(conn, glue::glue("DROP VIEW IF EXISTS {name}")) |
88 | 78 | } else { |
89 | 79 | DBI::dbRemoveTable(conn, name) |
|
93 | 83 | invisible(NULL) |
94 | 84 | } |
95 | 85 |
|
| 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 | + |
96 | 117 | #' Validate dplyr tbl object |
97 | 118 | #' @param tbl A tbl_duckdb_connection object |
98 | 119 | #' @keywords internal |
|
0 commit comments