Age Owner TLA Line data Source code
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 :
2890 alvherre 20 GIC 21 : PG_MODULE_MAGIC;
21 :
2890 alvherre 22 CBC 21 : PG_FUNCTION_INFO_V1(get_command_type);
2890 alvherre 23 GIC 21 : PG_FUNCTION_INFO_V1(get_command_tag);
252 michael 24 GNC 4 : PG_FUNCTION_INFO_V1(get_altertable_subcmdinfo);
2890 alvherre 25 ECB :
2820 26 : /*
27 : * Return the textual representation of the struct type used to represent a
28 : * command in struct CollectedCommand format.
29 : */
30 : Datum
2890 alvherre 31 GIC 186 : get_command_type(PG_FUNCTION_ARGS)
32 : {
2890 alvherre 33 CBC 186 : CollectedCommand *cmd = (CollectedCommand *) PG_GETARG_POINTER(0);
34 : const char *type;
2890 alvherre 35 ECB :
2890 alvherre 36 GIC 186 : switch (cmd->type)
37 : {
2890 alvherre 38 CBC 133 : case SCT_Simple:
2890 alvherre 39 GIC 133 : type = "simple";
2890 alvherre 40 CBC 133 : break;
41 40 : case SCT_AlterTable:
42 40 : type = "alter table";
43 40 : break;
44 9 : case SCT_Grant:
45 9 : type = "grant";
46 9 : break;
47 1 : case SCT_AlterOpFamily:
48 1 : type = "alter operator family";
49 1 : break;
50 1 : case SCT_AlterDefaultPrivileges:
51 1 : type = "alter default privileges";
52 1 : break;
53 1 : case SCT_CreateOpClass:
54 1 : type = "create operator class";
55 1 : break;
56 1 : case SCT_AlterTSConfig:
57 1 : type = "alter text search configuration";
58 1 : break;
2890 alvherre 59 LBC 0 : default:
60 0 : type = "unknown command type";
2890 alvherre 61 UBC 0 : break;
2890 alvherre 62 EUB : }
63 :
2890 alvherre 64 GIC 186 : PG_RETURN_TEXT_P(cstring_to_text(type));
65 : }
2890 alvherre 66 ECB :
67 : /*
68 : * Return the command tag corresponding to a parse node contained in a
69 : * CollectedCommand struct.
70 : */
71 : Datum
2890 alvherre 72 GIC 186 : get_command_tag(PG_FUNCTION_ARGS)
73 : {
2890 alvherre 74 CBC 186 : CollectedCommand *cmd = (CollectedCommand *) PG_GETARG_POINTER(0);
75 :
76 186 : if (!cmd->parsetree)
2890 alvherre 77 GIC 9 : PG_RETURN_NULL();
2890 alvherre 78 ECB :
1133 alvherre 79 CBC 177 : PG_RETURN_TEXT_P(cstring_to_text(CreateCommandName(cmd->parsetree)));
80 : }
2890 alvherre 81 ECB :
82 : /*
83 : * Return a text array representation of the subcommands of an ALTER TABLE
84 : * command.
85 : */
86 : Datum
252 michael 87 GNC 40 : get_altertable_subcmdinfo(PG_FUNCTION_ARGS)
88 : {
2890 alvherre 89 CBC 40 : CollectedCommand *cmd = (CollectedCommand *) PG_GETARG_POINTER(0);
2878 bruce 90 ECB : ListCell *cell;
252 michael 91 GNC 40 : ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
92 :
2890 alvherre 93 CBC 40 : if (cmd->type != SCT_AlterTable)
2890 alvherre 94 UIC 0 : elog(ERROR, "command is not ALTER TABLE");
2890 alvherre 95 ECB :
173 michael 96 GNC 40 : InitMaterializedSRF(fcinfo, 0);
97 :
235 tgl 98 40 : if (cmd->d.alterTable.subcmds == NIL)
252 michael 99 UNC 0 : elog(ERROR, "empty alter table subcommand list");
100 :
2890 alvherre 101 GBC 102 : foreach(cell, cmd->d.alterTable.subcmds)
102 : {
2890 alvherre 103 CBC 62 : CollectedATSubcmd *sub = lfirst(cell);
2238 peter_e 104 GIC 62 : AlterTableCmd *subcmd = castNode(AlterTableCmd, sub->parsetree);
252 michael 105 GNC 62 : const char *strtype = "unrecognized";
106 : Datum values[2];
107 : bool nulls[2];
108 :
109 62 : memset(values, 0, sizeof(values));
110 62 : memset(nulls, 0, sizeof(nulls));
2890 alvherre 111 EUB :
2890 alvherre 112 GIC 62 : switch (subcmd->subtype)
2890 alvherre 113 ECB : {
2890 alvherre 114 GIC 1 : case AT_AddColumn:
2890 alvherre 115 CBC 1 : strtype = "ADD COLUMN";
116 1 : break;
2890 alvherre 117 UIC 0 : case AT_AddColumnToView:
2890 alvherre 118 LBC 0 : strtype = "ADD COLUMN TO VIEW";
119 0 : break;
2890 alvherre 120 GIC 3 : case AT_ColumnDefault:
2890 alvherre 121 CBC 3 : strtype = "ALTER COLUMN SET DEFAULT";
2890 alvherre 122 GIC 3 : break;
961 tgl 123 CBC 1 : case AT_CookedColumnDefault:
124 1 : strtype = "ALTER COLUMN SET DEFAULT (precooked)";
125 1 : break;
2890 alvherre 126 GBC 1 : case AT_DropNotNull:
127 1 : strtype = "DROP NOT NULL";
128 1 : break;
2890 alvherre 129 CBC 2 : case AT_SetNotNull:
130 2 : strtype = "SET NOT NULL";
131 2 : break;
2 alvherre 132 GNC 10 : case AT_SetAttNotNull:
133 10 : strtype = "SET ATTNOTNULL";
134 10 : break;
252 michael 135 1 : case AT_DropExpression:
136 1 : strtype = "DROP EXPRESSION";
137 1 : break;
1447 tgl 138 LBC 0 : case AT_CheckNotNull:
139 0 : strtype = "CHECK NOT NULL";
140 0 : break;
2890 alvherre 141 CBC 3 : case AT_SetStatistics:
142 3 : strtype = "SET STATS";
143 3 : break;
2890 alvherre 144 LBC 0 : case AT_SetOptions:
145 0 : strtype = "SET OPTIONS";
146 0 : break;
147 0 : case AT_ResetOptions:
148 0 : strtype = "RESET OPTIONS";
149 0 : break;
2890 alvherre 150 CBC 3 : case AT_SetStorage:
151 3 : strtype = "SET STORAGE";
152 3 : break;
252 michael 153 GNC 1 : case AT_SetCompression:
154 1 : strtype = "SET COMPRESSION";
155 1 : break;
2890 alvherre 156 UBC 0 : case AT_DropColumn:
157 0 : strtype = "DROP COLUMN";
158 0 : break;
2890 alvherre 159 GBC 1 : case AT_AddIndex:
160 1 : strtype = "ADD INDEX";
161 1 : break;
2890 alvherre 162 UBC 0 : case AT_ReAddIndex:
163 0 : strtype = "(re) ADD INDEX";
164 0 : break;
2890 alvherre 165 CBC 13 : case AT_AddConstraint:
166 13 : strtype = "ADD CONSTRAINT";
167 13 : break;
2890 alvherre 168 GBC 1 : case AT_ReAddConstraint:
169 1 : strtype = "(re) ADD CONSTRAINT";
170 1 : break;
252 michael 171 GNC 1 : case AT_ReAddDomainConstraint:
172 1 : strtype = "(re) ADD DOMAIN CONSTRAINT";
173 1 : break;
2890 alvherre 174 LBC 0 : case AT_AlterConstraint:
175 0 : strtype = "ALTER CONSTRAINT";
176 0 : break;
2890 alvherre 177 UBC 0 : case AT_ValidateConstraint:
178 0 : strtype = "VALIDATE CONSTRAINT";
179 0 : break;
2890 alvherre 180 LBC 0 : case AT_AddIndexConstraint:
181 0 : strtype = "ADD CONSTRAINT (using index)";
182 0 : break;
183 0 : case AT_DropConstraint:
184 0 : strtype = "DROP CONSTRAINT";
185 0 : break;
2820 heikki.linnakangas 186 UBC 0 : case AT_ReAddComment:
187 0 : strtype = "(re) ADD COMMENT";
188 0 : break;
2890 alvherre 189 GBC 4 : case AT_AlterColumnType:
190 4 : strtype = "ALTER COLUMN SET TYPE";
191 4 : break;
2890 alvherre 192 UBC 0 : case AT_AlterColumnGenericOptions:
193 0 : strtype = "ALTER COLUMN SET OPTIONS";
194 0 : break;
195 0 : case AT_ChangeOwner:
196 0 : strtype = "CHANGE OWNER";
197 0 : break;
2890 alvherre 198 CBC 1 : case AT_ClusterOn:
199 1 : strtype = "CLUSTER";
200 1 : break;
2890 alvherre 201 UBC 0 : case AT_DropCluster:
202 0 : strtype = "DROP CLUSTER";
203 0 : break;
2890 alvherre 204 GBC 1 : case AT_SetLogged:
205 1 : strtype = "SET LOGGED";
206 1 : break;
2890 alvherre 207 CBC 1 : case AT_SetUnLogged:
208 1 : strtype = "SET UNLOGGED";
209 1 : break;
2890 alvherre 210 UBC 0 : case AT_DropOids:
211 0 : strtype = "DROP OIDS";
212 0 : break;
252 michael 213 UNC 0 : case AT_SetAccessMethod:
214 0 : strtype = "SET ACCESS METHOD";
215 0 : break;
2890 alvherre 216 LBC 0 : case AT_SetTableSpace:
217 0 : strtype = "SET TABLESPACE";
218 0 : break;
2890 alvherre 219 CBC 1 : case AT_SetRelOptions:
220 1 : strtype = "SET RELOPTIONS";
221 1 : break;
2890 alvherre 222 GBC 1 : case AT_ResetRelOptions:
223 1 : strtype = "RESET RELOPTIONS";
224 1 : break;
225 1 : case AT_ReplaceRelOptions:
226 1 : strtype = "REPLACE RELOPTIONS";
227 1 : break;
2890 alvherre 228 UBC 0 : case AT_EnableTrig:
229 0 : strtype = "ENABLE TRIGGER";
230 0 : break;
2890 alvherre 231 LBC 0 : case AT_EnableAlwaysTrig:
232 0 : strtype = "ENABLE TRIGGER (always)";
233 0 : break;
234 0 : case AT_EnableReplicaTrig:
235 0 : strtype = "ENABLE TRIGGER (replica)";
236 0 : break;
237 0 : case AT_DisableTrig:
238 0 : strtype = "DISABLE TRIGGER";
239 0 : break;
2890 alvherre 240 UBC 0 : case AT_EnableTrigAll:
241 0 : strtype = "ENABLE TRIGGER (all)";
242 0 : break;
243 0 : case AT_DisableTrigAll:
244 0 : strtype = "DISABLE TRIGGER (all)";
245 0 : break;
246 0 : case AT_EnableTrigUser:
247 0 : strtype = "ENABLE TRIGGER (user)";
248 0 : break;
249 0 : case AT_DisableTrigUser:
250 0 : strtype = "DISABLE TRIGGER (user)";
251 0 : break;
252 0 : case AT_EnableRule:
253 0 : strtype = "ENABLE RULE";
254 0 : break;
255 0 : case AT_EnableAlwaysRule:
256 0 : strtype = "ENABLE RULE (always)";
257 0 : break;
258 0 : case AT_EnableReplicaRule:
259 0 : strtype = "ENABLE RULE (replica)";
260 0 : break;
261 0 : case AT_DisableRule:
262 0 : strtype = "DISABLE RULE";
263 0 : break;
264 0 : case AT_AddInherit:
265 0 : strtype = "ADD INHERIT";
266 0 : break;
267 0 : case AT_DropInherit:
268 0 : strtype = "DROP INHERIT";
269 0 : break;
270 0 : case AT_AddOf:
271 0 : strtype = "OF";
272 0 : break;
273 0 : case AT_DropOf:
274 0 : strtype = "NOT OF";
275 0 : break;
276 0 : case AT_ReplicaIdentity:
277 0 : strtype = "REPLICA IDENTITY";
278 0 : break;
2890 alvherre 279 GBC 1 : case AT_EnableRowSecurity:
280 1 : strtype = "ENABLE ROW SECURITY";
281 1 : break;
282 1 : case AT_DisableRowSecurity:
283 1 : strtype = "DISABLE ROW SECURITY";
284 1 : break;
2744 sfrost 285 1 : case AT_ForceRowSecurity:
286 1 : strtype = "FORCE ROW SECURITY";
287 1 : break;
288 1 : case AT_NoForceRowSecurity:
289 1 : strtype = "NO FORCE ROW SECURITY";
290 1 : break;
2890 alvherre 291 LBC 0 : case AT_GenericOptions:
292 0 : strtype = "SET OPTIONS";
293 0 : break;
252 michael 294 GNC 1 : case AT_DetachPartition:
295 1 : strtype = "DETACH PARTITION";
296 1 : break;
297 1 : case AT_AttachPartition:
298 1 : strtype = "ATTACH PARTITION";
299 1 : break;
252 michael 300 UNC 0 : case AT_DetachPartitionFinalize:
301 0 : strtype = "DETACH PARTITION ... FINALIZE";
302 0 : break;
252 michael 303 GNC 1 : case AT_AddIdentity:
304 1 : strtype = "ADD IDENTITY";
305 1 : break;
306 1 : case AT_SetIdentity:
307 1 : strtype = "SET IDENTITY";
308 1 : break;
309 1 : case AT_DropIdentity:
310 1 : strtype = "DROP IDENTITY";
311 1 : break;
312 1 : case AT_ReAddStatistics:
313 1 : strtype = "(re) ADD STATS";
2820 alvherre 314 CBC 1 : break;
2890 alvherre 315 ECB : }
316 :
118 alvherre 317 GNC 62 : if (subcmd->recurse)
318 16 : values[0] = CStringGetTextDatum(psprintf("%s (and recurse)", strtype));
319 : else
320 46 : values[0] = CStringGetTextDatum(strtype);
252 michael 321 62 : if (OidIsValid(sub->address.objectId))
322 : {
323 : char *objdesc;
324 :
325 43 : objdesc = getObjectDescription((const ObjectAddress *) &sub->address, false);
326 43 : values[1] = CStringGetTextDatum(objdesc);
327 : }
328 : else
329 19 : nulls[1] = true;
330 :
331 62 : tuplestore_putvalues(rsinfo->setResult, rsinfo->setDesc, values, nulls);
252 michael 332 ECB : }
2890 alvherre 333 EUB :
252 michael 334 GNC 40 : return (Datum) 0;
2890 alvherre 335 ECB : }
|