mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
improve sqlparser
This commit is contained in:
+82
@@ -0,0 +1,82 @@
|
||||
/*
|
||||
* Copyright 2014 Alibaba.com All right reserved. This software is the
|
||||
* confidential and proprietary information of Alibaba.com ("Confidential
|
||||
* Information"). You shall not disclose such Confidential Information and shall
|
||||
* use it only in accordance with the terms of the license agreement you entered
|
||||
* into with Alibaba.com.
|
||||
*/
|
||||
package com.alibaba.druid.sql.dialect.mysql.ast.expr;
|
||||
|
||||
import com.alibaba.druid.sql.ast.SQLExpr;
|
||||
import com.alibaba.druid.sql.ast.SQLExprImpl;
|
||||
import com.alibaba.druid.sql.ast.SQLOrderingSpecification;
|
||||
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlASTVisitor;
|
||||
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
|
||||
|
||||
public class MySqlSelectGroupByExpr extends SQLExprImpl implements MySqlExpr {
|
||||
|
||||
protected SQLExpr expr;
|
||||
protected SQLOrderingSpecification type;
|
||||
|
||||
@Override
|
||||
protected void accept0(SQLASTVisitor visitor) {
|
||||
MySqlASTVisitor mysqlVisitor = (MySqlASTVisitor) visitor;
|
||||
if (mysqlVisitor.visit(this)) {
|
||||
acceptChild(visitor, this.expr);
|
||||
}
|
||||
|
||||
mysqlVisitor.endVisit(this);
|
||||
}
|
||||
|
||||
public SQLExpr getExpr() {
|
||||
return expr;
|
||||
}
|
||||
|
||||
public void setExpr(SQLExpr expr) {
|
||||
expr.setParent(this);
|
||||
this.expr = expr;
|
||||
}
|
||||
|
||||
public SQLOrderingSpecification getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public void setType(SQLOrderingSpecification type) {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj) {
|
||||
return true;
|
||||
}
|
||||
if (obj == null) {
|
||||
return false;
|
||||
}
|
||||
if (getClass() != obj.getClass()) {
|
||||
return false;
|
||||
}
|
||||
MySqlSelectGroupByExpr other = (MySqlSelectGroupByExpr) obj;
|
||||
if (expr != other.expr) {
|
||||
return false;
|
||||
}
|
||||
if (type == null) {
|
||||
if (other.type != null) {
|
||||
return false;
|
||||
}
|
||||
} else if (!type.equals(other.type)) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
result = prime * result + ((expr == null) ? 0 : expr.hashCode());
|
||||
result = prime * result + ((type == null) ? 0 : type.hashCode());
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -19,6 +19,7 @@ import com.alibaba.druid.sql.ast.SQLDataType;
|
||||
import com.alibaba.druid.sql.ast.SQLExpr;
|
||||
import com.alibaba.druid.sql.ast.SQLName;
|
||||
import com.alibaba.druid.sql.ast.SQLOrderBy;
|
||||
import com.alibaba.druid.sql.ast.SQLOrderingSpecification;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLAggregateExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLBinaryOperator;
|
||||
@@ -44,6 +45,7 @@ import com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlExtractExpr;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlIntervalExpr;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlIntervalUnit;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlMatchAgainstExpr;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlSelectGroupByExpr;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlMatchAgainstExpr.SearchModifier;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlOutFileExpr;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlUserName;
|
||||
@@ -861,4 +863,20 @@ public class MySqlExprParser extends SQLExprParser {
|
||||
}
|
||||
return aggregateExpr;
|
||||
}
|
||||
|
||||
public MySqlSelectGroupByExpr parseSelectGroupByItem() {
|
||||
MySqlSelectGroupByExpr item = new MySqlSelectGroupByExpr();
|
||||
|
||||
item.setExpr(expr());
|
||||
|
||||
if (lexer.token() == Token.ASC) {
|
||||
lexer.nextToken();
|
||||
item.setType(SQLOrderingSpecification.ASC);
|
||||
} else if (lexer.token() == Token.DESC) {
|
||||
lexer.nextToken();
|
||||
item.setType(SQLOrderingSpecification.DESC);
|
||||
}
|
||||
|
||||
return item;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -235,7 +235,7 @@ public class MySqlSelectParser extends SQLSelectParser {
|
||||
accept(Token.BY);
|
||||
|
||||
while (true) {
|
||||
groupBy.addItem(this.exprParser.expr());
|
||||
groupBy.addItem(this.getExprParser().parseSelectGroupByItem());
|
||||
if (!(lexer.token() == (Token.COMMA))) {
|
||||
break;
|
||||
}
|
||||
@@ -346,4 +346,8 @@ public class MySqlSelectParser extends SQLSelectParser {
|
||||
public Limit parseLimit() {
|
||||
return ((MySqlExprParser) this.exprParser).parseLimit();
|
||||
}
|
||||
|
||||
public MySqlExprParser getExprParser() {
|
||||
return (MySqlExprParser) exprParser;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,6 +28,7 @@ import com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlExtractExpr;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlIntervalExpr;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlMatchAgainstExpr;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlOutFileExpr;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlSelectGroupByExpr;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlUserName;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.CobarShowStatus;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlAlterTableAddColumn;
|
||||
@@ -566,4 +567,8 @@ public interface MySqlASTVisitor extends SQLASTVisitor {
|
||||
boolean visit(MySqlHintStatement x);
|
||||
|
||||
void endVisit(MySqlHintStatement x);
|
||||
|
||||
boolean visit(MySqlSelectGroupByExpr x);
|
||||
|
||||
void endVisit(MySqlSelectGroupByExpr x);
|
||||
} //
|
||||
|
||||
@@ -28,6 +28,7 @@ import com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlExtractExpr;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlIntervalExpr;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlMatchAgainstExpr;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlOutFileExpr;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlSelectGroupByExpr;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlUserName;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.CobarShowStatus;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlAlterTableAddColumn;
|
||||
@@ -1257,5 +1258,15 @@ public class MySqlASTVisitorAdapter extends SQLASTVisitorAdapter implements MySq
|
||||
public void endVisit(MySqlHintStatement x) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(MySqlSelectGroupByExpr x) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void endVisit(MySqlSelectGroupByExpr x) {
|
||||
|
||||
}
|
||||
|
||||
} //
|
||||
|
||||
@@ -50,6 +50,7 @@ import com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlExtractExpr;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlIntervalExpr;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlMatchAgainstExpr;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlOutFileExpr;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlSelectGroupByExpr;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlUserName;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.CobarShowStatus;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlAlterTableAddColumn;
|
||||
@@ -3124,5 +3125,21 @@ public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTV
|
||||
public void endVisit(MySqlHintStatement x) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(MySqlSelectGroupByExpr x) {
|
||||
x.getExpr().accept(this);
|
||||
if (x.getType() != null) {
|
||||
print(" ");
|
||||
print(x.getType().name().toUpperCase());
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void endVisit(MySqlSelectGroupByExpr x) {
|
||||
|
||||
}
|
||||
|
||||
} //
|
||||
|
||||
@@ -42,6 +42,7 @@ import com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlExtractExpr;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlIntervalExpr;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlMatchAgainstExpr;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlOutFileExpr;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlSelectGroupByExpr;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlUserName;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.CobarShowStatus;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlAlterTableAddColumn;
|
||||
@@ -1344,4 +1345,14 @@ public class MySqlSchemaStatVisitor extends SchemaStatVisitor implements MySqlAS
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(MySqlSelectGroupByExpr x) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void endVisit(MySqlSelectGroupByExpr x) {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+10
@@ -26,6 +26,7 @@ import com.alibaba.druid.sql.dialect.sqlserver.visitor.SQLServerASTVisitor;
|
||||
|
||||
public class SQLServerExecStatement extends SQLServerObjectImpl implements SQLServerStatement {
|
||||
|
||||
private SQLName returnStatus;
|
||||
private SQLName moduleName;
|
||||
private List<SQLExpr> parameters = new ArrayList<SQLExpr>();
|
||||
|
||||
@@ -43,9 +44,18 @@ public class SQLServerExecStatement extends SQLServerObjectImpl implements SQLSe
|
||||
|
||||
public void accept0(SQLServerASTVisitor visitor) {
|
||||
if (visitor.visit(this)) {
|
||||
acceptChild(visitor, this.returnStatus);
|
||||
acceptChild(visitor, this.moduleName);
|
||||
acceptChild(visitor, this.parameters);
|
||||
}
|
||||
visitor.endVisit(this);
|
||||
}
|
||||
|
||||
public SQLName getReturnStatus() {
|
||||
return returnStatus;
|
||||
}
|
||||
|
||||
public void setReturnStatus(SQLName returnStatus) {
|
||||
this.returnStatus = returnStatus;
|
||||
}
|
||||
}
|
||||
|
||||
+10
-3
@@ -76,9 +76,16 @@ public class SQLServerStatementParser extends SQLStatementParser {
|
||||
this.exprParser.exprList(execStmt.getParameters(), execStmt);
|
||||
accept(Token.RPAREN);
|
||||
} else {
|
||||
SQLName moduleName = this.exprParser.name();
|
||||
execStmt.setModuleName(moduleName);
|
||||
|
||||
SQLName sqlNameName = this.exprParser.name();
|
||||
|
||||
if (lexer.token() == Token.EQ) {
|
||||
lexer.nextToken();
|
||||
execStmt.setReturnStatus(sqlNameName);
|
||||
execStmt.setModuleName(this.exprParser.name());
|
||||
} else {
|
||||
execStmt.setModuleName(sqlNameName);
|
||||
}
|
||||
|
||||
this.exprParser.exprList(execStmt.getParameters(), execStmt);
|
||||
}
|
||||
statementList.add(execStmt);
|
||||
|
||||
+7
@@ -331,6 +331,13 @@ public class SQLServerOutputVisitor extends SQLASTOutputVisitor implements SQLSe
|
||||
@Override
|
||||
public boolean visit(SQLServerExecStatement x) {
|
||||
print("EXEC ");
|
||||
|
||||
SQLName returnStatus = x.getReturnStatus();
|
||||
if (returnStatus != null) {
|
||||
returnStatus.accept(this);
|
||||
print(" = ");
|
||||
}
|
||||
|
||||
SQLName moduleName = x.getModuleName();
|
||||
if (moduleName != null) {
|
||||
moduleName.accept(this);
|
||||
|
||||
-1
@@ -21,7 +21,6 @@ import com.alibaba.druid.sql.ast.SQLExpr;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLColumnDefinition;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLInsertStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock;
|
||||
import com.alibaba.druid.sql.dialect.oracle.ast.clause.OracleParameter;
|
||||
import com.alibaba.druid.sql.dialect.sqlserver.ast.SQLServerColumnDefinition;
|
||||
import com.alibaba.druid.sql.dialect.sqlserver.ast.SQLServerColumnDefinition.Identity;
|
||||
import com.alibaba.druid.sql.dialect.sqlserver.ast.SQLServerDeclareItem;
|
||||
|
||||
@@ -1989,7 +1989,7 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Printab
|
||||
|
||||
public void endVisit(SQLBooleanExpr x) {
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean visit(SQLUnionQueryTableSource x) {
|
||||
print("(");
|
||||
@@ -2006,4 +2006,5 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Printab
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -466,4 +466,5 @@ public class MySqlWallVisitor extends MySqlASTVisitorAdapter implements WallVisi
|
||||
public boolean visit(SQLCreateTriggerStatement x) {
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -94,6 +94,7 @@ import com.alibaba.druid.sql.ast.statement.SQLUpdateSetItem;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLUpdateStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLUseStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlOutFileExpr;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlSelectGroupByExpr;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlCommitStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlDeleteStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlDescribeStatement;
|
||||
@@ -1167,6 +1168,8 @@ public class WallVisitorUtils {
|
||||
return true;
|
||||
} else if (parent instanceof Limit) {
|
||||
return true;
|
||||
} else if (parent instanceof MySqlSelectGroupByExpr) {
|
||||
return true;
|
||||
}
|
||||
|
||||
obj = parent;
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
/*
|
||||
* Copyright 1999-2011 Alibaba Group Holding Ltd.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.alibaba.druid.bvt.sql.mysql;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Assert;
|
||||
|
||||
import com.alibaba.druid.sql.MysqlTest;
|
||||
import com.alibaba.druid.sql.ast.SQLStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser;
|
||||
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor;
|
||||
|
||||
public class MySqlSelectTest_20 extends MysqlTest {
|
||||
|
||||
public void test_0() throws Exception {
|
||||
String sql = "select bsvariety,max(bsh) as bsh,min(bsl) as bsl from exchange_market_info where bsdate>date_sub(now(),interval 1 day) group by bsvariety desc;";
|
||||
|
||||
|
||||
MySqlStatementParser parser = new MySqlStatementParser(sql);
|
||||
List<SQLStatement> statementList = parser.parseStatementList();
|
||||
SQLStatement statemen = statementList.get(0);
|
||||
print(statementList);
|
||||
|
||||
Assert.assertEquals(1, statementList.size());
|
||||
|
||||
MySqlSchemaStatVisitor visitor = new MySqlSchemaStatVisitor();
|
||||
statemen.accept(visitor);
|
||||
|
||||
System.out.println("Tables : " + visitor.getTables());
|
||||
System.out.println("fields : " + visitor.getColumns());
|
||||
System.out.println("coditions : " + visitor.getConditions());
|
||||
System.out.println("orderBy : " + visitor.getOrderByColumns());
|
||||
|
||||
Assert.assertEquals(1, visitor.getTables().size());
|
||||
Assert.assertEquals(4, visitor.getColumns().size());
|
||||
Assert.assertEquals(1, visitor.getConditions().size());
|
||||
Assert.assertEquals(0, visitor.getOrderByColumns().size());
|
||||
}
|
||||
}
|
||||
@@ -28,7 +28,7 @@ import com.alibaba.druid.util.JdbcConstants;
|
||||
|
||||
public class SQLServerExec_Test extends TestCase {
|
||||
|
||||
public void test_alter_first() throws Exception {
|
||||
public void test_0() throws Exception {
|
||||
String sql = "EXEC sp_rename 'rules.temp_plugin_config_key', 'plugin_config_key'";
|
||||
SQLServerStatementParser parser = new SQLServerStatementParser(sql);
|
||||
SQLStatement stmt = parser.parseStatementList().get(0);
|
||||
@@ -48,5 +48,27 @@ public class SQLServerExec_Test extends TestCase {
|
||||
Assert.assertEquals(0, visitor.getTables().size());
|
||||
Assert.assertEquals(0, visitor.getColumns().size());
|
||||
}
|
||||
|
||||
|
||||
public void test_2() throws Exception {
|
||||
String sql = "EXEC @returnstatus = dbo.ufnGetSalesOrderStatusText @Status = 2";
|
||||
SQLServerStatementParser parser = new SQLServerStatementParser(sql);
|
||||
SQLStatement stmt = parser.parseStatementList().get(0);
|
||||
parser.match(Token.EOF);
|
||||
|
||||
SQLServerSchemaStatVisitor visitor = new SQLServerSchemaStatVisitor();
|
||||
stmt.accept(visitor);
|
||||
|
||||
System.out.println("Tables : " + visitor.getTables());
|
||||
System.out.println("fields : " + visitor.getColumns());
|
||||
System.out.println("coditions : " + visitor.getConditions());
|
||||
System.out.println("orderBy : " + visitor.getOrderByColumns());
|
||||
|
||||
String output = SQLUtils.toSQLString(stmt, JdbcConstants.SQL_SERVER);
|
||||
Assert.assertEquals("EXEC @returnstatus = dbo.ufnGetSalesOrderStatusText @Status = 2", output);
|
||||
|
||||
Assert.assertEquals(0, visitor.getTables().size());
|
||||
Assert.assertEquals(0, visitor.getColumns().size());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user