Merge pull request #1065 from wenshao/master

improved sql ast visitor
This commit is contained in:
wenshao
2016-01-31 23:34:47 +08:00
10 changed files with 156 additions and 3 deletions
@@ -383,6 +383,7 @@ public class SchemaStatVisitor extends SQLASTVisitorAdapter {
case GreaterThan:
case GreaterThanOrEqual:
case LessThan:
case LessThanOrGreater:
case LessThanOrEqual:
case LessThanOrEqualOrGreaterThan:
case Like:
@@ -459,6 +460,14 @@ public class SchemaStatVisitor extends SQLASTVisitorAdapter {
if (aliasMap == null) {
return null;
}
if (expr instanceof SQLMethodInvokeExpr) {
SQLMethodInvokeExpr methodInvokeExp = (SQLMethodInvokeExpr) expr;
if (methodInvokeExp.getParameters().size() == 1) {
SQLExpr firstExpr = methodInvokeExp.getParameters().get(0);
return getColumn(firstExpr);
}
}
if (expr instanceof SQLPropertyExpr) {
SQLExpr owner = ((SQLPropertyExpr) expr).getOwner();
@@ -15,6 +15,7 @@
*/
package com.alibaba.druid.support.json;
import com.alibaba.druid.sql.visitor.SQLEvalVisitor;
import com.alibaba.druid.util.Utils;
import javax.management.openmbean.CompositeData;
@@ -127,6 +128,11 @@ public class JSONWriter {
writeMap((Map) o);
return;
}
if (o == SQLEvalVisitor.EVAL_VALUE_NULL) {
write("null");
return;
}
throw new IllegalArgumentException("not support type : " + o.getClass());
}
@@ -0,0 +1,74 @@
/*
* 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.support.opds.udf;
import java.util.ArrayList;
import java.util.List;
import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.visitor.SchemaStatVisitor;
import com.alibaba.druid.stat.TableStat;
import com.alibaba.druid.stat.TableStat.Column;
import com.alibaba.druid.stat.TableStat.Condition;
import com.alibaba.druid.support.json.JSONUtils;
import com.aliyun.odps.udf.UDF;
public class ExportConditions extends UDF {
public String evaluate(String sql) {
return evaluate(sql, null);
}
public String evaluate(String sql, String dbType) {
try {
List<SQLStatement> statementList = SQLUtils.parseStatements(sql, dbType);
SchemaStatVisitor visitor = SQLUtils.createSchemaStatVisitor(dbType);
for (SQLStatement stmt : statementList) {
stmt.accept(visitor);
}
List<List<Object>> rows = new ArrayList<List<Object>>();
List<Condition> conditions = visitor.getConditions();
for (int i = 0; i < conditions.size(); ++i) {
TableStat.Condition condition = conditions.get(i);
Column column = condition.getColumn();
String operator = condition.getOperator();
List<Object> values = condition.getValues();
List<Object> row = new ArrayList<Object>();
row.add(column.getTable());
row.add(column.getName());
row.add(operator);
if (values.size() == 0) {
row.add(null);
} else if (values.size() == 1) {
row.add(values.get(0));
} else {
row.add(values);
}
rows.add(row);
}
return JSONUtils.toJSONString(rows);
} catch (Throwable ex) {
System.err.println("error sql : " + sql);
ex.printStackTrace();
return null;
}
}
}
@@ -50,7 +50,7 @@ public class MySqlUpdateTest_3 extends MysqlTest {
Assert.assertEquals(1, visitor.getTables().size());
Assert.assertEquals(5, visitor.getColumns().size());
Assert.assertEquals(1, visitor.getConditions().size());
Assert.assertEquals(3, visitor.getConditions().size());
Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("scheme.table")));
@@ -67,7 +67,7 @@ public class OracleBlockTest5 extends OracleTest {
Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("ipay_contract")));
Assert.assertEquals(7, visitor.getColumns().size());
Assert.assertEquals(2, visitor.getConditions().size());
Assert.assertEquals(3, visitor.getConditions().size());
// Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("departments", "department_id")));
}
@@ -33,7 +33,7 @@ public class OracleHintTest extends TestCase {
public void test_hint2() throws Exception {
String sql = "SELECT /*+leading(e) index(e ORD_ORDER_ITEM_GS_BS_DI_IND)*/ distinct e.id from ord_order_item e where e.F1 = Date '2011-10-01'";
String countSQL = PagerUtils.count(sql, JdbcUtils.ORACLE);
Assert.assertEquals("SELECT /*+leading(e) index(e ORD_ORDER_ITEM_GS_BS_DI_IND)*/ DISTINCT COUNT(*)\nFROM ord_order_item e\nWHERE e.F1 = DATE '2011-10-01'", countSQL);
Assert.assertEquals("SELECT /*+leading(e) index(e ORD_ORDER_ITEM_GS_BS_DI_IND)*/ COUNT(DISTINCT e.id)\nFROM ord_order_item e\nWHERE e.F1 = DATE '2011-10-01'", countSQL);
}
public void test_hint3() throws Exception {
@@ -0,0 +1,16 @@
package com.alibaba.druid.bvt.support.odps.udf;
import org.junit.Assert;
import com.alibaba.druid.support.opds.udf.ExportConditions;
import junit.framework.TestCase;
public class ExportConditionsTest extends TestCase {
ExportConditions udf = new ExportConditions();
public void test_export_conditions() throws Exception {
String result = udf.evaluate("select * from t where id = 3 and name = 'chensheng'");
Assert.assertEquals("[[\"t\",\"id\",\"=\",3],[\"t\",\"name\",\"=\",\"chensheng\"]]", result);
}
}
@@ -0,0 +1,16 @@
package com.alibaba.druid.bvt.support.odps.udf;
import org.junit.Assert;
import com.alibaba.druid.support.opds.udf.ExportConditions;
import junit.framework.TestCase;
public class ExportConditionsTest2 extends TestCase {
ExportConditions udf = new ExportConditions();
public void test_export_conditions() throws Exception {
String result = udf.evaluate("select * from t where proof_account is not null");
Assert.assertEquals("[[\"t\",\"proof_account\",\"IS NOT\",null]]", result);
}
}
@@ -0,0 +1,16 @@
package com.alibaba.druid.bvt.support.odps.udf;
import org.junit.Assert;
import com.alibaba.druid.support.opds.udf.ExportConditions;
import junit.framework.TestCase;
public class ExportConditionsTest3 extends TestCase {
ExportConditions udf = new ExportConditions();
public void test_export_conditions() throws Exception {
String result = udf.evaluate("select * from t where trim(name) = 'abc'");
Assert.assertEquals("[[\"t\",\"name\",\"=\",\"abc\"]]", result);
}
}
@@ -0,0 +1,16 @@
package com.alibaba.druid.bvt.support.odps.udf;
import org.junit.Assert;
import com.alibaba.druid.support.opds.udf.ExportConditions;
import junit.framework.TestCase;
public class ExportConditionsTest4 extends TestCase {
ExportConditions udf = new ExportConditions();
public void test_export_conditions() throws Exception {
String result = udf.evaluate("select * from t where trim(name) <> 'abc'");
Assert.assertEquals("[[\"t\",\"name\",\"<>\",\"abc\"]]", result);
}
}