Skip to content

Commit

Permalink
优化postgreSQL解析ALTER COLUMN alibaba#5909
Browse files Browse the repository at this point in the history
优化postgreSQL解析ALTER COLUMN alibaba#5909
  • Loading branch information
lizongbo committed May 13, 2024
1 parent 94b7d8e commit a5d0bef
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public SQLColumnDefinition getColumn() {

public void setColumn(SQLColumnDefinition column) {
this.column = column;
column.setParent(this);
}

public boolean isSetNotNull() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -576,7 +576,7 @@ protected SQLAlterTableAlterColumn parseAlterColumn() {
}

SQLColumnDefinition column = this.exprParser.parseColumn();

column.setDbType(dbType);
SQLAlterTableAlterColumn alterColumn = new SQLAlterTableAlterColumn();
alterColumn.setColumn(column);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4793,6 +4793,8 @@ public SQLColumnDefinition parseColumn() {

public SQLColumnDefinition parseColumn(SQLObject parent) {
SQLColumnDefinition column = createColumnDefinition();
column.setParent(parent);
column.setDbType(dbType);
if (Token.IF == lexer.token) {
lexer.nextToken();
accept(Token.NOT);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3342,10 +3342,13 @@ public boolean visit(SQLColumnDefinition x) {
print0(ucase ? "IF NOT EXISTS " : "if not exists ");
}
x.getName().accept(this);

final SQLDataType dataType = x.getDataType();
if (dataType != null) {
print(' ');
if (JdbcUtils.isPgsqlDbType(dbType) && x.getParent() instanceof SQLAlterTableAlterColumn) {
print0(ucase ? " TYPE " : " type ");
} else {
print(' ');
}
dataType.accept(this);
}

Expand Down Expand Up @@ -5553,7 +5556,6 @@ public boolean visit(SQLAlterTableAlterColumn x) {
originColumn.accept(this);
print(' ');
}

x.getColumn().accept(this);

if (x.isSetNotNull()) { // postgresql
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public void test_0() throws Exception {
List<SQLStatement> statementList = parser.parseStatementList();
SQLStatement stmt = statementList.get(0);
assertEquals("ALTER TABLE organizations\n" +
"\tALTER COLUMN guarded BOOLEAN,\n" +
"\tALTER COLUMN guarded TYPE BOOLEAN,\n" +
"\tALTER COLUMN guarded DROP NOT NULL", stmt.toString());

Assert.assertEquals(1, statementList.size());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.alibaba.druid.bvt.sql.postgresql.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/5909>Issue来源</a>
* @see <a href="https://www.postgresql.org/docs/current/sql-altertable.html">...</a>
*/
public class Issue5909 {

@Test
public void test_parse_alter_column() {
for (DbType dbType : new DbType[]{DbType.postgresql, DbType.edb}) {
for (String sql : new String[]{
"ALTER TABLE mobino.alarm_count_day ALTER COLUMN warn_day TYPE character varying(100);",
}) {
SQLStatementParser parser = SQLParserUtils.createSQLStatementParser(sql, dbType);
List<SQLStatement> statementList = parser.parseStatementList();
assertEquals(1, statementList.size());
SQLParseAssertUtil.assertParseSql(sql, dbType);
}
}
}
}

0 comments on commit a5d0bef

Please sign in to comment.