From 154eeacd1afcc5720cc17aa31bd75a61d4aa885e Mon Sep 17 00:00:00 2001 From: Daniel Possenriede Date: Fri, 2 Apr 2021 12:28:20 +0200 Subject: [PATCH] add pqListTables() --- DESCRIPTION | 1 + NAMESPACE | 2 ++ R/PqGenerics.R | 68 +++++++++++++++++++++++++++++++++++++++++++++ man/pqListTables.Rd | 38 +++++++++++++++++++++++++ 4 files changed, 109 insertions(+) create mode 100644 R/PqGenerics.R create mode 100644 man/pqListTables.Rd diff --git a/DESCRIPTION b/DESCRIPTION index ec946efd..a0caf406 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -62,6 +62,7 @@ SystemRequirements: libpq >= 9.0: libpq-dev (deb) or Collate: 'PqDriver.R' 'PqConnection.R' + 'PqGenerics.R' 'PqResult.R' 'RPostgres-pkg.R' 'RcppExports.R' diff --git a/NAMESPACE b/NAMESPACE index e0098385..1b5b2671 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -7,6 +7,7 @@ export(Redshift) export(postgresDefault) export(postgresHasDefault) export(postgresWaitForNotify) +export(pqListTables) exportClasses(PqConnection) exportClasses(PqDriver) exportClasses(PqResult) @@ -51,6 +52,7 @@ exportMethods(dbUnloadDriver) exportMethods(dbUnquoteIdentifier) exportMethods(dbWithTransaction) exportMethods(dbWriteTable) +exportMethods(pqListTables) exportMethods(show) exportMethods(sqlData) import(DBI) diff --git a/R/PqGenerics.R b/R/PqGenerics.R new file mode 100644 index 00000000..18b88063 --- /dev/null +++ b/R/PqGenerics.R @@ -0,0 +1,68 @@ +# PostgreSQL-specific features and functions + +#' List remote tables +#' +#' Returns the unquoted names of remote tables and table-like objects accessible +#' through this connection. +#' This includes foreign and partitioned tables, (materialized) views, as well +#' as temporary objects. +#' +#' @inheritParams postgres-tables +#' +#' @family PqConnection generics +#' +#' @examples +#' # For running the examples on systems without PostgreSQL connection: +#' run <- postgresHasDefault() +#' +#' library(DBI) +#' if (run) con <- dbConnect(RPostgres::Postgres()) +#' if (run) dbListTables(con) +#' +#' if (run) dbWriteTable(con, "mtcars", mtcars, temporary = TRUE) +#' if (run) dbListTables(con) +#' +#' if (run) dbDisconnect(con) +#' +#' @export +setGeneric("pqListTables", + def = function(conn, ...) standardGeneric("pqListTables"), + valueClass = "character" +) + +#' @rdname pqListTables +#' @export +setMethod("pqListTables", "PqConnection", function(conn) { + major_server_version <- dbGetInfo(conn)$db.version %/% 10000 + + query <- paste0( + # pg_class docs: https://www.postgresql.org/docs/current/catalog-pg-class.html + "SELECT cl.relname AS name FROM pg_class AS cl ", + "JOIN pg_namespace AS n ON cl.relnamespace = n.oid ", + "WHERE (n.nspname = ANY (current_schemas(true))) ", + "AND (n.nspname <> 'pg_catalog') " + ) + + if (major_server_version >= 10) { + # relkind = 'p' and relispartition only supported from v10 onwards + query <- paste0( + query, + # r = ordinary table, v = view, m = materialized view, f = foreign table, p = partitioned table + "AND (cl.relkind IN ('r', 'v', 'm', 'f', 'p')) ", + "AND NOT cl.relispartition " + ) + } else { + query <- paste0( + query, + "AND (cl.relkind IN ('r', 'v', 'm', 'f')) " + ) + } + + query <- paste0( + query, + "ORDER BY name" + ) + + dbGetQuery(conn, query)[[1]] +}) + diff --git a/man/pqListTables.Rd b/man/pqListTables.Rd new file mode 100644 index 00000000..e58bf76e --- /dev/null +++ b/man/pqListTables.Rd @@ -0,0 +1,38 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/PqGenerics.R +\name{pqListTables} +\alias{pqListTables} +\alias{pqListTables,PqConnection-method} +\title{List remote tables} +\usage{ +pqListTables(conn, ...) + +\S4method{pqListTables}{PqConnection}(conn) +} +\arguments{ +\item{conn}{a \linkS4class{PqConnection} object, produced by +\code{\link[DBI:dbConnect]{DBI::dbConnect()}}} + +\item{...}{Ignored.} +} +\description{ +Returns the unquoted names of remote tables and table-like objects accessible +through this connection. +This includes foreign and partitioned tables, (materialized) views, as well +as temporary objects. +} +\examples{ +# For running the examples on systems without PostgreSQL connection: +run <- postgresHasDefault() + +library(DBI) +if (run) con <- dbConnect(RPostgres::Postgres()) +if (run) dbListTables(con) + +if (run) dbWriteTable(con, "mtcars", mtcars, temporary = TRUE) +if (run) dbListTables(con) + +if (run) dbDisconnect(con) + +} +\concept{PqConnection generics}