Skip to content

Commit 8e8994f

Browse files
committed
add pqExistsTable()
1 parent 8f34aaa commit 8e8994f

File tree

1 file changed

+66
-1
lines changed

1 file changed

+66
-1
lines changed

R/PqGenerics.R

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ setMethod("pqListTables", "PqConnection", function(conn) {
3838
dbGetQuery(conn, query)[["relname"]]
3939
})
4040

41-
list_tables_sql <- function(conn, where_schema = NULL, order_by = NULL) {
41+
list_tables_sql <- function(conn, where_schema = NULL, where_table = NULL, order_by = NULL) {
4242
major_server_version <- dbGetInfo(conn)$db.version %/% 10000
4343

4444
query <- paste0(
@@ -82,10 +82,75 @@ list_tables_sql <- function(conn, where_schema = NULL, order_by = NULL) {
8282
query <- paste0(query, where_schema)
8383
}
8484

85+
if (!is.null(where_table)) query <- paste0(query, where_table)
86+
8587
if (!is.null(order_by)) query <- paste0(query, "ORDER BY ", order_by)
8688

8789
query
8890
}
91+
#' Does a table exist?
92+
#'
93+
#' Returns if a table or (materialized) view given by name exists in the
94+
#' database.
95+
#'
96+
#' @inheritParams postgres-tables
97+
#'
98+
#' @family PqConnection generics
99+
#'
100+
#' @examples
101+
#' # For running the examples on systems without PostgreSQL connection:
102+
#' run <- postgresHasDefault()
103+
#'
104+
#' library(DBI)
105+
#' if (run) con <- dbConnect(RPostgres::Postgres())
106+
#' if (run) pqExistsTable(con, "mtcars")
107+
#'
108+
#' if (run) dbWriteTable(con, "mtcars", mtcars, temporary = TRUE)
109+
#' if (run) pqExistsTable(con, "mtcars")
110+
#'
111+
#' if (run) dbDisconnect(con)
112+
#'
113+
#' @export
114+
setGeneric("pqExistsTable",
115+
def = function(conn, name, ...) standardGeneric("pqExistsTable"),
116+
valueClass = "logical"
117+
)
118+
119+
#' @rdname pqExistsTable
120+
#' @export
121+
setMethod("pqExistsTable", c("PqConnection", "character"), function(conn, name, ...) {
122+
stopifnot(length(name) == 1L)
123+
# why not turn `name` into an Id(table = name) and pass that to exists_table()?
124+
name <- dbQuoteIdentifier(conn, name)
125+
id <- dbUnquoteIdentifier(conn, name)[[1]]
126+
pq_exists_table(conn, id)
127+
})
128+
129+
#' @export
130+
#' @rdname postgres-tables
131+
setMethod("pqExistsTable", c("PqConnection", "Id"), function(conn, name, ...) {
132+
pq_exists_table(conn, id = name)
133+
})
134+
135+
pq_exists_table <- function(conn, id) {
136+
name <- id@name
137+
stopifnot("table" %in% names(name))
138+
table_name <- dbQuoteString(conn, name[["table"]])
139+
where_table <- paste0("AND cl.relname = ", table_name, "\n")
140+
141+
if ("schema" %in% names(name)) {
142+
schema_name <- dbQuoteString(conn, name[["schema"]])
143+
where_schema <- paste0("AND n.nspname = ", schema_name, "\n")
144+
} else {
145+
where_schema <- NULL
146+
}
147+
query <- paste0(
148+
"SELECT EXISTS ( \n",
149+
list_tables_sql(conn, where_schema = where_schema, where_table = where_table),
150+
")"
151+
)
152+
dbGetQuery(conn, query)[[1]]
153+
}
89154

90155
#' List remote objects
91156
#'

0 commit comments

Comments
 (0)