mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
db2 sql parser support hierarchical.
This commit is contained in:
@@ -27,26 +27,28 @@ import com.alibaba.druid.sql.visitor.SQLASTVisitor;
|
||||
public class SQLSelectQueryBlock extends SQLObjectImpl implements SQLSelectQuery {
|
||||
|
||||
protected int distionOption;
|
||||
protected final List<SQLSelectItem> selectList = new ArrayList<SQLSelectItem>();
|
||||
protected final List<SQLSelectItem> selectList = new ArrayList<SQLSelectItem>();
|
||||
|
||||
protected SQLTableSource from;
|
||||
protected SQLExprTableSource into;
|
||||
protected SQLExpr where;
|
||||
|
||||
// for oracle & oceanbase
|
||||
protected SQLExpr startWith;
|
||||
protected SQLExpr connectBy;
|
||||
protected boolean prior = false;
|
||||
protected boolean noCycle = false;
|
||||
protected SQLOrderBy orderBySiblings;
|
||||
|
||||
protected SQLSelectGroupByClause groupBy;
|
||||
protected SQLOrderBy orderBy;
|
||||
protected boolean parenthesized = false;
|
||||
protected boolean forUpdate = false;
|
||||
protected boolean noWait = false;
|
||||
protected boolean parenthesized = false;
|
||||
protected boolean forUpdate = false;
|
||||
protected boolean noWait = false;
|
||||
protected SQLExpr waitTime;
|
||||
|
||||
protected SQLLimit limit;
|
||||
|
||||
// for oracle & oceanbase
|
||||
protected SQLExpr startWith;
|
||||
protected SQLExpr connectBy;
|
||||
protected boolean prior = false;
|
||||
protected boolean noCycle = false;
|
||||
|
||||
public SQLSelectQueryBlock(){
|
||||
|
||||
}
|
||||
@@ -100,6 +102,17 @@ public class SQLSelectQueryBlock extends SQLObjectImpl implements SQLSelectQuery
|
||||
this.orderBy = orderBy;
|
||||
}
|
||||
|
||||
public SQLOrderBy getOrderBySiblings() {
|
||||
return orderBySiblings;
|
||||
}
|
||||
|
||||
public void setOrderBySiblings(SQLOrderBy orderBySiblings) {
|
||||
if (orderBySiblings != null) {
|
||||
orderBySiblings.setParent(this);
|
||||
}
|
||||
this.orderBySiblings = orderBySiblings;
|
||||
}
|
||||
|
||||
public int getDistionOption() {
|
||||
return this.distionOption;
|
||||
}
|
||||
|
||||
@@ -76,6 +76,8 @@ public class DB2SelectParser extends SQLSelectParser {
|
||||
|
||||
parseWhere(queryBlock);
|
||||
|
||||
parseHierachical(queryBlock);
|
||||
|
||||
parseGroupBy(queryBlock);
|
||||
|
||||
if (lexer.token() == Token.ORDER) {
|
||||
|
||||
@@ -1089,6 +1089,8 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Paramet
|
||||
where.accept(this);
|
||||
}
|
||||
|
||||
printHierarchical(x);
|
||||
|
||||
if (x.getGroupBy() != null) {
|
||||
println();
|
||||
x.getGroupBy().accept(this);
|
||||
|
||||
@@ -0,0 +1,73 @@
|
||||
/*
|
||||
* 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.db2;
|
||||
|
||||
import com.alibaba.druid.sql.MysqlTest;
|
||||
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 java.util.List;
|
||||
|
||||
public class DB2SelectTest_hierarchical_1 extends MysqlTest {
|
||||
protected final String dbType = JdbcConstants.DB2;
|
||||
|
||||
public void test_0() throws Exception {
|
||||
String sql = "SELECT last_name, employee_id, manager_id, LEVEL\n" +
|
||||
" FROM employees\n" +
|
||||
" START WITH employee_id = 100\n" +
|
||||
" CONNECT BY PRIOR employee_id = manager_id\n" +
|
||||
" ORDER SIBLINGS BY last_name;"; //
|
||||
|
||||
List<SQLStatement> stmtList = SQLUtils.parseStatements(sql, dbType);
|
||||
SQLStatement stmt = stmtList.get(0);
|
||||
|
||||
{
|
||||
String result = SQLUtils.toSQLString(stmt, dbType);
|
||||
assertEquals("SELECT last_name, employee_id, manager_id, LEVEL\n" +
|
||||
"FROM employees\n" +
|
||||
"START WITH employee_id = 100\n" +
|
||||
"CONNECT BY PRIOR employee_id = manager_id\n" +
|
||||
"ORDER SIBLINGS BY last_name", result);
|
||||
}
|
||||
{
|
||||
String result = SQLUtils.toSQLString(stmt, dbType, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION);
|
||||
assertEquals("select last_name, employee_id, manager_id, LEVEL\n" +
|
||||
"from employees\n" +
|
||||
"start with employee_id = 100\n" +
|
||||
"connect by prior employee_id = manager_id\n" +
|
||||
"order siblings by last_name", result);
|
||||
}
|
||||
|
||||
assertEquals(1, stmtList.size());
|
||||
|
||||
SchemaStatVisitor visitor = SQLUtils.createSchemaStatVisitor(dbType);
|
||||
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(1, visitor.getTables().size());
|
||||
assertEquals(4, visitor.getColumns().size());
|
||||
assertEquals(0, visitor.getConditions().size());
|
||||
|
||||
// assertTrue(visitor.getTables().containsKey(new TableStat.Name("t_basic_store")));
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user