Refactors object address

pull/7388/head
gurkanindibay 2023-12-26 18:10:55 +03:00
parent 3e8658aff4
commit dbc20c4f79
7 changed files with 41 additions and 59 deletions

View File

@ -123,3 +123,24 @@ GetCommentForObject(Oid oid)
return comment;
}
/*
* CommentObjectAddress resolves the ObjectAddress for the object
* on which the comment is placed. Optionally errors if the object does not
* exist based on the missing_ok flag passed in by the caller.
*/
List *
CommentObjectAddress(Node *node, bool missing_ok, bool isPostprocess)
{
CommentStmt *stmt = castNode(CommentStmt, node);
Relation relation;
ObjectAddress objectAddress = get_object_address(stmt->objtype, stmt->object,
&relation, AccessExclusiveLock,
missing_ok);
ObjectAddress *objectAddressCopy = palloc0(sizeof(ObjectAddress));
*objectAddressCopy = objectAddress;
return list_make1(objectAddressCopy);
}

View File

@ -793,25 +793,3 @@ CreateDatabaseDDLCommand(Oid dbId)
return outerDbStmt->data;
}
/*
* DatabaseCommentObjectAddress resolves the ObjectAddress for the DATABASE
* on which the comment is placed. Optionally errors if the database does not
* exist based on the missing_ok flag passed in by the caller.
*/
List *
DatabaseCommentObjectAddress(Node *node, bool missing_ok, bool isPostprocess)
{
CommentStmt *stmt = castNode(CommentStmt, node);
Relation relation;
Assert(stmt->objtype == OBJECT_DATABASE);
ObjectAddress objectAddress = get_object_address(stmt->objtype, stmt->object,
&relation, AccessExclusiveLock,
missing_ok);
ObjectAddress *objectAddressCopy = palloc0(sizeof(ObjectAddress));
*objectAddressCopy = objectAddress;
return list_make1(objectAddressCopy);
}

View File

@ -15,6 +15,7 @@
#include "pg_version_constants.h"
#include "distributed/commands.h"
#include "distributed/comment.h"
#include "distributed/commands/utility_hook.h"
#include "distributed/deparser.h"
#include "distributed/version_compat.h"
@ -312,7 +313,7 @@ static DistributeObjectOps Role_Comment = {
.postprocess = NULL,
.objectType = OBJECT_DATABASE,
.operationType = DIST_OPS_ALTER,
.address = RoleCommentObjectAddress,
.address = CommentObjectAddress,
.markDistributed = false,
};
static DistributeObjectOps Any_CreateForeignServer = {
@ -551,7 +552,7 @@ static DistributeObjectOps Database_Comment = {
.postprocess = NULL,
.objectType = OBJECT_DATABASE,
.operationType = DIST_OPS_ALTER,
.address = DatabaseCommentObjectAddress,
.address = CommentObjectAddress,
.markDistributed = false,
};
@ -1000,7 +1001,7 @@ static DistributeObjectOps TextSearchConfig_Comment = {
.postprocess = NULL,
.objectType = OBJECT_TSCONFIGURATION,
.operationType = DIST_OPS_ALTER,
.address = TextSearchConfigurationCommentObjectAddress,
.address = CommentObjectAddress,
.markDistributed = false,
};
static DistributeObjectOps TextSearchConfig_Define = {
@ -1069,7 +1070,7 @@ static DistributeObjectOps TextSearchDict_Comment = {
.postprocess = NULL,
.objectType = OBJECT_TSDICTIONARY,
.operationType = DIST_OPS_ALTER,
.address = TextSearchDictCommentObjectAddress,
.address = CommentObjectAddress,
.markDistributed = false,
};
static DistributeObjectOps TextSearchDict_Define = {

View File

@ -1424,25 +1424,3 @@ RenameRoleStmtObjectAddress(Node *node, bool missing_ok, bool isPostprocess)
return list_make1(address);
}
/*
* RoleCommentObjectAddress resolves the ObjectAddress for the ROLE
* on which the comment is placed. Optionally errors if the role does not
* exist based on the missing_ok flag passed in by the caller.
*/
List *
RoleCommentObjectAddress(Node *node, bool missing_ok, bool isPostprocess)
{
CommentStmt *stmt = castNode(CommentStmt, node);
Relation relation;
Assert(stmt->objtype == OBJECT_ROLE);
ObjectAddress objectAddress = get_object_address(stmt->objtype, stmt->object,
&relation, AccessExclusiveLock,
missing_ok);
ObjectAddress *objectAddressCopy = palloc0(sizeof(ObjectAddress));
*objectAddressCopy = objectAddress;
return list_make1(objectAddressCopy);
}

View File

@ -27,13 +27,14 @@
#include "distributed/log_utils.h"
const char * const ObjectTypeNames[] =
const char *const ObjectTypeNames[] =
{
[OBJECT_DATABASE] = "DATABASE",
[OBJECT_ROLE] = "ROLE",
[OBJECT_TSCONFIGURATION] = "TEXT SEARCH CONFIGURATION",
[OBJECT_TSDICTIONARY] = "TEXT SEARCH DICTIONARY",
/* etc. */
[OBJECT_DATABASE] = "DATABASE",
[OBJECT_ROLE] = "ROLE",
[OBJECT_TSCONFIGURATION] = "TEXT SEARCH CONFIGURATION",
[OBJECT_TSDICTIONARY] = "TEXT SEARCH DICTIONARY",
/* etc. */
};
char *
@ -44,12 +45,12 @@ DeparseCommentStmt(Node *node)
initStringInfo(&str);
const char *objectName = quote_identifier(strVal(stmt->object));
const char *objectType = ObjectTypeNames[stmt->objtype];
const char *objectType = ObjectTypeNames[stmt->objtype];
char *comment = stmt->comment != NULL ? quote_literal_cstr(stmt->comment) : "NULL";
appendStringInfo(&str, "COMMENT ON %s %s IS %s;",objectType, objectName, comment);
appendStringInfo(&str, "COMMENT ON %s %s IS %s;", objectType, objectName, comment);
return str.data;
}

View File

@ -249,8 +249,6 @@ extern List * GenerateGrantDatabaseCommandList(void);
extern List * PostprocessAlterDatabaseRenameStmt(Node *node, const char *queryString);
extern void EnsureSupportedCreateDatabaseCommand(CreatedbStmt *stmt);
extern char * CreateDatabaseDDLCommand(Oid dbId);
extern List * DatabaseCommentObjectAddress(Node *node, bool missing_ok,
bool isPostprocess);
/* domain.c - forward declarations */
@ -523,7 +521,6 @@ extern List * RenameRoleStmtObjectAddress(Node *stmt, bool missing_ok, bool
extern void UnmarkRolesDistributed(List *roles);
extern List * FilterDistributedRoles(List *roles);
List * RoleCommentObjectAddress(Node *node, bool missing_ok, bool isPostprocess);
/* schema.c - forward declarations */
extern List * PostprocessCreateSchemaStmt(Node *node, const char *queryString);

View File

@ -8,6 +8,9 @@
*-------------------------------------------------------------------------
*/
#ifndef COMMENT_H
#define COMMENT_H
#include "postgres.h"
#include "nodes/parsenodes.h"
@ -21,3 +24,6 @@ typedef struct CommentStmtType
extern List * GetCommentPropagationCommands(Oid oid, char *objectName, ObjectType
objectType);
extern List * CommentObjectAddress(Node *node, bool missing_ok, bool isPostprocess);
# endif /* COMMENT_H */