FromExpressionInfo is returned by SqlPreparationFromExpressionVisitor.AnalyzeFromExpression.
        public Expression VisitSqlTableReferenceExpression(SqlTableReferenceExpression expression)
        {
            ArgumentUtility.CheckNotNull("expression", expression);

            var tableInfo = new UnresolvedGroupReferenceTableInfo(expression.SqlTable);
            var sqlTable  = new SqlTable(tableInfo, JoinSemantics.Inner);

            FromExpressionInfo = new FromExpressionInfo(sqlTable, new Ordering[0], new SqlTableReferenceExpression(sqlTable), null);

            return(expression);
        }
        protected override Expression VisitConstantExpression(ConstantExpression expression)
        {
            ArgumentUtility.CheckNotNull("expression", expression);

            var itemType = ReflectionUtility.GetItemTypeOfClosedGenericIEnumerable(expression.Type, "from expression");
            var sqlTable = _tableGenerator(new UnresolvedTableInfo(itemType));
            var sqlTableReferenceExpression = new SqlTableReferenceExpression(sqlTable);

            FromExpressionInfo = new FromExpressionInfo(sqlTable, new Ordering[0], sqlTableReferenceExpression, null);

            return(sqlTableReferenceExpression);
        }
        protected override Expression VisitMemberExpression(MemberExpression expression)
        {
            ArgumentUtility.CheckNotNull("expression", expression);

            var preparedMemberExpression = (MemberExpression)TranslateExpression(expression, Context, Stage, MethodCallTransformerProvider);

            var joinInfo                    = new UnresolvedCollectionJoinInfo(preparedMemberExpression.Expression, preparedMemberExpression.Member);
            var joinedTable                 = new SqlJoinedTable(joinInfo, JoinSemantics.Inner);
            var oldStyleJoinedTable         = _tableGenerator(joinedTable);
            var sqlTableReferenceExpression = new SqlTableReferenceExpression(oldStyleJoinedTable);

            FromExpressionInfo = new FromExpressionInfo(
                oldStyleJoinedTable, new Ordering[0], sqlTableReferenceExpression, new JoinConditionExpression(joinedTable));

            return(sqlTableReferenceExpression);
        }
        public void AddPreparedFromExpression(FromExpressionInfo fromExpressionInfo)
        {
            ArgumentUtility.CheckNotNull("fromExpressionInfo", fromExpressionInfo);

            if (fromExpressionInfo.WhereCondition != null)
            {
                SqlStatementBuilder.AddWhereCondition(fromExpressionInfo.WhereCondition);
            }

            foreach (var ordering in fromExpressionInfo.ExtractedOrderings)
            {
                SqlStatementBuilder.Orderings.Add(ordering);
            }

            SqlStatementBuilder.SqlTables.Add(fromExpressionInfo.SqlTable);
        }
