Skip to content

Commit 02b6f95

Browse files
committed
CreateView with comment for mysql
1 parent 425c72e commit 02b6f95

File tree

10 files changed

+213
-35
lines changed

10 files changed

+213
-35
lines changed

src/main/java/net/sf/jsqlparser/parser/feature/Feature.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,12 @@ public enum Feature {
483483
* SQL "CREATE MATERIALIZED VIEW" statement is allowed
484484
*/
485485
createViewMaterialized,
486+
487+
/**
488+
* SQL "CREATE VIEW(x comment 'x', y comment 'y') comment 'view'" statement is allowed
489+
*/
490+
createViewWithComment,
491+
486492
/**
487493
* SQL "CREATE TABLE" statement is allowed
488494
*
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*-
2+
* #%L
3+
* JSQLParser library
4+
* %%
5+
* Copyright (C) 2004 - 2019 JSQLParser
6+
* %%
7+
* Dual licensed under GNU LGPL 2.1 or Apache License 2.0
8+
* #L%
9+
*/
10+
package net.sf.jsqlparser.statement.create.view;
11+
12+
import java.io.Serializable;
13+
14+
public class ColumnWithCommentExpression implements Serializable {
15+
16+
private String columnName;
17+
private String commentText;
18+
19+
@Override
20+
public String toString() {
21+
StringBuilder b = new StringBuilder();
22+
if (columnName != null) {
23+
b.append(columnName);
24+
}
25+
if (commentText != null) {
26+
b.append(" COMMENT ");
27+
b.append(commentText);
28+
}
29+
return b.toString();
30+
}
31+
32+
public String getColumnName() {
33+
return columnName;
34+
}
35+
36+
public void setColumnName(String columnName) {
37+
this.columnName = columnName;
38+
}
39+
40+
public String getCommentText() {
41+
return commentText;
42+
}
43+
44+
public void setCommentText(String commentText) {
45+
this.commentText = commentText;
46+
}
47+
48+
public ColumnWithCommentExpression withCommentText(String commentText) {
49+
setCommentText(commentText);
50+
return this;
51+
}
52+
53+
public ColumnWithCommentExpression withColumnName(String columnName) {
54+
setColumnName(columnName);
55+
return this;
56+
}
57+
58+
}

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

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,14 @@ public class CreateView implements Statement {
2525
private Table view;
2626
private Select select;
2727
private boolean orReplace = false;
28-
private List<String> columnNames = null;
28+
private List<ColumnWithCommentExpression> columnNames = null;
2929
private boolean materialized = false;
3030
private ForceOption force = ForceOption.NONE;
3131
private TemporaryOption temp = TemporaryOption.NONE;
3232
private AutoRefreshOption autoRefresh = AutoRefreshOption.NONE;
3333
private boolean withReadOnly = false;
3434
private boolean ifNotExists = false;
35+
private List<String> viewCommentOptions = null;
3536

3637
@Override
3738
public void accept(StatementVisitor statementVisitor) {
@@ -65,11 +66,11 @@ public void setSelect(Select select) {
6566
this.select = select;
6667
}
6768

68-
public List<String> getColumnNames() {
69+
public List<ColumnWithCommentExpression> getColumnNames() {
6970
return columnNames;
7071
}
7172

72-
public void setColumnNames(List<String> columnNames) {
73+
public void setColumnNames(List<ColumnWithCommentExpression> columnNames) {
7374
this.columnNames = columnNames;
7475
}
7576

@@ -147,6 +148,9 @@ public String toString() {
147148
if (columnNames != null) {
148149
sql.append(PlainSelect.getStringList(columnNames, true, true));
149150
}
151+
if (viewCommentOptions != null) {
152+
sql.append(PlainSelect.getStringList(viewCommentOptions, false, false));
153+
}
150154
sql.append(" AS ").append(select);
151155
if (isWithReadOnly()) {
152156
sql.append(" WITH READ ONLY");
@@ -182,7 +186,7 @@ public CreateView withOrReplace(boolean orReplace) {
182186
return this;
183187
}
184188

185-
public CreateView withColumnNames(List<String> columnNames) {
189+
public CreateView withColumnNames(List<ColumnWithCommentExpression> columnNames) {
186190
this.setColumnNames(columnNames);
187191
return this;
188192
}
@@ -202,15 +206,25 @@ public CreateView withWithReadOnly(boolean withReadOnly) {
202206
return this;
203207
}
204208

205-
public CreateView addColumnNames(String... columnNames) {
206-
List<String> collection = Optional.ofNullable(getColumnNames()).orElseGet(ArrayList::new);
209+
public CreateView addColumnNames(ColumnWithCommentExpression... columnNames) {
210+
List<ColumnWithCommentExpression> collection =
211+
Optional.ofNullable(getColumnNames()).orElseGet(ArrayList::new);
207212
Collections.addAll(collection, columnNames);
208213
return this.withColumnNames(collection);
209214
}
210215

211-
public CreateView addColumnNames(Collection<String> columnNames) {
212-
List<String> collection = Optional.ofNullable(getColumnNames()).orElseGet(ArrayList::new);
216+
public CreateView addColumnNames(Collection<ColumnWithCommentExpression> columnNames) {
217+
List<ColumnWithCommentExpression> collection =
218+
Optional.ofNullable(getColumnNames()).orElseGet(ArrayList::new);
213219
collection.addAll(columnNames);
214220
return this.withColumnNames(collection);
215221
}
222+
223+
public List<String> getViewCommentOptions() {
224+
return viewCommentOptions;
225+
}
226+
227+
public void setViewCommentOptions(List<String> viewCommentOptions) {
228+
this.viewCommentOptions = viewCommentOptions;
229+
}
216230
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ public void deParse(CreateView createView) {
6969
if (createView.getColumnNames() != null) {
7070
buffer.append(PlainSelect.getStringList(createView.getColumnNames(), true, true));
7171
}
72+
if (createView.getViewCommentOptions() != null) {
73+
buffer.append(
74+
PlainSelect.getStringList(createView.getViewCommentOptions(), false, false));
75+
}
7276
buffer.append(" AS ");
7377

7478
Select select = createView.getSelect();

src/main/java/net/sf/jsqlparser/util/validation/feature/MariaDbVersion.java

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,10 @@
99
*/
1010
package net.sf.jsqlparser.util.validation.feature;
1111

12-
import net.sf.jsqlparser.parser.feature.Feature;
13-
1412
import java.util.Collections;
1513
import java.util.EnumSet;
1614
import java.util.Set;
15+
import net.sf.jsqlparser.parser.feature.Feature;
1716

1817
/**
1918
* Please add Features supported and place a link to public documentation
@@ -41,7 +40,8 @@ public enum MariaDbVersion implements Version {
4140
Feature.selectForUpdateSkipLocked,
4241

4342
// https://mariadb.com/kb/en/join-syntax/
44-
Feature.join, Feature.joinSimple, Feature.joinRight, Feature.joinNatural, Feature.joinLeft,
43+
Feature.join, Feature.joinSimple, Feature.joinRight, Feature.joinNatural,
44+
Feature.joinLeft,
4545
Feature.joinCross, Feature.joinOuter, Feature.joinInner, Feature.joinStraight,
4646
Feature.joinUsingColumns,
4747

@@ -64,8 +64,10 @@ public enum MariaDbVersion implements Version {
6464

6565
// https://mariadb.com/kb/en/insert/
6666
Feature.insert, Feature.insertValues, Feature.values,
67-
Feature.insertFromSelect, Feature.insertModifierPriority, Feature.insertModifierIgnore,
68-
Feature.insertUseSet, Feature.insertUseDuplicateKeyUpdate, Feature.insertReturningExpressionList,
67+
Feature.insertFromSelect, Feature.insertModifierPriority,
68+
Feature.insertModifierIgnore,
69+
Feature.insertUseSet, Feature.insertUseDuplicateKeyUpdate,
70+
Feature.insertReturningExpressionList,
6971

7072
// https://mariadb.com/kb/en/update/
7173
Feature.update,
@@ -96,7 +98,8 @@ public enum MariaDbVersion implements Version {
9698
Feature.dropView,
9799
// https://mariadb.com/kb/en/drop-sequence/
98100
Feature.dropSequence, Feature.dropTableIfExists, Feature.dropIndexIfExists,
99-
Feature.dropViewIfExists, Feature.dropSchemaIfExists, Feature.dropSequenceIfExists,
101+
Feature.dropViewIfExists, Feature.dropSchemaIfExists,
102+
Feature.dropSequenceIfExists,
100103

101104
// https://mariadb.com/kb/en/replace/
102105
Feature.upsert,
@@ -110,9 +113,11 @@ public enum MariaDbVersion implements Version {
110113
// https://mariadb.com/kb/en/create-view/
111114
Feature.createView,
112115
Feature.createOrReplaceView,
116+
Feature.createViewWithComment,
113117

114118
// https://mariadb.com/kb/en/create-table/
115-
Feature.createTable, Feature.createTableCreateOptionStrings, Feature.createTableTableOptionStrings,
119+
Feature.createTable, Feature.createTableCreateOptionStrings,
120+
Feature.createTableTableOptionStrings,
116121
Feature.createTableFromSelect, Feature.createTableIfNotExists,
117122
// https://mariadb.com/kb/en/create-index/
118123
Feature.createIndex,
@@ -143,7 +148,7 @@ public enum MariaDbVersion implements Version {
143148
Feature.commit,
144149
// https://mariadb.com/kb/en/optimizer-hints/
145150
Feature.mySqlHintStraightJoin,
146-
Feature.mysqlCalcFoundRows,
151+
Feature.mysqlCalcFoundRows,
147152
Feature.mysqlSqlCacheFlag)),
148153

149154
ORACLE_MODE("oracle_mode", V10_5_4.copy().add(Feature.selectUnique).getFeatures());

src/main/java/net/sf/jsqlparser/util/validation/feature/MySqlVersion.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,10 @@
99
*/
1010
package net.sf.jsqlparser.util.validation.feature;
1111

12-
import net.sf.jsqlparser.parser.feature.Feature;
13-
1412
import java.util.Collections;
1513
import java.util.EnumSet;
1614
import java.util.Set;
15+
import net.sf.jsqlparser.parser.feature.Feature;
1716

1817
/**
1918
* Please add Features supported and place a link to public documentation
@@ -33,7 +32,8 @@ public enum MySqlVersion implements Version {
3332
// https://dev.mysql.com/doc/refman/8.0/en/select.html
3433
Feature.select,
3534
Feature.selectGroupBy, Feature.selectHaving,
36-
Feature.limit, Feature.limitOffset, Feature.offset, Feature.offsetParam, Feature.orderBy,
35+
Feature.limit, Feature.limitOffset, Feature.offset, Feature.offsetParam,
36+
Feature.orderBy,
3737
Feature.selectForUpdate,
3838
Feature.selectForUpdateOfTable,
3939
Feature.selectForUpdateNoWait,
@@ -51,7 +51,8 @@ public enum MySqlVersion implements Version {
5151
Feature.function,
5252

5353
// https://dev.mysql.com/doc/refman/8.0/en/join.html
54-
Feature.join, Feature.joinSimple, Feature.joinLeft, Feature.joinRight, Feature.joinOuter,
54+
Feature.join, Feature.joinSimple, Feature.joinLeft, Feature.joinRight,
55+
Feature.joinOuter,
5556
Feature.joinNatural, Feature.joinInner, Feature.joinCross, Feature.joinStraight,
5657
Feature.joinUsingColumns,
5758

@@ -99,9 +100,11 @@ public enum MySqlVersion implements Version {
99100
Feature.createSchema,
100101
// https://dev.mysql.com/doc/refman/8.0/en/create-view.html
101102
Feature.createView,
103+
Feature.createViewWithComment,
102104
Feature.createOrReplaceView,
103105
// https://dev.mysql.com/doc/refman/8.0/en/create-table.html
104-
Feature.createTable, Feature.createTableCreateOptionStrings, Feature.createTableTableOptionStrings,
106+
Feature.createTable, Feature.createTableCreateOptionStrings,
107+
Feature.createTableTableOptionStrings,
105108
Feature.createTableFromSelect, Feature.createTableIfNotExists,
106109
// https://dev.mysql.com/doc/refman/8.0/en/create-index.html
107110
Feature.createIndex,

src/main/java/net/sf/jsqlparser/util/validation/validator/CreateViewValidator.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ public void validate(CreateView createView) {
3333
Feature.createViewTemporary);
3434
validateFeature(c, createView.isMaterialized(), Feature.createViewMaterialized);
3535
validateName(c, NamedObject.view, createView.getView().getFullyQualifiedName(), false);
36+
validateFeature(c, createView.getViewCommentOptions() != null,
37+
Feature.createViewWithComment);
3638
}
3739
SelectValidator v = getValidator(SelectValidator.class);
3840
Select select = createView.getSelect();

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

Lines changed: 58 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5705,13 +5705,46 @@ Analyze Analyze():
57055705
}
57065706
}
57075707

5708+
ColumnWithCommentExpression ColumnWithCommentExpressionItem():
5709+
{
5710+
Token tk = null;
5711+
String item = null;
5712+
ColumnWithCommentExpression columnWithCommentExp = new ColumnWithCommentExpression();
5713+
}
5714+
{
5715+
( item = RelObjectName() { columnWithCommentExp.withColumnName(item); } )
5716+
[
5717+
<K_COMMENT> tk=<S_CHAR_LITERAL> { columnWithCommentExp.withColumnName(item).withCommentText(tk.image); }
5718+
]
5719+
{
5720+
return columnWithCommentExp;
5721+
}
5722+
}
5723+
5724+
List<ColumnWithCommentExpression> ColumnWithCommentExpressionItemList():
5725+
{
5726+
List<ColumnWithCommentExpression> retval = new ArrayList<ColumnWithCommentExpression>();
5727+
ColumnWithCommentExpression img = null;
5728+
}
5729+
{
5730+
"("
5731+
img = ColumnWithCommentExpressionItem() { retval.add(img); }
5732+
( "," img=ColumnWithCommentExpressionItem() { retval.add(img); } )*
5733+
5734+
")"
5735+
{
5736+
return retval;
5737+
}
5738+
}
5739+
57085740
CreateView CreateView(boolean isUsingOrReplace):
57095741
{
57105742
CreateView createView = new CreateView();
57115743
Table view = null;
57125744
Select select = null;
5713-
List<String> columnNames = null;
5745+
List<ColumnWithCommentExpression> columnNames = null;
57145746
Token tk = null;
5747+
List<String> commentTokens = null;
57155748
}
57165749
{
57175750
{ createView.setOrReplace(isUsingOrReplace);}
@@ -5727,13 +5760,36 @@ CreateView CreateView(boolean isUsingOrReplace):
57275760
<K_VIEW> view=Table() { createView.setView(view); }
57285761
[LOOKAHEAD(3) <K_AUTO> <K_REFRESH> (tk=<K_YES> | tk=<K_NO>) { createView.setAutoRefresh(AutoRefreshOption.from(tk.image)); } ]
57295762
[LOOKAHEAD(3) <K_IF> <K_NOT> <K_EXISTS> {createView.setIfNotExists(true);}]
5730-
[ columnNames = ColumnsNamesList() { createView.setColumnNames(columnNames); } ]
5763+
[ columnNames = ColumnWithCommentExpressionItemList( ) { createView.setColumnNames(columnNames); } ]
5764+
[ commentTokens=CreateViewTailComment( ) { createView.setViewCommentOptions(commentTokens);} ]
57315765
<K_AS>
57325766
select=Select( ) { createView.setSelect(select); }
57335767
[ <K_WITH> <K_READ> <K_ONLY> { createView.setWithReadOnly(true); } ]
57345768
{ return createView; }
57355769
}
57365770

5771+
List<String> CreateViewTailComment():
5772+
{
5773+
Token tk = null;
5774+
Token tk2 = null;
5775+
String op = null;
5776+
List<String> result = new ArrayList<String>();
5777+
}
5778+
{
5779+
tk=<K_COMMENT>
5780+
[ "=" { op = "="; } ]
5781+
tk2 = <S_CHAR_LITERAL> {
5782+
result.add("");
5783+
result.add(tk.image);
5784+
if (op != null) {
5785+
result.add(op);
5786+
}
5787+
result.add(tk2.image);
5788+
}
5789+
{ return result;}
5790+
}
5791+
5792+
57375793
ReferentialAction.Action Action():
57385794
{
57395795
ReferentialAction.Action action = null;
@@ -5778,7 +5834,6 @@ List<String> CreateParameter():
57785834
Token tk = null, tk2 = null;
57795835
Expression exp = null;
57805836
ColDataType colDataType;
5781-
57825837
List<String> param = new ArrayList<String>();
57835838
}
57845839
{

0 commit comments

Comments
 (0)