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-2023, 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 "funcapi.h"
15 #include "nodes/execnodes.h"
16 #include "tcop/deparse_utility.h"
17 #include "tcop/utility.h"
18 #include "utils/builtins.h"
19 
21 
25 
26 /*
27  * Return the textual representation of the struct type used to represent a
28  * command in struct CollectedCommand format.
29  */
30 Datum
32 {
34  const char *type;
35 
36  switch (cmd->type)
37  {
38  case SCT_Simple:
39  type = "simple";
40  break;
41  case SCT_AlterTable:
42  type = "alter table";
43  break;
44  case SCT_Grant:
45  type = "grant";
46  break;
47  case SCT_AlterOpFamily:
48  type = "alter operator family";
49  break;
51  type = "alter default privileges";
52  break;
53  case SCT_CreateOpClass:
54  type = "create operator class";
55  break;
56  case SCT_AlterTSConfig:
57  type = "alter text search configuration";
58  break;
59  default:
60  type = "unknown command type";
61  break;
62  }
63 
65 }
66 
67 /*
68  * Return the command tag corresponding to a parse node contained in a
69  * CollectedCommand struct.
70  */
71 Datum
73 {
75 
76  if (!cmd->parsetree)
78 
80 }
81 
82 /*
83  * Return a text array representation of the subcommands of an ALTER TABLE
84  * command.
85  */
86 Datum
88 {
90  ListCell *cell;
91  ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
92 
93  if (cmd->type != SCT_AlterTable)
94  elog(ERROR, "command is not ALTER TABLE");
95 
96  InitMaterializedSRF(fcinfo, 0);
97 
98  if (cmd->d.alterTable.subcmds == NIL)
99  elog(ERROR, "empty alter table subcommand list");
100 
101  foreach(cell, cmd->d.alterTable.subcmds)
102  {
103  CollectedATSubcmd *sub = lfirst(cell);
105  const char *strtype = "unrecognized";
106  Datum values[2];
107  bool nulls[2];
108 
109  memset(values, 0, sizeof(values));
110  memset(nulls, 0, sizeof(nulls));
111 
112  switch (subcmd->subtype)
113  {
114  case AT_AddColumn:
115  strtype = "ADD COLUMN";
116  break;
117  case AT_AddColumnToView:
118  strtype = "ADD COLUMN TO VIEW";
119  break;
120  case AT_ColumnDefault:
121  strtype = "ALTER COLUMN SET DEFAULT";
122  break;
124  strtype = "ALTER COLUMN SET DEFAULT (precooked)";
125  break;
126  case AT_DropNotNull:
127  strtype = "DROP NOT NULL";
128  break;
129  case AT_SetNotNull:
130  strtype = "SET NOT NULL";
131  break;
132  case AT_SetAttNotNull:
133  strtype = "SET ATTNOTNULL";
134  break;
135  case AT_DropExpression:
136  strtype = "DROP EXPRESSION";
137  break;
138  case AT_SetStatistics:
139  strtype = "SET STATS";
140  break;
141  case AT_SetOptions:
142  strtype = "SET OPTIONS";
143  break;
144  case AT_ResetOptions:
145  strtype = "RESET OPTIONS";
146  break;
147  case AT_SetStorage:
148  strtype = "SET STORAGE";
149  break;
150  case AT_SetCompression:
151  strtype = "SET COMPRESSION";
152  break;
153  case AT_DropColumn:
154  strtype = "DROP COLUMN";
155  break;
156  case AT_AddIndex:
157  strtype = "ADD INDEX";
158  break;
159  case AT_ReAddIndex:
160  strtype = "(re) ADD INDEX";
161  break;
162  case AT_AddConstraint:
163  strtype = "ADD CONSTRAINT";
164  break;
165  case AT_ReAddConstraint:
166  strtype = "(re) ADD CONSTRAINT";
167  break;
169  strtype = "(re) ADD DOMAIN CONSTRAINT";
170  break;
171  case AT_AlterConstraint:
172  strtype = "ALTER CONSTRAINT";
173  break;
175  strtype = "VALIDATE CONSTRAINT";
176  break;
178  strtype = "ADD CONSTRAINT (using index)";
179  break;
180  case AT_DropConstraint:
181  strtype = "DROP CONSTRAINT";
182  break;
183  case AT_ReAddComment:
184  strtype = "(re) ADD COMMENT";
185  break;
186  case AT_AlterColumnType:
187  strtype = "ALTER COLUMN SET TYPE";
188  break;
190  strtype = "ALTER COLUMN SET OPTIONS";
191  break;
192  case AT_ChangeOwner:
193  strtype = "CHANGE OWNER";
194  break;
195  case AT_ClusterOn:
196  strtype = "CLUSTER";
197  break;
198  case AT_DropCluster:
199  strtype = "DROP CLUSTER";
200  break;
201  case AT_SetLogged:
202  strtype = "SET LOGGED";
203  break;
204  case AT_SetUnLogged:
205  strtype = "SET UNLOGGED";
206  break;
207  case AT_DropOids:
208  strtype = "DROP OIDS";
209  break;
210  case AT_SetAccessMethod:
211  strtype = "SET ACCESS METHOD";
212  break;
213  case AT_SetTableSpace:
214  strtype = "SET TABLESPACE";
215  break;
216  case AT_SetRelOptions:
217  strtype = "SET RELOPTIONS";
218  break;
219  case AT_ResetRelOptions:
220  strtype = "RESET RELOPTIONS";
221  break;
223  strtype = "REPLACE RELOPTIONS";
224  break;
225  case AT_EnableTrig:
226  strtype = "ENABLE TRIGGER";
227  break;
228  case AT_EnableAlwaysTrig:
229  strtype = "ENABLE TRIGGER (always)";
230  break;
232  strtype = "ENABLE TRIGGER (replica)";
233  break;
234  case AT_DisableTrig:
235  strtype = "DISABLE TRIGGER";
236  break;
237  case AT_EnableTrigAll:
238  strtype = "ENABLE TRIGGER (all)";
239  break;
240  case AT_DisableTrigAll:
241  strtype = "DISABLE TRIGGER (all)";
242  break;
243  case AT_EnableTrigUser:
244  strtype = "ENABLE TRIGGER (user)";
245  break;
246  case AT_DisableTrigUser:
247  strtype = "DISABLE TRIGGER (user)";
248  break;
249  case AT_EnableRule:
250  strtype = "ENABLE RULE";
251  break;
252  case AT_EnableAlwaysRule:
253  strtype = "ENABLE RULE (always)";
254  break;
256  strtype = "ENABLE RULE (replica)";
257  break;
258  case AT_DisableRule:
259  strtype = "DISABLE RULE";
260  break;
261  case AT_AddInherit:
262  strtype = "ADD INHERIT";
263  break;
264  case AT_DropInherit:
265  strtype = "DROP INHERIT";
266  break;
267  case AT_AddOf:
268  strtype = "OF";
269  break;
270  case AT_DropOf:
271  strtype = "NOT OF";
272  break;
273  case AT_ReplicaIdentity:
274  strtype = "REPLICA IDENTITY";
275  break;
277  strtype = "ENABLE ROW SECURITY";
278  break;
280  strtype = "DISABLE ROW SECURITY";
281  break;
282  case AT_ForceRowSecurity:
283  strtype = "FORCE ROW SECURITY";
284  break;
286  strtype = "NO FORCE ROW SECURITY";
287  break;
288  case AT_GenericOptions:
289  strtype = "SET OPTIONS";
290  break;
291  case AT_DetachPartition:
292  strtype = "DETACH PARTITION";
293  break;
294  case AT_AttachPartition:
295  strtype = "ATTACH PARTITION";
296  break;
298  strtype = "DETACH PARTITION ... FINALIZE";
299  break;
300  case AT_AddIdentity:
301  strtype = "ADD IDENTITY";
302  break;
303  case AT_SetIdentity:
304  strtype = "SET IDENTITY";
305  break;
306  case AT_DropIdentity:
307  strtype = "DROP IDENTITY";
308  break;
309  case AT_ReAddStatistics:
310  strtype = "(re) ADD STATS";
311  break;
312  }
313 
314  if (subcmd->recurse)
315  values[0] = CStringGetTextDatum(psprintf("%s (and recurse)", strtype));
316  else
317  values[0] = CStringGetTextDatum(strtype);
318  if (OidIsValid(sub->address.objectId))
319  {
320  char *objdesc;
321 
322  objdesc = getObjectDescription((const ObjectAddress *) &sub->address, false);
323  values[1] = CStringGetTextDatum(objdesc);
324  }
325  else
326  nulls[1] = true;
327 
328  tuplestore_putvalues(rsinfo->setResult, rsinfo->setDesc, values, nulls);
329  }
330 
331  return (Datum) 0;
332 }
static Datum values[MAXATTR]
Definition: bootstrap.c:156
#define CStringGetTextDatum(s)
Definition: builtins.h:94
#define OidIsValid(objectId)
Definition: c.h:764
@ SCT_Simple
@ SCT_AlterTSConfig
@ SCT_AlterDefaultPrivileges
@ SCT_Grant
@ SCT_CreateOpClass
@ SCT_AlterOpFamily
@ SCT_AlterTable
#define ERROR
Definition: elog.h:39
#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
void InitMaterializedSRF(FunctionCallInfo fcinfo, bits32 flags)
Definition: funcapi.c:76
if(TABLE==NULL||TABLE_index==NULL)
Definition: isn.c:77
#define castNode(_type_, nodeptr)
Definition: nodes.h:197
char * getObjectDescription(const ObjectAddress *object, bool missing_ok)
@ AT_AddIndexConstraint
Definition: parsenodes.h:2233
@ AT_DropOf
Definition: parsenodes.h:2264
@ AT_SetOptions
Definition: parsenodes.h:2221
@ AT_DropIdentity
Definition: parsenodes.h:2276
@ AT_SetAttNotNull
Definition: parsenodes.h:2218
@ AT_DisableTrigUser
Definition: parsenodes.h:2256
@ AT_DropNotNull
Definition: parsenodes.h:2216
@ AT_AddOf
Definition: parsenodes.h:2263
@ AT_ResetOptions
Definition: parsenodes.h:2222
@ AT_ReplicaIdentity
Definition: parsenodes.h:2265
@ AT_ReplaceRelOptions
Definition: parsenodes.h:2248
@ AT_EnableRowSecurity
Definition: parsenodes.h:2266
@ AT_AddColumnToView
Definition: parsenodes.h:2213
@ AT_ResetRelOptions
Definition: parsenodes.h:2247
@ AT_EnableReplicaTrig
Definition: parsenodes.h:2251
@ AT_DropOids
Definition: parsenodes.h:2243
@ AT_SetIdentity
Definition: parsenodes.h:2275
@ AT_ReAddStatistics
Definition: parsenodes.h:2277
@ AT_SetUnLogged
Definition: parsenodes.h:2242
@ AT_DisableTrig
Definition: parsenodes.h:2252
@ AT_SetCompression
Definition: parsenodes.h:2224
@ AT_DropExpression
Definition: parsenodes.h:2219
@ AT_AddIndex
Definition: parsenodes.h:2226
@ AT_EnableReplicaRule
Definition: parsenodes.h:2259
@ AT_ReAddIndex
Definition: parsenodes.h:2227
@ AT_DropConstraint
Definition: parsenodes.h:2234
@ AT_SetNotNull
Definition: parsenodes.h:2217
@ AT_ClusterOn
Definition: parsenodes.h:2239
@ AT_AddIdentity
Definition: parsenodes.h:2274
@ AT_ForceRowSecurity
Definition: parsenodes.h:2268
@ AT_EnableAlwaysRule
Definition: parsenodes.h:2258
@ AT_SetAccessMethod
Definition: parsenodes.h:2244
@ AT_AlterColumnType
Definition: parsenodes.h:2236
@ AT_DetachPartitionFinalize
Definition: parsenodes.h:2273
@ AT_AddInherit
Definition: parsenodes.h:2261
@ AT_ReAddDomainConstraint
Definition: parsenodes.h:2230
@ AT_EnableTrig
Definition: parsenodes.h:2249
@ AT_DropColumn
Definition: parsenodes.h:2225
@ AT_ReAddComment
Definition: parsenodes.h:2235
@ AT_AlterColumnGenericOptions
Definition: parsenodes.h:2237
@ AT_DisableTrigAll
Definition: parsenodes.h:2254
@ AT_EnableRule
Definition: parsenodes.h:2257
@ AT_NoForceRowSecurity
Definition: parsenodes.h:2269
@ AT_DetachPartition
Definition: parsenodes.h:2272
@ AT_SetStatistics
Definition: parsenodes.h:2220
@ AT_AttachPartition
Definition: parsenodes.h:2271
@ AT_AddConstraint
Definition: parsenodes.h:2228
@ AT_DropInherit
Definition: parsenodes.h:2262
@ AT_EnableAlwaysTrig
Definition: parsenodes.h:2250
@ AT_SetLogged
Definition: parsenodes.h:2241
@ AT_SetStorage
Definition: parsenodes.h:2223
@ AT_DisableRule
Definition: parsenodes.h:2260
@ AT_DisableRowSecurity
Definition: parsenodes.h:2267
@ AT_SetRelOptions
Definition: parsenodes.h:2246
@ AT_ChangeOwner
Definition: parsenodes.h:2238
@ AT_EnableTrigUser
Definition: parsenodes.h:2255
@ AT_ReAddConstraint
Definition: parsenodes.h:2229
@ AT_SetTableSpace
Definition: parsenodes.h:2245
@ AT_GenericOptions
Definition: parsenodes.h:2270
@ AT_ColumnDefault
Definition: parsenodes.h:2214
@ AT_CookedColumnDefault
Definition: parsenodes.h:2215
@ AT_AlterConstraint
Definition: parsenodes.h:2231
@ AT_EnableTrigAll
Definition: parsenodes.h:2253
@ AT_DropCluster
Definition: parsenodes.h:2240
@ AT_ValidateConstraint
Definition: parsenodes.h:2232
@ AT_AddColumn
Definition: parsenodes.h:2212
#define lfirst(lc)
Definition: pg_list.h:172
#define NIL
Definition: pg_list.h:68
uintptr_t Datum
Definition: postgres.h:64
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
AlterTableType subtype
Definition: parsenodes.h:2290
ObjectAddress address
CollectedCommandType type
union CollectedCommand::@112 d
struct CollectedCommand::@112::@114 alterTable
TupleDesc setDesc
Definition: execnodes.h:334
Tuplestorestate * setResult
Definition: execnodes.h:333
PG_MODULE_MAGIC
Datum get_altertable_subcmdinfo(PG_FUNCTION_ARGS)
Datum get_command_type(PG_FUNCTION_ARGS)
PG_FUNCTION_INFO_V1(get_command_type)
Datum get_command_tag(PG_FUNCTION_ARGS)
void tuplestore_putvalues(Tuplestorestate *state, TupleDesc tdesc, Datum *values, bool *isnull)
Definition: tuplestore.c:750
static const char * CreateCommandName(Node *parsetree)
Definition: utility.h:103
text * cstring_to_text(const char *s)
Definition: varlena.c:182
const char * type