Skip to content

Commit 72f6d86

Browse files
committed
Fix
1 parent 2c88a41 commit 72f6d86

File tree

6 files changed

+50
-65
lines changed

6 files changed

+50
-65
lines changed

src/main/java/net/sf/jsqlparser/schema/Column.java

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,20 @@
99
*/
1010
package net.sf.jsqlparser.schema;
1111

12+
import java.util.List;
1213
import net.sf.jsqlparser.expression.ArrayConstructor;
1314
import net.sf.jsqlparser.expression.Expression;
1415
import net.sf.jsqlparser.expression.ExpressionVisitor;
1516
import net.sf.jsqlparser.parser.ASTNodeAccessImpl;
1617

17-
import java.util.List;
18-
1918
/**
2019
* A column. It can have the table name it belongs to.
2120
*/
2221
public class Column extends ASTNodeAccessImpl implements Expression, MultiPartName {
2322

2423
private Table table;
2524
private String columnName;
25+
private String commentText;
2626
private ArrayConstructor arrayConstructor;
2727

2828
public Column() {}
@@ -56,19 +56,19 @@ public Column setArrayConstructor(ArrayConstructor arrayConstructor) {
5656
* <p>
5757
* The inference is based only on local information, and not on the whole SQL command. For
5858
* example, consider the following query: <blockquote>
59-
*
59+
*
6060
* <pre>
6161
* SELECT x FROM Foo
6262
* </pre>
63-
*
63+
*
6464
* </blockquote> Given the {@code Column} called {@code x}, this method would return
6565
* {@code null}, and not the info about the table {@code Foo}. On the other hand, consider:
6666
* <blockquote>
67-
*
67+
*
6868
* <pre>
6969
* SELECT t.x FROM Foo t
7070
* </pre>
71-
*
71+
*
7272
* </blockquote> Here, we will get a {@code Table} object for a table called {@code t}. But
7373
* because the inference is local, such object will not know that {@code t} is just an alias for
7474
* {@code Foo}.
@@ -114,6 +114,11 @@ public String getFullyQualifiedName(boolean aliases) {
114114
fqn.append(columnName);
115115
}
116116

117+
if (commentText != null) {
118+
fqn.append(" COMMENT ");
119+
fqn.append(commentText);
120+
}
121+
117122
if (arrayConstructor != null) {
118123
fqn.append(arrayConstructor);
119124
}
@@ -146,4 +151,17 @@ public Column withColumnName(String columnName) {
146151
this.setColumnName(columnName);
147152
return this;
148153
}
154+
155+
public Column withCommentText(String commentText) {
156+
this.setCommentText(commentText);
157+
return this;
158+
}
159+
160+
public void setCommentText(String commentText) {
161+
this.commentText = commentText;
162+
}
163+
164+
public String getCommentText() {
165+
return commentText;
166+
}
149167
}

src/main/java/net/sf/jsqlparser/statement/create/table/ColumnDefinition.java

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,14 @@
99
*/
1010
package net.sf.jsqlparser.statement.create.table;
1111

12+
import net.sf.jsqlparser.statement.select.PlainSelect;
13+
1214
import java.io.Serializable;
1315
import java.util.ArrayList;
1416
import java.util.Collection;
1517
import java.util.Collections;
1618
import java.util.List;
1719
import java.util.Optional;
18-
import net.sf.jsqlparser.statement.select.PlainSelect;
1920

2021
/**
2122
* Globally used definition class for columns.
@@ -64,18 +65,10 @@ public void setColumnName(String string) {
6465

6566
@Override
6667
public String toString() {
67-
String spec = toStringDataTypeAndSpec();
68-
if (spec.isEmpty()) {
69-
return columnName;
70-
}
7168
return columnName + " " + toStringDataTypeAndSpec();
7269
}
7370

7471
public String toStringDataTypeAndSpec() {
75-
if (colDataType == null) {
76-
return columnSpecs == null || columnSpecs.isEmpty() ? ""
77-
: PlainSelect.getStringList(columnSpecs, false, false);
78-
}
7972
return colDataType + (columnSpecs != null && !columnSpecs.isEmpty()
8073
? " " + PlainSelect.getStringList(columnSpecs, false, false)
8174
: "");

src/main/java/net/sf/jsqlparser/statement/create/view/CreateView.java

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,12 @@
99
*/
1010
package net.sf.jsqlparser.statement.create.view;
1111

12-
import java.util.ArrayList;
13-
import java.util.Collection;
14-
import java.util.Collections;
1512
import java.util.List;
16-
import java.util.Optional;
13+
import net.sf.jsqlparser.expression.operators.relational.ExpressionList;
14+
import net.sf.jsqlparser.schema.Column;
1715
import net.sf.jsqlparser.schema.Table;
1816
import net.sf.jsqlparser.statement.Statement;
1917
import net.sf.jsqlparser.statement.StatementVisitor;
20-
import net.sf.jsqlparser.statement.create.table.ColumnDefinition;
2118
import net.sf.jsqlparser.statement.select.PlainSelect;
2219
import net.sf.jsqlparser.statement.select.Select;
2320

@@ -26,7 +23,7 @@ public class CreateView implements Statement {
2623
private Table view;
2724
private Select select;
2825
private boolean orReplace = false;
29-
private List<ColumnDefinition> columnNames = null;
26+
private ExpressionList<Column> columnNames = null;
3027
private boolean materialized = false;
3128
private ForceOption force = ForceOption.NONE;
3229
private TemporaryOption temp = TemporaryOption.NONE;
@@ -67,11 +64,11 @@ public void setSelect(Select select) {
6764
this.select = select;
6865
}
6966

70-
public List<ColumnDefinition> getColumnNames() {
67+
public ExpressionList<Column> getColumnNames() {
7168
return columnNames;
7269
}
7370

74-
public void setColumnNames(List<ColumnDefinition> columnNames) {
71+
public void setColumnNames(ExpressionList<Column> columnNames) {
7572
this.columnNames = columnNames;
7673
}
7774

@@ -147,7 +144,9 @@ public String toString() {
147144
sql.append(" AUTO REFRESH ").append(autoRefresh.name());
148145
}
149146
if (columnNames != null) {
150-
sql.append(PlainSelect.getStringList(columnNames, true, true));
147+
sql.append("(");
148+
sql.append(columnNames);
149+
sql.append(")");
151150
}
152151
if (viewCommentOptions != null) {
153152
sql.append(PlainSelect.getStringList(viewCommentOptions, false, false));
@@ -187,7 +186,7 @@ public CreateView withOrReplace(boolean orReplace) {
187186
return this;
188187
}
189188

190-
public CreateView withColumnNames(List<ColumnDefinition> columnNames) {
189+
public CreateView withColumnNames(ExpressionList<Column> columnNames) {
191190
this.setColumnNames(columnNames);
192191
return this;
193192
}
@@ -207,20 +206,6 @@ public CreateView withWithReadOnly(boolean withReadOnly) {
207206
return this;
208207
}
209208

210-
public CreateView addColumnNames(ColumnDefinition... columnNames) {
211-
List<ColumnDefinition> collection =
212-
Optional.ofNullable(getColumnNames()).orElseGet(ArrayList::new);
213-
Collections.addAll(collection, columnNames);
214-
return this.withColumnNames(collection);
215-
}
216-
217-
public CreateView addColumnNames(Collection<ColumnDefinition> columnNames) {
218-
List<ColumnDefinition> collection =
219-
Optional.ofNullable(getColumnNames()).orElseGet(ArrayList::new);
220-
collection.addAll(columnNames);
221-
return this.withColumnNames(collection);
222-
}
223-
224209
public List<String> getViewCommentOptions() {
225210
return viewCommentOptions;
226211
}

src/main/java/net/sf/jsqlparser/util/deparser/CreateViewDeParser.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,9 @@ public void deParse(CreateView createView) {
6767
buffer.append(" AUTO REFRESH ").append(createView.getAutoRefresh().name());
6868
}
6969
if (createView.getColumnNames() != null) {
70-
buffer.append(PlainSelect.getStringList(createView.getColumnNames(), true, true));
70+
buffer.append("(");
71+
buffer.append(createView.getColumnNames());
72+
buffer.append(")");
7173
}
7274
if (createView.getViewCommentOptions() != null) {
7375
buffer.append(

src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1767,16 +1767,18 @@ Column Column() #Column :
17671767
{
17681768
List<String> data = new ArrayList<String>();
17691769
ArrayConstructor arrayConstructor = null;
1770+
Token tk = null;
17701771
}
17711772
{
17721773
data = RelObjectNameList()
1773-
1774+
[ <K_COMMENT> tk=<S_CHAR_LITERAL> ]
17741775
// @todo: we better should return a SEQUENCE instead of a COLUMN
17751776
[ "." <K_NEXTVAL> { data.add("nextval"); } ]
17761777

17771778
[ LOOKAHEAD(2) arrayConstructor = ArrayConstructor(false) ]
17781779
{
17791780
Column col = new Column(data);
1781+
if (tk != null) { col.withCommentText(tk.image); }
17801782
if (arrayConstructor!=null) {
17811783
col.setArrayConstructor(arrayConstructor);
17821784
}
@@ -5705,42 +5707,28 @@ Analyze Analyze():
57055707
}
57065708
}
57075709

5708-
ColumnDefinition ColumnDefinitionItem():
5709-
{
5710-
Token tk = null;
5711-
Token tk2 = null;
5712-
String item = null;
5713-
ColumnDefinition columnWithComment = new ColumnDefinition();
5714-
}
5710+
ExpressionList<Column> ColumnWithCommentList():
57155711
{
5716-
( item=RelObjectName() { columnWithComment.withColumnName(item); } )
5717-
[ tk=<K_COMMENT> tk2=<S_CHAR_LITERAL> { columnWithComment.addColumnSpecs(tk.image).addColumnSpecs(tk2.image); } ]
5718-
{
5719-
return columnWithComment;
5720-
}
5721-
}
5722-
5723-
List<ColumnDefinition> ColumnDefinitionItemList():
5724-
{
5725-
List<ColumnDefinition> retval = new ArrayList<ColumnDefinition>();
5726-
ColumnDefinition img = null;
5712+
ExpressionList<Column> expressions = new ExpressionList<Column>();
5713+
Column img = null;
57275714
}
57285715
{
57295716
"("
5730-
img=ColumnDefinitionItem() { retval.add(img); }
5731-
( "," img=ColumnDefinitionItem() { retval.add(img); } )*
5717+
img=Column() { expressions.add(img); }
5718+
( "," img=Column() { expressions.add(img); } )*
57325719
")"
57335720
{
5734-
return retval;
5721+
return expressions;
57355722
}
57365723
}
57375724

5725+
57385726
CreateView CreateView(boolean isUsingOrReplace):
57395727
{
57405728
CreateView createView = new CreateView();
57415729
Table view = null;
57425730
Select select = null;
5743-
List<ColumnDefinition> columnNames = null;
5731+
ExpressionList<Column> columnNames = null;
57445732
Token tk = null;
57455733
List<String> commentTokens = null;
57465734
}
@@ -5758,7 +5746,7 @@ CreateView CreateView(boolean isUsingOrReplace):
57585746
<K_VIEW> view=Table() { createView.setView(view); }
57595747
[LOOKAHEAD(3) <K_AUTO> <K_REFRESH> (tk=<K_YES> | tk=<K_NO>) { createView.setAutoRefresh(AutoRefreshOption.from(tk.image)); } ]
57605748
[LOOKAHEAD(3) <K_IF> <K_NOT> <K_EXISTS> {createView.setIfNotExists(true);}]
5761-
[ columnNames=ColumnDefinitionItemList( ) { createView.setColumnNames(columnNames); } ]
5749+
[ columnNames=ColumnWithCommentList( ) { createView.setColumnNames(columnNames); } ]
57625750
[ commentTokens=CreateViewTailComment( ) { createView.setViewCommentOptions(commentTokens); } ]
57635751
<K_AS>
57645752
select=Select( ) { createView.setSelect(select); }

src/test/java/net/sf/jsqlparser/statement/create/CreateViewTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
*/
1010
package net.sf.jsqlparser.statement.create;
1111

12-
import static net.sf.jsqlparser.test.TestUtils.*;
1312
import static net.sf.jsqlparser.test.TestUtils.assertSqlCanBeParsedAndDeparsed;
1413
import static org.assertj.core.api.Assertions.assertThatThrownBy;
1514
import static org.junit.jupiter.api.Assertions.assertEquals;

0 commit comments

Comments
 (0)