Skip to content

Commit

Permalink
improved sql parser
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed Nov 27, 2024
1 parent 82adc39 commit b515c48
Show file tree
Hide file tree
Showing 30 changed files with 648 additions and 386 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,11 @@ public SQLExpr getExpr() {
return expr;
}

public void setExpr(SQLExpr expr) {
this.expr = expr;
public void setExpr(SQLExpr x) {
if (x != null) {
x.setParent(this);
}
this.expr = x;
}

public List<SQLExpr> getValues() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,9 +215,8 @@ public boolean equalsIgoreOrder(SQLBinaryOpExpr other) {
&& Utils.equals(this.right, other.left));
}

public SQLBinaryOpExpr clone() {
SQLBinaryOpExpr x = new SQLBinaryOpExpr();

protected void cloneTo(SQLBinaryOpExpr x) {
super.cloneTo(x);
if (left != null) {
x.setLeft(left.clone());
}
Expand All @@ -232,6 +231,11 @@ public SQLBinaryOpExpr clone() {
x.hint = hint.clone();
}
x.setParenthesized(parenthesized);
}

public SQLBinaryOpExpr clone() {
SQLBinaryOpExpr x = new SQLBinaryOpExpr();
cloneTo(x);
return x;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,10 @@ public void setResolvedColumn(SQLColumnDefinition resolvedColumn) {
this.resolvedColumn = resolvedColumn;
}

public void setResolvedColumn(SQLObject resolvedColumn) {
this.resolvedColumn = resolvedColumn;
}

public void setResolvedColumn(SQLSelectItem selectItem) {
this.resolvedColumn = selectItem;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,7 @@ public SQLMethodInvokeExpr clone() {
}

public void cloneTo(SQLMethodInvokeExpr x) {
super.cloneTo(x);
x.methodName = methodName;

if (owner != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public class SQLAlterTableAddColumn extends SQLObjectImpl implements SQLAlterTab
private boolean first;
private Boolean restrict;
private boolean cascade;
private boolean ifNotExists;

public SQLAlterTableAddColumn() {
}
Expand All @@ -43,6 +44,14 @@ protected void accept0(SQLASTVisitor visitor) {
visitor.endVisit(this);
}

public boolean isIfNotExists() {
return ifNotExists;
}

public void setIfNotExists(boolean ifNotExists) {
this.ifNotExists = ifNotExists;
}

public List<SQLColumnDefinition> getColumns() {
return columns;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,17 @@
import com.alibaba.druid.sql.ast.SQLExpr;
import com.alibaba.druid.sql.ast.SQLName;
import com.alibaba.druid.sql.ast.SQLStatementImpl;
import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr;
import com.alibaba.druid.sql.visitor.SQLASTVisitor;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class SQLDropTableStatement extends SQLStatementImpl implements SQLDropStatement {
private List<SQLCommentHint> hints;

protected List<SQLExprTableSource> tableSources = new ArrayList<SQLExprTableSource>();
protected List<SQLExprTableSource> tableSources = new ArrayList<>();
protected boolean purge;
protected boolean cascade;
protected boolean restrict;
Expand Down Expand Up @@ -77,6 +79,10 @@ public void setName(SQLName name) {
this.addTableSource(new SQLExprTableSource(name));
}

public void addTableSource(String name) {
this.addTableSource(new SQLIdentifierExpr(name));
}

public void addTableSource(SQLName name) {
this.addTableSource(new SQLExprTableSource(name));
}
Expand Down Expand Up @@ -185,4 +191,64 @@ public SQLName getName() {
}
return null;
}

protected void cloneTo(SQLDropTableStatement x) {
if (hints != null) {
x.hints = new ArrayList<>(hints);
}
tableSources.forEach(e -> x.addTableSource(e.clone()));
x.purge = purge;
x.cascade = cascade;
x.restrict = restrict;
x.ifExists = ifExists;
x.temporary = temporary;
x.external = external;
x.isDropPartition = isDropPartition;
if (where != null) {
x.where = where.clone();
}
}

public SQLDropTableStatement clone() {
SQLDropTableStatement x = new SQLDropTableStatement();
cloneTo(x);
return x;
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}

SQLDropTableStatement that = (SQLDropTableStatement) o;
return purge == that.purge
&& cascade == that.cascade
&& restrict == that.restrict
&& ifExists == that.ifExists
&& temporary == that.temporary
&& external == that.external
&& isDropPartition == that.isDropPartition
&& Objects.equals(hints, that.hints)
&& Objects.equals(tableSources, that.tableSources)
&& Objects.equals(where, that.where);
}

@Override
public int hashCode() {
int result = Objects.hashCode(hints);
result = 31 * result + Objects.hashCode(tableSources);
result = 31 * result + Boolean.hashCode(purge);
result = 31 * result + Boolean.hashCode(cascade);
result = 31 * result + Boolean.hashCode(restrict);
result = 31 * result + Boolean.hashCode(ifExists);
result = 31 * result + Boolean.hashCode(temporary);
result = 31 * result + Boolean.hashCode(external);
result = 31 * result + Boolean.hashCode(isDropPartition);
result = 31 * result + Objects.hashCode(where);
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ public SQLColumnDefinition findColumn(long columnNameHash) {
return null;
}

public SQLObject resolveColum(long columnNameHash) {
public SQLObject resolveColumn(long columnNameHash) {
if (schemaObject != null) {
SQLStatement stmt = schemaObject.getStatement();
if (stmt instanceof SQLCreateTableStatement) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,7 @@ public SQLColumnDefinition findColumn(long columnNameHash) {
return null;
}

public SQLObject resolveColum(long columnNameHash) {
public SQLObject resolveColumn(long columnNameHash) {
if (left != null) {
SQLObject column = left.resolveColum(columnNameHash);
if (column != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,10 @@ public DbType getDbType() {
public SQLSelect clone() {
SQLSelect x = new SQLSelect();

x.withSubQuery = this.withSubQuery;
if (withSubQuery != null) {
x.withSubQuery = withSubQuery.clone();
}

if (query != null) {
x.setQuery(query.clone());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,16 @@ public boolean isDistinct() {
return this.distionOption == SQLSetQuantifier.DISTINCT;
}

public void setDistinct(boolean distinct) {
if (distinct) {
this.distionOption = SQLSetQuantifier.DISTINCT;
} else {
if (this.distionOption == SQLSetQuantifier.DISTINCT) {
this.distionOption = 0;
}
}
}

public List<SQLSelectItem> getSelectList() {
return this.selectList;
}
Expand Down Expand Up @@ -761,6 +771,10 @@ protected void acceptChild(SQLASTVisitor visitor) {
this.groupBy.accept(visitor);
}

if (this.qualify != null) {
this.qualify.accept(visitor);
}

if (this.orderBy != null) {
this.orderBy.accept(visitor);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package com.alibaba.druid.sql.ast.statement;

import com.alibaba.druid.sql.ast.SQLName;
import com.alibaba.druid.sql.ast.SQLObject;
import com.alibaba.druid.sql.visitor.SQLASTVisitor;

import java.util.ArrayList;
Expand Down Expand Up @@ -73,6 +74,16 @@ protected void accept0(SQLASTVisitor visitor) {
visitor.endVisit(this);
}

public SQLObject resolveColumn(long columnNameHash) {
if (select != null) {
SQLSelectQueryBlock queryBlock = select.getQueryBlock();
if (queryBlock != null) {
return queryBlock.findSelectItem(columnNameHash);
}
}
return null;
}

public void cloneTo(SQLSubqueryTableSource x) {
x.alias = alias;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,12 @@ public interface SQLTableSource extends SQLObject {

SQLColumnDefinition findColumn(long columnNameHash);

SQLObject resolveColum(long columnNameHash);
@Deprecated
default SQLObject resolveColum(long columnNameHash) {
return resolveColumn(columnNameHash);
}

SQLObject resolveColumn(long columnNameHash);

SQLTableSource findTableSourceWithColumn(String columnName);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ public SQLColumnDefinition findColumn(long columnNameHash) {
return null;
}

public SQLObject resolveColum(long columnNameHash) {
public SQLObject resolveColumn(long columnNameHash) {
return findColumn(columnNameHash);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,20 @@ public class SQLWithSubqueryClause extends SQLObjectImpl {
private Boolean recursive;
private final List<Entry> entries = new ArrayList<Entry>();

public SQLWithSubqueryClause clone() {
SQLWithSubqueryClause x = new SQLWithSubqueryClause();
protected void cloneTo(SQLWithSubqueryClause x) {
super.cloneTo(x);
x.recursive = recursive;

for (Entry entry : entries) {
Entry entry2 = entry.clone();
entry2.setParent(x);
x.entries.add(entry2);
}
}

public SQLWithSubqueryClause clone() {
SQLWithSubqueryClause x = new SQLWithSubqueryClause();
cloneTo(x);
return x;
}

Expand Down Expand Up @@ -110,6 +114,16 @@ public void cloneTo(Entry x) {
x.prefixAlias = prefixAlias;
}

public SQLObject resolveColumn(long columnNameHash) {
if (subQuery != null) {
SQLSelectQueryBlock queryBlock = subQuery.getQueryBlock();
if (queryBlock != null) {
return queryBlock.findSelectItem(columnNameHash);
}
}
return null;
}

@Override
public boolean replace(SQLExpr expr, SQLExpr target) {
if (flashback == expr) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -626,14 +626,16 @@ public void parseCreateTableSupportSchema() {
}

@Override
protected boolean parseAlterTableAddColumnBefore() {
if (lexer.identifierEquals("COLUMNS")) {
lexer.nextToken();
if (lexer.token() == LPAREN) {
lexer.nextToken();
return true;
}
} else if (lexer.token() == LPAREN) {
protected boolean parseAlterTableAddColumnBefore(SQLAlterTableAddColumn x) {
lexer.nextIfIdentifier("COLUMNS");

if (lexer.nextIf(Token.IF)) {
accept(Token.NOT);
accept(Token.EXISTS);
x.setIfNotExists(true);
}

if (lexer.token() == LPAREN) {
lexer.nextToken();
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1080,4 +1080,32 @@ public boolean visit(SQLCurrentTimeExpr x) {
print0("()");
return false;
}

@Override
public boolean visit(SQLDropDatabaseStatement x) {
print0(ucase ? "DROP " : "drop ");

if (x.isPhysical()) {
print0(ucase ? "PHYSICAL " : "physical ");
}

print0(ucase ? "SCHEMA " : "schema ");

if (x.isIfExists()) {
print0(ucase ? "IF EXISTS " : "if exists ");
}

x.getDatabase().accept(this);

final Boolean restrict = x.getRestrict();
if (restrict != null && restrict.booleanValue()) {
print0(ucase ? " RESTRICT" : " restrict");
}

if (x.isCascade()) {
print0(ucase ? " CASCADE" : " cascade");
}

return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1116,7 +1116,7 @@ protected void parseSelectList(SQLSelectQueryBlock queryBlock) {
//https://github.com/alibaba/druid/issues/5708
if (lexer.hasComment()
&& lexer.isKeepComments()
&& lexer.getComments().size() == 1
&& !lexer.getComments().isEmpty()
&& lexer.getComments().get(0).startsWith("--")) {
selectItem.addAfterComment(lexer.readAndResetComments());
}
Expand Down
Loading

0 comments on commit b515c48

Please sign in to comment.