Java源码示例:com.alibaba.druid.stat.TableStat.Relationship
示例1
/**
* Equal or not equal to relationship transfer
* E.g. ntest.id = mtest.id and xtest.id = ntest.id
* we try to add a derivative relationship "xtest.id = mtest.id"
* so when there has limit in mtest.id the xtest.id can also get the limit
* P.S.:Only order insensitive operator can be optimize,like = or <=>
*
*/
private void relationMerge(Set<Relationship> relationships) {
HashSet<Relationship> loopReSet = new HashSet<>();
loopReSet.addAll(relationships);
for (Relationship re : loopReSet) {
for (Relationship inv : loopReSet) {
if (inv.getOperator().equals(re.getOperator()) && (inv.getOperator().equals("=") || inv.getOperator().equals("<=>"))) {
List<Column> tempSet = new ArrayList<>();
addAndCheckDuplicate(tempSet, re.getLeft());
addAndCheckDuplicate(tempSet, re.getRight());
addAndCheckDuplicate(tempSet, inv.getLeft());
addAndCheckDuplicate(tempSet, inv.getRight());
if (tempSet.size() == 2) {
Relationship rs1 = new Relationship(tempSet.get(0), tempSet.get(1), inv.getOperator());
Relationship rs2 = new Relationship(tempSet.get(1), tempSet.get(0), inv.getOperator());
if (!relationships.contains(rs1) && !relationships.contains(rs2)) {
relationships.add(rs1);
}
}
}
}
}
}
示例2
/**
* 子查询中存在关联查询的情况下,检查关联字段是否是分片字段
* @param rulemap
* @param
* @return
*/
private boolean checkRuleField(Map<String,RuleConfig> rulemap,MycatSchemaStatVisitor visitor){
if(!MycatServer.getInstance().getConfig().getSystem().isSubqueryRelationshipCheck()){
return true;
}
Set<Relationship> ships = visitor.getRelationships();
Iterator<Relationship> iter = ships.iterator();
while(iter.hasNext()){
Relationship ship = iter.next();
String lefttable = ship.getLeft().getTable().toUpperCase();
String righttable = ship.getRight().getTable().toUpperCase();
// 如果是同一个表中的关联条件,不做处理
if(lefttable.equals(righttable)){
return true;
}
RuleConfig leftconfig = rulemap.get(lefttable);
RuleConfig rightconfig = rulemap.get(righttable);
if(null!=leftconfig&&null!=rightconfig
&&leftconfig.equals(rightconfig)
&&leftconfig.getColumn().equals(ship.getLeft().getName().toUpperCase())
&&rightconfig.getColumn().equals(ship.getRight().getName().toUpperCase())){
return true;
}
}
return false;
}
示例3
/**
* turn all the condition in or into conditionList
* exp (conditionA OR conditionB) into conditionList{conditionA,conditionB}
* so the conditionA,conditionB can be group with outer conditions
*
*/
private void resetConditionsFromWhereUnit(WhereUnit whereUnit) {
if (!whereUnit.isFinishedExtend()) {
List<List<Condition>> retList = new ArrayList<>();
List<Condition> outSideCondition = new ArrayList<>();
outSideCondition.addAll(conditions);
List<Relationship> outSideRelationship = new ArrayList<>();
outSideRelationship.addAll(relationships);
this.conditions.clear();
this.relationships.clear();
for (SQLExpr sqlExpr : whereUnit.getSplitedExprList()) {
sqlExpr.accept(this);
List<Condition> conds = new ArrayList<>();
conds.addAll(getConditions());
conds.addAll(outSideCondition);
Set<Relationship> relations = new HashSet<>();
relations.addAll(getRelationships());
relations.addAll(outSideRelationship);
ConditionUtil.extendConditionsFromRelations(conds, relations);
retList.add(conds);
this.conditions.clear();
this.relationships.clear();
}
whereUnit.setOrConditionList(retList);
for (WhereUnit subWhere : whereUnit.getSubWhereUnit()) {
resetConditionsFromWhereUnit(subWhere);
}
whereUnit.setFinishedExtend(true);
}
}
示例4
public Set<Relationship> getOutRelationships() {
return outRelationships;
}
示例5
public void addOutRelationships(Set<Relationship> relationships) {
this.outRelationships.addAll(relationships);
}