mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
improved db2 parser. for issue #2548
This commit is contained in:
@@ -18,9 +18,10 @@ package com.alibaba.druid.sql.ast;
|
||||
import java.util.List;
|
||||
|
||||
public interface SQLStatement extends SQLObject {
|
||||
String getDbType();
|
||||
boolean isAfterSemi();
|
||||
void setAfterSemi(boolean afterSemi);
|
||||
SQLStatement clone();
|
||||
List<SQLObject> getChildren();
|
||||
String getDbType();
|
||||
boolean isAfterSemi();
|
||||
void setAfterSemi(boolean afterSemi);
|
||||
SQLStatement clone();
|
||||
List<SQLObject> getChildren();
|
||||
String toLowerCaseString();
|
||||
}
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
|
||||
package com.alibaba.druid.sql.ast.statement;
|
||||
|
||||
import com.alibaba.druid.sql.ast.SQLDataType;
|
||||
import com.alibaba.druid.sql.ast.SQLExpr;
|
||||
import com.alibaba.druid.sql.ast.SQLName;
|
||||
import com.alibaba.druid.sql.ast.SQLObjectImpl;
|
||||
@@ -23,16 +24,12 @@ import com.alibaba.druid.sql.visitor.SQLASTVisitor;
|
||||
|
||||
public class SQLAlterTableAlterColumn extends SQLObjectImpl implements SQLAlterTableItem {
|
||||
private SQLName originColumn;
|
||||
|
||||
private SQLColumnDefinition column;
|
||||
|
||||
private boolean setNotNull;
|
||||
|
||||
private boolean dropNotNull;
|
||||
|
||||
private SQLExpr setDefault;
|
||||
|
||||
private boolean dropDefault;
|
||||
private SQLDataType dataType;
|
||||
|
||||
@Override
|
||||
protected void accept0(SQLASTVisitor visitor) {
|
||||
@@ -87,10 +84,21 @@ public class SQLAlterTableAlterColumn extends SQLObjectImpl implements SQLAlterT
|
||||
return originColumn;
|
||||
}
|
||||
|
||||
public void setOriginColumn(SQLName originColumn) {
|
||||
if (originColumn != null) {
|
||||
originColumn.setParent(this);
|
||||
public void setOriginColumn(SQLName x) {
|
||||
if (x != null) {
|
||||
x.setParent(this);
|
||||
}
|
||||
this.originColumn = originColumn;
|
||||
this.originColumn = x;
|
||||
}
|
||||
|
||||
public SQLDataType getDataType() {
|
||||
return dataType;
|
||||
}
|
||||
|
||||
public void setDataType(SQLDataType x) {
|
||||
if (x != null) {
|
||||
x.setParent(this);
|
||||
}
|
||||
this.dataType = x;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -194,4 +194,8 @@ public class SQLInsertStatement extends SQLInsertInto implements SQLStatement {
|
||||
public String toString() {
|
||||
return SQLUtils.toSQLString(this, dbType);
|
||||
}
|
||||
|
||||
public String toLowerCaseString() {
|
||||
return SQLUtils.toSQLString(this, dbType, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,12 +17,14 @@ package com.alibaba.druid.sql.dialect.db2.parser;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import com.alibaba.druid.sql.ast.SQLDataType;
|
||||
import com.alibaba.druid.sql.ast.SQLExpr;
|
||||
import com.alibaba.druid.sql.ast.SQLStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLAlterTableAlterColumn;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLColumnDefinition;
|
||||
import com.alibaba.druid.sql.dialect.db2.ast.stmt.DB2ValuesStatement;
|
||||
import com.alibaba.druid.sql.parser.*;
|
||||
import com.alibaba.druid.util.FnvHash;
|
||||
|
||||
|
||||
public class DB2StatementParser extends SQLStatementParser {
|
||||
@@ -75,10 +77,17 @@ public class DB2StatementParser extends SQLStatementParser {
|
||||
lexer.nextToken();
|
||||
accept(Token.NULL);
|
||||
alterColumn.setSetNotNull(true);
|
||||
} else {
|
||||
accept(Token.DEFAULT);
|
||||
} else if (lexer.token() == Token.DEFAULT) {
|
||||
lexer.nextToken();
|
||||
SQLExpr defaultValue = this.exprParser.expr();
|
||||
alterColumn.setSetDefault(defaultValue);
|
||||
} else if (lexer.identifierEquals(FnvHash.Constants.DATA)) {
|
||||
lexer.nextToken();
|
||||
acceptIdentifier("TYPE");
|
||||
SQLDataType dataType = this.exprParser.parseDataType();
|
||||
alterColumn.setDataType(dataType);
|
||||
} else {
|
||||
throw new ParserException("TODO : " + lexer.info());
|
||||
}
|
||||
} else if (lexer.token() == Token.DROP) {
|
||||
lexer.nextToken();
|
||||
|
||||
@@ -3817,13 +3817,22 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Paramet
|
||||
if (x.isSetNotNull()) { // postgresql
|
||||
print0(ucase ? " SET NOT NULL" : " set not null");
|
||||
}
|
||||
|
||||
if (x.isDropNotNull()) { // postgresql
|
||||
print0(ucase ? " DROP NOT NULL" : " drop not null");
|
||||
}
|
||||
|
||||
if (x.getSetDefault() != null) { // postgresql
|
||||
print0(ucase ? " SET DEFAULT " : " set default ");
|
||||
x.getSetDefault().accept(this);
|
||||
}
|
||||
|
||||
final SQLDataType dataType = x.getDataType();
|
||||
if (dataType != null) {
|
||||
print0(ucase ? " SET DATA TYPE " : " set data type ");
|
||||
dataType.accept(this);
|
||||
}
|
||||
|
||||
if (x.isDropDefault()) { // postgresql
|
||||
print0(ucase ? " DROP DEFAULT" : " drop default");
|
||||
}
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
/*
|
||||
* Copyright 1999-2018 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.bvt.sql.db2;
|
||||
|
||||
import com.alibaba.druid.sql.SQLUtils;
|
||||
import com.alibaba.druid.sql.ast.SQLStatement;
|
||||
import com.alibaba.druid.sql.dialect.oracle.visitor.OracleSchemaStatVisitor;
|
||||
import com.alibaba.druid.sql.visitor.SchemaStatVisitor;
|
||||
import com.alibaba.druid.util.JdbcConstants;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
public class DB2AlterTableTest_2 extends TestCase {
|
||||
|
||||
public void test_alter_constraint() throws Exception {
|
||||
String sql = "alter table audit.SQLHIS alter column EXPLAINMSG set data type VARCHAR(255)";
|
||||
SQLStatement stmt = SQLUtils.parseStatements(sql, JdbcConstants.DB2).get(0);
|
||||
|
||||
SchemaStatVisitor visitor = SQLUtils.createSchemaStatVisitor(JdbcConstants.DB2);
|
||||
stmt.accept(visitor);
|
||||
|
||||
System.out.println("Tables : " + visitor.getTables());
|
||||
System.out.println("fields : " + visitor.getColumns());
|
||||
System.out.println("coditions : " + visitor.getConditions());
|
||||
System.out.println("orderBy : " + visitor.getOrderByColumns());
|
||||
|
||||
assertEquals("ALTER TABLE audit.SQLHIS\n" +
|
||||
"\tALTER COLUMN EXPLAINMSG SET DATA TYPE VARCHAR(255)", stmt.toString());
|
||||
|
||||
assertEquals("alter table audit.SQLHIS\n" +
|
||||
"\talter column EXPLAINMSG set data type VARCHAR(255)", stmt.toLowerCaseString());
|
||||
|
||||
assertEquals(1, visitor.getTables().size());
|
||||
assertEquals(1, visitor.getColumns().size());
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user