forked from JSQLParser/JSqlParser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathColumn.java
More file actions
345 lines (293 loc) · 10.7 KB
/
Column.java
File metadata and controls
345 lines (293 loc) · 10.7 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
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
/*-
* #%L
* JSQLParser library
* %%
* Copyright (C) 2004 - 2019 JSQLParser
* %%
* Dual licensed under GNU LGPL 2.1 or Apache License 2.0
* #L%
*/
package net.sf.jsqlparser.schema;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import net.sf.jsqlparser.expression.ArrayConstructor;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.ExpressionVisitor;
import net.sf.jsqlparser.expression.operators.relational.SupportsOldOracleJoinSyntax;
import net.sf.jsqlparser.parser.ASTNodeAccessImpl;
import net.sf.jsqlparser.statement.ReturningReferenceType;
/**
* A column. It can have the table name it belongs to.
*/
public class Column extends ASTNodeAccessImpl implements Expression, MultiPartName {
private Table table;
private String columnName;
private String commentText;
private ArrayConstructor arrayConstructor;
private String tableDelimiter = ".";
private int oldOracleJoinSyntax = SupportsOldOracleJoinSyntax.NO_ORACLE_JOIN;
private ReturningReferenceType returningReferenceType = null;
private String returningQualifier = null;
// holds the physical table when resolved against an actual schema information
private Table resolvedTable = null;
public Column() {}
public Column(Table table, String columnName) {
setTable(table);
setColumnName(columnName);
}
public Column(List<String> nameParts) {
this(nameParts, nameParts.size() > 1 ? Collections.nCopies(nameParts.size() - 1, ".")
: new ArrayList<>());
}
public Column(List<String> nameParts, List<String> delimiters) {
this(
nameParts.size() > 1 ? new Table(nameParts.subList(0, nameParts.size() - 1),
delimiters.subList(0, delimiters.size() - 1)) : null,
nameParts.get(nameParts.size() - 1));
setTableDelimiter(delimiters.isEmpty() ? "." : delimiters.get(delimiters.size() - 1));
}
public Column(String columnName) {
this();
setColumnName(columnName);
}
public ArrayConstructor getArrayConstructor() {
return arrayConstructor;
}
public Column setArrayConstructor(ArrayConstructor arrayConstructor) {
this.arrayConstructor = arrayConstructor;
return this;
}
/**
* Retrieve the information regarding the {@code Table} this {@code Column} does belong to, if
* any can be inferred.
* <p>
* The inference is based only on local information, and not on the whole SQL command. For
* example, consider the following query: <blockquote>
*
* <pre>
* SELECT x FROM Foo
* </pre>
*
* </blockquote> Given the {@code Column} called {@code x}, this method would return
* {@code null}, and not the info about the table {@code Foo}. On the other hand, consider:
* <blockquote>
*
* <pre>
* SELECT t.x FROM Foo t
* </pre>
*
* </blockquote> Here, we will get a {@code Table} object for a table called {@code t}. But
* because the inference is local, such object will not know that {@code t} is just an alias for
* {@code Foo}.
*
* @return an instance of {@link net.sf.jsqlparser.schema.Table} representing the table this
* column does belong to, if it can be inferred. Can be {@code null}.
*/
public Table getTable() {
return table;
}
public String getTableName() {
return table != null ? table.getName() : null;
}
public String getUnquotedTableName() {
return table != null ? table.getUnquotedName() : null;
}
public String getSchemaName() {
return table != null ? table.getSchemaName() : null;
}
public String getUnquotedSchemaName() {
return table != null ? table.getUnquotedSchemaName() : null;
}
public String getCatalogName() {
return table != null ? table.getCatalogName() : null;
}
public String getUnquotedCatalogName() {
return table != null ? table.getUnquotedCatalogName() : null;
}
public void setTable(Table table) {
this.table = table;
}
public String getColumnName() {
return columnName;
}
public String getUnquotedColumnName() {
return MultiPartName.unquote(columnName);
}
public void setColumnName(String name) {
// BigQuery seems to allow things like: `catalogName.schemaName.tableName` in only one pair
// of quotes
// however, some people believe that Dots in Names are a good idea, so provide a switch-off
boolean splitNamesOnDelimiter = System.getProperty("SPLIT_NAMES_ON_DELIMITER") == null ||
!List
.of("0", "N", "n", "FALSE", "false", "OFF", "off")
.contains(System.getProperty("SPLIT_NAMES_ON_DELIMITER"));
setName(name, splitNamesOnDelimiter);
}
public void setName(String name, boolean splitNamesOnDelimiter) {
if (MultiPartName.isQuoted(name) && name.contains(".") && splitNamesOnDelimiter) {
String[] parts = MultiPartName.unquote(name).split("\\.");
switch (parts.length) {
case 3:
this.table = new Table("\"" + parts[0] + "\".\"" + parts[1] + "\"");
this.columnName = "\"" + parts[2] + "\"";
break;
case 2:
this.table = new Table("\"" + parts[0] + "\"");
this.columnName = "\"" + parts[1] + "\"";
break;
case 1:
this.columnName = "\"" + parts[0] + "\"";
break;
default:
throw new RuntimeException("Invalid column name: " + name);
}
} else if (name.contains(".") && splitNamesOnDelimiter) {
String[] parts = MultiPartName.unquote(name).split("\\.");
switch (parts.length) {
case 3:
this.table = new Table(parts[0] + "." + parts[1]);
this.columnName = parts[2];
break;
case 2:
this.table = new Table(parts[0]);
this.columnName = parts[1];
break;
case 1:
this.columnName = parts[0];
break;
default:
throw new RuntimeException("Invalid column name: " + name);
}
} else {
this.columnName = name;
}
}
public String getTableDelimiter() {
return tableDelimiter;
}
public void setTableDelimiter(String tableDelimiter) {
this.tableDelimiter = tableDelimiter;
}
public int getOldOracleJoinSyntax() {
return oldOracleJoinSyntax;
}
public void setOldOracleJoinSyntax(int oldOracleJoinSyntax) {
this.oldOracleJoinSyntax = oldOracleJoinSyntax;
}
@Override
public String getFullyQualifiedName() {
return getFullyQualifiedName(false);
}
@Override
public String getUnquotedName() {
return MultiPartName.unquote(columnName);
}
public String getFullyQualifiedName(boolean aliases) {
StringBuilder fqn = new StringBuilder();
if (returningQualifier != null) {
fqn.append(returningQualifier);
} else if (table != null) {
if (table.getAlias() != null && aliases) {
fqn.append(table.getAlias().getName());
} else {
fqn.append(table.getFullyQualifiedName());
}
}
if (fqn.length() > 0) {
fqn.append(tableDelimiter);
}
if (columnName != null) {
fqn.append(columnName);
}
if (commentText != null) {
fqn.append(" COMMENT ");
fqn.append(commentText);
}
if (arrayConstructor != null) {
fqn.append(arrayConstructor);
}
return fqn.toString();
}
// old and confusing, don't use it!
@Deprecated
public String getName(boolean aliases) {
return columnName;
}
@Override
public <T, S> T accept(ExpressionVisitor<T> expressionVisitor, S context) {
return expressionVisitor.visit(this, context);
}
@Override
public String toString() {
return getFullyQualifiedName(true)
+ (oldOracleJoinSyntax != SupportsOldOracleJoinSyntax.NO_ORACLE_JOIN ? "(+)" : "")
+ (commentText != null ? " /* " + commentText + "*/ " : "");
}
public Column withTable(Table table) {
this.setTable(table);
return this;
}
public Column withColumnName(String columnName) {
this.setColumnName(columnName);
return this;
}
public Column withCommentText(String commentText) {
this.setCommentText(commentText);
return this;
}
public Column withTableDelimiter(String delimiter) {
this.setTableDelimiter(delimiter);
return this;
}
public Column withOldOracleJoinSyntax(int oldOracleJoinSyntax) {
this.setOldOracleJoinSyntax(oldOracleJoinSyntax);
return this;
}
public ReturningReferenceType getReturningReferenceType() {
return returningReferenceType;
}
public Column setReturningReferenceType(ReturningReferenceType returningReferenceType) {
this.returningReferenceType = returningReferenceType;
return this;
}
public String getReturningQualifier() {
return returningQualifier;
}
public Column setReturningQualifier(String returningQualifier) {
this.returningQualifier = returningQualifier;
return this;
}
public Column withReturningReference(ReturningReferenceType returningReferenceType,
String returningQualifier) {
this.returningReferenceType = returningReferenceType;
this.returningQualifier = returningQualifier;
return this;
}
public String getCommentText() {
return commentText;
}
public void setCommentText(String commentText) {
this.commentText = commentText;
}
/**
* Gets the actual table when resolved against a physical schema information.
*
* @return the actual table when resolved against a physical schema information
*/
public Table getResolvedTable() {
return resolvedTable;
}
/**
* Sets resolved table.
*
* @param resolvedTable the resolved table
* @return this column
*/
public Column setResolvedTable(Table resolvedTable) {
// clone, not reference
this.resolvedTable =
resolvedTable != null ? new Table(resolvedTable.getFullyQualifiedName()) : null;
return this;
}
}