Skip to content

Commit

Permalink
优化 drop column if exists的解析支持 alibaba#5797
Browse files Browse the repository at this point in the history
优化 drop column if exists的解析支持 alibaba#5797
  • Loading branch information
lizongbo committed Mar 24, 2024
1 parent 1a9f216 commit 3dd61cd
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8002,7 +8002,11 @@ public void parseAlterDrop(SQLAlterTableStatement stmt) {
} else if (lexer.token() == Token.COLUMN) {
lexer.nextToken();
SQLAlterTableDropColumnItem item = new SQLAlterTableDropColumnItem();

if (lexer.token() == Token.IF) {
lexer.nextToken();
accept(Token.EXISTS);
item.setIfExists(true);
}
SQLName name = exprParser.name();
name.setParent(item);
item.addColumn(name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1746,6 +1746,11 @@ public void parseAlterDrop(SQLAlterTableStatement stmt) {
} else if (lexer.token() == Token.COLUMN) {
lexer.nextToken();
SQLAlterTableDropColumnItem item = new SQLAlterTableDropColumnItem();
if (lexer.token() == Token.IF) {
lexer.nextToken();
accept(Token.EXISTS);
item.setIfExists(true);
}
this.exprParser.names(item.getColumns());
stmt.addItem(item);
} else if (lexer.token() == Token.PARTITION) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -590,7 +590,11 @@ public void parseAlterDrop(SQLAlterTableStatement stmt) {
} else if (lexer.token() == Token.COLUMN) {
lexer.nextToken();
SQLAlterTableDropColumnItem item = new SQLAlterTableDropColumnItem();

if (lexer.token() == Token.IF) {
lexer.nextToken();
accept(Token.EXISTS);
item.setIfExists(true);
}
SQLName name = exprParser.name();
name.setParent(item);
item.addColumn(name);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.alibaba.druid.bvt.sql.mysql.issues;

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 org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

/**
* @author lizongbo
* @see <a href="https://github.com/alibaba/druid/issues/5797">Issue来源</a>
*/
public class Issue5797 {

@Test
public void test_parse_create_table() {
for (DbType dbType : new DbType[]{
DbType.mysql,
DbType.postgresql,
DbType.oracle,
DbType.mariadb,
DbType.sqlserver,
DbType.h2,
DbType.hive,
//DbType.gaussdb,
//DbType.oscar,
// DbType.db2,
// DbType.dm, DbType.kingbase,

}) {

for (String sql : new String[]{
"-- 给课程表增加类型字段\n"
+ "alter table info_course drop column if exists course_type_id",

}) {
System.out.println(dbType + "原始的sql===" + sql);
SQLStatementParser parser = SQLParserUtils.createSQLStatementParser(sql, dbType);
List<SQLStatement> statementList = parser.parseStatementList();
com.alibaba.druid.sql.ast.statement.SQLJoinTableSource ggg;
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());
}
}
}
}

0 comments on commit 3dd61cd

Please sign in to comment.