mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
SQLASTOutputVisitor support exportTables.
This commit is contained in:
@@ -47,6 +47,9 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Paramet
|
||||
protected boolean groupItemSingleLine = false;
|
||||
|
||||
protected List<Object> parameters;
|
||||
protected Set<String> tables;
|
||||
|
||||
protected boolean exportTables = false;
|
||||
|
||||
protected String dbType;
|
||||
|
||||
@@ -108,6 +111,10 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Paramet
|
||||
return parameters;
|
||||
}
|
||||
|
||||
public Set<String> getTables() {
|
||||
return this.tables;
|
||||
}
|
||||
|
||||
public void setParameters(List<Object> parameters) {
|
||||
this.parameters = parameters;
|
||||
}
|
||||
@@ -152,6 +159,14 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Paramet
|
||||
this.parameterizedMergeInList = parameterizedMergeInList;
|
||||
}
|
||||
|
||||
public boolean isExportTables() {
|
||||
return exportTables;
|
||||
}
|
||||
|
||||
public void setExportTables(boolean exportTables) {
|
||||
this.exportTables = exportTables;
|
||||
}
|
||||
|
||||
public void print(char value) {
|
||||
if (this.appender == null) {
|
||||
return;
|
||||
@@ -1066,9 +1081,21 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Paramet
|
||||
return false;
|
||||
}
|
||||
|
||||
protected void addTable(String table) {
|
||||
if (tables == null) {
|
||||
tables = new HashSet<String>();
|
||||
}
|
||||
this.tables.add(table);
|
||||
}
|
||||
|
||||
protected void printTableSourceExpr(SQLExpr expr) {
|
||||
if (exportTables) {
|
||||
addTable(expr.toString());
|
||||
}
|
||||
|
||||
if (tableMapping != null && expr instanceof SQLName) {
|
||||
String tableName = expr.toString();
|
||||
|
||||
String destTableName = tableMapping.get(tableName);
|
||||
if (destTableName != null) {
|
||||
tableName = destTableName;
|
||||
|
||||
+67
@@ -0,0 +1,67 @@
|
||||
/*
|
||||
* Copyright 1999-2101 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.mysql.select;
|
||||
|
||||
import com.alibaba.druid.sql.MysqlTest;
|
||||
import com.alibaba.druid.sql.SQLUtils;
|
||||
import com.alibaba.druid.sql.ast.SQLStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser;
|
||||
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor;
|
||||
import com.alibaba.druid.sql.visitor.SQLASTOutputVisitor;
|
||||
import com.alibaba.druid.util.JdbcConstants;
|
||||
import org.junit.Assert;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class MySqlSelectTest_exportTables extends MysqlTest {
|
||||
|
||||
public void test_0() throws Exception {
|
||||
String sql = "select *\n" +
|
||||
"from my_table\n" +
|
||||
"where level between 10-5 and 10+5\n" +
|
||||
"order by -ABS(10 - level) desc\n" +
|
||||
"limit 0,100";
|
||||
|
||||
|
||||
MySqlStatementParser parser = new MySqlStatementParser(sql);
|
||||
List<SQLStatement> statementList = parser.parseStatementList();
|
||||
SQLStatement stmt = statementList.get(0);
|
||||
// print(statementList);
|
||||
|
||||
assertEquals(1, statementList.size());
|
||||
|
||||
StringBuffer out = new StringBuffer();
|
||||
SQLASTOutputVisitor visitor = SQLUtils.createOutputVisitor(out, JdbcConstants.MYSQL);
|
||||
visitor.setExportTables(true);
|
||||
|
||||
stmt.accept(visitor);
|
||||
|
||||
|
||||
assertEquals("SELECT *\n" +
|
||||
"FROM my_table\n" +
|
||||
"WHERE level BETWEEN 10 - 5 AND 10 + 5\n" +
|
||||
"ORDER BY -ABS(10 - level) DESC\n" +
|
||||
"LIMIT 0, 100", //
|
||||
out.toString());
|
||||
|
||||
assertNotNull(visitor.getTables());
|
||||
assertEquals(1, visitor.getTables().size());
|
||||
assertTrue(visitor.getTables().contains("my_table"));
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user