@@ -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,77 @@ 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
+ # 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
+ }
89
156
90
157
# ' List remote objects
91
158
# '
0 commit comments