use of com.alibaba.druid.stat.TableStat.Condition in project dble by actiontech.
the class DefaultDruidParser method buildRouteCalculateUnits.
private List<RouteCalculateUnit> buildRouteCalculateUnits(Map<String, String> tableAliasMap, List<List<Condition>> conditionList) {
List<RouteCalculateUnit> retList = new ArrayList<>();
// find partition column in condition
for (List<Condition> aConditionList : conditionList) {
RouteCalculateUnit routeCalculateUnit = new RouteCalculateUnit();
for (Condition condition : aConditionList) {
List<Object> values = condition.getValues();
if (values.size() == 0) {
continue;
}
if (checkConditionValues(values)) {
String columnName = StringUtil.removeBackQuote(condition.getColumn().getName().toUpperCase());
String tableName = StringUtil.removeBackQuote(condition.getColumn().getTable());
if (DbleServer.getInstance().getSystemVariables().isLowerCaseTableNames()) {
tableName = tableName.toLowerCase();
}
if (tableAliasMap != null && tableAliasMap.get(tableName) == null) {
// ignore subQuery's alias
continue;
}
if (tableAliasMap != null && tableAliasMap.get(tableName) != null && !tableAliasMap.get(tableName).equals(tableName)) {
tableName = tableAliasMap.get(tableName);
}
String operator = condition.getOperator();
// execute only between ,in and =
if (operator.equals("between")) {
RangeValue rv = new RangeValue(values.get(0), values.get(1), RangeValue.EE);
routeCalculateUnit.addShardingExpr(tableName, columnName, rv);
} else if (operator.equals("=") || operator.toLowerCase().equals("in")) {
routeCalculateUnit.addShardingExpr(tableName, columnName, values.toArray());
}
}
}
retList.add(routeCalculateUnit);
}
return retList;
}
use of com.alibaba.druid.stat.TableStat.Condition in project druid by alibaba.
the class SchemaStatVisitor method handleCondition.
protected void handleCondition(SQLExpr expr, String operator, SQLExpr... valueExprs) {
if (expr instanceof SQLCastExpr) {
expr = ((SQLCastExpr) expr).getExpr();
} else if (expr instanceof SQLMethodInvokeExpr) {
SQLMethodInvokeExpr func = (SQLMethodInvokeExpr) expr;
List<SQLExpr> arguments = func.getArguments();
if (func.methodNameHashCode64() == FnvHash.Constants.COALESCE && arguments.size() > 0) {
boolean allLiteral = true;
for (int i = 1; i < arguments.size(); ++i) {
SQLExpr arg = arguments.get(i);
if (!(arg instanceof SQLLiteralExpr)) {
allLiteral = false;
}
}
if (allLiteral) {
expr = arguments.get(0);
}
}
}
Column column = getColumn(expr);
if (column == null && expr instanceof SQLBinaryOpExpr && valueExprs.length == 1 && valueExprs[0] instanceof SQLLiteralExpr) {
SQLBinaryOpExpr left = (SQLBinaryOpExpr) expr;
SQLLiteralExpr right = (SQLLiteralExpr) valueExprs[0];
if (left.getRight() instanceof SQLIntegerExpr && right instanceof SQLIntegerExpr) {
long v0 = ((SQLIntegerExpr) left.getRight()).getNumber().longValue();
long v1 = ((SQLIntegerExpr) right).getNumber().longValue();
SQLBinaryOperator op = left.getOperator();
long v;
switch(op) {
case Add:
v = v1 - v0;
break;
case Subtract:
v = v1 + v0;
break;
default:
return;
}
handleCondition(left.getLeft(), operator, new SQLIntegerExpr(v));
return;
}
}
if (column == null) {
return;
}
Condition condition = null;
for (Condition item : this.getConditions()) {
if (item.getColumn().equals(column) && item.getOperator().equals(operator)) {
condition = item;
break;
}
}
if (condition == null) {
condition = new Condition(column, operator);
this.conditions.add(condition);
}
for (SQLExpr item : valueExprs) {
Column valueColumn = getColumn(item);
if (valueColumn != null) {
continue;
}
Object value;
if (item instanceof SQLCastExpr) {
item = ((SQLCastExpr) item).getExpr();
}
if (item instanceof SQLMethodInvokeExpr || item instanceof SQLCurrentTimeExpr) {
value = item.toString();
} else {
value = SQLEvalVisitorUtils.eval(dbType, item, parameters, false);
if (value == SQLEvalVisitor.EVAL_VALUE_NULL) {
value = null;
}
}
condition.addValue(value);
}
}
use of com.alibaba.druid.stat.TableStat.Condition in project druid by alibaba.
the class ExportConditions method evaluate.
public String evaluate(String sql, String dbTypeName, Boolean compactValues) {
DbType dbType = dbTypeName == null ? null : DbType.valueOf(dbTypeName);
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) {
if (compactValues != null && compactValues.booleanValue()) {
row.add(values);
} else {
row.add(values.get(0));
}
} else {
row.add(values);
}
rows.add(row);
}
return JSONUtils.toJSONString(rows);
} catch (Exception ex) {
System.err.println("error sql : " + sql);
ex.printStackTrace();
return null;
}
}
use of com.alibaba.druid.stat.TableStat.Condition in project Mycat-Server by MyCATApache.
the class MycatSchemaStatVisitor method visit.
@Override
public boolean visit(SQLBetweenExpr x) {
String begin = null;
if (x.beginExpr instanceof SQLCharExpr) {
begin = (String) ((SQLCharExpr) x.beginExpr).getValue();
} else {
begin = x.beginExpr.toString();
}
String end = null;
if (x.endExpr instanceof SQLCharExpr) {
end = (String) ((SQLCharExpr) x.endExpr).getValue();
} else {
end = x.endExpr.toString();
}
Column column = getColumn(x);
if (column == null) {
return true;
}
Condition condition = null;
for (Condition item : this.getConditions()) {
if (item.getColumn().equals(column) && item.getOperator().equals("between")) {
condition = item;
break;
}
}
if (condition == null) {
condition = new Condition();
condition.setColumn(column);
condition.setOperator("between");
this.conditions.add(condition);
}
condition.getValues().add(begin);
condition.getValues().add(end);
return true;
}
use of com.alibaba.druid.stat.TableStat.Condition in project Mycat-Server by MyCATApache.
the class MycatSchemaStatVisitor method getConditionsFromWhereUnit.
private void getConditionsFromWhereUnit(WhereUnit whereUnit) {
List<List<Condition>> retList = new ArrayList<List<Condition>>();
// or语句外层的条件:如where condition1 and (condition2 or condition3),condition1就会在外层条件中,因为之前提取
List<Condition> outSideCondition = new ArrayList<Condition>();
// stashOutSideConditions();
outSideCondition.addAll(conditions);
this.conditions.clear();
for (SQLExpr sqlExpr : whereUnit.getSplitedExprList()) {
sqlExpr.accept(this);
// List<Condition> conditions = new ArrayList<Condition>();
// conditions.addAll(getConditions()); conditions.addAll(outSideCondition);
/**
* 合并两个条件列表的元素为一个条件列表,减少不必要多的条件项</br>
*
* @author SvenAugustus
*/
List<Condition> conditions = mergeSqlConditionList(getConditions(), outSideCondition);
retList.add(conditions);
this.conditions.clear();
}
whereUnit.setConditionList(retList);
for (WhereUnit subWhere : whereUnit.getSubWhereUnit()) {
getConditionsFromWhereUnit(subWhere);
}
}
Aggregations