@@ -38,7 +38,7 @@ setMethod("pqListTables", "PqConnection", function(conn) {
38
38
dbGetQuery(conn , query )[[" relname" ]]
39
39
})
40
40
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 ) {
42
42
major_server_version <- dbGetInfo(conn )$ db.version %/% 10000
43
43
44
44
query <- paste0(
@@ -82,10 +82,75 @@ list_tables_sql <- function(conn, where_schema = NULL, order_by = NULL) {
82
82
query <- paste0(query , where_schema )
83
83
}
84
84
85
+ if (! is.null(where_table )) query <- paste0(query , where_table )
86
+
85
87
if (! is.null(order_by )) query <- paste0(query , " ORDER BY " , order_by )
86
88
87
89
query
88
90
}
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
+ }
89
154
90
155
# ' List remote objects
91
156
# '
0 commit comments