mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
improved odps sql parser, support more 'change column' syntax.
This commit is contained in:
@@ -17,10 +17,12 @@
|
||||
package com.alibaba.druid.sql.ast.statement;
|
||||
|
||||
import com.alibaba.druid.sql.ast.SQLExpr;
|
||||
import com.alibaba.druid.sql.ast.SQLName;
|
||||
import com.alibaba.druid.sql.ast.SQLObjectImpl;
|
||||
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
|
||||
|
||||
public class SQLAlterTableAlterColumn extends SQLObjectImpl implements SQLAlterTableItem {
|
||||
private SQLName originColumn;
|
||||
|
||||
private SQLColumnDefinition column;
|
||||
|
||||
@@ -81,4 +83,14 @@ public class SQLAlterTableAlterColumn extends SQLObjectImpl implements SQLAlterT
|
||||
this.dropDefault = dropDefault;
|
||||
}
|
||||
|
||||
public SQLName getOriginColumn() {
|
||||
return originColumn;
|
||||
}
|
||||
|
||||
public void setOriginColumn(SQLName originColumn) {
|
||||
if (originColumn != null) {
|
||||
originColumn.setParent(this);
|
||||
}
|
||||
this.originColumn = originColumn;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -42,6 +42,9 @@ public class SQLAlterTableRenameColumn extends SQLObjectImpl implements SQLAlter
|
||||
}
|
||||
|
||||
public void setColumn(SQLName column) {
|
||||
if (column != null) {
|
||||
column.setParent(this);
|
||||
}
|
||||
this.column = column;
|
||||
}
|
||||
|
||||
@@ -50,6 +53,9 @@ public class SQLAlterTableRenameColumn extends SQLObjectImpl implements SQLAlter
|
||||
}
|
||||
|
||||
public void setTo(SQLName to) {
|
||||
if (to != null) {
|
||||
to.setParent(this);
|
||||
}
|
||||
this.to = to;
|
||||
}
|
||||
|
||||
|
||||
@@ -888,4 +888,13 @@ public class OdpsOutputVisitor extends SQLASTOutputVisitor implements OdpsASTVis
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(SQLAlterTableRenameColumn x) {
|
||||
print0(ucase ? "CHANGE COLUMN " : "change column ");
|
||||
x.getColumn().accept(this);
|
||||
print0(ucase ? " RENAME TO " : " rename to ");
|
||||
x.getTo().accept(this);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -885,18 +885,38 @@ public class SQLStatementParser extends SQLParser {
|
||||
lexer.nextToken();
|
||||
accept(Token.COLUMN);
|
||||
SQLName columnName = this.exprParser.name();
|
||||
accept(Token.COMMENT);
|
||||
SQLExpr comment = this.exprParser.primary();
|
||||
|
||||
SQLColumnDefinition columnDefinition = new SQLColumnDefinition();
|
||||
columnDefinition.setName(columnName);
|
||||
columnDefinition.setComment(comment);
|
||||
if (identifierEquals("RENAME")) {
|
||||
lexer.nextToken();
|
||||
accept(Token.TO);
|
||||
SQLName toName = this.exprParser.name();
|
||||
SQLAlterTableRenameColumn renameColumn = new SQLAlterTableRenameColumn();
|
||||
|
||||
SQLAlterTableAlterColumn changeColumn = new SQLAlterTableAlterColumn();
|
||||
renameColumn.setColumn(columnName);
|
||||
renameColumn.setTo(toName);
|
||||
|
||||
changeColumn.setColumn(columnDefinition);
|
||||
stmt.addItem(renameColumn);
|
||||
} else if (lexer.token() == Token.COMMENT) {
|
||||
lexer.nextToken();
|
||||
SQLExpr comment = this.exprParser.primary();
|
||||
|
||||
stmt.addItem(changeColumn);
|
||||
SQLColumnDefinition columnDefinition = new SQLColumnDefinition();
|
||||
columnDefinition.setName(columnName);
|
||||
columnDefinition.setComment(comment);
|
||||
|
||||
SQLAlterTableAlterColumn changeColumn = new SQLAlterTableAlterColumn();
|
||||
|
||||
changeColumn.setColumn(columnDefinition);
|
||||
|
||||
stmt.addItem(changeColumn);
|
||||
} else {
|
||||
SQLColumnDefinition column = this.exprParser.parseColumn();
|
||||
|
||||
SQLAlterTableAlterColumn alterColumn = new SQLAlterTableAlterColumn();
|
||||
alterColumn.setColumn(column);
|
||||
alterColumn.setOriginColumn(columnName);
|
||||
stmt.addItem(alterColumn);
|
||||
}
|
||||
} else if (lexer.token() == Token.WITH) {
|
||||
lexer.nextToken();
|
||||
acceptIdentifier("NOCHECK");
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
/*
|
||||
* 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.bvt.sql.odps;
|
||||
|
||||
import com.alibaba.druid.sql.SQLUtils;
|
||||
import com.alibaba.druid.sql.ast.SQLStatement;
|
||||
import com.alibaba.druid.sql.visitor.SchemaStatVisitor;
|
||||
import com.alibaba.druid.util.JdbcConstants;
|
||||
import junit.framework.TestCase;
|
||||
import org.junit.Assert;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class OdpsAlterTableChangeColumnTest2 extends TestCase {
|
||||
|
||||
public void test_select() throws Exception {
|
||||
String sql = "alter table sale_detail change column old_col_name rename to new_col_name;";//
|
||||
Assert.assertEquals("ALTER TABLE sale_detail\n" +
|
||||
"\tCHANGE COLUMN old_col_name RENAME TO new_col_name;", SQLUtils.formatOdps(sql));
|
||||
Assert.assertEquals("alter table sale_detail\n" +
|
||||
"\tchange column old_col_name rename to new_col_name;", SQLUtils.formatOdps(sql, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION));
|
||||
|
||||
List<SQLStatement> statementList = SQLUtils.parseStatements(sql, JdbcConstants.ODPS);
|
||||
SQLStatement stmt = statementList.get(0);
|
||||
|
||||
Assert.assertEquals(1, statementList.size());
|
||||
|
||||
SchemaStatVisitor visitor = SQLUtils.createSchemaStatVisitor(JdbcConstants.ODPS);
|
||||
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());
|
||||
|
||||
Assert.assertEquals(1, visitor.getTables().size());
|
||||
Assert.assertEquals(2, visitor.getColumns().size());
|
||||
Assert.assertEquals(0, visitor.getConditions().size());
|
||||
|
||||
// Assert.assertTrue(visitor.getColumns().contains(new Column("abc", "name")));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
/*
|
||||
* 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.bvt.sql.odps;
|
||||
|
||||
import com.alibaba.druid.sql.SQLUtils;
|
||||
import com.alibaba.druid.sql.ast.SQLStatement;
|
||||
import com.alibaba.druid.sql.visitor.SchemaStatVisitor;
|
||||
import com.alibaba.druid.util.JdbcConstants;
|
||||
import junit.framework.TestCase;
|
||||
import org.junit.Assert;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class OdpsAlterTableChangeColumnTest3 extends TestCase {
|
||||
|
||||
public void test_select() throws Exception {
|
||||
String sql = "ALTER TABLE table_name CHANGE COLUMN old_col_name new_col_name bigint COMMENT column_comment;";//
|
||||
Assert.assertEquals("ALTER TABLE table_name\n" +
|
||||
"\tCHANGE COLUMN new_col_name BIGINT COMMENT column_comment;", SQLUtils.formatOdps(sql));
|
||||
Assert.assertEquals("alter table table_name\n" +
|
||||
"\tchange column new_col_name bigint comment column_comment;", SQLUtils.formatOdps(sql, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION));
|
||||
|
||||
List<SQLStatement> statementList = SQLUtils.parseStatements(sql, JdbcConstants.ODPS);
|
||||
SQLStatement stmt = statementList.get(0);
|
||||
|
||||
Assert.assertEquals(1, statementList.size());
|
||||
|
||||
SchemaStatVisitor visitor = SQLUtils.createSchemaStatVisitor(JdbcConstants.ODPS);
|
||||
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());
|
||||
|
||||
Assert.assertEquals(1, visitor.getTables().size());
|
||||
Assert.assertEquals(1, visitor.getColumns().size());
|
||||
Assert.assertEquals(0, visitor.getConditions().size());
|
||||
|
||||
// Assert.assertTrue(visitor.getColumns().contains(new Column("abc", "name")));
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user