Refactors grant statements (#7153)

DESCRIPTION: Refactors all grant statements to use common code blocks to
deparse
pull/7089/head^2
Gürkan İndibay 2023-09-01 09:49:46 +03:00 committed by GitHub
parent f03291a8c8
commit 4a1a5491ce
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 167 additions and 196 deletions

View File

@ -0,0 +1,110 @@
#include "postgres.h"
#include "lib/stringinfo.h"
#include "nodes/parsenodes.h"
#include "distributed/deparser.h"
#include "distributed/citus_ruleutils.h"
/*
* Append the 'WITH GRANT OPTION' clause to the given buffer if the given
* statement is a 'GRANT' statement and the grant option is specified.
*/
void
AppendWithGrantOption(StringInfo buf, GrantStmt *stmt)
{
if (stmt->is_grant && stmt->grant_option)
{
appendStringInfo(buf, " WITH GRANT OPTION");
}
}
/*
* Append the 'GRANT OPTION FOR' clause to the given buffer if the given
* statement is a 'REVOKE' statement and the grant option is specified.
*/
void
AppendGrantOptionFor(StringInfo buf, GrantStmt *stmt)
{
if (!stmt->is_grant && stmt->grant_option)
{
appendStringInfo(buf, "GRANT OPTION FOR ");
}
}
/*
* Append the 'RESTRICT' or 'CASCADE' clause to the given buffer if the given
* statement is a 'REVOKE' statement and the behavior is specified.
*/
void
AppendGrantRestrictAndCascadeForRoleSpec(StringInfo buf, DropBehavior behavior, bool
isGrant)
{
if (!isGrant)
{
if (behavior == DROP_RESTRICT)
{
appendStringInfo(buf, " RESTRICT");
}
else if (behavior == DROP_CASCADE)
{
appendStringInfo(buf, " CASCADE");
}
}
}
/*
* Append the 'RESTRICT' or 'CASCADE' clause to the given buffer using 'GrantStmt',
* if the given statement is a 'REVOKE' statement and the behavior is specified.
*/
void
AppendGrantRestrictAndCascade(StringInfo buf, GrantStmt *stmt)
{
AppendGrantRestrictAndCascadeForRoleSpec(buf, stmt->behavior, stmt->is_grant);
}
/*
* Append the 'GRANTED BY' clause to the given buffer if the given statement is a
* 'GRANT' statement and the grantor is specified.
*/
void
AppendGrantedByInGrantForRoleSpec(StringInfo buf, RoleSpec *grantor, bool isGrant)
{
if (isGrant && grantor)
{
appendStringInfo(buf, " GRANTED BY %s", RoleSpecString(grantor, true));
}
}
/*
* Append the 'GRANTED BY' clause to the given buffer using 'GrantStmt',
* if the given statement is a 'GRANT' statement and the grantor is specified.
*/
void
AppendGrantedByInGrant(StringInfo buf, GrantStmt *stmt)
{
AppendGrantedByInGrantForRoleSpec(buf, stmt->grantor, stmt->is_grant);
}
void
AppendGrantSharedPrefix(StringInfo buf, GrantStmt *stmt)
{
appendStringInfo(buf, "%s ", stmt->is_grant ? "GRANT" : "REVOKE");
AppendGrantOptionFor(buf, stmt);
AppendGrantPrivileges(buf, stmt);
}
void
AppendGrantSharedSuffix(StringInfo buf, GrantStmt *stmt)
{
AppendGrantGrantees(buf, stmt);
AppendWithGrantOption(buf, stmt);
AppendGrantRestrictAndCascade(buf, stmt);
AppendGrantedByInGrant(buf, stmt);
appendStringInfo(buf, ";");
}

View File

@ -18,8 +18,8 @@
#include "nodes/parsenodes.h"
#include "utils/builtins.h"
#include "distributed/citus_ruleutils.h"
#include "distributed/deparser.h"
#include "distributed/citus_ruleutils.h"
static void AppendAlterDatabaseOwnerStmt(StringInfo buf, AlterOwnerStmt *stmt);
@ -74,41 +74,11 @@ AppendGrantOnDatabaseStmt(StringInfo buf, GrantStmt *stmt)
{
Assert(stmt->objtype == OBJECT_DATABASE);
appendStringInfo(buf, "%s ", stmt->is_grant ? "GRANT" : "REVOKE");
if (!stmt->is_grant && stmt->grant_option)
{
appendStringInfo(buf, "GRANT OPTION FOR ");
}
AppendGrantPrivileges(buf, stmt);
AppendGrantSharedPrefix(buf, stmt);
AppendGrantDatabases(buf, stmt);
AppendGrantGrantees(buf, stmt);
if (stmt->is_grant && stmt->grant_option)
{
appendStringInfo(buf, " WITH GRANT OPTION");
}
if (!stmt->is_grant)
{
if (stmt->behavior == DROP_RESTRICT)
{
appendStringInfo(buf, " RESTRICT");
}
else if (stmt->behavior == DROP_CASCADE)
{
appendStringInfo(buf, " CASCADE");
}
}
if (stmt->grantor)
{
appendStringInfo(buf, " GRANTED BY %s", RoleSpecString(stmt->grantor, true));
}
appendStringInfo(buf, ";");
AppendGrantSharedSuffix(buf, stmt);
}

View File

@ -21,7 +21,6 @@
static void AppendGrantOnFDWStmt(StringInfo buf, GrantStmt *stmt);
static void AppendGrantOnFDWNames(StringInfo buf, GrantStmt *stmt);
char *
DeparseGrantOnFDWStmt(Node *node)
{
@ -41,36 +40,9 @@ static void
AppendGrantOnFDWStmt(StringInfo buf, GrantStmt *stmt)
{
Assert(stmt->objtype == OBJECT_FDW);
appendStringInfo(buf, "%s ", stmt->is_grant ? "GRANT" : "REVOKE");
if (!stmt->is_grant && stmt->grant_option)
{
appendStringInfo(buf, "GRANT OPTION FOR ");
}
AppendGrantPrivileges(buf, stmt);
AppendGrantSharedPrefix(buf, stmt);
AppendGrantOnFDWNames(buf, stmt);
AppendGrantGrantees(buf, stmt);
if (stmt->is_grant && stmt->grant_option)
{
appendStringInfo(buf, " WITH GRANT OPTION");
}
if (!stmt->is_grant)
{
if (stmt->behavior == DROP_RESTRICT)
{
appendStringInfo(buf, " RESTRICT");
}
else if (stmt->behavior == DROP_CASCADE)
{
appendStringInfo(buf, " CASCADE");
}
}
appendStringInfo(buf, ";");
AppendGrantSharedSuffix(buf, stmt);
}

View File

@ -298,36 +298,9 @@ static void
AppendGrantOnForeignServerStmt(StringInfo buf, GrantStmt *stmt)
{
Assert(stmt->objtype == OBJECT_FOREIGN_SERVER);
appendStringInfo(buf, "%s ", stmt->is_grant ? "GRANT" : "REVOKE");
if (!stmt->is_grant && stmt->grant_option)
{
appendStringInfo(buf, "GRANT OPTION FOR ");
}
AppendGrantPrivileges(buf, stmt);
AppendGrantSharedPrefix(buf, stmt);
AppendGrantOnForeignServerServers(buf, stmt);
AppendGrantGrantees(buf, stmt);
if (stmt->is_grant && stmt->grant_option)
{
appendStringInfo(buf, " WITH GRANT OPTION");
}
if (!stmt->is_grant)
{
if (stmt->behavior == DROP_RESTRICT)
{
appendStringInfo(buf, " RESTRICT");
}
else if (stmt->behavior == DROP_CASCADE)
{
appendStringInfo(buf, " CASCADE");
}
}
appendStringInfo(buf, ";");
AppendGrantSharedSuffix(buf, stmt);
}

View File

@ -749,35 +749,11 @@ AppendGrantOnFunctionStmt(StringInfo buf, GrantStmt *stmt)
"GRANT .. ALL FUNCTIONS/PROCEDURES IN SCHEMA is not supported for formatting.");
}
appendStringInfoString(buf, stmt->is_grant ? "GRANT " : "REVOKE ");
if (!stmt->is_grant && stmt->grant_option)
{
appendStringInfoString(buf, "GRANT OPTION FOR ");
}
AppendGrantPrivileges(buf, stmt);
AppendGrantSharedPrefix(buf, stmt);
AppendGrantOnFunctionFunctions(buf, stmt);
AppendGrantGrantees(buf, stmt);
if (stmt->is_grant && stmt->grant_option)
{
appendStringInfoString(buf, " WITH GRANT OPTION");
}
if (!stmt->is_grant)
{
if (stmt->behavior == DROP_RESTRICT)
{
appendStringInfoString(buf, " RESTRICT");
}
else if (stmt->behavior == DROP_CASCADE)
{
appendStringInfoString(buf, " CASCADE");
}
}
appendStringInfoString(buf, ";");
AppendGrantSharedSuffix(buf, stmt);
}

