-
Notifications
You must be signed in to change notification settings - Fork 8.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
增加解析MySQL CREATE TABLE AS WITH SELECT 语法的能力 #5421
增加解析MySQL CREATE TABLE AS WITH SELECT 语法的能力 #5421 但是t1表名没在stat信息里,看了应该是在SchemaResolveVisitorFactory的1902行加上MySqlCreateTableStatement的适配,但是试了还是不行,得进一步分析
- Loading branch information
Showing
5 changed files
with
101 additions
and
0 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
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
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
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
76 changes: 76 additions & 0 deletions
76
core/src/test/java/com/alibaba/druid/bvt/sql/mysql/issues/Issue5421.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,76 @@ | ||
package com.alibaba.druid.bvt.sql.mysql.issues; | ||
|
||
import java.util.Map; | ||
|
||
import com.alibaba.druid.DbType; | ||
import com.alibaba.druid.sql.SQLUtils; | ||
import com.alibaba.druid.sql.ast.SQLStatement; | ||
import com.alibaba.druid.sql.parser.SQLParserUtils; | ||
import com.alibaba.druid.sql.parser.SQLStatementParser; | ||
import com.alibaba.druid.sql.visitor.SchemaStatVisitor; | ||
import com.alibaba.druid.stat.TableStat; | ||
|
||
import org.apache.commons.lang3.StringUtils; | ||
import org.junit.Test; | ||
|
||
import static org.junit.Assert.assertFalse; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
/** | ||
* 验证 create table as with语句的解析 | ||
* @author lizongbo | ||
* @see <a href="https://github.com/alibaba/druid/issues/5421">Issue来源</a> | ||
*/ | ||
public class Issue5421 { | ||
|
||
@Test | ||
public void test_create_table_with() throws Exception { | ||
for (DbType dbType : new DbType[]{DbType.mysql}) { | ||
for (String sql : new String[]{ | ||
"create table if not exists t2 as\n" | ||
+ "with X as ( select * from t1)\n" | ||
+ "select * from X;", | ||
}) { | ||
SQLStatementParser parser = SQLParserUtils.createSQLStatementParser(sql, dbType); | ||
SQLStatement statement = parser.parseStatement(); | ||
System.out.println("原始的sql===" + sql); | ||
sql=normalizeSql(sql); | ||
System.out.println("归一化的sql===" + sql); | ||
String newSql=normalizeSql(statement.toString())+";"; | ||
System.out.println("生成的sql===" + newSql); | ||
assertTrue(newSql.equalsIgnoreCase(sql)); | ||
SchemaStatVisitor visitor = SQLUtils.createSchemaStatVisitor(dbType); | ||
statement.accept(visitor); | ||
//@todo 为什么这里的表名只有t2,而没有t1,还需要进一步分析 | ||
System.out.println("getTables==" + visitor.getTables()); | ||
Map<TableStat.Name, TableStat> tableMap = visitor.getTables(); | ||
assertFalse(tableMap.isEmpty()); | ||
assertTrue(tableMap.containsKey(new TableStat.Name("t2"))); | ||
|
||
} | ||
} | ||
} | ||
|
||
static String normalizeSql(String sql) { | ||
sql = StringUtils.replace(sql, " ( ", "("); | ||
sql = StringUtils.replace(sql, "( ", "("); | ||
sql = StringUtils.replace(sql, " )", ")"); | ||
sql = StringUtils.replace(sql, "\t", " "); | ||
sql = StringUtils.replace(sql, "\n", " "); | ||
sql = StringUtils.replace(sql, "\'", "\""); | ||
sql = StringUtils.replace(sql, " ( ", "("); | ||
sql = StringUtils.replace(sql, " (", "("); | ||
sql = StringUtils.replace(sql, "( ", "("); | ||
sql = StringUtils.replace(sql, " )", ")"); | ||
sql = StringUtils.replace(sql, " )", ")"); | ||
sql = StringUtils.replace(sql, "( ", "("); | ||
sql = StringUtils.replace(sql, " ", " "); | ||
sql = StringUtils.replace(sql, " ", " "); | ||
sql = StringUtils.replace(sql, " ", " "); | ||
sql = StringUtils.replace(sql, " ", " "); | ||
sql = StringUtils.replace(sql, "( ", "("); | ||
sql = StringUtils.replace(sql, ", ", ","); | ||
sql = StringUtils.replace(sql, " ,", ","); | ||
return sql; | ||
} | ||
} |