private IDictionary <Column, ICollection <IRouteValue> > CreateRouteValueMap( ISqlCommandContext <ISqlCommand> sqlCommandContext, AndPredicateSegment andPredicate, ParameterContext parameterContext) { IDictionary <Column, ICollection <IRouteValue> > result = new Dictionary <Column, ICollection <IRouteValue> >(); foreach (var predicate in andPredicate.GetPredicates()) { var tableName = sqlCommandContext.GetTablesContext() .FindTableName(predicate.GetColumn(), schemaMetaData); if (tableName == null || !shardingRule.IsShardingColumn(predicate.GetColumn().GetIdentifier().GetValue(), tableName)) { continue; } Column column = new Column(predicate.GetColumn().GetIdentifier().GetValue(), tableName); var routeValue = ConditionValueGeneratorFactory.Generate(predicate.GetPredicateRightValue(), column, parameterContext); if (routeValue == null) { continue; } if (!result.ContainsKey(column)) { result.Add(column, new LinkedList <IRouteValue>()); } result[column].Add(routeValue); } return(result); }
private AndPredicateSegment CreateAndPredicate(AndPredicateSegment left, AndPredicateSegment right) { AndPredicateSegment result = new AndPredicateSegment(); result.GetPredicates().AddAll(left.GetPredicates()); result.GetPredicates().AddAll(right.GetPredicates()); return(result); }
public override IASTNode VisitWhereClause(SqlServerCommandParser.WhereClauseContext context) { WhereSegment result = new WhereSegment(context.Start.StartIndex, context.Stop.StopIndex); var segment = Visit(context.expr()); if (segment is OrPredicateSegment orPredicateSegment) { result.GetAndPredicates().AddAll(orPredicateSegment.GetAndPredicates()); } else if (segment is PredicateSegment predicateSegment) { var andPredicate = new AndPredicateSegment(); andPredicate.GetPredicates().Add(predicateSegment); result.GetAndPredicates().Add(andPredicate); } return(result); }
public override IASTNode VisitWhereClause(MySqlCommandParser.WhereClauseContext ctx) { WhereSegment result = new WhereSegment(ctx.Start.StartIndex, ctx.Stop.StopIndex); IASTNode segment = Visit(ctx.expr()); if (segment is OrPredicateSegment orPredicateSegment) { result.GetAndPredicates().AddAll(((OrPredicateSegment)segment).GetAndPredicates()); } else if (segment is PredicateSegment) { AndPredicateSegment andPredicate = new AndPredicateSegment(); andPredicate.GetPredicates().Add((PredicateSegment)segment); result.GetAndPredicates().Add(andPredicate); } return(result); }
private object GetShardingValue(AndPredicateSegment andPredicate, ParameterContext parameterContext, String shardingColumn) { foreach (var predicate in andPredicate.GetPredicates()) { if (!shardingColumn.EqualsIgnoreCase(predicate.GetColumn().GetIdentifier().GetValue())) { continue; } IPredicateRightValue rightValue = predicate.GetPredicateRightValue(); if (rightValue is PredicateCompareRightValue predicateCompareRightValue) { var segment = predicateCompareRightValue.GetExpression(); return(GetPredicateCompareShardingValue(segment, parameterContext)); } if (rightValue is PredicateInRightValue predicateInRightValue) { ICollection <IExpressionSegment> segments = predicateInRightValue.SqlExpressions; return(GetPredicateInShardingValue(segments, parameterContext)); } } return(null); }
private ICollection <AndPredicateSegment> GetAndPredicates(IASTNode astNode) { if (astNode is OrPredicateSegment orPredicateSegment) { return(orPredicateSegment.GetAndPredicates()); } if (astNode is AndPredicateSegment andPredicateSegment) { return(new List <AndPredicateSegment>() { andPredicateSegment }); } if (astNode is PredicateSegment predicateSegment) { var andPredicate = new AndPredicateSegment(); andPredicate.GetPredicates().Add(predicateSegment); return(new List <AndPredicateSegment>() { andPredicate }); } return(new LinkedList <AndPredicateSegment>()); }