1 /*-------------------------------------------------------------------------
2  *
3  * deparse_collation_stmts.c
4  *	  All routines to deparse collation statements.
5  *	  This file contains all entry points specific for type statement deparsing as well as
6  *	  functions that are currently only used for deparsing of the type statements.
7  *
8  * Copyright (c) Citus Data, Inc.
9  *
10  *-------------------------------------------------------------------------
11  */
12 
13 #include "postgres.h"
14 
15 #include "catalog/namespace.h"
16 #include "lib/stringinfo.h"
17 #include "nodes/value.h"
18 #include "utils/builtins.h"
19 
20 #include "distributed/deparser.h"
21 #include "distributed/citus_ruleutils.h"
22 
23 static void AppendDropCollationStmt(StringInfo buf, DropStmt *stmt);
24 static void AppendRenameCollationStmt(StringInfo buf, RenameStmt *stmt);
25 static void AppendAlterCollationSchemaStmt(StringInfo buf, AlterObjectSchemaStmt *stmt);
26 static void AppendAlterCollationOwnerStmt(StringInfo buf, AlterOwnerStmt *stmt);
27 static void AppendNameList(StringInfo buf, List *objects);
28 
29 
30 /*
31  * DeparseDropCollationStmt builds and returns a string representing the DropStmt
32  */
33 char *
DeparseDropCollationStmt(Node * node)34 DeparseDropCollationStmt(Node *node)
35 {
36 	DropStmt *stmt = castNode(DropStmt, node);
37 	StringInfoData str = { 0 };
38 	initStringInfo(&str);
39 
40 	Assert(stmt->removeType == OBJECT_COLLATION);
41 
42 	AppendDropCollationStmt(&str, stmt);
43 
44 	return str.data;
45 }
46 
47 
48 /*
49  * AppendDropCollationStmt appends a string representing the DropStmt to a buffer
50  */
51 static void
AppendDropCollationStmt(StringInfo buf,DropStmt * stmt)52 AppendDropCollationStmt(StringInfo buf, DropStmt *stmt)
53 {
54 	appendStringInfoString(buf, "DROP COLLATION ");
55 	if (stmt->missing_ok)
56 	{
57 		appendStringInfoString(buf, "IF EXISTS ");
58 	}
59 	AppendNameList(buf, stmt->objects);
60 	if (stmt->behavior == DROP_CASCADE)
61 	{
62 		appendStringInfoString(buf, " CASCADE");
63 	}
64 }
65 
66 
67 /*
68  * DeparseRenameCollationStmt builds and returns a string representing the RenameStmt
69  */
70 char *
DeparseRenameCollationStmt(Node * node)71 DeparseRenameCollationStmt(Node *node)
72 {
73 	RenameStmt *stmt = castNode(RenameStmt, node);
74 	StringInfoData str = { 0 };
75 	initStringInfo(&str);
76 
77 	Assert(stmt->renameType == OBJECT_COLLATION);
78 
79 	AppendRenameCollationStmt(&str, stmt);
80 
81 	return str.data;
82 }
83 
84 
85 /*
86  * AppendRenameCollationStmt appends a string representing the RenameStmt to a buffer
87  */
88 static void
AppendRenameCollationStmt(StringInfo buf,RenameStmt * stmt)89 AppendRenameCollationStmt(StringInfo buf, RenameStmt *stmt)
90 {
91 	List *names = (List *) stmt->object;
92 
93 	appendStringInfo(buf, "ALTER COLLATION %s RENAME TO %s;",
94 					 NameListToQuotedString(names),
95 					 quote_identifier(stmt->newname));
96 }
97 
98 
99 /*
100  * DeparseAlterCollationSchemaStmt builds and returns a string representing the AlterObjectSchemaStmt
101  */
102 char *
DeparseAlterCollationSchemaStmt(Node * node)103 DeparseAlterCollationSchemaStmt(Node *node)
104 {
105 	AlterObjectSchemaStmt *stmt = castNode(AlterObjectSchemaStmt, node);
106 	StringInfoData str = { 0 };
107 	initStringInfo(&str);
108 
109 	Assert(stmt->objectType == OBJECT_COLLATION);
110 
111 	AppendAlterCollationSchemaStmt(&str, stmt);
112 
113 	return str.data;
114 }
115 
116 
117 /*
118  * AppendAlterCollationSchemaStmt appends a string representing the AlterObjectSchemaStmt to a buffer
119  */
120 static void
AppendAlterCollationSchemaStmt(StringInfo buf,AlterObjectSchemaStmt * stmt)121 AppendAlterCollationSchemaStmt(StringInfo buf, AlterObjectSchemaStmt *stmt)
122 {
123 	Assert(stmt->objectType == OBJECT_COLLATION);
124 
125 	List *names = (List *) stmt->object;
126 	appendStringInfo(buf, "ALTER COLLATION %s SET SCHEMA %s;", NameListToQuotedString(
127 						 names),
128 					 quote_identifier(stmt->newschema));
129 }
130 
131 
132 /*
133  * DeparseAlterCollationOwnerStmt builds and returns a string representing the AlterOwnerStmt
134  */
135 char *
DeparseAlterCollationOwnerStmt(Node * node)136 DeparseAlterCollationOwnerStmt(Node *node)
137 {
138 	AlterOwnerStmt *stmt = castNode(AlterOwnerStmt, node);
139 	StringInfoData str = { 0 };
140 	initStringInfo(&str);
141 
142 	Assert(stmt->objectType == OBJECT_COLLATION);
143 
144 	AppendAlterCollationOwnerStmt(&str, stmt);
145 
146 	return str.data;
147 }
148 
149 
150 /*
151  * AppendAlterCollationOwnerStmt appends a string representing the AlterOwnerStmt to a buffer
152  */
153 static void
AppendAlterCollationOwnerStmt(StringInfo buf,AlterOwnerStmt * stmt)154 AppendAlterCollationOwnerStmt(StringInfo buf, AlterOwnerStmt *stmt)
155 {
156 	Assert(stmt->objectType == OBJECT_COLLATION);
157 
158 	List *names = (List *) stmt->object;
159 	appendStringInfo(buf, "ALTER COLLATION %s OWNER TO %s;", NameListToQuotedString(
160 						 names),
161 					 RoleSpecString(stmt->newowner, true));
162 }
163 
164 
165 static void
AppendNameList(StringInfo buf,List * objects)166 AppendNameList(StringInfo buf, List *objects)
167 {
168 	ListCell *objectCell = NULL;
169 
170 	foreach(objectCell, objects)
171 	{
172 		List *name = castNode(List, lfirst(objectCell));
173 
174 		if (objectCell != list_head(objects))
175 		{
176 			appendStringInfo(buf, ", ");
177 		}
178 
179 		appendStringInfoString(buf, NameListToQuotedString(name));
180 	}
181 }
182