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
191 lines (160 loc) · 5.15 KB
/
Column.java
File metadata and controls
191 lines (160 loc) · 5.15 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
/*-
* #%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.parser.ASTNodeAccessImpl;
/**
* 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 = ".";
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(null, 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 void setTable(Table table) {
this.table = table;
}
public String getColumnName() {
return columnName;
}
public void setColumnName(String string) {
columnName = string;
}
public String getTableDelimiter() {
return tableDelimiter;
}
public void setTableDelimiter(String tableDelimiter) {
this.tableDelimiter = tableDelimiter;
}
@Override
public String getFullyQualifiedName() {
return getFullyQualifiedName(false);
}
public String getFullyQualifiedName(boolean aliases) {
StringBuilder fqn = new StringBuilder();
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 void accept(ExpressionVisitor expressionVisitor) {
expressionVisitor.visit(this);
}
@Override
public String toString() {
return getFullyQualifiedName(true);
}
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 void setCommentText(String commentText) {
this.commentText = commentText;
}
public String getCommentText() {
return commentText;
}
}