Compare commits

...

4 Commits

Author SHA1 Message Date
gaattc f6e9c16197
Merge 2f11505ce7 into 31177b91f6 2025-07-14 20:06:45 +08:00
wenshao 31177b91f6 sonartype central
Java CI / Test JDK ${{ matrix.java }}, ${{ matrix.os }} (11, ubuntu-latest) (push) Waiting to run Details
Java CI / Test JDK ${{ matrix.java }}, ${{ matrix.os }} (17, ubuntu-latest) (push) Waiting to run Details
Java CI / Test JDK ${{ matrix.java }}, ${{ matrix.os }} (21, ubuntu-latest) (push) Waiting to run Details
Java CI / Test JDK ${{ matrix.java }}, ${{ matrix.os }} (8, ubuntu-latest) (push) Waiting to run Details
2025-07-12 08:45:16 +08:00
gaattc 2f11505ce7 #6467 supporting PG syntax 'ALTER COLUMN ... SET DATA TYPE ...' add test
Signed-off-by: gaattc <gaattc000@gmail.com>
2025-07-05 16:45:00 +08:00
gaattc 924cd088b7 #6467 supporting PG syntax 'ALTER COLUMN ... SET DATA TYPE ...'
Signed-off-by: gaattc <gaattc000@gmail.com>
2025-07-03 15:38:46 +08:00
8 changed files with 126 additions and 12 deletions

View File

@ -4,7 +4,7 @@
<parent>
<groupId>com.alibaba</groupId>
<artifactId>druid-parent</artifactId>
<version>1.2.25</version>
<version>1.2.26-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>druid</artifactId>

View File

