|
21 | 21 | .ps.Call("ps_connection_updated", id) |
22 | 22 | } |
23 | 23 |
|
| 24 | +connection_focus <- function(id) { |
| 25 | + .ps.Call("ps_connection_focus", id) |
| 26 | +} |
| 27 | + |
24 | 28 | #' @export |
25 | 29 | .ps.connection_observer <- function() { |
26 | 30 | connections <- new.env(parent = emptyenv()) |
|
45 | 49 | for (id in ls(envir = connections)) { |
46 | 50 | con <- get(id, envir = connections) |
47 | 51 | if (identical(con$host, host) && identical(con$type, type)) { |
| 52 | + connection_focus(id) |
48 | 53 | return(invisible(id)) |
49 | 54 | } |
50 | 55 | } |
|
67 | 72 | # until the end of the connection. |
68 | 73 | objectTypes = connection_flatten_object_types(listObjectTypes()) |
69 | 74 | ) |
| 75 | + connection_focus(id) |
70 | 76 | invisible(id) |
71 | 77 | } |
72 | 78 |
|
@@ -170,6 +176,36 @@ connection_flatten_object_types <- function(object_tree) { |
170 | 176 | return(TRUE) |
171 | 177 | } |
172 | 178 |
|
| 179 | +# Helper to reconstruct ODBC connection code from connection info |
| 180 | +odbc_connection_code <- function(info) { |
| 181 | + # Try to reconstruct a reasonable connection string |
| 182 | + # Priority: DSN > connection string parameters |
| 183 | + if (!is.null(info$sourcename) && nzchar(info$sourcename)) { |
| 184 | + # DSN-based connection |
| 185 | + sprintf('odbc::dbConnect(odbc::odbc(), dsn = "%s")', info$sourcename) |
| 186 | + } else { |
| 187 | + # Build connection string from available parameters |
| 188 | + params <- character() |
| 189 | + |
| 190 | + if (!is.null(info$servername) && nzchar(info$servername)) { |
| 191 | + params <- c(params, sprintf('server = "%s"', info$servername)) |
| 192 | + } |
| 193 | + if (!is.null(info$dbname) && nzchar(info$dbname)) { |
| 194 | + params <- c(params, sprintf('database = "%s"', info$dbname)) |
| 195 | + } |
| 196 | + |
| 197 | + if (length(params) > 0) { |
| 198 | + sprintf( |
| 199 | + "odbc::dbConnect(odbc::odbc(), %s)", |
| 200 | + paste(params, collapse = ", ") |
| 201 | + ) |
| 202 | + } else { |
| 203 | + # Fallback if we can't determine connection parameters |
| 204 | + "# Connection opened from Variables Pane" |
| 205 | + } |
| 206 | + } |
| 207 | +} |
| 208 | + |
173 | 209 | #' @export |
174 | 210 | .ps.connection_icon <- function(id, ...) { |
175 | 211 | con <- get(id, getOption("connectionObserver")$.connections) |
@@ -283,6 +319,107 @@ connection_flatten_object_types <- function(object_tree) { |
283 | 319 | actions = NULL |
284 | 320 | ) |
285 | 321 |
|
286 | | - if (is.null(id)) return("hello") |
| 322 | + if (is.null(id)) { |
| 323 | + return("hello") |
| 324 | + } |
287 | 325 | id |
288 | 326 | } |
| 327 | + |
| 328 | +# ODBC Connection Support |
| 329 | +# These methods enable viewing ODBC connections in the Connections Pane |
| 330 | +# from the Variables Pane |
| 331 | + |
| 332 | +# Register ODBC connection methods when the odbc package is loaded |
| 333 | +setHook( |
| 334 | + packageEvent("odbc", "onLoad"), |
| 335 | + function(...) { |
| 336 | + # Mark ODBC connections as "connection" kind |
| 337 | + .ark.register_method( |
| 338 | + "ark_positron_variable_kind", |
| 339 | + "OdbcConnection", |
| 340 | + function(x) "connection" |
| 341 | + ) |
| 342 | + |
| 343 | + # Enable viewer for valid ODBC connections |
| 344 | + .ark.register_method( |
| 345 | + "ark_positron_variable_has_viewer", |
| 346 | + "OdbcConnection", |
| 347 | + function(x) { |
| 348 | + odbc::dbIsValid(x) |
| 349 | + } |
| 350 | + ) |
| 351 | + |
| 352 | + # View an ODBC connection in the Connections Pane |
| 353 | + .ark.register_method( |
| 354 | + "ark_positron_variable_view", |
| 355 | + "OdbcConnection", |
| 356 | + function(x) { |
| 357 | + # Reconstruct the connection code from the connection info |
| 358 | + info <- x@info |
| 359 | + code <- odbc_connection_code(info) |
| 360 | + |
| 361 | + # Use odbc's built-in connection observer integration |
| 362 | + odbc:::on_connection_opened(x, code = code) |
| 363 | + invisible(TRUE) |
| 364 | + } |
| 365 | + ) |
| 366 | + } |
| 367 | +) |
| 368 | + |
| 369 | +# BigQuery Connection Support |
| 370 | +# These methods enable viewing BigQuery connections in the Connections Pane |
| 371 | +# from the Variables Pane |
| 372 | + |
| 373 | +# Register BigQuery connection methods when the bigrquery package is loaded |
| 374 | +setHook( |
| 375 | + packageEvent("bigrquery", "onLoad"), |
| 376 | + function(...) { |
| 377 | + # Mark BigQuery connections as "connection" kind |
| 378 | + .ark.register_method( |
| 379 | + "ark_positron_variable_kind", |
| 380 | + "BigQueryConnection", |
| 381 | + function(x) "connection" |
| 382 | + ) |
| 383 | + |
| 384 | + # Enable viewer for valid BigQuery connections |
| 385 | + .ark.register_method( |
| 386 | + "ark_positron_variable_has_viewer", |
| 387 | + "BigQueryConnection", |
| 388 | + function(x) { |
| 389 | + DBI::dbIsValid(x) |
| 390 | + } |
| 391 | + ) |
| 392 | + |
| 393 | + # View a BigQuery connection in the Connections Pane |
| 394 | + .ark.register_method( |
| 395 | + "ark_positron_variable_view", |
| 396 | + "BigQueryConnection", |
| 397 | + function(x) { |
| 398 | + # Reconstruct the connection code |
| 399 | + code <- bigrquery_connection_code(x) |
| 400 | + |
| 401 | + # Use bigrquery's built-in connection observer integration |
| 402 | + bigrquery:::on_connection_opened(x, code = code) |
| 403 | + invisible(TRUE) |
| 404 | + } |
| 405 | + ) |
| 406 | + } |
| 407 | +) |
| 408 | + |
| 409 | +# Helper to reconstruct BigQuery connection code |
| 410 | +bigrquery_connection_code <- function(con) { |
| 411 | + project <- con@project |
| 412 | + dataset <- con@dataset |
| 413 | + billing <- con@billing |
| 414 | + |
| 415 | + params <- c(sprintf('project = "%s"', project)) |
| 416 | + |
| 417 | + if (!is.null(dataset) && nzchar(dataset)) { |
| 418 | + params <- c(params, sprintf('dataset = "%s"', dataset)) |
| 419 | + } |
| 420 | + if (!is.null(billing) && nzchar(billing) && billing != project) { |
| 421 | + params <- c(params, sprintf('billing = "%s"', billing)) |
| 422 | + } |
| 423 | + |
| 424 | + sprintf("DBI::dbConnect(bigrquery::bigquery(), %s)", paste(params, collapse = ", ")) |
| 425 | +} |
0 commit comments