Skip to content

Commit

Permalink
Style
Browse files Browse the repository at this point in the history
  • Loading branch information
krlmlr committed Apr 11, 2021
1 parent ab0c440 commit 942af90
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 28 deletions.
54 changes: 28 additions & 26 deletions R/rstudioConnections.R
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
# Functions used to connect to Connections Pane in Rstudio
# Implementing connections contract: https://rstudio.github.io/rstudio-extensions/connections-contract.html
sqlite_ListObjectTypes <- function(con) {
object_types <- list(table = list(contains="data"))
object_types <- list(table = list(contains = "data"))

types <- dbGetQuery(con, "SELECT DISTINCT type FROM sqlite_master")[[1]]
if (any(types =="view")){
object_types <- c(object_types, view=list(contains="data"))
if (any(types == "view")) {
object_types <- c(object_types, view = list(contains = "data"))
}
object_types
}

sqlite_ListObjects <- function(con, catalog = NULL, schema = NULL, name = NULL, type = NULL, ...) {
objects <- dbGetQuery(con, "SELECT name,type FROM sqlite_master")
objects <- objects[objects$type %in% c("table","view"),]
objects <- objects[objects$type %in% c("table", "view"), ]
objects
}

sqlite_ListColumns <- function(con, table = NULL, view = NULL,
catalog = NULL, schema = NULL, ...) {
if (is.null(table)){
catalog = NULL, schema = NULL, ...) {
if (is.null(table)) {
table <- view
}

tb <- dbGetQuery(
con,
paste("SELECT * FROM",dbQuoteIdentifier(con, table),"WHERE FALSE")
paste("SELECT * FROM", dbQuoteIdentifier(con, table), "WHERE FALSE")
)

name <- names(tb)
Expand All @@ -44,7 +44,7 @@ sqlite_PreviewObject <- function(con, rowLimit, table = NULL, view = NULL, ...)
}

sqlite_ConnectionIcon <- function(con) {
system.file("icons/sqlite.png", package="RSQLite")
system.file("icons/sqlite.png", package = "RSQLite")
}

sqlite_ConnectionActions <- function(con) {
Expand All @@ -62,41 +62,42 @@ sqlite_ConnectionActions <- function(con) {
}


get_host <- function(con){
if (con@dbname == ""){
get_host <- function(con) {
if (con@dbname == "") {
return("")
}
paste0("<",con@dbname, ">")
paste0("<", con@dbname, ">")
}

##### Functions that trigger update in Rstudio Connections tab

on_connection_opened <- function(con) {
observer <- getOption("connectionObserver")
if (is.null(observer))
if (is.null(observer)) {
return(invisible(NULL))
}

code <- paste0(
"library(DBI)
con <- dbConnect(RSQLite::SQLite(), dbname=\"",con@dbname,"\")
")
"library(DBI)
con <- dbConnect(RSQLite::SQLite(), dbname=\"", con@dbname, "\")
"
)
icon <- sqlite_ConnectionIcon(con)

host <- get_host(con)

# let observer know that connection has opened
# let observer know that connection has opened
observer$connectionOpened(
# connection type
type = "RSQLite",

# name displayed in connection pane (to be improved)
displayName = paste0("SQLite "
, host
, if (con@dbname == "") " (temporary)"
),

displayName = paste0(
"SQLite ",
host,
if (con@dbname == "") " (temporary)"
),
host = host,

icon = icon,

# connection code
Expand All @@ -106,8 +107,7 @@ con <- dbConnect(RSQLite::SQLite(), dbname=\"",con@dbname,"\")
disconnect = function() {
dbDisconnect(con, shutdown = TRUE)
},

listObjectTypes = function () {
listObjectTypes = function() {
sqlite_ListObjectTypes(con)
},

Expand Down Expand Up @@ -136,17 +136,19 @@ con <- dbConnect(RSQLite::SQLite(), dbname=\"",con@dbname,"\")

on_connection_updated <- function(con, hint) {
observer <- getOption("connectionObserver")
if (is.null(observer))
if (is.null(observer)) {
return(invisible(NULL))
}

host <- get_host(con)
observer$connectionUpdated("RSQLite", host, hint = hint)
}

on_connection_closed <- function(con) {
observer <- getOption("connectionObserver")
if (is.null(observer))
if (is.null(observer)) {
return(invisible(NULL))
}

host <- get_host(con)
observer$connectionClosed("RSQLite", host)
Expand Down
4 changes: 2 additions & 2 deletions R/table.R
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ setMethod("dbWriteTable", c("SQLiteConnection", "character", "data.frame"),
on.exit(NULL)

# rstudio connections tab
on_connection_updated(conn, paste0("Updating table '",name,"'"))
on_connection_updated(conn, paste0("Updating table '", name, "'"))

invisible(TRUE)
}
Expand Down Expand Up @@ -368,7 +368,7 @@ setMethod("dbRemoveTable", c("SQLiteConnection", "character"),
dbExecute(conn, paste0("DROP TABLE ", extra, name))

# rstudio connections tab
on_connection_updated(conn, paste0("Removing table '",name,"'"))
on_connection_updated(conn, paste0("Removing table '", name, "'"))

invisible(TRUE)
}
Expand Down

0 comments on commit 942af90

Please sign in to comment.