Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
CreateView with comment for mysql
  • Loading branch information
jxnu-liguobin committed Dec 14, 2023
commit 02b6f95be2f8370445b181594f3adaae734ee7a2
6 changes: 6 additions & 0 deletions src/main/java/net/sf/jsqlparser/parser/feature/Feature.java
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,12 @@ public enum Feature {
* SQL "CREATE MATERIALIZED VIEW" statement is allowed
*/
createViewMaterialized,

/**
* SQL "CREATE VIEW(x comment 'x', y comment 'y') comment 'view'" statement is allowed
*/
createViewWithComment,

/**
* SQL "CREATE TABLE" statement is allowed
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*-
* #%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.statement.create.view;

import java.io.Serializable;

public class ColumnWithCommentExpression implements Serializable {

private String columnName;
private String commentText;

@Override
public String toString() {
StringBuilder b = new StringBuilder();
if (columnName != null) {
b.append(columnName);
}
if (commentText != null) {
b.append(" COMMENT ");
b.append(commentText);
}
return b.toString();
}

public String getColumnName() {
return columnName;
}

public void setColumnName(String columnName) {
this.columnName = columnName;
}

public String getCommentText() {
return commentText;
}

public void setCommentText(String commentText) {
this.commentText = commentText;
}

public ColumnWithCommentExpression withCommentText(String commentText) {
setCommentText(commentText);
return this;
}

public ColumnWithCommentExpression withColumnName(String columnName) {
setColumnName(columnName);
return this;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,14 @@ public class CreateView implements Statement {
private Table view;
private Select select;
private boolean orReplace = false;
private List<String> columnNames = null;
private List<ColumnWithCommentExpression> columnNames = null;
private boolean materialized = false;
private ForceOption force = ForceOption.NONE;
private TemporaryOption temp = TemporaryOption.NONE;
private AutoRefreshOption autoRefresh = AutoRefreshOption.NONE;
private boolean withReadOnly = false;
private boolean ifNotExists = false;
private List<String> viewCommentOptions = null;

@Override
public void accept(StatementVisitor statementVisitor) {
Expand Down Expand Up @@ -65,11 +66,11 @@ public void setSelect(Select select) {
this.select = select;
}

public List<String> getColumnNames() {
public List<ColumnWithCommentExpression> getColumnNames() {
return columnNames;
}

public void setColumnNames(List<String> columnNames) {
public void setColumnNames(List<ColumnWithCommentExpression> columnNames) {
this.columnNames = columnNames;
}

Expand Down Expand Up @@ -147,6 +148,9 @@ public String toString() {
if (columnNames != null) {
sql.append(PlainSelect.getStringList(columnNames, true, true));
}
if (viewCommentOptions != null) {
sql.append(PlainSelect.getStringList(viewCommentOptions, false, false));
}
sql.append(" AS ").append(select);
if (isWithReadOnly()) {
sql.append(" WITH READ ONLY");
Expand Down Expand Up @@ -182,7 +186,7 @@ public CreateView withOrReplace(boolean orReplace) {
return this;
}

public CreateView withColumnNames(List<String> columnNames) {
public CreateView withColumnNames(List<ColumnWithCommentExpression> columnNames) {
this.setColumnNames(columnNames);
return this;
}
Expand All @@ -202,15 +206,25 @@ public CreateView withWithReadOnly(boolean withReadOnly) {
return this;
}

public CreateView addColumnNames(String... columnNames) {
List<String> collection = Optional.ofNullable(getColumnNames()).orElseGet(ArrayList::new);
public CreateView addColumnNames(ColumnWithCommentExpression... columnNames) {
List<ColumnWithCommentExpression> collection =
Optional.ofNullable(getColumnNames()).orElseGet(ArrayList::new);
Collections.addAll(collection, columnNames);
return this.withColumnNames(collection);
}

public CreateView addColumnNames(Collection<String> columnNames) {
List<String> collection = Optional.ofNullable(getColumnNames()).orElseGet(ArrayList::new);
public CreateView addColumnNames(Collection<ColumnWithCommentExpression> columnNames) {
List<ColumnWithCommentExpression> collection =
Optional.ofNullable(getColumnNames()).orElseGet(ArrayList::new);
collection.addAll(columnNames);
return this.withColumnNames(collection);
}

public List<String> getViewCommentOptions() {
return viewCommentOptions;
}

public void setViewCommentOptions(List<String> viewCommentOptions) {
this.viewCommentOptions = viewCommentOptions;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ public void deParse(CreateView createView) {
if (createView.getColumnNames() != null) {
buffer.append(PlainSelect.getStringList(createView.getColumnNames(), true, true));
}
if (createView.getViewCommentOptions() != null) {
buffer.append(
PlainSelect.getStringList(createView.getViewCommentOptions(), false, false));
}
buffer.append(" AS ");

Select select = createView.getSelect();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@
*/
package net.sf.jsqlparser.util.validation.feature;

import net.sf.jsqlparser.parser.feature.Feature;

import java.util.Collections;
import java.util.EnumSet;
import java.util.Set;
import net.sf.jsqlparser.parser.feature.Feature;

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

// https://mariadb.com/kb/en/join-syntax/
Feature.join, Feature.joinSimple, Feature.joinRight, Feature.joinNatural, Feature.joinLeft,
Feature.join, Feature.joinSimple, Feature.joinRight, Feature.joinNatural,
Feature.joinLeft,
Feature.joinCross, Feature.joinOuter, Feature.joinInner, Feature.joinStraight,
Feature.joinUsingColumns,

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

// https://mariadb.com/kb/en/insert/
Feature.insert, Feature.insertValues, Feature.values,
Feature.insertFromSelect, Feature.insertModifierPriority, Feature.insertModifierIgnore,
Feature.insertUseSet, Feature.insertUseDuplicateKeyUpdate, Feature.insertReturningExpressionList,
Feature.insertFromSelect, Feature.insertModifierPriority,
Feature.insertModifierIgnore,
Feature.insertUseSet, Feature.insertUseDuplicateKeyUpdate,
Feature.insertReturningExpressionList,

// https://mariadb.com/kb/en/update/
Feature.update,
Expand Down Expand Up @@ -96,7 +98,8 @@ public enum MariaDbVersion implements Version {
Feature.dropView,
// https://mariadb.com/kb/en/drop-sequence/
Feature.dropSequence, Feature.dropTableIfExists, Feature.dropIndexIfExists,
Feature.dropViewIfExists, Feature.dropSchemaIfExists, Feature.dropSequenceIfExists,
Feature.dropViewIfExists, Feature.dropSchemaIfExists,
Feature.dropSequenceIfExists,

// https://mariadb.com/kb/en/replace/
Feature.upsert,
Expand All @@ -110,9 +113,11 @@ public enum MariaDbVersion implements Version {
// https://mariadb.com/kb/en/create-view/
Feature.createView,
Feature.createOrReplaceView,
Feature.createViewWithComment,

// https://mariadb.com/kb/en/create-table/
Feature.createTable, Feature.createTableCreateOptionStrings, Feature.createTableTableOptionStrings,
Feature.createTable, Feature.createTableCreateOptionStrings,
Feature.createTableTableOptionStrings,
Feature.createTableFromSelect, Feature.createTableIfNotExists,
// https://mariadb.com/kb/en/create-index/
Feature.createIndex,
Expand Down Expand Up @@ -143,7 +148,7 @@ public enum MariaDbVersion implements Version {
Feature.commit,
// https://mariadb.com/kb/en/optimizer-hints/
Feature.mySqlHintStraightJoin,
Feature.mysqlCalcFoundRows,
Feature.mysqlCalcFoundRows,
Feature.mysqlSqlCacheFlag)),

ORACLE_MODE("oracle_mode", V10_5_4.copy().add(Feature.selectUnique).getFeatures());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@
*/
package net.sf.jsqlparser.util.validation.feature;

import net.sf.jsqlparser.parser.feature.Feature;

import java.util.Collections;
import java.util.EnumSet;
import java.util.Set;
import net.sf.jsqlparser.parser.feature.Feature;

/**
* Please add Features supported and place a link to public documentation
Expand All @@ -33,7 +32,8 @@ public enum MySqlVersion implements Version {
// https://dev.mysql.com/doc/refman/8.0/en/select.html
Feature.select,
Feature.selectGroupBy, Feature.selectHaving,
Feature.limit, Feature.limitOffset, Feature.offset, Feature.offsetParam, Feature.orderBy,
Feature.limit, Feature.limitOffset, Feature.offset, Feature.offsetParam,
Feature.orderBy,
Feature.selectForUpdate,
Feature.selectForUpdateOfTable,
Feature.selectForUpdateNoWait,
Expand All @@ -51,7 +51,8 @@ public enum MySqlVersion implements Version {
Feature.function,

// https://dev.mysql.com/doc/refman/8.0/en/join.html
Feature.join, Feature.joinSimple, Feature.joinLeft, Feature.joinRight, Feature.joinOuter,
Feature.join, Feature.joinSimple, Feature.joinLeft, Feature.joinRight,
Feature.joinOuter,
Feature.joinNatural, Feature.joinInner, Feature.joinCross, Feature.joinStraight,
Feature.joinUsingColumns,

Expand Down Expand Up @@ -99,9 +100,11 @@ public enum MySqlVersion implements Version {
Feature.createSchema,
// https://dev.mysql.com/doc/refman/8.0/en/create-view.html
Feature.createView,
Feature.createViewWithComment,
Feature.createOrReplaceView,
// https://dev.mysql.com/doc/refman/8.0/en/create-table.html
Feature.createTable, Feature.createTableCreateOptionStrings, Feature.createTableTableOptionStrings,
Feature.createTable, Feature.createTableCreateOptionStrings,
Feature.createTableTableOptionStrings,
Feature.createTableFromSelect, Feature.createTableIfNotExists,
// https://dev.mysql.com/doc/refman/8.0/en/create-index.html
Feature.createIndex,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ public void validate(CreateView createView) {
Feature.createViewTemporary);
validateFeature(c, createView.isMaterialized(), Feature.createViewMaterialized);
validateName(c, NamedObject.view, createView.getView().getFullyQualifiedName(), false);
validateFeature(c, createView.getViewCommentOptions() != null,
Feature.createViewWithComment);
}
SelectValidator v = getValidator(SelectValidator.class);
Select select = createView.getSelect();
Expand Down
61 changes: 58 additions & 3 deletions src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt
Original file line number Diff line number Diff line change
Expand Up @@ -5705,13 +5705,46 @@ Analyze Analyze():
}
}

ColumnWithCommentExpression ColumnWithCommentExpressionItem():
{
Token tk = null;
String item = null;
ColumnWithCommentExpression columnWithCommentExp = new ColumnWithCommentExpression();
}
{
( item = RelObjectName() { columnWithCommentExp.withColumnName(item); } )
[
<K_COMMENT> tk=<S_CHAR_LITERAL> { columnWithCommentExp.withColumnName(item).withCommentText(tk.image); }
]
{
return columnWithCommentExp;
}
}

List<ColumnWithCommentExpression> ColumnWithCommentExpressionItemList():
{
List<ColumnWithCommentExpression> retval = new ArrayList<ColumnWithCommentExpression>();
ColumnWithCommentExpression img = null;
}
{
"("
img = ColumnWithCommentExpressionItem() { retval.add(img); }
( "," img=ColumnWithCommentExpressionItem() { retval.add(img); } )*

")"
{
return retval;
}
}

CreateView CreateView(boolean isUsingOrReplace):
{
CreateView createView = new CreateView();
Table view = null;
Select select = null;
List<String> columnNames = null;
List<ColumnWithCommentExpression> columnNames = null;
Token tk = null;
List<String> commentTokens = null;
}
{
{ createView.setOrReplace(isUsingOrReplace);}
Expand All @@ -5727,13 +5760,36 @@ CreateView CreateView(boolean isUsingOrReplace):
<K_VIEW> view=Table() { createView.setView(view); }
[LOOKAHEAD(3) <K_AUTO> <K_REFRESH> (tk=<K_YES> | tk=<K_NO>) { createView.setAutoRefresh(AutoRefreshOption.from(tk.image)); } ]
[LOOKAHEAD(3) <K_IF> <K_NOT> <K_EXISTS> {createView.setIfNotExists(true);}]
[ columnNames = ColumnsNamesList() { createView.setColumnNames(columnNames); } ]
[ columnNames = ColumnWithCommentExpressionItemList( ) { createView.setColumnNames(columnNames); } ]
[ commentTokens=CreateViewTailComment( ) { createView.setViewCommentOptions(commentTokens);} ]
<K_AS>
select=Select( ) { createView.setSelect(select); }
[ <K_WITH> <K_READ> <K_ONLY> { createView.setWithReadOnly(true); } ]
{ return createView; }
}

List<String> CreateViewTailComment():
{
Token tk = null;
Token tk2 = null;
String op = null;
List<String> result = new ArrayList<String>();
}
{
tk=<K_COMMENT>
[ "=" { op = "="; } ]
tk2 = <S_CHAR_LITERAL> {
result.add("");
result.add(tk.image);
if (op != null) {
result.add(op);
}
result.add(tk2.image);
}
{ return result;}
}


ReferentialAction.Action Action():
{
ReferentialAction.Action action = null;
Expand Down Expand Up @@ -5778,7 +5834,6 @@ List<String> CreateParameter():
Token tk = null, tk2 = null;
Expression exp = null;
ColDataType colDataType;

List<String> param = new ArrayList<String>();
}
{
Expand Down
Loading