Skip to content

Commit

Permalink
feat: Add support comment in create view for MySQL and MariaDb (#1913)
Browse files Browse the repository at this point in the history
* CreateView with comment for mysql

* Add test

* Fix

* Fix

* Fix

* Fix
  • Loading branch information
jxnu-liguobin authored Dec 14, 2023
1 parent 425c72e commit 4d47e0a
Show file tree
Hide file tree
Showing 10 changed files with 171 additions and 52 deletions.
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
30 changes: 24 additions & 6 deletions src/main/java/net/sf/jsqlparser/schema/Column.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,20 @@
*/
package net.sf.jsqlparser.schema;

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;

import java.util.List;

/**
* 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;

public Column() {}
Expand Down Expand Up @@ -56,19 +56,19 @@ public Column setArrayConstructor(ArrayConstructor arrayConstructor) {
* <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}.
Expand Down Expand Up @@ -114,6 +114,11 @@ public String getFullyQualifiedName(boolean aliases) {
fqn.append(columnName);
}

if (commentText != null) {
fqn.append(" COMMENT ");
fqn.append(commentText);
}

if (arrayConstructor != null) {
fqn.append(arrayConstructor);
}
Expand Down Expand Up @@ -146,4 +151,17 @@ public Column withColumnName(String columnName) {
this.setColumnName(columnName);
return this;
}

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

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

public String getCommentText() {
return commentText;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@
*/
package net.sf.jsqlparser.statement.create.view;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import net.sf.jsqlparser.expression.operators.relational.ExpressionList;
import net.sf.jsqlparser.schema.Column;
import net.sf.jsqlparser.schema.Table;
import net.sf.jsqlparser.statement.Statement;
import net.sf.jsqlparser.statement.StatementVisitor;
Expand All @@ -25,13 +23,14 @@ public class CreateView implements Statement {
private Table view;
private Select select;
private boolean orReplace = false;
private List<String> columnNames = null;
private ExpressionList<Column> 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 +64,11 @@ public void setSelect(Select select) {
this.select = select;
}

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

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

Expand Down Expand Up @@ -145,7 +144,12 @@ public String toString() {
sql.append(" AUTO REFRESH ").append(autoRefresh.name());
}
if (columnNames != null) {
sql.append(PlainSelect.getStringList(columnNames, true, true));
sql.append("(");
sql.append(columnNames);
sql.append(")");
}
if (viewCommentOptions != null) {
sql.append(PlainSelect.getStringList(viewCommentOptions, false, false));
}
sql.append(" AS ").append(select);
if (isWithReadOnly()) {
Expand Down Expand Up @@ -182,7 +186,7 @@ public CreateView withOrReplace(boolean orReplace) {
return this;
}

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

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

public CreateView addColumnNames(Collection<String> columnNames) {
List<String> collection = Optional.ofNullable(getColumnNames()).orElseGet(ArrayList::new);
collection.addAll(columnNames);
return this.withColumnNames(collection);
public void setViewCommentOptions(List<String> viewCommentOptions) {
this.viewCommentOptions = viewCommentOptions;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,13 @@ public void deParse(CreateView createView) {
buffer.append(" AUTO REFRESH ").append(createView.getAutoRefresh().name());
}
if (createView.getColumnNames() != null) {
buffer.append(PlainSelect.getStringList(createView.getColumnNames(), true, true));
buffer.append("(");
buffer.append(createView.getColumnNames());
buffer.append(")");
}
if (createView.getViewCommentOptions() != null) {
buffer.append(
PlainSelect.getStringList(createView.getViewCommentOptions(), false, false));
}
buffer.append(" AS ");

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
Loading

0 comments on commit 4d47e0a

Please sign in to comment.