Skip to content

Commit

Permalink
Connection pane improvements (#599)
Browse files Browse the repository at this point in the history
* Provide (undocumented) hint argument
* Drop one level of nesting
* Use dataset if set in connection
  • Loading branch information
hadley authored Jan 19, 2024
1 parent 0861ab6 commit 11fa757
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 31 deletions.
48 changes: 21 additions & 27 deletions R/connections-page.R
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ on_connection_closed <- function(con) {
}

# https://rstudio.github.io/rstudio-extensions/connections-contract.html#connection-updated
on_connection_updated <- function(con) {
on_connection_updated <- function(con, hint) {
observer <- getOption("connectionObserver")
if (is.null(observer))
return(invisible(NULL))

observer$connectionUpdated(bq_type, con@project)
observer$connectionUpdated(bq_type, con@project, hint = hint)
}

# https://rstudio.github.io/rstudio-extensions/connections-contract.html#connection-opened
Expand All @@ -50,32 +50,28 @@ on_connection_opened <- function(con, code) {

observer$connectionOpened(
type = bq_type,
displayName = paste0(c(bq_type, con@project), collapse = "-"),
displayName = paste0(bq_type, ": ", con@project),
host = con@project,
icon = system.file("icons/bigquery-512-color.png", package = "bigrquery"),

# connection code
connectCode = code,

# only action is to close connections pane
disconnect = function() dbDisconnect(con),
disconnect = function() {},

listObjectTypes = function() {
list(
project = list(
dataset = list(
icon = system.file("icons/dataset.png", package = "bigrquery"),
contains = list(
dataset = list(
icon = system.file("icons/dataset.png", package = "bigrquery"),
contains = list(
table = list(
icon = system.file("icons/table.png", package = "bigrquery"),
contains = "data"
),
view = list(
icon = system.file("icons/view.png", package = "bigrquery"),
contains = "data"
)
)
table = list(
icon = system.file("icons/table.png", package = "bigrquery"),
contains = "data"
),
view = list(
icon = system.file("icons/view.png", package = "bigrquery"),
contains = "data"
)
)
)
Expand All @@ -88,8 +84,8 @@ on_connection_opened <- function(con, code) {
},

# column enumeration code
listColumns = function(project = NULL, dataset = NULL, table = NULL, view = NULL, ...) {
x <- bq_table(project, dataset, paste0(table, view))
listColumns = function(dataset = con@dataset, table = NULL, view = NULL, ...) {
x <- bq_table(con@project, dataset, paste0(table, view))
fields <- bq_table_fields(x)

tibble::tibble(
Expand All @@ -99,8 +95,8 @@ on_connection_opened <- function(con, code) {
},

# table preview code
previewObject = function(rowLimit, project = NULL, dataset = NULL, table = NULL, view = NULL, ...) {
x <- bq_table(project, dataset, paste0(table, view))
previewObject = function(rowLimit, dataset = con@dataset, table = NULL, view = NULL, ...) {
x <- bq_table(con@project, dataset, paste0(table, view))
bq_table_download(x, max_results = rowLimit)
},

Expand All @@ -112,21 +108,19 @@ on_connection_opened <- function(con, code) {
)
}

list_bigquery_objects <- function(con, project = NULL, dataset = NULL, ...) {
if (is.null(project)) {
tibble::tibble(type = "project", name = con@project)
} else if (is.null(dataset)) {
list_bigquery_objects <- function(con, dataset = con@dataset, ...) {
if (is.null(dataset)) {
# Catching VPC/Permission errors to crash gracefully
bq_datasets <- tryCatch(
bq_project_datasets(project, warn = FALSE),
bq_project_datasets(con@project, warn = FALSE),
error = function(e) list()
)
datasets <- map_chr(bq_datasets, `[[`, "dataset")

tibble::tibble(type = "dataset", name = datasets)
} else {
# Catching VPC/Permission errors to crash gracefully
ds <- bq_dataset(project, dataset)
ds <- bq_dataset(con@project, dataset)
bq_tables <- tryCatch(bq_dataset_tables(ds), error = function(e) list())
tables <- map_chr(bq_tables, `[[`, "table")
types <- map_chr(bq_tables, `[[`, "type")
Expand Down
6 changes: 3 additions & 3 deletions R/dbi-connection.R
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ dbAppendTable_bq <- function(conn, name, value, ..., row.names = NULL) {
write_disposition = "WRITE_APPEND",
...
)
on_connection_updated(conn)
on_connection_updated(conn, name)

invisible(TRUE)
}
Expand Down Expand Up @@ -289,7 +289,7 @@ dbCreateTable_bq <- function(conn,

tb <- as_bq_table(conn, name)
bq_table_create(tb, fields)
on_connection_updated(conn)
on_connection_updated(conn, name)

invisible(TRUE)
}
Expand Down Expand Up @@ -363,7 +363,7 @@ setMethod("dbListFields", c("BigQueryConnection", "Id"), dbListFields_bq)
dbRemoveTable_bq <- function(conn, name, ...) {
tb <- as_bq_table(conn, name)
bq_table_delete(tb)
on_connection_updated(conn)
on_connection_updated(conn, name)
invisible(TRUE)
}

Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-bq-dataset.R
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,6 @@ test_that("can list tables in a dataset", {

expect_equal(
bq_dataset_tables(ds),
list(bq_table(ds, "mtcars"))
list(bq_table(ds, "mtcars"), bq_table(ds, "simple-five"))
)
})

0 comments on commit 11fa757

Please sign in to comment.