Skip to content

Commit

Permalink
1. 支持 pg crate/alter/drop schema 语句中同时指定库。
Browse files Browse the repository at this point in the history
  • Loading branch information
zycgit authored and wenshao committed Dec 12, 2024
1 parent 23bfb31 commit 1bc2b19
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,23 @@
*/
package com.alibaba.druid.sql.dialect.postgresql.ast.stmt;

import com.alibaba.druid.sql.ast.SQLName;
import com.alibaba.druid.sql.ast.SQLStatementImpl;
import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr;
import com.alibaba.druid.sql.ast.statement.SQLAlterStatement;
import com.alibaba.druid.sql.dialect.postgresql.visitor.PGASTVisitor;
import com.alibaba.druid.sql.visitor.SQLASTVisitor;

public class PGAlterSchemaStatement extends SQLStatementImpl implements PGSQLStatement, SQLAlterStatement {
private SQLIdentifierExpr schemaName;
private SQLName schemaName;
private SQLIdentifierExpr newName;
private SQLIdentifierExpr newOwner;

public SQLIdentifierExpr getSchemaName() {
public SQLName getSchemaName() {
return schemaName;
}

public void setSchemaName(SQLIdentifierExpr schemaName) {
public void setSchemaName(SQLName schemaName) {
this.schemaName = schemaName;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package com.alibaba.druid.sql.dialect.postgresql.ast.stmt;

import com.alibaba.druid.sql.ast.SQLName;
import com.alibaba.druid.sql.ast.SQLStatementImpl;
import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr;
import com.alibaba.druid.sql.ast.statement.SQLCreateStatement;
Expand All @@ -25,17 +26,17 @@
import java.util.List;

public class PGCreateSchemaStatement extends SQLStatementImpl implements PGSQLStatement, SQLCreateStatement {
private SQLIdentifierExpr schemaName;
private SQLName schemaName;
private SQLIdentifierExpr userName;
private boolean ifNotExists;
private boolean authorization;
private List<SQLCreateStatement> createStatements = new ArrayList<>();

public SQLIdentifierExpr getSchemaName() {
public SQLName getSchemaName() {
return schemaName;
}

public void setSchemaName(SQLIdentifierExpr schemaName) {
public void setSchemaName(SQLName schemaName) {
this.schemaName = schemaName;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
*/
package com.alibaba.druid.sql.dialect.postgresql.ast.stmt;

import com.alibaba.druid.sql.ast.SQLName;
import com.alibaba.druid.sql.ast.SQLStatementImpl;
import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr;
import com.alibaba.druid.sql.ast.statement.SQLDropStatement;
import com.alibaba.druid.sql.dialect.postgresql.visitor.PGASTVisitor;
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
Expand All @@ -25,25 +25,25 @@
import java.util.List;

public class PGDropSchemaStatement extends SQLStatementImpl implements PGSQLStatement, SQLDropStatement {
private SQLIdentifierExpr schemaName;
private List<SQLIdentifierExpr> multipleNames = new ArrayList<>();
private SQLName schemaName;
private List<SQLName> multipleNames = new ArrayList<>();
private boolean ifExists;
private boolean cascade;
private boolean restrict;

public SQLIdentifierExpr getSchemaName() {
public SQLName getSchemaName() {
return this.schemaName;
}

public void setSchemaName(SQLIdentifierExpr schemaName) {
public void setSchemaName(SQLName schemaName) {
this.schemaName = schemaName;
}

public List<SQLIdentifierExpr> getMultipleNames() {
public List<SQLName> getMultipleNames() {
return this.multipleNames;
}

public void setMultipleNames(List<SQLIdentifierExpr> multipleNames) {
public void setMultipleNames(List<SQLName> multipleNames) {
this.multipleNames = multipleNames;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -299,8 +299,7 @@ public PGCreateSchemaStatement parseCreateSchema() {
SQLIdentifierExpr userName = (SQLIdentifierExpr) this.exprParser.expr();
stmt.setUserName(userName);
} else {
SQLIdentifierExpr schemaName = (SQLIdentifierExpr) this.exprParser.expr();
stmt.setSchemaName(schemaName);
stmt.setSchemaName(this.exprParser.name());

if (lexer.identifierEquals("AUTHORIZATION")) {
lexer.nextToken();
Expand Down Expand Up @@ -350,7 +349,7 @@ protected SQLStatement alterSchema() {
accept(Token.SCHEMA);

PGAlterSchemaStatement stmt = new PGAlterSchemaStatement();
stmt.setSchemaName(this.exprParser.identifier());
stmt.setSchemaName(this.exprParser.name());

if (lexer.identifierEquals(FnvHash.Constants.RENAME)) {
lexer.nextToken();
Expand Down Expand Up @@ -416,13 +415,13 @@ public PGDropSchemaStatement parseDropSchema(boolean physical) {
stmt.setIfExists(true);
}

SQLIdentifierExpr name = this.exprParser.identifier();
SQLName name = this.exprParser.name();
stmt.setSchemaName(name);
stmt.getMultipleNames().add(name);

while (lexer.token() == COMMA) {
lexer.nextToken();
stmt.getMultipleNames().add(this.exprParser.identifier());
stmt.getMultipleNames().add(this.exprParser.name());
}

if (lexer.token() == Token.CASCADE) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -729,7 +729,7 @@ public boolean visit(PGDropSchemaStatement x) {
printUcase("IF EXISTS ");
}

List<SQLIdentifierExpr> multipleName = x.getMultipleNames();
List<SQLName> multipleName = x.getMultipleNames();
for (int i = 0; i < multipleName.size(); i++) {
if (i > 0) {
printUcase(", ");
Expand Down
26 changes: 25 additions & 1 deletion core/src/test/resources/bvt/parser/postgresql/20.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
create schema abc
--------------------
CREATE SCHEMA abc
------------------------------------------------------------------------------------------------------------------------
create schema abc.abc
--------------------
CREATE SCHEMA abc.abc
------------------------------------------------------------------------------------------------------------------------
create schema abc create table test_user (id int primary key,name varchar(50));
--------------------
CREATE SCHEMA abc CREATE TABLE test_user (
Expand Down Expand Up @@ -27,4 +35,20 @@ DROP SCHEMA IF EXISTS abc CASCADE
------------------------------------------------------------------------------------------------------------------------
drop schema abc, ccc ,aac
--------------------
DROP SCHEMA abc, ccc, aac
DROP SCHEMA abc, ccc, aac
------------------------------------------------------------------------------------------------------------------------
create schema abc
--------------------
CREATE SCHEMA abc
------------------------------------------------------------------------------------------------------------------------
alter schema test.ABC_TEST rename to abc_test
--------------------
ALTER SCHEMA test.ABC_TEST RENAME TO abc_test
------------------------------------------------------------------------------------------------------------------------
drop schema abc.test
--------------------
DROP SCHEMA abc.test
------------------------------------------------------------------------------------------------------------------------
drop schema test.abc, test2.ccc ,test3.aac
--------------------
DROP SCHEMA test.abc, test2.ccc, test3.aac

0 comments on commit 1bc2b19

Please sign in to comment.