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
Prev Previous commit
Fix
  • Loading branch information
jxnu-liguobin committed Dec 14, 2023
commit 72f6d86473d110545337c15f4c1d24bf7dd509ac
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,13 +9,14 @@
*/
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 @@ -64,18 +65,10 @@ 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 @@ -9,15 +9,12 @@
*/
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;
import net.sf.jsqlparser.statement.create.table.ColumnDefinition;
import net.sf.jsqlparser.statement.select.PlainSelect;
import net.sf.jsqlparser.statement.select.Select;

Expand All @@ -26,7 +23,7 @@ public class CreateView implements Statement {
private Table view;
private Select select;
private boolean orReplace = false;
private List<ColumnDefinition> columnNames = null;
private ExpressionList<Column> columnNames = null;
private boolean materialized = false;
private ForceOption force = ForceOption.NONE;
private TemporaryOption temp = TemporaryOption.NONE;
Expand Down Expand Up @@ -67,11 +64,11 @@ public void setSelect(Select select) {
this.select = select;
}

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

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

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

public CreateView withColumnNames(List<ColumnDefinition> columnNames) {
public CreateView withColumnNames(ExpressionList<Column> columnNames) {
this.setColumnNames(columnNames);
return this;
}
Expand All @@ -207,20 +206,6 @@ public CreateView withWithReadOnly(boolean withReadOnly) {
return this;
}

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<ColumnDefinition> columnNames) {
List<ColumnDefinition> collection =
Optional.ofNullable(getColumnNames()).orElseGet(ArrayList::new);
collection.addAll(columnNames);
return this.withColumnNames(collection);
}

public List<String> getViewCommentOptions() {
return viewCommentOptions;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@ 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(
Expand Down
36 changes: 12 additions & 24 deletions src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt
Original file line number Diff line number Diff line change
Expand Up @@ -1767,16 +1767,18 @@ Column Column() #Column :
{
List<String> data = new ArrayList<String>();
ArrayConstructor arrayConstructor = null;
Token tk = null;
}
{
data = RelObjectNameList()

[ <K_COMMENT> tk=<S_CHAR_LITERAL> ]
// @todo: we better should return a SEQUENCE instead of a COLUMN
[ "." <K_NEXTVAL> { data.add("nextval"); } ]

[ LOOKAHEAD(2) arrayConstructor = ArrayConstructor(false) ]
{
Column col = new Column(data);
if (tk != null) { col.withCommentText(tk.image); }
if (arrayConstructor!=null) {
col.setArrayConstructor(arrayConstructor);
}
Expand Down Expand Up @@ -5705,42 +5707,28 @@ Analyze Analyze():
}
}

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

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


CreateView CreateView(boolean isUsingOrReplace):
{
CreateView createView = new CreateView();
Table view = null;
Select select = null;
List<ColumnDefinition> columnNames = null;
ExpressionList<Column> columnNames = null;
Token tk = null;
List<String> commentTokens = null;
}
Expand All @@ -5758,7 +5746,7 @@ 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=ColumnDefinitionItemList( ) { createView.setColumnNames(columnNames); } ]
[ columnNames=ColumnWithCommentList( ) { createView.setColumnNames(columnNames); } ]
[ commentTokens=CreateViewTailComment( ) { createView.setViewCommentOptions(commentTokens); } ]
<K_AS>
select=Select( ) { createView.setSelect(select); }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
*/
package net.sf.jsqlparser.statement.create;

import static net.sf.jsqlparser.test.TestUtils.*;
import static net.sf.jsqlparser.test.TestUtils.assertSqlCanBeParsedAndDeparsed;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.jupiter.api.Assertions.assertEquals;
Expand Down