protected override void VisitProperty(PropertyConstraint node) { var op = ConvertOperator(node.Operator); var source = Query.From.FindTable(node.ConcreteType); var column = source.Column(node.Property); _whereResult = f.Constraint(column, op, node.Value); }
private IConstraint CreateColumnConstraint(string comparison, string value) { var op = PropertyOperator.Equal; switch (comparison.ToLower()) { case "eq": op = PropertyOperator.Equal; break; case "ne": op = PropertyOperator.NotEqual; break; case "gt": op = PropertyOperator.Greater; break; case "ge": op = PropertyOperator.GreaterEqual; break; case "lt": op = PropertyOperator.Less; break; case "le": op = PropertyOperator.LessEqual; break; case "contains": op = PropertyOperator.Contains; break; case "startswith": op = PropertyOperator.StartsWith; break; case "endswith": op = PropertyOperator.EndsWith; break; case "notcontains": op = PropertyOperator.NotContains; break; case "notstartswith": op = PropertyOperator.NotStartsWith; break; case "notendswith": op = PropertyOperator.NotEndsWith; break; default: throw new NotSupportedException("不支持这个操作符:" + comparison + "。"); } return(_f.Constraint(_column, op, value)); }
/// <summary> /// 通过目前已经收集到的属性、操作符、值,来生成一个属性条件结果。 /// 并清空已经收集的信息。 /// </summary> private void MakeConstraint() { if (_propertyResult != null && _operator.HasValue) { if (_hasValueResult) { _query.Where = f.Constraint(_propertyResult, _operator.Value, _valueResult); _hasValueResult = false; } else { _query.Where = f.Constraint(_propertyResult, _operator.Value, _rightPropertyResult); _rightPropertyResult = null; } _propertyResult = null; _operator = null; } }
/// <summary> /// 通过目前已经收集到的属性、操作符、值,来生成一个属性条件结果。 /// 并清空已经收集的信息。 /// </summary> private bool MakeConstraint() { if (_propertyResult != null && _operator.HasValue) { var op = _operator.Value; if (_reverseWhere) { op = PropertyOperatorHelper.Reverse(op); } if (_hasValueResult) { _constraintResult = f.Constraint(_propertyResult, op, _valueResult); _valueResult = null; _hasValueResult = false; } else { _constraintResult = f.Constraint(_propertyResult, op, _rightPropertyResult); _rightPropertyResult = null; } _propertyResult = null; _operator = null; } if (_constraintResult != null) { if (_nullableRefConstraint != null) { var concat = _reverseWhere ? BinaryOperator.Or : BinaryOperator.And; _constraintResult = f.Binary(_nullableRefConstraint, concat, _constraintResult); _nullableRefConstraint = null; } _query.Where = _constraintResult; _constraintResult = null; return(true); } return(false); }
protected override Expression VisitMethodCall(MethodCallExpression exp) { var method = exp.Method; _isAny = method.Name == LinqConsts.EnumerableMethod_Any; //先访问表达式 book.ChapterList.Cast<Chapter>(),获取列表属性。 var invoker = exp.Arguments[0]; _parentPropertyFinder.Find(invoker); var listPropertyTable = _parentPropertyFinder.PropertyOwnerTable; var listProperty = _parentPropertyFinder.Property as IListProperty; if (listProperty == null) { throw EntityQueryBuilder.OperationNotSupported(invoker); } //为该列表对应的实体创建表对象、查询对象。 var childRepo = RepositoryFactoryHost.Factory.FindByEntity(listProperty.ListEntityType); var childTable = f.Table(childRepo); _query = f.Query( from: childTable, selection: f.Literal("1") ); var qgc = QueryGenerationContext.Get(_parentQuery); qgc.Bind(_query); childTable.Alias = qgc.NextTableAlias(); //Any、All 方法如果有第二个参数,那么第二个参数就是条件表达式。如:c => c.Name == chapterName if (exp.Arguments.Count == 2) { var reverseWhere = !_isAny;//如果是 All,则需要反转里面的所有操作符。 var queryBuilder = new EntityQueryBuilder(childRepo, reverseWhere); queryBuilder.BuildQuery(exp.Arguments[1], _query); } //添加子表查询与父实体的关系条件:WHERE c.BookId = b.Id var parentProperty = childRepo.FindParentPropertyInfo(true); var parentRefIdProperty = (parentProperty.ManagedProperty as IRefProperty).RefIdProperty; var toParentConstraint = f.Constraint(childTable.Column(parentRefIdProperty), listPropertyTable.IdColumn); _query.Where = f.And(toParentConstraint, _query.Where); return(exp); }
private IConstraint CreatePropertyConstraint(string property, string comparison, string value) { var mp = _properties.Find(property, true); if (mp == null) { throw new InvalidProgramException(string.Format("查询条件解析出错,没有找到名称为 {0} 的属性。", property)); } var column = _mainTable.Column(mp); var op = PropertyOperator.Equal; switch (comparison.ToLower()) { case "eq": op = PropertyOperator.Equal; break; case "ne": op = PropertyOperator.NotEqual; break; case "gt": op = PropertyOperator.Greater; break; case "ge": op = PropertyOperator.GreaterEqual; break; case "lt": op = PropertyOperator.Less; break; case "le": op = PropertyOperator.LessEqual; break; default: throw new NotSupportedException("不支持这个操作符:" + comparison + "。"); } return(_f.Constraint(column, op, value)); }
private IConstraint CreateColumnConstraint(string comparison, string value) { #region 转换操作符 var op = PropertyOperator.Equal; switch (comparison.ToLower()) { case "eq": op = PropertyOperator.Equal; break; case "ne": op = PropertyOperator.NotEqual; break; case "gt": op = PropertyOperator.Greater; break; case "ge": op = PropertyOperator.GreaterEqual; break; case "lt": op = PropertyOperator.Less; break; case "le": op = PropertyOperator.LessEqual; break; case "contains": op = PropertyOperator.Contains; break; case "startswith": op = PropertyOperator.StartsWith; break; case "endswith": op = PropertyOperator.EndsWith; break; case "notcontains": op = PropertyOperator.NotContains; break; case "notstartswith": op = PropertyOperator.NotStartsWith; break; case "notendswith": op = PropertyOperator.NotEndsWith; break; default: throw new NotSupportedException("不支持这个操作符:" + comparison + "。"); } #endregion #region 把表达式中的值转换为列的类型对应的值。(同时,兼容处理枚举的 Label 值。) object columnValue = null; var propertyType = _column.Property.PropertyType; var innerType = TypeHelper.IgnoreNullable(propertyType); if (innerType.IsEnum) { columnValue = EnumViewModel.LabelToEnum(value, innerType); } else { columnValue = TypeHelper.CoerceValue(_column.Property.PropertyType, value); } #endregion return(_f.Constraint(_column, op, columnValue)); }