/
view_database.R
652 lines (549 loc) · 18 KB
/
view_database.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
#' View Database Connection with Octopus
#' @description This function opens a shiny instance where the database can
#' be viewed.
#'
#' @param con A database connection object. The result of DBI::dbConnect().
#' @param options A named list of options to be passed along to shinyApp().
#' @param max_file_upload_size An integer. The max number of bits allowed in file uploads.
#'
#' @importFrom shiny shinyApp
#' @importFrom shiny showNotification
#' @importFrom shiny updateSelectInput
#' @importFrom shiny updateSelectizeInput
#' @importFrom shiny tags
#' @importFrom shiny div
#' @importFrom shiny p
#' @importFrom shiny h3
#' @importFrom shiny selectInput
#' @importFrom shiny fileInput
#' @importFrom shiny showModal
#' @importFrom shiny modalDialog
#' @importFrom shiny tagList
#' @importFrom shiny modalButton
#' @importFrom shiny removeModal
#' @importFrom shiny observeEvent
#' @importFrom shiny stopApp
#' @importFrom shiny bootstrapPage
#' @importFrom shiny req
#' @importFrom shinyAce aceEditor
#' @importFrom shinyAce updateAceEditor
#' @importFrom shinyjs useShinyjs
#' @importFrom shinyjs onclick
#' @importFrom shinyjs hideElement
#' @importFrom shinyjs showElement
#' @importFrom shinyjs onevent
#' @importFrom shinyjs html
#' @importFrom glue glue
#' @importFrom DT renderDataTable
#' @importFrom rio import
#' @importFrom janitor clean_names
#' @importFrom DBI dbSendQuery
#' @importFrom DBI dbGetQuery
#' @importFrom httr GET
#' @importFrom httr use_proxy
#' @importFrom httr content
#' @importFrom bslib bs_theme
#' @importFrom utils write.csv
#'
#'
#' @return An R Shiny instance.
#' @export
#'
view_database <-
function(con, options = list(), max_file_upload_size = 2000 * 1024^2){
ui <- shiny::bootstrapPage(
theme = bslib::bs_theme(
version = 5
),
# Initiate shinyjs
shinyjs::useShinyjs(),
# User Interface----------------------------------------------------------
shiny::div(
class = "container pt-3",
shiny::div(
class = "row mx-0 px-0 justify-content-center",
shiny::div(
class = "col-12 col-md-5 col-lg-4 col-xl-3 mx-3",
id = "viewDiv",
## Manage UI -------------------------------------------------------
shiny::div(
class = "row bg-light py-3 mb-3 px-3 border rounded shadow",
# Header
shiny::tags$h3(
class = "text-center",
"Manage"
),
shiny::hr(),
# Select schema
shiny::selectInput(
"schema",
label = shiny::strong("Schema"),
choices = c("Loading..."),
width = "100%"
),
# Select table
shiny::selectInput(
"tables",
label = shiny::strong("Table"),
choices = c("Loading..."),
width = "100%"
),
shiny::div(
class = "btn-group mt-2 mb-4 w-100",
# View Tables
shiny::actionButton(
inputId = "viewTable",
icon = shiny::icon("expand"),
"View"
),
# Delete Tables
shiny::actionButton(
inputId = "deleteTable",
icon = shiny::icon("trash"),
"Delete"
)
),
# File upload to database
shiny::fileInput(
"newTableUpload",
label = shiny::strong("Upload File"),
accept = c(".csv", ".xlsx"),
width = "100%"
),
),
## Query UI --------------------------------------------------------
shiny::div(
class = "d-none d-md-block row bg-light py-3 mt-3 px-3 border rounded shadow",
# Header
shiny::tags$h3(
class = "text-center",
"Query"
),
shiny::hr(),
shiny::div(
class = "btn-group mt-3 mb-3 w-100",
# Run Query
shiny::actionButton(
inputId = "submitQuery",
icon = shiny::icon("play"),
"Run"
),
# Format Query
shiny::actionButton(
inputId = "formatQuery",
icon = shiny::icon("indent"),
"Format"
)
)
),
),
shiny::div(
class = "d-none d-md-block col-12 col-md-6 col-lg-7 bg-light pb-2 pt-1 mt-3 mt-md-0 border rounded shadow",
id = "queryDiv",
shiny::div(
class = "row h-100 pt-1",
shiny::div(
class = "col",
# Send query to database
shinyAce::aceEditor(
"query",
mode = "pgsql",
height = "100%",
value = "",
showPrintMargin = FALSE,
fontSize = 16,
highlightActiveLine = FALSE,
autoComplete = "enabled",
autoCompleters = c("static"),
hotkeys = list(
run_key = list(
win = "Ctrl-Shift-Enter",
mac = "CMD-SHIFT-ENTER"
),
format_key = list(
win = "Ctrl-Shift-F",
mac = "CMD-SHIFT-F"
)
),
debounce = 1
),
)
)
),
)
)
)
server <- function(input, output, session) {
# Database Functions -----------------------------------------------------
driver <- class(con)
tryCatch({
database_functions <- get_database_functions(driver)
get_schemas <- database_functions[[1]]
get_tables <- database_functions[[2]]
get_n_rows <- database_functions[[3]]
get_preview <- database_functions[[4]]
delete_table <- database_functions[[5]]
write_table <- database_functions[[6]]
}, error = function(error){
shiny::showNotification(error$message)
})
# Initialize Inputs -----------------------------------------------
tryCatch({
schemas <- get_schemas(con)
# Update schema list
shiny::updateSelectizeInput(
session,
"schema",
choices = schemas,
selected = schemas[1],
server = TRUE
)
# Set initial tables
current_tables <- get_tables(con, schemas[1])
shiny::updateSelectizeInput(
session,
"tables",
choices = current_tables,
selected = current_tables[1],
server = TRUE
)
# Update table select on schema change
shinyjs::onevent("change", "schema", {
shiny::showNotification(
"Loading...",
duration = NULL,
id = "loading-notification"
)
current_tables <- get_tables(con, input$schema)
shiny::updateSelectizeInput(
session,
"tables",
choices = current_tables,
selected = current_tables[1],
server = TRUE
)
shiny::removeNotification(
"loading-notification"
)
})
# Update column name suggestions
shinyjs::onevent("change", "tables",{
if (input$tables %in% current_tables){
# Set Ace Editors Auto Complete Suggestions
current_schema <- input$schema
current_table <- input$tables
current_table_preview <- tryCatch({
get_preview(
con,
current_schema,
current_table
)
}, error = function(error){
data.frame()
})
auto_complete_suggestions <- list()
auto_complete_suggestions[["Schema"]] <- current_schema
auto_complete_suggestions[["Table"]] <- current_table
auto_complete_suggestions[["Column"]] <- colnames(current_table_preview)
shinyAce::updateAceEditor(
session = session,
editorId = "query",
autoCompleteList = auto_complete_suggestions
)
}
})
}, error = function(error){
shiny::showNotification(error$message)
})
# View Table ------------------------------------------------------------
# View tables on click view button
shinyjs::onclick("viewTable", {
shiny::showNotification(
"Loading Table...",
duration = NULL,
id = "loading-notification"
)
tryCatch({
# Get the number of rows
n_rows <- get_n_rows(
con,
input$schema,
input$tables
)
result <- get_preview(
con,
input$schema,
input$tables
)
table_modal_w_download_full_Server(
id = "preview",
con = con,
schema = input$schema,
table = input$tables,
n_rows = n_rows
)
table_modal_w_download_UI(
id = "preview",
title = glue::glue("Preview Table: {input$tables}"),
download_title = "Download Table",
n_rows = n_rows,
result = result
)
}, error = function(error){
shiny::showNotification(error$message)
})
shiny::removeNotification(
"loading-notification"
)
})
# Delete Table -----------------------------------------------------------
# Allow deleting a table
shinyjs::onclick("deleteTable", {
table <- input$tables
shiny::showModal(
shiny::modalDialog(
easyClose = TRUE,
shiny::h3("Confirm Deletion"),
shiny::p(glue("Are you sure you want to delete the table: {table}?")),
footer = shiny::tags$button(
id = "confirmDelete",
class = "btn btn-outline-danger",
"Confirm"
)
)
)
# Confirm delete
shinyjs::onclick("confirmDelete", asis = TRUE, {
shiny::removeModal()
tryCatch({
shiny::showNotification(
"Loading...",
duration = NULL,
id = "loading-notification"
)
result <- delete_table(
con,
input$schema,
input$tables
)
shiny::removeNotification(
"loading-notification"
)
# Notify success
shiny::showNotification(result)
# Update select input
current_tables <- get_tables(con, input$schema)
shiny::updateSelectizeInput(
session,
"tables",
choices = current_tables,
selected = current_tables[1],
server = TRUE
)
}, error = function(error){
shiny::showNotification(error$message)
})
})
})
# Upload Table -----------------------------------------------------------
# Increase file upload limit
options(shiny.maxRequestSize = max_file_upload_size)
# Upload file to DB
shiny::observeEvent(input$newTableUpload, {
shiny::showNotification(
"Reading File...",
duration = NULL,
id = "loading-notification"
)
# Read file
file <- input$newTableUpload
shiny::req(file)
new_table <- rio::import(
file$datapath
)
shiny::removeNotification(
"loading-notification"
)
shiny::showModal(
shiny::modalDialog(
easyClose = TRUE,
size = "s",
shiny::tagList(
shiny::textInput(
"newTableName",
"Confirm Table Name",
value = gsub("\\..+", "", file$name)
),
shiny::selectInput(
"cleanColumnNames",
"Clean column names?",
choices = c("Yes", "No"),
selected = "Yes"
),
shiny::selectInput(
"tempTable",
"Temporary table?",
choices = c("Yes", "No"),
selected = "Yes"
)
),
footer = shiny::tagList(
shiny::tags$button(
id = "confirmNewTableName",
class = "btn btn-outline-primary",
"data-bs-dismiss" = "modal",
"Confirm"
)
)
)
)
shinyjs::onclick("confirmNewTableName", {
if (input$cleanColumnNames == "Yes") {
new_table <-
janitor::clean_names(new_table)
if (driver == "Snowflake"){
colnames(new_table) <- toupper(colnames(new_table))
}
}
tryCatch({
shiny::showNotification(
"Uploading...",
duration = NULL,
id = "loading-notification"
)
# Write data frame to DB
result <- write_table(
con,
schema = input$schema,
table_name = input$newTableName,
data = new_table,
temporary = ifelse(
input$tempTable == "Yes",
TRUE,
FALSE
)
)
shiny::removeNotification(
"loading-notification"
)
# Show result
shiny::showNotification(result, duration = 3)
# Update select input
current_tables <- get_tables(con, input$schema)
shiny::updateSelectizeInput(
session,
"tables",
choices = current_tables,
selected = input$newTableName,
server = TRUE
)
}, error = function(error){
shiny::showNotification(error$message)
})
})
})
# Query ----------------------------------------------------------------
query_submit <- function(){
tryCatch({
n_rows <- get_n_rows(
con = con,
schema = input$schema,
table = input$tables,
query = input$query
)
result <- submit_query(
query = input$query,
con = con,
n_rows = n_rows
)
table_modal_w_download_Server(
id = "query",
result = result
)
table_modal_w_download_UI(
id = "query",
title = "Query Preview",
download_title = "Download",
n_rows = n_rows,
result = result
)
# Update select input
previous_table <- input$tables
current_tables <- get_tables(con, input$schema)
shiny::updateSelectizeInput(
session,
"tables",
choices = current_tables,
selected = ifelse(
previous_table %in% current_tables,
previous_table,
current_tables[1]
),
server = TRUE
)
}, error = function(error){
shiny::showNotification(
error$message
)
})
}
# Hotkey Query Submit
observeEvent(input$query_run_key, {
shiny::showNotification(
"Loading...",
duration = NULL,
id = "loading-notification"
)
query_submit()
shiny::removeNotification(
"loading-notification"
)
})
# Run Button Query Submit
shinyjs::onclick("submitQuery", {
shiny::showNotification(
"Loading...",
duration = NULL,
id = "loading-notification"
)
query_submit()
shiny::removeNotification(
"loading-notification"
)
})
# Format -----------------------------------------------------------------
# Reformat SQL code
format_code <- function(){
original_query <- input$query
tryCatch(
{
response <-
httr::GET(
glue::glue(
"https://sqlformat.org/api/v1/format",
"?reindent=1",
"&keyword_case=upper",
"&sql={URLencode(original_query)}"
),
httr::use_proxy(
Sys.getenv("https_proxy")
)
)
shinyAce::updateAceEditor(
session = session,
editorId = "query",
value = httr::content(response, as = "parsed")$result
)
},
error = function(error) {
shiny::showNotification(error$message)
}
)
}
# Hotkey to Format
observeEvent(input$query_format_key, format_code())
# Format Button
shinyjs::onclick("formatQuery", format_code())
}
shiny::shinyApp(ui, server, options = options)
}