forked from alibaba/druid
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
优化order关键字作为字段别名的解析逻辑 alibaba#5362
- Loading branch information
Showing
3 changed files
with
73 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
core/src/test/java/com/alibaba/druid/bvt/sql/mysql/issues/Issue5362.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package com.alibaba.druid.bvt.sql.mysql.issues; | ||
|
||
import java.util.List; | ||
|
||
import com.alibaba.druid.DbType; | ||
import com.alibaba.druid.sql.SQLParseAssertUtil; | ||
import com.alibaba.druid.sql.ast.SQLStatement; | ||
import com.alibaba.druid.sql.parser.SQLParserUtils; | ||
import com.alibaba.druid.sql.parser.SQLStatementParser; | ||
|
||
import org.junit.Test; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
/** | ||
* @author lizongbo | ||
* @see <a href="https://github.com/alibaba/druid/issues/5362>Issue来源</a> | ||
*/ | ||
public class Issue5362 { | ||
|
||
@Test | ||
public void test_parse_asorder() { | ||
for (DbType dbType : DbType.values()) { | ||
for (String sql : new String[]{ | ||
"select b.order + 1 as order from book b", | ||
}) { | ||
SQLParseAssertUtil.assertParseSql(sql, dbType); | ||
} | ||
} | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
core/src/test/java/com/alibaba/druid/sql/SQLParseAssertUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package com.alibaba.druid.sql; | ||
|
||
import java.util.List; | ||
|
||
import com.alibaba.druid.DbType; | ||
import com.alibaba.druid.sql.ast.SQLStatement; | ||
import com.alibaba.druid.sql.parser.SQLParserUtils; | ||
import com.alibaba.druid.sql.parser.SQLStatementParser; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
/** | ||
* @author lizongbo | ||
*/ | ||
public class SQLParseAssertUtil { | ||
|
||
/** | ||
* 将sql解析成语法树1,然后再将语法树1生成sql1,将sql1再解析成语法树2,然后再将语法树2生成sql2,比较两次解析生成的sql是否一致 | ||
* 针对有些sql虽然正常解析,但是再次生成的sql不正确的场景,加强验证,确认其幂等性 | ||
* @param sql | ||
* @param dbType | ||
*/ | ||
public static void assertParseSql(String sql, DbType dbType) { | ||
System.out.println(dbType + "原始的sql===" + sql); | ||
SQLStatementParser parser = SQLParserUtils.createSQLStatementParser(sql, dbType); | ||
List<SQLStatement> statementList = parser.parseStatementList(); | ||
String sqlGen = statementList.toString(); | ||
System.out.println(dbType + "首次解析生成的sql===" + sqlGen); | ||
StringBuilder sb = new StringBuilder(); | ||
for (SQLStatement statement : statementList) { | ||
sb.append(statement.toString()).append(";"); | ||
} | ||
sb.deleteCharAt(sb.length() - 1); | ||
parser = SQLParserUtils.createSQLStatementParser(sb.toString(), dbType); | ||
List<SQLStatement> statementListNew = parser.parseStatementList(); | ||
String sqlGenNew = statementList.toString(); | ||
System.out.println(dbType + "再次解析生成的sql===" + sqlGenNew); | ||
assertEquals(statementList.toString(), statementListNew.toString()); | ||
} | ||
|
||
} |