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