View File

@ -29,6 +29,8 @@ static void AppendRoleOption(StringInfo buf, ListCell *optionCell);
static void AppendRoleList(StringInfo buf, List *roleList);
static void AppendDropRoleStmt(StringInfo buf, DropRoleStmt *stmt);
static void AppendGrantRoleStmt(StringInfo buf, GrantRoleStmt *stmt);
static void AppendRevokeAdminOptionFor(StringInfo buf, GrantRoleStmt *stmt);
static void AppendGrantWithAdminOption(StringInfo buf, GrantRoleStmt *stmt);
/*
@ -342,14 +344,15 @@ DeparseGrantRoleStmt(Node *node)
/*
* AppendGrantRoleStmt generates the string representation of the
* GrantRoleStmt and appends it to the buffer.
* Append the 'RESTRICT' or 'CASCADE' clause to the given buffer if the given
* statement is a 'REVOKE' statement and the behavior is specified.
* After PostgreSQL 16, the behavior is specified in the 'opt' field of
* GrantRoleStmt and may have multiple values.
* Here, compile time version is checked to support both versions.
*/
static void
AppendGrantRoleStmt(StringInfo buf, GrantRoleStmt *stmt)
AppendRevokeAdminOptionFor(StringInfo buf, GrantRoleStmt *stmt)
{
appendStringInfo(buf, "%s ", stmt->is_grant ? "GRANT" : "REVOKE");
#if PG_VERSION_NUM >= PG_VERSION_16
if (!stmt->is_grant)
{
@ -369,13 +372,12 @@ AppendGrantRoleStmt(StringInfo buf, GrantRoleStmt *stmt)
appendStringInfo(buf, "ADMIN OPTION FOR ");
}
#endif
}
AppendRoleList(buf, stmt->granted_roles);
appendStringInfo(buf, "%s ", stmt->is_grant ? " TO " : " FROM ");
AppendRoleList(buf, stmt->grantee_roles);
static void
AppendGrantWithAdminOption(StringInfo buf, GrantRoleStmt *stmt)
{
if (stmt->is_grant)
{
#if PG_VERSION_NUM >= PG_VERSION_16
@ -394,23 +396,27 @@ AppendGrantRoleStmt(StringInfo buf, GrantRoleStmt *stmt)
appendStringInfo(buf, " WITH ADMIN OPTION");
}
#endif
}
}
if (stmt->grantor)
{
appendStringInfo(buf, " GRANTED BY %s", RoleSpecString(stmt->grantor, true));
}
}
else
{
if (stmt->behavior == DROP_RESTRICT)
{
appendStringInfo(buf, " RESTRICT");
}
else if (stmt->behavior == DROP_CASCADE)
{
appendStringInfo(buf, " CASCADE");
}
}
/*
* AppendGrantRoleStmt generates the string representation of the
* GrantRoleStmt and appends it to the buffer.
*/
static void
AppendGrantRoleStmt(StringInfo buf, GrantRoleStmt *stmt)
{
appendStringInfo(buf, "%s ", stmt->is_grant ? "GRANT" : "REVOKE");
AppendRevokeAdminOptionFor(buf, stmt);
AppendRoleList(buf, stmt->granted_roles);
appendStringInfo(buf, "%s ", stmt->is_grant ? " TO " : " FROM ");
AppendRoleList(buf, stmt->grantee_roles);
AppendGrantWithAdminOption(buf, stmt);
AppendGrantedByInGrantForRoleSpec(buf, stmt->grantor, stmt->is_grant);
AppendGrantRestrictAndCascadeForRoleSpec(buf, stmt->behavior, stmt->is_grant);
AppendGrantedByInGrantForRoleSpec(buf, stmt->grantor, stmt->is_grant);
appendStringInfo(buf, ";");
}

