Skip to content

Commit e41b2eb

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

File tree

1 file changed

+68
-1
lines changed

1 file changed

+68
-1
lines changed

R/PqGenerics.R

Lines changed: 68 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,77 @@ 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+
# use (Un)QuoteIdentifier roundtrip instead of Id(table = name)
124+
# so that quoted names (possibly incl. schema) can be passed to `name` e.g.
125+
# name = dbQuoteIdentifier(conn, Id(schema = "sname", table = "tname"))
126+
name <- dbQuoteIdentifier(conn, name)
127+
id <- dbUnquoteIdentifier(conn, name)[[1]]
128+
pq_exists_table(conn, id)
129+
})
130+
131+
#' @export
132+
#' @rdname postgres-tables
133+
setMethod("pqExistsTable", c("PqConnection", "Id"), function(conn, name, ...) {
134+
pq_exists_table(conn, id = name)
135+
})
136+
137+
pq_exists_table <- function(conn, id) {
138+
name <- id@name
139+
stopifnot("table" %in% names(name))
140+
table_name <- dbQuoteString(conn, name[["table"]])
141+
where_table <- paste0("AND cl.relname = ", table_name, "\n")
142+
143+
if ("schema" %in% names(name)) {
144+
schema_name <- dbQuoteString(conn, name[["schema"]])
145+
where_schema <- paste0("AND n.nspname = ", schema_name, "\n")
146+
} else {
147+
where_schema <- NULL
148+
}
149+
query <- paste0(
150+
"SELECT EXISTS ( \n",
151+
list_tables_sql(conn, where_schema = where_schema, where_table = where_table),
152+
")"
153+
)
154+
dbGetQuery(conn, query)[[1]]
155+
}
89156

90157
#' List remote objects
91158
#'

0 commit comments

Comments
 (0)