Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support db2/sqlserver some ddl sql #6343

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@
*/
package com.alibaba.druid.sql.dialect.db2.ast;

import com.alibaba.druid.sql.ast.SQLDbTypedObject;
import com.alibaba.druid.sql.ast.SQLObject;
import com.alibaba.druid.sql.dialect.db2.visitor.DB2ASTVisitor;
import com.alibaba.druid.util.FnvHash;

public interface DB2Object extends SQLObject {
public interface DB2Object extends SQLObject, SQLDbTypedObject {
void accept0(DB2ASTVisitor visitor);

interface Constants {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Copyright 1999-2017 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.druid.sql.dialect.db2.ast.stmt;

public enum AsSelectWith {
WITH_DATA
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright 1999-2017 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.druid.sql.dialect.db2.ast.stmt;

public enum ConstraintType {
Unique,
ForeignKey,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Copyright 1999-2017 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.druid.sql.dialect.db2.ast.stmt;

import com.alibaba.druid.DbType;
import com.alibaba.druid.sql.ast.statement.SQLAlterTableDropConstraint;
import com.alibaba.druid.sql.ast.statement.SQLAlterTableItem;
import com.alibaba.druid.sql.dialect.db2.ast.DB2Object;
import com.alibaba.druid.sql.dialect.db2.visitor.DB2ASTVisitor;
import com.alibaba.druid.sql.visitor.SQLASTVisitor;

public class DB2AlterTableDropConstraint extends SQLAlterTableDropConstraint implements SQLAlterTableItem, DB2Object {
protected DbType dbType;
private ConstraintType constraintType;

public DB2AlterTableDropConstraint() {
this.dbType = DbType.db2;
}

public DB2AlterTableDropConstraint(DbType dbType) {
this.dbType = dbType;
}

public ConstraintType getConstraintType() { return constraintType; }

public void setConstraintType(ConstraintType constraintType) { this.constraintType = constraintType; }

@Override
protected void accept0(SQLASTVisitor v) {
if (v instanceof DB2ASTVisitor) {
this.accept0((DB2ASTVisitor) v);
} else {
throw new UnsupportedOperationException(this.getClass().getName());
}
}

@Override
public void accept0(DB2ASTVisitor visitor) {
if (visitor.visit(this)) {
acceptChild(visitor, this.getConstraintName());
}
visitor.endVisit(this);
}

@Override
public DbType getDbType() { return this.dbType; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package com.alibaba.druid.sql.dialect.db2.ast.stmt;

import com.alibaba.druid.DbType;
import com.alibaba.druid.sql.ast.SQLName;
import com.alibaba.druid.sql.ast.SQLStatementImpl;
import com.alibaba.druid.sql.ast.statement.SQLCreateStatement;
Expand All @@ -29,6 +30,14 @@ public class DB2CreateSchemaStatement extends SQLStatementImpl implements DB2Sta
private SQLName schemaName;
private List<SQLCreateStatement> createStatements = new ArrayList<>();

public DB2CreateSchemaStatement() {
this.dbType = DbType.db2;
}

public DB2CreateSchemaStatement(DbType dbType) {
this.dbType = dbType;
}

public SQLName getName() {
return this.getSchemaName();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package com.alibaba.druid.sql.dialect.db2.ast.stmt;

import com.alibaba.druid.DbType;
import com.alibaba.druid.sql.ast.SQLName;
import com.alibaba.druid.sql.ast.statement.SQLCreateTableStatement;
import com.alibaba.druid.sql.dialect.db2.ast.DB2Statement;
Expand All @@ -25,10 +26,27 @@ public class DB2CreateTableStatement extends SQLCreateTableStatement implements
private boolean dataCaptureNone;
private boolean dataCaptureChanges;

private AsSelectWith selectWith;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这种为什么不用一个boolean解决?

Copy link
Contributor Author

@zycgit zycgit Feb 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

因为还有其它方式,本次只支持了其中一种。如:
create table new_table_name1 as (select * from table1) definition only;
create table new_table_name2 as (select * from table1) with data;
create table new_table_name3 as (select * from table1) WITH no data;

image

protected SQLName database;
protected SQLName validproc;
protected SQLName indexIn;

public DB2CreateTableStatement() {
this.dbType = DbType.db2;
}

public DB2CreateTableStatement(DbType dbType) {
this.dbType = dbType;
}

public AsSelectWith getSelectWith() {
return this.selectWith;
}

public void setSelectWith(AsSelectWith selectWith) {
this.selectWith = selectWith;
}

public boolean isDataCaptureNone() {
return dataCaptureNone;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package com.alibaba.druid.sql.dialect.db2.ast.stmt;

import com.alibaba.druid.DbType;
import com.alibaba.druid.sql.ast.SQLName;
import com.alibaba.druid.sql.ast.SQLStatementImpl;
import com.alibaba.druid.sql.ast.statement.SQLDropStatement;
Expand All @@ -28,6 +29,14 @@ public class DB2DropSchemaStatement extends SQLStatementImpl implements DB2State
private boolean restrict;
private boolean cascade;

public DB2DropSchemaStatement() {
this.dbType = DbType.db2;
}

public DB2DropSchemaStatement(DbType dbType) {
this.dbType = dbType;
}

public SQLName getName() {
return this.getSchemaName();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Copyright 1999-2017 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.druid.sql.dialect.db2.ast.stmt;

import com.alibaba.druid.DbType;
import com.alibaba.druid.sql.ast.SQLName;
import com.alibaba.druid.sql.ast.SQLStatementImpl;
import com.alibaba.druid.sql.ast.statement.SQLAlterStatement;
import com.alibaba.druid.sql.dialect.db2.ast.DB2Statement;
import com.alibaba.druid.sql.dialect.db2.visitor.DB2ASTVisitor;
import com.alibaba.druid.sql.visitor.SQLASTVisitor;

public class DB2RenameTableStatement extends SQLStatementImpl implements DB2Statement, SQLAlterStatement {
private SQLName name;
private SQLName to;

public DB2RenameTableStatement() {
this.dbType = DbType.db2;
}

public DB2RenameTableStatement(DbType dbType) {
this.dbType = dbType;
}

public SQLName getName() {
return name;
}

public void setName(SQLName name) {
this.name = name;
}

public SQLName getTo() {
return to;
}

public void setTo(SQLName to) {
this.to = to;
}

@Override
protected void accept0(SQLASTVisitor v) {
if (v instanceof DB2ASTVisitor) {
this.accept0((DB2ASTVisitor) v);
} else {
throw new UnsupportedOperationException(this.getClass().getName());
}
}

@Override
public void accept0(DB2ASTVisitor visitor) {
if (visitor.visit(this)) {
acceptChild(visitor, name);
acceptChild(visitor, to);
}
visitor.endVisit(this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,11 @@ public void accept0(DB2ASTVisitor visitor) {
}

public DB2SelectQueryBlock() {
dbType = DbType.db2;
this.dbType = DbType.db2;
}

public DB2SelectQueryBlock(DbType dbType) {
this.dbType = dbType;
}

public Isolation getIsolation() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,22 @@
*/
package com.alibaba.druid.sql.dialect.db2.ast.stmt;

import com.alibaba.druid.DbType;
import com.alibaba.druid.sql.ast.SQLExpr;
import com.alibaba.druid.sql.dialect.db2.ast.DB2StatementImpl;
import com.alibaba.druid.sql.dialect.db2.visitor.DB2ASTVisitor;

public class DB2ValuesStatement extends DB2StatementImpl {
private SQLExpr expr;

public DB2ValuesStatement() {
this.dbType = DbType.db2;
}

public DB2ValuesStatement(DbType dbType) {
this.dbType = dbType;
}

@Override
public void accept0(DB2ASTVisitor visitor) {
if (visitor.visit(this)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.alibaba.druid.sql.ast.SQLName;
import com.alibaba.druid.sql.ast.SQLPartitionByHash;
import com.alibaba.druid.sql.ast.statement.*;
import com.alibaba.druid.sql.dialect.db2.ast.stmt.AsSelectWith;
import com.alibaba.druid.sql.dialect.db2.ast.stmt.DB2CreateTableStatement;
import com.alibaba.druid.sql.parser.ParserException;
import com.alibaba.druid.sql.parser.SQLCreateTableParser;
Expand Down Expand Up @@ -92,4 +93,20 @@ protected void parseCreateTableRest(SQLCreateTableStatement stmt) {
protected DB2CreateTableStatement newCreateStatement() {
return new DB2CreateTableStatement();
}

@Override
protected void createTableQuery(SQLCreateTableStatement createTable) {
if (lexer.nextIf(Token.AS)) {
SQLSelect select = createTableQueryRest();
createTable.setSelect(select);

if (lexer.token() == Token.WITH) {
lexer.nextToken();
if (lexer.nextIfIdentifier("data")) {
lexer.nextToken();
((DB2CreateTableStatement) createTable).setSelectWith(AsSelectWith.WITH_DATA);
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ protected Keywords loadKeywords() {
map.put("EXISTS", Token.EXISTS);
map.put("RESTRICT", Token.RESTRICT);
map.put("CASCADE", Token.CASCADE);
map.put("UNIQUE", Token.UNIQUE);
map.put("FOREIGN", Token.FOREIGN);

return new Keywords(map);
}
Expand Down
Loading