@ -787,6 +787,12 @@ public class PGSQLStatementParser extends SQLStatementParser {
lexer.nextToken();
accept(Token.NULL);
alterColumn.setSetNotNull(true);
} else if (lexer.identifierEquals("DATA")) {
// alter column ... set data type ...
lexer.nextToken();
accept(Token.TYPE);
SQLDataType dataType = this.exprParser.parseDataType();
alterColumn.setDataType(dataType);
} else {
accept(Token.DEFAULT);
SQLExpr defaultValue = this.exprParser.expr();

View File

@ -0,0 +1,108 @@
package com.alibaba.druid.sql.dialect.postgresql.parser;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.ast.statement.SQLAlterTableAlterColumn;
import com.alibaba.druid.sql.ast.statement.SQLAlterTableStatement;
import org.junit.Test;
import java.util.List;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
/**
* @author gaattc
* @since 1.2.24
*/
public class PGSQLStatementParserTest {
/**
* ALTER COLUMN ... SET DATA TYPE ...
*/
@Test
public void testAlterColumnSetDataType() {
String sql = "alter table if exists products alter column price set data type decimal(12,2)";
PGSQLStatementParser parser = new PGSQLStatementParser(sql);
List<SQLStatement> statementList = parser.parseStatementList();
assertEquals(1, statementList.size());
SQLAlterTableStatement stmt = (SQLAlterTableStatement) statementList.get(0);
assertEquals(SQLAlterTableStatement.class, stmt.getClass());
assertEquals("products", stmt.getTableName());
assertTrue(stmt.isIfExists());
SQLAlterTableAlterColumn alterColumnItem = (SQLAlterTableAlterColumn) stmt.getItems().get(0);
assertNotNull(alterColumnItem);
assertEquals("price", alterColumnItem.getColumn().getName().getSimpleName());
assertNotNull(alterColumnItem.getDataType());
assertEquals("decimal", alterColumnItem.getDataType().getName().toLowerCase());
assertEquals(2, alterColumnItem.getDataType().getArguments().size());
assertEquals("12", alterColumnItem.getDataType().getArguments().get(0).toString());
assertEquals("2", alterColumnItem.getDataType().getArguments().get(1).toString());
String outputSql = stmt.toString();
assertEquals("ALTER TABLE IF EXISTS products\n\tALTER COLUMN price SET DATA TYPE decimal(12, 2)", outputSql);
}
/**
* ALTER COLUMN ... SET DEFAULT ...
*/
@Test
public void testAlterColumnSetDefault() {
String sql = "ALTER TABLE products ALTER COLUMN price SET DEFAULT 7.77";
PGSQLStatementParser parser = new PGSQLStatementParser(sql);
List<SQLStatement> statementList = parser.parseStatementList();
SQLAlterTableStatement stmt = (SQLAlterTableStatement) statementList.get(0);
SQLAlterTableAlterColumn alterColumnItem = (SQLAlterTableAlterColumn) stmt.getItems().get(0);
assertEquals("price", alterColumnItem.getColumn().getName().getSimpleName());
assertNotNull(alterColumnItem.getSetDefault());
assertEquals("7.77", alterColumnItem.getSetDefault().toString());
assertNull(alterColumnItem.getDataType());
}
/**
* ALTER COLUMN ... SET NOT NULL
*/
@Test
public void testAlterColumnSetNotNull() {
String sql = "ALTER TABLE products ALTER COLUMN price SET NOT NULL";
PGSQLStatementParser parser = new PGSQLStatementParser(sql);
List<SQLStatement> statementList = parser.parseStatementList();
SQLAlterTableStatement stmt = (SQLAlterTableStatement) statementList.get(0);
SQLAlterTableAlterColumn alterColumnItem = (SQLAlterTableAlterColumn) stmt.getItems().get(0);
assertEquals("price", alterColumnItem.getColumn().getName().getSimpleName());
assertTrue(alterColumnItem.isSetNotNull());
assertNull(alterColumnItem.getDataType());
}
/**
* DROP
*/
@Test
public void testAlterColumnDropDefault() {
String sql = "ALTER TABLE products ALTER COLUMN price DROP DEFAULT";
PGSQLStatementParser parser = new PGSQLStatementParser(sql);
List<SQLStatement> statementList = parser.parseStatementList();
SQLAlterTableStatement stmt = (SQLAlterTableStatement) statementList.get(0);
SQLAlterTableAlterColumn alterColumnItem = (SQLAlterTableAlterColumn) stmt.getItems().get(0);
assertEquals("price", alterColumnItem.getColumn().getName().getSimpleName());
assertTrue(alterColumnItem.isDropDefault());
assertNull(alterColumnItem.getDataType());
}
}

View File

@ -34,7 +34,7 @@
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.2.25</version>
<version>1.2.26-SNAPSHOT</version>
</dependency>
<!-- Spring and Spring Boot dependencies -->
<dependency>

View File

@ -5,7 +5,7 @@
<parent>
<groupId>com.alibaba</groupId>
<artifactId>druid-parent</artifactId>
<version>1.2.25</version>
<version>1.2.26-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>druid-spring-boot-3-starter</artifactId>

View File

@ -5,7 +5,7 @@
<parent>
<groupId>com.alibaba</groupId>
<artifactId>druid-parent</artifactId>
<version>1.2.25</version>
<version>1.2.26-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>druid-spring-boot-starter</artifactId>

View File

@ -4,7 +4,7 @@
<parent>
<groupId>com.alibaba</groupId>
<artifactId>druid-parent</artifactId>
<version>1.2.25</version>
<version>1.2.26-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<groupId>com.alibaba</groupId>

14
pom.xml
View File

@ -6,7 +6,7 @@
<groupId>com.alibaba</groupId>
<artifactId>druid-parent</artifactId>
<version>1.2.25</version>
<version>1.2.26-SNAPSHOT</version>
<name>${project.artifactId}</name>
<description>A JDBC datasource implementation.</description>
<packaging>pom</packaging>
@ -81,12 +81,12 @@
<distributionManagement>
<repository>
<id>ossrh</id>
<url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url>
<id>central</id>
<url>https://central.sonatype.org/service/local/staging/deploy/maven2/</url>
</repository>
<snapshotRepository>
<id>ossrh</id>
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
<id>central</id>
<url>https://central.sonatype.com/repository/maven-snapshots/</url>
</snapshotRepository>
</distributionManagement>
@ -470,8 +470,8 @@
</execution>
</executions>
<configuration>
<serverId>ossrh</serverId>
<nexusUrl>https://oss.sonatype.org/</nexusUrl>
<serverId>central</serverId>
<nexusUrl>https://central.sonatype.org/</nexusUrl>
<autoReleaseAfterClose>true</autoReleaseAfterClose>
<!--
If you are deploying to Maven Central,