-
Notifications
You must be signed in to change notification settings - Fork 24
/
ddl_deparse.c
345 lines (329 loc) · 7.99 KB
/
ddl_deparse.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/*----------------------------------------------------------------------
* ddl_deparse.c
* Taken directly from Postgres test_ddl_deparse test module.
*
*----------------------------------------------------------------------
*/
#include "postgres.h"
#include "catalog/pg_type.h"
#include "tcop/deparse_utility.h"
#include "tcop/utility.h"
#include "utils/builtins.h"
PG_MODULE_MAGIC;
PG_FUNCTION_INFO_V1(get_command_type);
PG_FUNCTION_INFO_V1(get_command_tag);
PG_FUNCTION_INFO_V1(get_altertable_subcmdinfo);
/*
* Return the textual representation of the struct type used to represent a
* command in struct CollectedCommand format.
*/
Datum
get_command_type(PG_FUNCTION_ARGS)
{
CollectedCommand *cmd = (CollectedCommand *) PG_GETARG_POINTER(0);
const char *type;
switch (cmd->type)
{
case SCT_Simple:
type = "simple";
break;
case SCT_AlterTable:
type = "alter table";
break;
case SCT_Grant:
type = "grant";
break;
case SCT_AlterOpFamily:
type = "alter operator family";
break;
case SCT_AlterDefaultPrivileges:
type = "alter default privileges";
break;
case SCT_CreateOpClass:
type = "create operator class";
break;
case SCT_AlterTSConfig:
type = "alter text search configuration";
break;
default:
type = "unknown command type";
break;
}
PG_RETURN_TEXT_P(cstring_to_text(type));
}
/*
* Return the command tag corresponding to a parse node contained in a
* CollectedCommand struct.
*/
Datum
get_command_tag(PG_FUNCTION_ARGS)
{
CollectedCommand *cmd = (CollectedCommand *) PG_GETARG_POINTER(0);
if (!cmd->parsetree)
PG_RETURN_NULL();
#if PG_VERSION_NUM >= 130000
PG_RETURN_TEXT_P(cstring_to_text(CreateCommandName(cmd->parsetree)));
#else
PG_RETURN_TEXT_P(cstring_to_text(CreateCommandTag(cmd->parsetree)));
#endif
}
/*
* Return a text array representation of the subcommands of an ALTER TABLE
* command.
*/
Datum
get_altertable_subcmdinfo(PG_FUNCTION_ARGS)
{
CollectedCommand *cmd = (CollectedCommand *) PG_GETARG_POINTER(0);
ArrayBuildState *astate = NULL;
ListCell *cell;
if (cmd->type != SCT_AlterTable)
elog(ERROR, "command is not ALTER TABLE");
foreach(cell, cmd->d.alterTable.subcmds)
{
CollectedATSubcmd *sub = lfirst(cell);
AlterTableCmd *subcmd = castNode(AlterTableCmd, sub->parsetree);
const char *strtype;
switch (subcmd->subtype)
{
#if PG_VERSION_NUM < 120000
case AT_AddOids:
strtype = "ADD OIDS";
break;
case AT_AddOidsRecurse:
strtype = "ADD OIDS (and recurse)";
break;
#endif
#if PG_VERSION_NUM >= 120000
case AT_CheckNotNull:
strtype = "CHECK NOT NULL";
break;
#endif
#if PG_VERSION_NUM >= 130000
case AT_CookedColumnDefault:
strtype = "ALTER COLUMN SET DEFAULT (precooked)";
break;
#endif
#if PG_VERSION_NUM < 160000
case AT_AddColumnRecurse:
strtype = "ADD COLUMN (and recurse)";
break;
case AT_DropColumnRecurse:
strtype = "DROP COLUMN (and recurse)";
break;
case AT_AddConstraintRecurse:
strtype = "ADD CONSTRAINT (and recurse)";
break;
case AT_ValidateConstraintRecurse:
strtype = "VALIDATE CONSTRAINT (and recurse)";
break;
case AT_DropConstraintRecurse:
strtype = "DROP CONSTRAINT (and recurse)";
break;
#endif
#if PG_VERSION_NUM >= 160000
case AT_DropExpression:
strtype = "DROP EXPRESSION";
break;
case AT_SetCompression:
strtype = "SET COMPRESSION";
break;
case AT_ReAddDomainConstraint:
strtype = "(re) ADD DOMAIN CONSTRAINT";
break;
case AT_SetAccessMethod:
strtype = "SET ACCESS METHOD";
break;
case AT_DetachPartition:
strtype = "DETACH PARTITION";
break;
case AT_AttachPartition:
strtype = "ATTACH PARTITION";
break;
case AT_DetachPartitionFinalize:
strtype = "DETACH PARTITION ... FINALIZE";
break;
case AT_AddIdentity:
strtype = "ADD IDENTITY";
break;
case AT_SetIdentity:
strtype = "SET IDENTITY";
break;
case AT_DropIdentity:
strtype = "DROP IDENTITY";
break;
case AT_ReAddStatistics:
strtype = "(re) ADD STATS";
break;
#endif
case AT_AddColumn:
strtype = "ADD COLUMN";
break;
case AT_AddColumnToView:
strtype = "ADD COLUMN TO VIEW";
break;
case AT_ColumnDefault:
strtype = "ALTER COLUMN SET DEFAULT";
break;
case AT_DropNotNull:
strtype = "DROP NOT NULL";
break;
case AT_SetNotNull:
strtype = "SET NOT NULL";
break;
case AT_SetStatistics:
strtype = "SET STATS";
break;
case AT_SetOptions:
strtype = "SET OPTIONS";
break;
case AT_ResetOptions:
strtype = "RESET OPTIONS";
break;
case AT_SetStorage:
strtype = "SET STORAGE";
break;
case AT_DropColumn:
strtype = "DROP COLUMN";
break;
case AT_AddIndex:
strtype = "ADD INDEX";
break;
case AT_ReAddIndex:
strtype = "(re) ADD INDEX";
break;
case AT_AddConstraint:
strtype = "ADD CONSTRAINT";
break;
case AT_ReAddConstraint:
strtype = "(re) ADD CONSTRAINT";
break;
case AT_AlterConstraint:
strtype = "ALTER CONSTRAINT";
break;
case AT_ValidateConstraint:
strtype = "VALIDATE CONSTRAINT";
break;
case AT_AddIndexConstraint:
strtype = "ADD CONSTRAINT (using index)";
break;
case AT_DropConstraint:
strtype = "DROP CONSTRAINT";
break;
case AT_ReAddComment:
strtype = "(re) ADD COMMENT";
break;
case AT_AlterColumnType:
strtype = "ALTER COLUMN SET TYPE";
break;
case AT_AlterColumnGenericOptions:
strtype = "ALTER COLUMN SET OPTIONS";
break;
case AT_ChangeOwner:
strtype = "CHANGE OWNER";
break;
case AT_ClusterOn:
strtype = "CLUSTER";
break;
case AT_DropCluster:
strtype = "DROP CLUSTER";
break;
case AT_SetLogged:
strtype = "SET LOGGED";
break;
case AT_SetUnLogged:
strtype = "SET UNLOGGED";
break;
case AT_DropOids:
strtype = "DROP OIDS";
break;
case AT_SetTableSpace:
strtype = "SET TABLESPACE";
break;
case AT_SetRelOptions:
strtype = "SET RELOPTIONS";
break;
case AT_ResetRelOptions:
strtype = "RESET RELOPTIONS";
break;
case AT_ReplaceRelOptions:
strtype = "REPLACE RELOPTIONS";
break;
case AT_EnableTrig:
strtype = "ENABLE TRIGGER";
break;
case AT_EnableAlwaysTrig:
strtype = "ENABLE TRIGGER (always)";
break;
case AT_EnableReplicaTrig:
strtype = "ENABLE TRIGGER (replica)";
break;
case AT_DisableTrig:
strtype = "DISABLE TRIGGER";
break;
case AT_EnableTrigAll:
strtype = "ENABLE TRIGGER (all)";
break;
case AT_DisableTrigAll:
strtype = "DISABLE TRIGGER (all)";
break;
case AT_EnableTrigUser:
strtype = "ENABLE TRIGGER (user)";
break;
case AT_DisableTrigUser:
strtype = "DISABLE TRIGGER (user)";
break;
case AT_EnableRule:
strtype = "ENABLE RULE";
break;
case AT_EnableAlwaysRule:
strtype = "ENABLE RULE (always)";
break;
case AT_EnableReplicaRule:
strtype = "ENABLE RULE (replica)";
break;
case AT_DisableRule:
strtype = "DISABLE RULE";
break;
case AT_AddInherit:
strtype = "ADD INHERIT";
break;
case AT_DropInherit:
strtype = "DROP INHERIT";
break;
case AT_AddOf:
strtype = "OF";
break;
case AT_DropOf:
strtype = "NOT OF";
break;
case AT_ReplicaIdentity:
strtype = "REPLICA IDENTITY";
break;
case AT_EnableRowSecurity:
strtype = "ENABLE ROW SECURITY";
break;
case AT_DisableRowSecurity:
strtype = "DISABLE ROW SECURITY";
break;
case AT_ForceRowSecurity:
strtype = "FORCE ROW SECURITY";
break;
case AT_NoForceRowSecurity:
strtype = "NO FORCE ROW SECURITY";
break;
case AT_GenericOptions:
strtype = "SET OPTIONS";
break;
default:
strtype = "unrecognized";
break;
}
astate =
accumArrayResult(astate, CStringGetTextDatum(strtype),
false, TEXTOID, CurrentMemoryContext);
}
if (astate == NULL)
elog(ERROR, "empty alter table subcommand list");
PG_RETURN_ARRAYTYPE_P(DatumGetPointer(makeArrayResult(astate, CurrentMemoryContext)));
}