PostgreSQL Source Code  git master
test_ddl_deparse.c
Go to the documentation of this file.
1 /*----------------------------------------------------------------------
2  * test_ddl_deparse.c
3  * Support functions for the test_ddl_deparse module
4  *
5  * Copyright (c) 2014-2022, PostgreSQL Global Development Group
6  *
7  * IDENTIFICATION
8  * src/test/modules/test_ddl_deparse/test_ddl_deparse.c
9  *----------------------------------------------------------------------
10  */
11 #include "postgres.h"
12 
13 #include "catalog/pg_type.h"
14 #include "tcop/deparse_utility.h"
15 #include "tcop/utility.h"
16 #include "utils/builtins.h"
17 
19 
23 
24 /*
25  * Return the textual representation of the struct type used to represent a
26  * command in struct CollectedCommand format.
27  */
28 Datum
30 {
32  const char *type;
33 
34  switch (cmd->type)
35  {
36  case SCT_Simple:
37  type = "simple";
38  break;
39  case SCT_AlterTable:
40  type = "alter table";
41  break;
42  case SCT_Grant:
43  type = "grant";
44  break;
45  case SCT_AlterOpFamily:
46  type = "alter operator family";
47  break;
49  type = "alter default privileges";
50  break;
51  case SCT_CreateOpClass:
52  type = "create operator class";
53  break;
54  case SCT_AlterTSConfig:
55  type = "alter text search configuration";
56  break;
57  default:
58  type = "unknown command type";
59  break;
60  }
61 
63 }
64 
65 /*
66  * Return the command tag corresponding to a parse node contained in a
67  * CollectedCommand struct.
68  */
69 Datum
71 {
73 
74  if (!cmd->parsetree)
76 
78 }
79 
80 /*
81  * Return a text array representation of the subcommands of an ALTER TABLE
82  * command.
83  */
84 Datum
86 {
88  ArrayBuildState *astate = NULL;
89  ListCell *cell;
90 
91  if (cmd->type != SCT_AlterTable)
92  elog(ERROR, "command is not ALTER TABLE");
93 
94  foreach(cell, cmd->d.alterTable.subcmds)
95  {
96  CollectedATSubcmd *sub = lfirst(cell);
98  const char *strtype;
99 
100  switch (subcmd->subtype)
101  {
102  case AT_AddColumn:
103  strtype = "ADD COLUMN";
104  break;
105  case AT_AddColumnRecurse:
106  strtype = "ADD COLUMN (and recurse)";
107  break;
108  case AT_AddColumnToView:
109  strtype = "ADD COLUMN TO VIEW";
110  break;
111  case AT_ColumnDefault:
112  strtype = "ALTER COLUMN SET DEFAULT";
113  break;
115  strtype = "ALTER COLUMN SET DEFAULT (precooked)";
116  break;
117  case AT_DropNotNull:
118  strtype = "DROP NOT NULL";
119  break;
120  case AT_SetNotNull:
121  strtype = "SET NOT NULL";
122  break;
123  case AT_CheckNotNull:
124  strtype = "CHECK NOT NULL";
125  break;
126  case AT_SetStatistics:
127  strtype = "SET STATS";
128  break;
129  case AT_SetOptions:
130  strtype = "SET OPTIONS";
131  break;
132  case AT_ResetOptions:
133  strtype = "RESET OPTIONS";
134  break;
135  case AT_SetStorage:
136  strtype = "SET STORAGE";
137  break;
138  case AT_DropColumn:
139  strtype = "DROP COLUMN";
140  break;
142  strtype = "DROP COLUMN (and recurse)";
143  break;
144  case AT_AddIndex:
145  strtype = "ADD INDEX";
146  break;
147  case AT_ReAddIndex:
148  strtype = "(re) ADD INDEX";
149  break;
150  case AT_AddConstraint:
151  strtype = "ADD CONSTRAINT";
152  break;
154  strtype = "ADD CONSTRAINT (and recurse)";
155  break;
156  case AT_ReAddConstraint:
157  strtype = "(re) ADD CONSTRAINT";
158  break;
159  case AT_AlterConstraint:
160  strtype = "ALTER CONSTRAINT";
161  break;
163  strtype = "VALIDATE CONSTRAINT";
164  break;
166  strtype = "VALIDATE CONSTRAINT (and recurse)";
167  break;
169  strtype = "ADD CONSTRAINT (using index)";
170  break;
171  case AT_DropConstraint:
172  strtype = "DROP CONSTRAINT";
173  break;
175  strtype = "DROP CONSTRAINT (and recurse)";
176  break;
177  case AT_ReAddComment:
178  strtype = "(re) ADD COMMENT";
179  break;
180  case AT_AlterColumnType:
181  strtype = "ALTER COLUMN SET TYPE";
182  break;
184  strtype = "ALTER COLUMN SET OPTIONS";
185  break;
186  case AT_ChangeOwner:
187  strtype = "CHANGE OWNER";
188  break;
189  case AT_ClusterOn:
190  strtype = "CLUSTER";
191  break;
192  case AT_DropCluster:
193  strtype = "DROP CLUSTER";
194  break;
195  case AT_SetLogged:
196  strtype = "SET LOGGED";
197  break;
198  case AT_SetUnLogged:
199  strtype = "SET UNLOGGED";
200  break;
201  case AT_DropOids:
202  strtype = "DROP OIDS";
203  break;
204  case AT_SetTableSpace:
205  strtype = "SET TABLESPACE";
206  break;
207  case AT_SetRelOptions:
208  strtype = "SET RELOPTIONS";
209  break;
210  case AT_ResetRelOptions:
211  strtype = "RESET RELOPTIONS";
212  break;
214  strtype = "REPLACE RELOPTIONS";
215  break;
216  case AT_EnableTrig:
217  strtype = "ENABLE TRIGGER";
218  break;
219  case AT_EnableAlwaysTrig:
220  strtype = "ENABLE TRIGGER (always)";
221  break;
223  strtype = "ENABLE TRIGGER (replica)";
224  break;
225  case AT_DisableTrig:
226  strtype = "DISABLE TRIGGER";
227  break;
228  case AT_EnableTrigAll:
229  strtype = "ENABLE TRIGGER (all)";
230  break;
231  case AT_DisableTrigAll:
232  strtype = "DISABLE TRIGGER (all)";
233  break;
234  case AT_EnableTrigUser:
235  strtype = "ENABLE TRIGGER (user)";
236  break;
237  case AT_DisableTrigUser:
238  strtype = "DISABLE TRIGGER (user)";
239  break;
240  case AT_EnableRule:
241  strtype = "ENABLE RULE";
242  break;
243  case AT_EnableAlwaysRule:
244  strtype = "ENABLE RULE (always)";
245  break;
247  strtype = "ENABLE RULE (replica)";
248  break;
249  case AT_DisableRule:
250  strtype = "DISABLE RULE";
251  break;
252  case AT_AddInherit:
253  strtype = "ADD INHERIT";
254  break;
255  case AT_DropInherit:
256  strtype = "DROP INHERIT";
257  break;
258  case AT_AddOf:
259  strtype = "OF";
260  break;
261  case AT_DropOf:
262  strtype = "NOT OF";
263  break;
264  case AT_ReplicaIdentity:
265  strtype = "REPLICA IDENTITY";
266  break;
268  strtype = "ENABLE ROW SECURITY";
269  break;
271  strtype = "DISABLE ROW SECURITY";
272  break;
273  case AT_ForceRowSecurity:
274  strtype = "FORCE ROW SECURITY";
275  break;
277  strtype = "NO FORCE ROW SECURITY";
278  break;
279  case AT_GenericOptions:
280  strtype = "SET OPTIONS";
281  break;
282  default:
283  strtype = "unrecognized";
284  break;
285  }
286 
287  astate =
288  accumArrayResult(astate, CStringGetTextDatum(strtype),
289  false, TEXTOID, CurrentMemoryContext);
290  }
291 
292  if (astate == NULL)
293  elog(ERROR, "empty alter table subcommand list");
294 
296 }
#define PG_RETURN_ARRAYTYPE_P(x)
Definition: array.h:258
ArrayBuildState * accumArrayResult(ArrayBuildState *astate, Datum dvalue, bool disnull, Oid element_type, MemoryContext rcontext)
Definition: arrayfuncs.c:5123
Datum makeArrayResult(ArrayBuildState *astate, MemoryContext rcontext)
Definition: arrayfuncs.c:5187
#define CStringGetTextDatum(s)
Definition: builtins.h:85
@ SCT_Simple
@ SCT_AlterTSConfig
@ SCT_AlterDefaultPrivileges
@ SCT_Grant
@ SCT_CreateOpClass
@ SCT_AlterOpFamily
@ SCT_AlterTable
#define ERROR
Definition: elog.h:33
#define elog(elevel,...)
Definition: elog.h:218
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:276
#define PG_RETURN_NULL()
Definition: fmgr.h:345
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:372
#define PG_FUNCTION_ARGS
Definition: fmgr.h:193
MemoryContext CurrentMemoryContext
Definition: mcxt.c:42
#define castNode(_type_, nodeptr)
Definition: nodes.h:642
@ AT_AddIndexConstraint
Definition: parsenodes.h:2251
@ AT_DropOf
Definition: parsenodes.h:2283
@ AT_CheckNotNull
Definition: parsenodes.h:2234
@ AT_SetOptions
Definition: parsenodes.h:2236
@ AT_DisableTrigUser
Definition: parsenodes.h:2275
@ AT_DropNotNull
Definition: parsenodes.h:2231
@ AT_AddOf
Definition: parsenodes.h:2282
@ AT_ResetOptions
Definition: parsenodes.h:2237
@ AT_ReplicaIdentity
Definition: parsenodes.h:2284
@ AT_DropColumnRecurse
Definition: parsenodes.h:2241
@ AT_ReplaceRelOptions
Definition: parsenodes.h:2267
@ AT_EnableRowSecurity
Definition: parsenodes.h:2285
@ AT_AddColumnToView
Definition: parsenodes.h:2228
@ AT_ResetRelOptions
Definition: parsenodes.h:2266
@ AT_AddConstraintRecurse
Definition: parsenodes.h:2245
@ AT_EnableReplicaTrig
Definition: parsenodes.h:2270
@ AT_DropOids
Definition: parsenodes.h:2262
@ AT_SetUnLogged
Definition: parsenodes.h:2261
@ AT_DisableTrig
Definition: parsenodes.h:2271
@ AT_AddIndex
Definition: parsenodes.h:2242
@ AT_EnableReplicaRule
Definition: parsenodes.h:2278
@ AT_ReAddIndex
Definition: parsenodes.h:2243
@ AT_DropConstraint
Definition: parsenodes.h:2252
@ AT_SetNotNull
Definition: parsenodes.h:2232
@ AT_ClusterOn
Definition: parsenodes.h:2258
@ AT_ForceRowSecurity
Definition: parsenodes.h:2287
@ AT_EnableAlwaysRule
Definition: parsenodes.h:2277
@ AT_AlterColumnType
Definition: parsenodes.h:2255
@ AT_AddInherit
Definition: parsenodes.h:2280
@ AT_EnableTrig
Definition: parsenodes.h:2268
@ AT_AddColumnRecurse
Definition: parsenodes.h:2227
@ AT_DropColumn
Definition: parsenodes.h:2240
@ AT_ValidateConstraintRecurse
Definition: parsenodes.h:2250
@ AT_ReAddComment
Definition: parsenodes.h:2254
@ AT_AlterColumnGenericOptions
Definition: parsenodes.h:2256
@ AT_DisableTrigAll
Definition: parsenodes.h:2273
@ AT_EnableRule
Definition: parsenodes.h:2276
@ AT_NoForceRowSecurity
Definition: parsenodes.h:2288
@ AT_SetStatistics
Definition: parsenodes.h:2235
@ AT_AddConstraint
Definition: parsenodes.h:2244
@ AT_DropInherit
Definition: parsenodes.h:2281
@ AT_EnableAlwaysTrig
Definition: parsenodes.h:2269
@ AT_SetLogged
Definition: parsenodes.h:2260
@ AT_SetStorage
Definition: parsenodes.h:2238
@ AT_DisableRule
Definition: parsenodes.h:2279
@ AT_DisableRowSecurity
Definition: parsenodes.h:2286
@ AT_SetRelOptions
Definition: parsenodes.h:2265
@ AT_ChangeOwner
Definition: parsenodes.h:2257
@ AT_EnableTrigUser
Definition: parsenodes.h:2274
@ AT_ReAddConstraint
Definition: parsenodes.h:2246
@ AT_DropConstraintRecurse
Definition: parsenodes.h:2253
@ AT_SetTableSpace
Definition: parsenodes.h:2264
@ AT_GenericOptions
Definition: parsenodes.h:2289
@ AT_ColumnDefault
Definition: parsenodes.h:2229
@ AT_CookedColumnDefault
Definition: parsenodes.h:2230
@ AT_AlterConstraint
Definition: parsenodes.h:2248
@ AT_EnableTrigAll
Definition: parsenodes.h:2272
@ AT_DropCluster
Definition: parsenodes.h:2259
@ AT_ValidateConstraint
Definition: parsenodes.h:2249
@ AT_AddColumn
Definition: parsenodes.h:2226
#define lfirst(lc)
Definition: pg_list.h:169
uintptr_t Datum
Definition: postgres.h:411
AlterTableType subtype
Definition: parsenodes.h:2309
CollectedCommandType type
union CollectedCommand::@117 d
struct CollectedCommand::@117::@119 alterTable
PG_MODULE_MAGIC
Datum get_command_type(PG_FUNCTION_ARGS)
Datum get_altertable_subcmdtypes(PG_FUNCTION_ARGS)
PG_FUNCTION_INFO_V1(get_command_type)
Datum get_command_tag(PG_FUNCTION_ARGS)
static const char * CreateCommandName(Node *parsetree)
Definition: utility.h:103
text * cstring_to_text(const char *s)
Definition: varlena.c:188