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