Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add support comment in create view for MySQL and MariaDb #1913

Merged
merged 6 commits into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
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
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
Expand Up @@ -9,14 +9,13 @@
*/
package net.sf.jsqlparser.statement.create.table;

import net.sf.jsqlparser.statement.select.PlainSelect;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import net.sf.jsqlparser.statement.select.PlainSelect;

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

@Override
public String toString() {
String spec = toStringDataTypeAndSpec();
if (spec.isEmpty()) {
return columnName;
}
return columnName + " " + toStringDataTypeAndSpec();
}

public String toStringDataTypeAndSpec() {
if (colDataType == null) {
return columnSpecs == null || columnSpecs.isEmpty() ? ""
: PlainSelect.getStringList(columnSpecs, false, false);
}
return colDataType + (columnSpecs != null && !columnSpecs.isEmpty()
? " " + PlainSelect.getStringList(columnSpecs, false, false)
: "");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import net.sf.jsqlparser.schema.Table;
import net.sf.jsqlparser.statement.Statement;
import net.sf.jsqlparser.statement.StatementVisitor;
import net.sf.jsqlparser.statement.create.table.ColumnDefinition;
import net.sf.jsqlparser.statement.select.PlainSelect;
import net.sf.jsqlparser.statement.select.Select;

Expand All @@ -25,13 +26,14 @@ public class CreateView implements Statement {
private Table view;
private Select select;
private boolean orReplace = false;
private List<String> columnNames = null;
private List<ColumnDefinition> 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 +67,11 @@ public void setSelect(Select select) {
this.select = select;
}

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

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

Expand Down Expand Up @@ -147,6 +149,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 +187,7 @@ public CreateView withOrReplace(boolean orReplace) {
return this;
}

public CreateView withColumnNames(List<String> columnNames) {
public CreateView withColumnNames(List<ColumnDefinition> columnNames) {
this.setColumnNames(columnNames);
return this;
}
Expand All @@ -202,15 +207,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(ColumnDefinition... columnNames) {
List<ColumnDefinition> 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<ColumnDefinition> columnNames) {
List<ColumnDefinition> 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
59 changes: 56 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,44 @@ Analyze Analyze():
}
}

ColumnDefinition ColumnDefinitionItem():
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There should be a similar Production already, please try to re-use it.

{
Token tk = null;
Token tk2 = null;
String item = null;
ColumnDefinition columnWithComment = new ColumnDefinition();
}
{
( item=RelObjectName() { columnWithComment.withColumnName(item); } )
[ tk=<K_COMMENT> tk2=<S_CHAR_LITERAL> { columnWithComment.addColumnSpecs(tk.image).addColumnSpecs(tk2.image); } ]
{
return columnWithComment;
}
}

List<ColumnDefinition> ColumnDefinitionItemList():
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please try to use an ExpressionList<Column> here.

{
List<ColumnDefinition> retval = new ArrayList<ColumnDefinition>();
ColumnDefinition img = null;
}
{
"("
img=ColumnDefinitionItem() { retval.add(img); }
( "," img=ColumnDefinitionItem() { retval.add(img); } )*
")"
{
return retval;
}
}

CreateView CreateView(boolean isUsingOrReplace):
{
CreateView createView = new CreateView();
Table view = null;
Select select = null;
List<String> columnNames = null;
List<ColumnDefinition> columnNames = null;
Token tk = null;
List<String> commentTokens = null;
}
{
{ createView.setOrReplace(isUsingOrReplace);}
Expand All @@ -5727,13 +5758,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=ColumnDefinitionItemList( ) { 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 +5832,6 @@ List<String> CreateParameter():
Token tk = null, tk2 = null;
Expression exp = null;
ColDataType colDataType;

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