Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added browse_db_model function #18

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ License: MIT + file LICENSE
Suggests:
yaml,
DiagrammeR,
DiagrammeRsvg,
testthat (>= 2.1.0),
V8,
rsvg
LazyData: TRUE
RoxygenNote: 6.0.1
RoxygenNote: 6.1.1
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ S3method(as.data_model,list)
S3method(print,data_model)
S3method(print,datamodelr_graph)
export(as.data_model)
export(browse_db_model)
export(dm_add_colors)
export(dm_add_reference_)
export(dm_add_references)
Expand Down
41 changes: 41 additions & 0 deletions R/browse_db_model.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#' Make a database diagram using a connection and display to browser
#'
#' @param connection A database connection.
#' @param dbType One of 'sqlserver', or 'postgres'.
#' @param rankdir Direction of diagram default is 'BT'.
#' @param view_type One of 'all', 'keys_only', or 'view_only'.
#' @param focus A character vector of tables to be filtered to.
#' @param wd The working directory to use.
#' @param fileName The fileName.
#' @keywords database diagram
#' @export
#' @examples
#' browse_db_model()

browse_db_model <-
function(connection = NULL,
dbType = 'sqlserver',
rankdir = 'BT',
view_type = 'keys_only',
focus = NULL,
wd = getwd(),
fileName = 'dbDiagram.svg') {
query <- datamodelr::dm_re_query(dbType)
db <- odbc::dbGetQuery(connection, query)

# convert to a data model
db <- datamodelr::as.data_model(db)

if (!is.null(focus)) {
focus <- list(tables = focus)
}

graph <-
datamodelr::dm_create_graph(db,
rankdir = rankdir,
view_type = view_type,
focus = focus)

rsvg::rsvg_svg(charToRaw(DiagrammeRsvg::export_svg(DiagrammeR::grViz(graph$dot_code))), fileName)
browseURL(paste0('file://', file.path(wd, fileName)))
}
33 changes: 33 additions & 0 deletions man/browse_db_model.Rd

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