Skip to content

Commit

Permalink
add pqListTables()
Browse files Browse the repository at this point in the history
  • Loading branch information
dpprdan committed Apr 2, 2021
1 parent 03a5b84 commit 154eeac
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 0 deletions.
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export(Redshift)
export(postgresDefault)
export(postgresHasDefault)
export(postgresWaitForNotify)
export(pqListTables)
exportClasses(PqConnection)
exportClasses(PqDriver)
exportClasses(PqResult)
Expand Down Expand Up @@ -51,6 +52,7 @@ exportMethods(dbUnloadDriver)
exportMethods(dbUnquoteIdentifier)
exportMethods(dbWithTransaction)
exportMethods(dbWriteTable)
exportMethods(pqListTables)
exportMethods(show)
exportMethods(sqlData)
import(DBI)
Expand Down
68 changes: 68 additions & 0 deletions R/PqGenerics.R
Original file line number Diff line number Diff line change
@@ -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]]
})

38 changes: 38 additions & 0 deletions man/pqListTables.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 154eeac

Please sign in to comment.