Repository navigation
Expand file tree
/
Copy pathCommentTarget.java
More file actions
276 lines (230 loc) · 8.39 KB
/
Copy pathCommentTarget.java
File metadata and controls
276 lines (230 loc) · 8.39 KB
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
/*-
* #%L
* JSQLParser library
* %%
* Copyright (C) 2004 - 2026 JSQLParser
* %%
* Dual licensed under GNU LGPL 2.1 or Apache License 2.0
* #L%
*/
package net.sf.jsqlparser.statement.comment;
import java.io.Serializable;
import java.util.function.Consumer;
import net.sf.jsqlparser.schema.Table;
import net.sf.jsqlparser.statement.RoutineReference;
import net.sf.jsqlparser.statement.create.table.ColDataType;
import net.sf.jsqlparser.statement.create.table.ExclusionOperator;
/** A catalog object addressed by COMMENT, rather than a function invocation or a query. */
public class CommentTarget implements Serializable {
public enum Kind {
INDEX, SCHEMA, SEQUENCE, DOMAIN, TYPE, MATERIALIZED_VIEW, FUNCTION, CONSTRAINT,
TRIGGER, POLICY, RULE, PROCEDURE, ROUTINE, AGGREGATE, CAST,
COLLATION, FOREIGN_TABLE, STATISTICS, DATABASE, ROLE, TABLESPACE, ACCESS_METHOD, EXTENSION,
FOREIGN_DATA_WRAPPER, LANGUAGE, SERVER, EVENT_TRIGGER, PUBLICATION, SUBSCRIPTION,
OPERATOR, OPERATOR_CLASS, OPERATOR_FAMILY, LARGE_OBJECT, CONVERSION, TRANSFORM,
TEXT_SEARCH_CONFIGURATION, TEXT_SEARCH_DICTIONARY, TEXT_SEARCH_PARSER, TEXT_SEARCH_TEMPLATE;
/**
* Whether the target is a routine signature, exposed by {@link CommentTarget#getRoutine()}.
*/
public boolean isRoutine() {
return this == FUNCTION || this == PROCEDURE || this == ROUTINE || this == AGGREGATE;
}
/**
* Whether the target is named within the table or domain of
* {@link CommentTarget#getRelation()}.
*/
public boolean isOwnedByRelation() {
return this == CONSTRAINT || this == TRIGGER || this == POLICY || this == RULE;
}
/**
* Whether the target is named within the index access method of
* {@link CommentTarget#getAccessMethod()}.
*/
public boolean isOwnedByAccessMethod() {
return this == OPERATOR_CLASS || this == OPERATOR_FAMILY;
}
}
private Kind kind;
private Table name;
private RoutineReference routine;
private Table relation;
private boolean onDomain;
private boolean procedural;
private ColDataType sourceType;
private ColDataType targetType;
private ExclusionOperator operator;
private ColDataType leftOperandType;
private ColDataType rightOperandType;
private String accessMethod;
private Long largeObjectOid;
private ColDataType transformType;
private String language;
public Kind getKind() {
return kind;
}
public void setKind(Kind kind) {
this.kind = kind;
}
/**
* The object's identifier; using Table preserves the individual name components. Routine, cast,
* operator, large object and transform targets have no name.
*/
public Table getName() {
return name;
}
public void setName(Table name) {
this.name = name;
}
public RoutineReference getRoutine() {
return routine;
}
public void setRoutine(RoutineReference routine) {
this.routine = routine;
}
/**
* The table owning a constraint, trigger, policy or rule, or the domain owning a constraint,
* distinguished by {@link #isOnDomain()}.
*/
public Table getRelation() {
return relation;
}
public void setRelation(Table relation) {
this.relation = relation;
}
public boolean isOnDomain() {
return onDomain;
}
public void setOnDomain(boolean onDomain) {
this.onDomain = onDomain;
}
/** Whether a {@link Kind#LANGUAGE} target was written as PROCEDURAL LANGUAGE. */
public boolean isProcedural() {
return procedural;
}
public void setProcedural(boolean procedural) {
this.procedural = procedural;
}
/** The source type of a {@link Kind#CAST} target. */
public ColDataType getSourceType() {
return sourceType;
}
public void setSourceType(ColDataType sourceType) {
this.sourceType = sourceType;
}
/** The target type of a {@link Kind#CAST} target. */
public ColDataType getTargetType() {
return targetType;
}
public void setTargetType(ColDataType targetType) {
this.targetType = targetType;
}
/**
* The possibly schema-qualified operator of an {@link Kind#OPERATOR} target, read by the
* operator reference shared with exclusion constraints and ALTER EXTENSION.
*/
public ExclusionOperator getOperator() {
return operator;
}
public void setOperator(ExclusionOperator operator) {
this.operator = operator;
}
/** The left operand type of an {@link Kind#OPERATOR} target; null stands for NONE. */
public ColDataType getLeftOperandType() {
return leftOperandType;
}
public void setLeftOperandType(ColDataType leftOperandType) {
this.leftOperandType = leftOperandType;
}
/** The right operand type of an {@link Kind#OPERATOR} target; null stands for NONE. */
public ColDataType getRightOperandType() {
return rightOperandType;
}
public void setRightOperandType(ColDataType rightOperandType) {
this.rightOperandType = rightOperandType;
}
/**
* The index access method of an {@link Kind#OPERATOR_CLASS} or {@link Kind#OPERATOR_FAMILY}
* target.
*/
public String getAccessMethod() {
return accessMethod;
}
public void setAccessMethod(String accessMethod) {
this.accessMethod = accessMethod;
}
/** The OID of a {@link Kind#LARGE_OBJECT} target. */
public Long getLargeObjectOid() {
return largeObjectOid;
}
public void setLargeObjectOid(Long largeObjectOid) {
this.largeObjectOid = largeObjectOid;
}
/** The data type of a {@link Kind#TRANSFORM} target. */
public ColDataType getTransformType() {
return transformType;
}
public void setTransformType(ColDataType transformType) {
this.transformType = transformType;
}
/** The procedural language of a {@link Kind#TRANSFORM} target. */
public String getLanguage() {
return language;
}
public void setLanguage(String language) {
this.language = language;
}
/**
* Returns only an explicitly named table, foreign table or view, never an index, type,
* function, statistics object or domain.
*/
public Table getReferencedRelation() {
if (namesRelation()) {
return name;
}
return kind != null && kind.isOwnedByRelation() && !onDomain ? relation : null;
}
private boolean namesRelation() {
return kind == Kind.MATERIALIZED_VIEW || kind == Kind.FOREIGN_TABLE;
}
public StringBuilder appendTo(StringBuilder builder, Consumer<Table> relationWriter) {
if (kind == Kind.LANGUAGE && procedural) {
builder.append("PROCEDURAL ");
}
builder.append(kind.name().replace('_', ' ')).append(' ');
if (kind.isRoutine()) {
builder.append(routine);
} else if (kind == Kind.CAST) {
builder.append('(').append(sourceType).append(" AS ").append(targetType).append(')');
} else if (kind == Kind.OPERATOR) {
builder.append(operator).append(" (").append(operandType(leftOperandType))
.append(", ").append(operandType(rightOperandType)).append(')');
} else if (kind == Kind.LARGE_OBJECT) {
builder.append(largeObjectOid);
} else if (kind == Kind.TRANSFORM) {
builder.append("FOR ").append(transformType).append(" LANGUAGE ").append(language);
} else if (namesRelation()) {
relationWriter.accept(name);
} else {
builder.append(name);
if (kind.isOwnedByRelation()) {
builder.append(" ON ");
if (onDomain) {
builder.append("DOMAIN ").append(relation);
} else {
relationWriter.accept(relation);
}
} else if (kind.isOwnedByAccessMethod()) {
builder.append(" USING ").append(accessMethod);
}
}
return builder;
}
private static Object operandType(ColDataType type) {
return type == null ? "NONE" : type;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
return appendTo(builder, builder::append).toString();
}
}