Esempio n. 5
0
        protected override Expression VisitQuerySourceReference(QuerySourceReferenceExpression expression)
        {
            var groupJoinClause = expression.ReferencedQuerySource as GroupJoinClause;

            if (groupJoinClause != null)
            {
                var fromExpressionInfo = AnalyzeFromExpression(
                    groupJoinClause.JoinClause.InnerSequence,
                    Stage,
                    _generator,
                    MethodCallTransformerProvider,
                    Context,
                    _tableGenerator,
                    _orderingExtractionPolicy);

                Context.AddExpressionMapping(new QuerySourceReferenceExpression(groupJoinClause.JoinClause), fromExpressionInfo.ItemSelector);

                var whereCondition =
                    Stage.PrepareWhereExpression(
                        Expression.Equal(groupJoinClause.JoinClause.OuterKeySelector, groupJoinClause.JoinClause.InnerKeySelector), Context);

                if (fromExpressionInfo.WhereCondition != null)
                {
                    whereCondition = Expression.AndAlso(fromExpressionInfo.WhereCondition, whereCondition);
                }

                FromExpressionInfo = new FromExpressionInfo(
                    fromExpressionInfo.SqlTable,
                    fromExpressionInfo.ExtractedOrderings.ToArray(),
                    fromExpressionInfo.ItemSelector,
                    whereCondition);

                return(new SqlTableReferenceExpression(fromExpressionInfo.SqlTable));
            }

            return(base.VisitQuerySourceReference(expression));
        }
    public void AddPreparedFromExpression_NoOrderings ()
    {
      var sqlTable = SqlStatementModelObjectMother.CreateSqlTable ();
      var preparedFromExpressionInfo = new FromExpressionInfo (
          sqlTable, new Ordering[0], new SqlTableReferenceExpression (sqlTable), null);

      _visitor.SqlStatementBuilder.Orderings.Clear ();

      _visitor.AddPreparedFromExpression (preparedFromExpressionInfo);

      Assert.That (_visitor.SqlStatementBuilder.Orderings.Count, Is.EqualTo (0));
    }
    public void AddJoinClause ()
    {
      var sqlTable = SqlStatementModelObjectMother.CreateSqlTable();
      var preparedExpression = new SqlTableReferenceExpression (sqlTable);
      var joinClause = ExpressionHelper.CreateJoinClause<Cook>();
      var preparedFromExpressionInfo = new FromExpressionInfo (
          sqlTable, new Ordering[] { }, new SqlTableReferenceExpression (sqlTable), null);

      _stageMock
          .Expect (
              mock =>
              mock.PrepareFromExpression (
                  Arg<Expression>.Matches (e => e == joinClause.InnerSequence),
                  Arg<ISqlPreparationContext>.Matches (c => c != _context),
                  Arg < Func<ITableInfo, SqlTable>>.Is.Anything))
          .Return (preparedFromExpressionInfo);
      _stageMock
          .Expect (
              mock =>
              mock.PrepareWhereExpression (
                  Arg<Expression>.Matches (
                      e => ((BinaryExpression) e).Left == joinClause.OuterKeySelector && ((BinaryExpression) e).Right == joinClause.InnerKeySelector),
                  Arg<ISqlPreparationContext>.Matches (c => c != _context)))
          .Return (preparedExpression);
      _stageMock.Replay();

      var result = _visitor.AddJoinClause (joinClause);

      _stageMock.VerifyAllExpectations();
      Assert.That (result, Is.SameAs (sqlTable));
      Assert.That (_visitor.SqlStatementBuilder.WhereCondition, Is.SameAs (preparedExpression));
    }
    public void AddPreparedFromExpression (FromExpressionInfo fromExpressionInfo)
    {
      ArgumentUtility.CheckNotNull ("fromExpressionInfo", fromExpressionInfo);

      if (fromExpressionInfo.WhereCondition != null)
        SqlStatementBuilder.AddWhereCondition (fromExpressionInfo.WhereCondition);

      foreach (var ordering in fromExpressionInfo.ExtractedOrderings)
        SqlStatementBuilder.Orderings.Add (ordering);

      SqlStatementBuilder.SqlTables.Add (fromExpressionInfo.SqlTable);
    }
    protected override Expression VisitQuerySourceReferenceExpression (QuerySourceReferenceExpression expression)
    {
      var groupJoinClause = expression.ReferencedQuerySource as GroupJoinClause;
      if (groupJoinClause != null)
      {
        var fromExpressionInfo = AnalyzeFromExpression (
            groupJoinClause.JoinClause.InnerSequence,
            Stage,
            _generator,
            MethodCallTransformerProvider,
            Context,
            _tableGenerator);

        Context.AddExpressionMapping (new QuerySourceReferenceExpression (groupJoinClause.JoinClause), fromExpressionInfo.ItemSelector);

        var whereCondition =
            Stage.PrepareWhereExpression (
                Expression.Equal (groupJoinClause.JoinClause.OuterKeySelector, groupJoinClause.JoinClause.InnerKeySelector), Context);

        if (fromExpressionInfo.WhereCondition != null)
          whereCondition = Expression.AndAlso (fromExpressionInfo.WhereCondition, whereCondition);

        FromExpressionInfo = new FromExpressionInfo (
            fromExpressionInfo.SqlTable,
            fromExpressionInfo.ExtractedOrderings.ToArray(),
            fromExpressionInfo.ItemSelector,
            whereCondition);

        return new SqlTableReferenceExpression (fromExpressionInfo.SqlTable);
      }

      return base.VisitQuerySourceReferenceExpression (expression);
    }
    public void HandleResultOperator_GroupByAfterTopExpression ()
    {
      var topExpression = Expression.Constant ("top");
      _sqlStatementBuilder.TopExpression = topExpression;

      var keySelector = new SqlColumnDefinitionExpression(typeof(string), "c", "Name", false);
      var elementSelector = Expression.Constant ("elementSelector");
      var resultOperator = new GroupResultOperator ("itemName", keySelector, elementSelector);
      
      var originalStatement = _sqlStatementBuilder.GetSqlStatement ();
      var originalDataInfo = _sqlStatementBuilder.DataInfo;

      var fakeFromExpressionInfo = new FromExpressionInfo (
          new SqlTable (new ResolvedSubStatementTableInfo("sc", originalStatement), JoinSemantics.Inner), new Ordering[0], elementSelector, null);
      
      _stageMock
          .Expect (mock => mock.PrepareResultOperatorItemExpression (keySelector, _context))
          .Return (keySelector);
      _stageMock
          .Expect (mock => mock.PrepareResultOperatorItemExpression (elementSelector, _context))
          .Return (elementSelector);
      _stageMock
          .Expect (
              mock => mock.PrepareFromExpression (Arg<Expression>.Is.Anything, Arg.Is (_context), Arg<Func<ITableInfo, SqlTable>>.Is.Anything))
          .Return (fakeFromExpressionInfo)
          .WhenCalled (mi =>
          {
            var fromExpression = (Expression) mi.Arguments[0];
            CheckExpressionMovedIntoSubStatement (fromExpression, originalStatement);
          });
      
      _handler.HandleResultOperator (resultOperator, _sqlStatementBuilder, _generator, _stageMock, _context);

      _stageMock.VerifyAllExpectations();
      Assert.That (_sqlStatementBuilder.GroupByExpression, Is.SameAs (keySelector));
      Assert.That (_sqlStatementBuilder.SqlTables.Count, Is.EqualTo (1));
      Assert.That (_sqlStatementBuilder.SqlTables[0], Is.SameAs (fakeFromExpressionInfo.SqlTable));

      Assert.That (_sqlStatementBuilder.DataInfo, Is.Not.SameAs (originalDataInfo));
      Assert.That (
          _sqlStatementBuilder.DataInfo.DataType,
          Is.EqualTo (typeof (IQueryable<>).MakeGenericType (typeof (IGrouping<,>).MakeGenericType (typeof (string), typeof (string)))));
    }
    public void VisitMainFromClause_CreatesFromExpression ()
    {
      var preparedSqlTable = SqlStatementModelObjectMother.CreateSqlTable();
      var preparedFromExpressionInfo = new FromExpressionInfo (
          preparedSqlTable, new Ordering[] { }, new SqlTableReferenceExpression (preparedSqlTable), null);

      _stageMock
          .Expect (
              mock => mock.PrepareFromExpression (
                  Arg<Expression>.Matches (e => e == _mainFromClause.FromExpression),
                  Arg<ISqlPreparationContext>.Matches (c => c != _context),
                  Arg<Func<ITableInfo, SqlTable>>.Is.Anything))
          .Return (preparedFromExpressionInfo);

      _stageMock.Replay();

      _visitor.VisitMainFromClause (_mainFromClause, _queryModel);
      _stageMock.VerifyAllExpectations();

      Assert.That (_visitor.SqlStatementBuilder.SqlTables, Is.EqualTo (new[] { preparedSqlTable }));
      Assert.That (
          ((SqlTableReferenceExpression) _visitor.Context.GetExpressionMapping (new QuerySourceReferenceExpression (_mainFromClause))).SqlTable,
          Is.SameAs (preparedSqlTable));
    }
    protected override Expression VisitMemberExpression (MemberExpression expression)
    {
      ArgumentUtility.CheckNotNull ("expression", expression);

      var preparedMemberExpression = (MemberExpression) TranslateExpression (expression, Context, Stage, MethodCallTransformerProvider);

      var joinInfo = new UnresolvedCollectionJoinInfo (preparedMemberExpression.Expression, preparedMemberExpression.Member);
      var joinedTable = new SqlJoinedTable (joinInfo, JoinSemantics.Inner);
      var oldStyleJoinedTable = _tableGenerator (joinedTable);
      var sqlTableReferenceExpression = new SqlTableReferenceExpression (oldStyleJoinedTable);
      FromExpressionInfo = new FromExpressionInfo (
          oldStyleJoinedTable, new Ordering[0], sqlTableReferenceExpression, new JoinConditionExpression (joinedTable));

      return sqlTableReferenceExpression;
    }
    public void AddPreparedFromExpression_OrderingIsAdded ()
    {
      var sqlTable = SqlStatementModelObjectMother.CreateSqlTable ();
      var ordering = new Ordering(Expression.Constant("order"), OrderingDirection.Asc);
      var preparedFromExpressionInfo = new FromExpressionInfo (
          sqlTable, new[] { ordering }, new SqlTableReferenceExpression (sqlTable), null);
      
      _visitor.SqlStatementBuilder.Orderings.Clear ();
      
      _visitor.AddPreparedFromExpression (preparedFromExpressionInfo);

      Assert.That (_visitor.SqlStatementBuilder.Orderings.Count, Is.EqualTo (1));
      Assert.That (_visitor.SqlStatementBuilder.Orderings[0], Is.SameAs (ordering));
    }
    public void AddQuerySource ()
    {
      var sqlTable = SqlStatementModelObjectMother.CreateSqlTable();
      var preparedFromExpressionInfo = new FromExpressionInfo (
          sqlTable, new Ordering[] { }, new SqlTableReferenceExpression (sqlTable), null);

      _stageMock
          .Expect (
              mock =>
              mock.PrepareFromExpression (
                  Arg.Is (_mainFromClause.FromExpression),
                  Arg<ISqlPreparationContext>.Matches (c => c != _context),
                  Arg<Func<ITableInfo, SqlTable>>.Is.Anything))
          .Return (preparedFromExpressionInfo)
          .WhenCalled (mi => 
          {
            var tableCreator = (Func<ITableInfo, SqlTable>) mi.Arguments[2];
            var sampleTableInfo = new UnresolvedTableInfo (typeof (Cook));
            
            var table = tableCreator (sampleTableInfo);

            Assert.That (table, Is.TypeOf (typeof (SqlTable)));
            Assert.That (((SqlTable) table).TableInfo, Is.SameAs (sampleTableInfo));
            Assert.That (table.JoinSemantics, Is.EqualTo (JoinSemantics.Inner));
          });
      _stageMock.Replay();

      var result = _visitor.AddQuerySource (_mainFromClause, _mainFromClause.FromExpression);

      _stageMock.VerifyAllExpectations();
      Assert.That (result, Is.SameAs (sqlTable));
      Assert.That (_visitor.Context.GetExpressionMapping (new QuerySourceReferenceExpression (_mainFromClause)), Is.Not.Null);
    }
    public void VisitJoinClause_CreatesWhereCondition ()
    {
      var joinClause = ExpressionHelper.CreateJoinClause<Cook>();
      var preparedSqlTable = SqlStatementModelObjectMother.CreateSqlTable();
      var preparedFromExpressionInfo = new FromExpressionInfo (
          preparedSqlTable, new Ordering[] { }, new SqlTableReferenceExpression (preparedSqlTable), null);

      var fakeWhereCondition = Expression.Constant (1);

      _stageMock
          .Expect (
              mock =>
              mock.PrepareFromExpression (
                  Arg<Expression>.Matches (e => e == joinClause.InnerSequence),
                  Arg<ISqlPreparationContext>.Is.Anything,
                  Arg<Func<ITableInfo, SqlTable>>.Is.Anything))
          .Return (preparedFromExpressionInfo);
      _stageMock
          .Expect (
              mock =>
              mock.PrepareWhereExpression (
                  Arg<Expression>.Is.Anything,
                  Arg<ISqlPreparationContext>.Matches (c => c != _context)))
          .Return (fakeWhereCondition);
      _stageMock.Replay();

      _visitor.VisitJoinClause (joinClause, _queryModel, 5);

      _stageMock.VerifyAllExpectations();

      Assert.That (_visitor.SqlStatementBuilder.WhereCondition, Is.SameAs (fakeWhereCondition));
    }
    public void VisitAdditionalFromClause_WithWhereCondition_AddsWhereCondition ()
    {
      var fromClause = ExpressionHelper.CreateAdditionalFromClause();
      
      var preparedTable = new SqlTable (SqlStatementModelObjectMother.CreateResolvedTableInfo(), JoinSemantics.Inner);
      // TODO RMLNQSQL-2: Add existing where condition and assert that the new where condition is added, does not replace the original one.
      var whereCondition = ExpressionHelper.CreateExpression(typeof(bool));
      var preparedFromExpressionInfo = new FromExpressionInfo (
          preparedTable,
          new Ordering[] { },
          new SqlTableReferenceExpression (preparedTable),
          whereCondition);

      _stageMock
        .Expect (
          mock => mock.PrepareFromExpression (
              Arg<Expression>.Matches (e => e == fromClause.FromExpression),
              Arg<ISqlPreparationContext>.Matches (c => c != _context),
              Arg < Func<ITableInfo, SqlTable>>.Is.Anything))
          .Return (preparedFromExpressionInfo);

      _stageMock.Replay();

      _visitor.VisitAdditionalFromClause (fromClause, _queryModel, 0);

      _stageMock.VerifyAllExpectations();

      Assert.That (_visitor.SqlStatementBuilder.WhereCondition, Is.SameAs(whereCondition));
    }
    public void VisitAdditionalFromClause_AddsSqlTable_AndContextMapping ()
    {
      var fakeSqlTableForMainFromClause = SqlStatementModelObjectMother.CreateSqlTable();
      _visitor.SqlStatementBuilder.SqlTables.Add (fakeSqlTableForMainFromClause);

      var constantExpression = Expression.Constant (0);
      var additionalFromClause = new AdditionalFromClause ("additional", typeof (int), constantExpression);
      _queryModel.BodyClauses.Add (additionalFromClause);

      var preparedSqlTable = SqlStatementModelObjectMother.CreateSqlTable();
      var preparedFromExpressionInfo = new FromExpressionInfo (
          preparedSqlTable, 
          new Ordering[] { }, 
          new SqlTableReferenceExpression (preparedSqlTable), 
          null);

      _stageMock
          .Expect (
              mock => mock.PrepareFromExpression (
                  Arg<Expression>.Matches (e => e == constantExpression),
                  Arg<ISqlPreparationContext>.Matches (c => c != _context),
                  Arg<Func<ITableInfo, SqlTable>>.Is.Anything))
          .Return (preparedFromExpressionInfo);

      _stageMock.Replay();

      _visitor.VisitAdditionalFromClause (additionalFromClause, _queryModel, 0);

      _stageMock.VerifyAllExpectations();

      Assert.That (_visitor.SqlStatementBuilder.SqlTables, Is.EqualTo (new[] { fakeSqlTableForMainFromClause, preparedSqlTable }));
      var contextMapping = _visitor.Context.GetExpressionMapping (new QuerySourceReferenceExpression (additionalFromClause));
      Assert.That (contextMapping, Is.Not.Null);
      Assert.That (((SqlTableReferenceExpression) contextMapping).SqlTable, Is.SameAs (preparedSqlTable));
    }
    public void AddPreparedFromExpression_WhereConditionIsSet ()
    {
      var sqlTable = SqlStatementModelObjectMother.CreateSqlTable ();
      var whereCondition = Expression.Constant (true);
      var preparedFromExpressionInfo = new FromExpressionInfo (
          sqlTable, new Ordering[0], new SqlTableReferenceExpression (sqlTable), whereCondition);

      _visitor.SqlStatementBuilder.WhereCondition = null;

      _visitor.AddPreparedFromExpression (preparedFromExpressionInfo);

      Assert.That (_visitor.SqlStatementBuilder.WhereCondition, Is.SameAs (whereCondition));
    }
    public void AddPreparedFromExpression_WhereConditionIsAdded ()
    {
      var sqlTable = SqlStatementModelObjectMother.CreateSqlTable ();
      var whereCondition = Expression.Constant(true);
      var preparedFromExpressionInfo = new FromExpressionInfo (
          sqlTable, new Ordering[0] , new SqlTableReferenceExpression (sqlTable), whereCondition);

      var originalWhereCondition = Expression.Constant (false);
      _visitor.SqlStatementBuilder.WhereCondition = originalWhereCondition;

      _visitor.AddPreparedFromExpression (preparedFromExpressionInfo);

      var expectedCombinedWhereCondition = Expression.AndAlso (originalWhereCondition, whereCondition);
      SqlExpressionTreeComparer.CheckAreEqualTrees (expectedCombinedWhereCondition, _visitor.SqlStatementBuilder.WhereCondition);
    }
    public Expression VisitSqlTableReferenceExpression (SqlTableReferenceExpression expression)
    {
      ArgumentUtility.CheckNotNull ("expression", expression);

      var tableInfo = new UnresolvedGroupReferenceTableInfo (expression.SqlTable);
      var sqlTable = new SqlTable (tableInfo, JoinSemantics.Inner);
      FromExpressionInfo = new FromExpressionInfo (sqlTable, new Ordering[0], new SqlTableReferenceExpression (sqlTable), null);

      return expression;
    }
    public void AddPreparedFromExpression_NoWhereCondition ()
    {
      var sqlTable = SqlStatementModelObjectMother.CreateSqlTable ();
      var preparedFromExpressionInfo = new FromExpressionInfo (
          sqlTable, new Ordering[0], new SqlTableReferenceExpression (sqlTable), null);

      var originalWhereCondition = Expression.Constant (false);
      _visitor.SqlStatementBuilder.WhereCondition = originalWhereCondition;

      _visitor.AddPreparedFromExpression (preparedFromExpressionInfo);

      Assert.That (_visitor.SqlStatementBuilder.WhereCondition, Is.SameAs (originalWhereCondition));
    }
    protected override Expression VisitConstantExpression (ConstantExpression expression)
    {
      ArgumentUtility.CheckNotNull ("expression", expression);

      var itemType = ReflectionUtility.GetItemTypeOfClosedGenericIEnumerable (expression.Type, "from expression");
      var sqlTable = _tableGenerator (new UnresolvedTableInfo (itemType));
      var sqlTableReferenceExpression = new SqlTableReferenceExpression (sqlTable);
      FromExpressionInfo = new FromExpressionInfo (sqlTable, new Ordering[0], sqlTableReferenceExpression, null);

      return sqlTableReferenceExpression;
    }
    public void HandleResultOperator_AllAfterGroupExpression ()
    {
      _sqlStatementBuilder.GroupByExpression = Expression.Constant ("group");
      
      var predicate = Expression.Constant (true);
      var preparedPredicate = Expression.Constant (false);
      var resultOperator = new AllResultOperator (predicate);
      var sqlStatement = _sqlStatementBuilder.GetSqlStatement ();
      var fakePreparedSelectProjection = Expression.Constant (false);
      var sqlTable = _sqlStatementBuilder.SqlTables[0];
      var fakeFromExpressionInfo = new FromExpressionInfo (sqlTable, new Ordering[0], new SqlTableReferenceExpression (sqlTable), null);

      _stageMock
          .Expect (
              mock => mock.PrepareFromExpression (Arg<Expression>.Is.Anything, Arg.Is (_context), Arg<Func<ITableInfo, SqlTable>>.Is.Anything))
          .Return (fakeFromExpressionInfo);
      _stageMock
          .Expect (mock => mock.PrepareWhereExpression (
              Arg<Expression>.Matches (e => e.NodeType == ExpressionType.Not && (((UnaryExpression) e).Operand == predicate)),
              Arg<ISqlPreparationContext>.Matches (c => c == _context)))
          .Return (preparedPredicate);
      _stageMock
          .Expect (mock => mock.PrepareSelectExpression (Arg<Expression>.Matches (e => e.NodeType == ExpressionType.Not), Arg.Is (_context)))
          .WhenCalled (
              mi =>
              {
                var selectProjection = (Expression) mi.Arguments[0];

                Assert.That (selectProjection, Is.TypeOf (typeof (UnaryExpression)));
                Assert.That (selectProjection.NodeType, Is.EqualTo(ExpressionType.Not));
                Assert.That (((UnaryExpression) selectProjection).Operand, Is.TypeOf (typeof (SqlExistsExpression)));
                Assert.That (((SqlExistsExpression) ((UnaryExpression) selectProjection).Operand).Expression, Is.TypeOf (typeof (SqlSubStatementExpression)));
              })
          .Return (fakePreparedSelectProjection);
      _stageMock.Replay ();

      _handler.HandleResultOperator (resultOperator, _sqlStatementBuilder, _generator, _stageMock, _context);
    }