View File

@ -178,35 +178,11 @@ AppendGrantOnSchemaStmt(StringInfo buf, GrantStmt *stmt)
{
Assert(stmt->objtype == OBJECT_SCHEMA);
appendStringInfo(buf, "%s ", stmt->is_grant ? "GRANT" : "REVOKE");
if (!stmt->is_grant && stmt->grant_option)
{
appendStringInfo(buf, "GRANT OPTION FOR ");
}
AppendGrantPrivileges(buf, stmt);
AppendGrantSharedPrefix(buf, stmt);
AppendGrantOnSchemaSchemas(buf, stmt);
AppendGrantGrantees(buf, stmt);
if (stmt->is_grant && stmt->grant_option)
{
appendStringInfo(buf, " WITH GRANT OPTION");
}
if (!stmt->is_grant)
{
if (stmt->behavior == DROP_RESTRICT)
{
appendStringInfo(buf, " RESTRICT");
}
else if (stmt->behavior == DROP_CASCADE)
{
appendStringInfo(buf, " CASCADE");
}
}
appendStringInfo(buf, ";");
AppendGrantSharedSuffix(buf, stmt);
}

View File

@ -389,35 +389,11 @@ AppendGrantOnSequenceStmt(StringInfo buf, GrantStmt *stmt)
"GRANT .. ALL SEQUENCES IN SCHEMA is not supported for formatting.");
}
appendStringInfoString(buf, stmt->is_grant ? "GRANT " : "REVOKE ");
if (!stmt->is_grant && stmt->grant_option)
{
appendStringInfoString(buf, "GRANT OPTION FOR ");
}
AppendGrantPrivileges(buf, stmt);
AppendGrantSharedPrefix(buf, stmt);
AppendGrantOnSequenceSequences(buf, stmt);
AppendGrantGrantees(buf, stmt);
if (stmt->is_grant && stmt->grant_option)
{
appendStringInfoString(buf, " WITH GRANT OPTION");
}
if (!stmt->is_grant)
{
if (stmt->behavior == DROP_RESTRICT)
{
appendStringInfoString(buf, " RESTRICT");
}
else if (stmt->behavior == DROP_CASCADE)
{
appendStringInfoString(buf, " CASCADE");
}
}
appendStringInfoString(buf, ";");
AppendGrantSharedSuffix(buf, stmt);
}

View File

@ -109,6 +109,18 @@ extern char * DeparseAlterSchemaOwnerStmt(Node *node);
extern void AppendGrantPrivileges(StringInfo buf, GrantStmt *stmt);
extern void AppendGrantGrantees(StringInfo buf, GrantStmt *stmt);
extern void AppendWithGrantOption(StringInfo buf, GrantStmt *stmt);
extern void AppendGrantOptionFor(StringInfo buf, GrantStmt *stmt);
extern void AppendGrantRestrictAndCascadeForRoleSpec(StringInfo buf, DropBehavior
behavior, bool isGrant);
extern void AppendGrantRestrictAndCascade(StringInfo buf, GrantStmt *stmt);
extern void AppendGrantedByInGrantForRoleSpec(StringInfo buf, RoleSpec *grantor, bool
isGrant);
extern void AppendGrantedByInGrant(StringInfo buf, GrantStmt *stmt);
extern void AppendGrantSharedPrefix(StringInfo buf, GrantStmt *stmt);
extern void AppendGrantSharedSuffix(StringInfo buf, GrantStmt *stmt);
/* forward declarations for deparse_statistics_stmts.c */
extern char * DeparseCreateStatisticsStmt(Node *node);