public static IWhereFragment SimpleArrayContains(ISerializer serializer, QueryModel queryModel, ContainsResultOperator contains)
        {
            var from = queryModel.MainFromClause.FromExpression;
            var visitor = new FindMembers();
            visitor.Visit(from);

            var members = visitor.Members;

            var constant = contains.Item as ConstantExpression;

            if (constant != null)
            {
                var array = Array.CreateInstance(constant.Type, 1);
                array.SetValue(constant.Value, 0);

                var dict = new Dictionary<string, object>();
                dict.Add(members.Last().Name, array);

                members.Reverse().Skip(1).Each(m =>
                {
                    dict = new Dictionary<string, object>() { {m.Name, dict} };
                });

                return new ContainmentWhereFragment(serializer, dict);
            }

            throw new NotSupportedException();
        }
Example #2
0
		private static void FlattenSubQuery(SubQueryExpression subQueryExpression, QueryModel queryModel)
		{
			// we can not flattern subquery if outer query has body clauses.
			var subQueryModel = subQueryExpression.QueryModel;
			var subQueryMainFromClause = subQueryModel.MainFromClause;
			if (queryModel.BodyClauses.Count == 0)
			{
				foreach (var resultOperator in subQueryModel.ResultOperators)
					queryModel.ResultOperators.Add(resultOperator);

				foreach (var bodyClause in subQueryModel.BodyClauses)
					queryModel.BodyClauses.Add(bodyClause);
			}
			else
			{
				var cro = new ContainsResultOperator(new QuerySourceReferenceExpression(subQueryMainFromClause));

				var newSubQueryModel = subQueryModel.Clone();
				newSubQueryModel.ResultOperators.Add(cro);
				newSubQueryModel.ResultTypeOverride = typeof (bool);

				var where = new WhereClause(new SubQueryExpression(newSubQueryModel));
				queryModel.BodyClauses.Add(where);

				if (!queryModel.BodyClauses.OfType<OrderByClause>().Any())
				{
					var orderByClauses = subQueryModel.BodyClauses.OfType<OrderByClause>();
					foreach (var orderByClause in orderByClauses)
						queryModel.BodyClauses.Add(orderByClause);
				}
			}

			var visitor1 = new PagingRewriterSelectClauseVisitor();
			queryModel.SelectClause.TransformExpressions(visitor1.Swap);

			// Point all query source references to the outer from clause
			var visitor2 = new SwapQuerySourceVisitor(queryModel.MainFromClause, subQueryMainFromClause);
			queryModel.TransformExpressions(visitor2.Swap);

			// Replace the outer query source
			queryModel.MainFromClause = subQueryMainFromClause;
		}
    public void HandleResultOperator ()
    {
      var itemExpression = Expression.Constant (new Cook ());
      var resultOperator = new ContainsResultOperator (itemExpression);
      var sqlStatement = _sqlStatementBuilder.GetSqlStatement ();

      var preparedExpression = Expression.Constant (new Cook (), typeof (Cook));
      _stageMock.Expect (mock => mock.PrepareResultOperatorItemExpression (itemExpression, _context)).Return (preparedExpression);
      _stageMock.Replay ();
      
      _handler.HandleResultOperator (resultOperator, _sqlStatementBuilder, _generator, _stageMock, _context);

      _stageMock.VerifyAllExpectations ();
      
      Assert.That (_sqlStatementBuilder.DataInfo, Is.TypeOf (typeof (StreamedScalarValueInfo)));
      Assert.That (((StreamedScalarValueInfo) _sqlStatementBuilder.DataInfo).DataType, Is.EqualTo (typeof (Boolean)));
      
      var expectedExpression = new SqlInExpression (preparedExpression, new SqlSubStatementExpression (sqlStatement));
      
      SqlExpressionTreeComparer.CheckAreEqualTrees (expectedExpression, _sqlStatementBuilder.SelectProjection);
    }
        private static Expression HandleContains(
            EntityQueryModelVisitor entityQueryModelVisitor,
            ContainsResultOperator containsResultOperator,
            QueryModel queryModel)
        {
            var item = entityQueryModelVisitor
                .ReplaceClauseReferences(
                    containsResultOperator.Item,
                    queryModel.MainFromClause);

            return CallWithPossibleCancellationToken(
                entityQueryModelVisitor.LinqOperatorProvider.Contains
                    .MakeGenericMethod(entityQueryModelVisitor.StreamedSequenceInfo.ResultItemType),
                entityQueryModelVisitor.Expression,
                item);
        }
 public void SetUp ()
 {
   _resultOperator = new ContainsResultOperator (Expression.Constant (2));
 }
    public void TransformExpressions ()
    {
      var oldExpression = ExpressionHelper.CreateExpression ();
      var newExpression = ExpressionHelper.CreateExpression ();
      var resultOperator = new ContainsResultOperator (oldExpression);

      resultOperator.TransformExpressions (ex =>
      {
        Assert.That (ex, Is.SameAs (oldExpression));
        return newExpression;
      });

      Assert.That (resultOperator.Item, Is.SameAs (newExpression));
    }
 public void GetConstantItem_NoConstantExpression ()
 {
   var resultOperator = new ContainsResultOperator (new QuerySourceReferenceExpression (ExpressionHelper.CreateMainFromClause_Int ()));
   resultOperator.GetConstantItem<object> ();
 }
 private void VisitContainsResultOperator(ContainsResultOperator containsResultOperator, Remotion.Linq.QueryModel queryModel, int index)
 {
     Call call = new Call(MethodNames.In);
     call.Arguments.Add(Identifier.Current);
     _result = call;
 }
        private void VisitContainsResultOperator(ContainsResultOperator containsResultOperator, Remotion.Linq.QueryModel queryModel, int index)
        {
            if (_query.IsSubQuery)
            {
                _visitor.VisitExpression(containsResultOperator.Item);
                QueryComponent item = _visitor.RetrieveComponent();
                if (item is IExpression)
                {
                    Filter filter = new Filter(new BinaryOperator(MethodNames.Equal, _mainFromComponent.About, (IExpression)item));
                    if (!_mainFromComponent.Elements.Contains(filter))
                    {
                        _mainFromComponent.Elements.Add(filter);
                    }
                }

                EntityConstrain constrain = new EntityConstrain(
                    new Identifier(_mainFromComponent.About.Name + "_p"),
                    new Identifier(_mainFromComponent.About.Name + "_o"),
                    containsResultOperator.Item);
                if (!_mainFromComponent.Elements.Contains(constrain))
                {
                    _mainFromComponent.Elements.Add(constrain);
                }

                Call call = new Call(MethodNames.Any);
                call.Arguments.Add(_query);
                _result = call;
            }
            else
            {
                throw new NotSupportedException(System.String.Format("Cannot perform 'Contains' operation on top level query."));
            }
        }