-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
371 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
#' As row policy | ||
#' @param x some input | ||
#' @export | ||
as_row_policy <- function(x) { | ||
UseMethod("as_row_policy") | ||
} | ||
#' @export | ||
as_row_policy.row_policy <- function(x) { | ||
return(x) | ||
} | ||
#' @export | ||
as_row_policy.tbl_sql <- function(x) { | ||
tmp <- list( | ||
data = x, | ||
name = NULL, | ||
commands = NULL, | ||
user = NULL, | ||
existing_rows = NULL, | ||
new_rows = NULL, | ||
type = NULL | ||
) | ||
structure(tmp, class = "row_policy") | ||
} | ||
#' @export | ||
print.row_policy <- function(x, ...) { | ||
cat_line(glue("<row_policy> {x$name}")) | ||
if (!is_really_empty(x$user)) { | ||
cat_me("user", x$user) | ||
} | ||
if (!is_really_empty(x$commands)) { | ||
cat_me("commands", x$commands) | ||
} | ||
if (!is_really_empty(x$existing_rows)) { | ||
cat_me("existing rows", x$existing_rows) | ||
} | ||
if (!is_really_empty(x$new_rows)) { | ||
cat_me("new rows", x$new_rows) | ||
} | ||
if (!is_really_empty(x$privilege)) { | ||
cat_me("type", x$type) | ||
for (i in x$privilege) { | ||
cat_me(x = i$commands, y = i$cols %|||% "<all cols>", indent = " ") | ||
} | ||
} | ||
print(x$data) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
#' Row policy | ||
#' | ||
#' @export | ||
#' @inheritParams grant | ||
#' @param name (character) scalar name for the policy. required | ||
#' @examplesIf interactive() && has_postgres() | ||
#' library(RPostgres) | ||
#' con <- dbConnect(Postgres()) | ||
#' rls_tbl(con, "passwd") %>% | ||
#' row_policy("my_policy") | ||
row_policy <- function(.data, name) { | ||
pipe_autoexec(toggle = rls_env$auto_pipe) | ||
assert_is(name, "character") | ||
assert_scalar(name) | ||
.data <- as_row_policy(.data) | ||
.data$name <- name | ||
.data | ||
} | ||
|
||
#' Commands | ||
#' | ||
#' @export | ||
#' @inheritParams grant | ||
#' @examplesIf interactive() && has_postgres() | ||
#' library(RPostgres) | ||
#' con <- dbConnect(Postgres()) | ||
#' rls_tbl(con, "passwd") %>% | ||
#' row_policy("my_policy") %>% | ||
#' commands(update) | ||
commands <- function(.data, ...) { | ||
pipe_autoexec(toggle = rls_env$auto_pipe) | ||
.data <- as_row_policy(.data) | ||
.data$commands <- dot_names(...) | ||
.data | ||
} | ||
|
||
#' Create rule for existing rows | ||
#' | ||
#' @export | ||
#' @inheritParams grant | ||
#' @param using an expression to use to check against existing rows | ||
#' @param sql (character) sql syntax to use for existing rows | ||
#' @details Use either `using` or `sql`, not both | ||
#' @examplesIf interactive() && has_postgres() | ||
#' library(RPostgres) | ||
#' con <- dbConnect(Postgres()) | ||
#' rls_tbl(con, "passwd") %>% | ||
#' row_policy("my_policy") %>% | ||
#' commands(update) %>% | ||
#' rows_existing(sql = 'current_user = "user_name"') | ||
rows_existing <- function(.data, using = NULL, sql = NULL) { | ||
pipe_autoexec(toggle = rls_env$auto_pipe) | ||
using_quo <- enquo(using) | ||
stopifnot("Can not using and sql parameters together" = | ||
xor(!rlang::quo_is_null(using_quo), !is_empty(sql))) | ||
.data <- as_row_policy(.data) | ||
if (rlang::is_null(sql)) { | ||
.data$existing_rows <- translate_sql(!!using_quo, con = as_con(.data)) | ||
} else { | ||
.data$existing_rows <- sql | ||
} | ||
.data | ||
} | ||
|
||
#' Create rule for new rows | ||
#' | ||
#' @export | ||
#' @importFrom dbplyr translate_sql | ||
#' @inheritParams grant | ||
#' @param check an expression to use to check against addition of | ||
#' new rows or editing of existing rows | ||
#' @param sql (character) sql syntax to use for new rows | ||
#' @details Use either `check` or `sql`, not both | ||
#' @examplesIf interactive() && has_postgres() | ||
#' library(RPostgres) | ||
#' con <- dbConnect(Postgres()) | ||
#' | ||
#' rls_tbl(con, "passwd") %>% | ||
#' row_policy("a_policy") %>% | ||
#' commands(update) %>% | ||
#' rows_existing(TRUE) %>% | ||
#' rows_new(TRUE) %>% | ||
#' to(jane) | ||
#' | ||
#' rls_tbl(con, "passwd") %>% | ||
#' row_policy("my_policy") %>% | ||
#' commands(update) %>% | ||
#' rows_existing(sql = 'current_user = "user_name"') %>% | ||
#' rows_new(home_phone == "098-765-4321") %>% | ||
#' to(jane) | ||
rows_new <- function(.data, check = NULL, sql = NULL) { | ||
pipe_autoexec(toggle = rls_env$auto_pipe) | ||
check_quo <- enquo(check) | ||
stopifnot("Can not check and sql parameters together" = | ||
xor(!rlang::quo_is_null(check_quo), !is_empty(sql))) | ||
.data <- as_row_policy(.data) | ||
if (rlang::is_null(sql)) { | ||
.data$new_rows <- translate_sql(!!check_quo, con = as_con(.data)) | ||
} else { | ||
.data$new_rows <- sql | ||
} | ||
.data | ||
} | ||
|
||
as_con <- function(x) { | ||
assert_is(x, "row_policy") | ||
x$data$src$con | ||
} | ||
|
||
combine_if <- function(statement, item, fun = \(x) x) { | ||
ifelse(!rlang::is_null(item), paste(statement, fun(item)), "") | ||
} | ||
|
||
express <- function(x) { | ||
glue("({ifelse(x == 'TRUE', tolower(x), x)})") | ||
} | ||
|
||
translate_row_policy <- function(policy, con) { | ||
is_conn(con) | ||
create_statement <- switch(class(con), | ||
RedshiftConnection = "CREATE RLS", | ||
PqConnection = "CREATE" | ||
) | ||
sql_create_policy <- glue(" | ||
{create_statement} POLICY {policy$name} ON {attr(policy$data, 'table')} | ||
{combine_if('FOR', policy$commands)} | ||
{combine_if('TO', policy$user)} | ||
{combine_if('USING', policy$existing_rows, express)} | ||
{combine_if('WITH CHECK', policy$new_rows, express)} | ||
") | ||
sql(gsub("\n\\s+\n", "\n", sql_create_policy)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.