Defines a SQL column as an item with a given name coming from a given table alias. The column is represented as [alias].[name].
Наследование: SqlColumnExpression
    public void HandleResultOperator ()
    {
      var keySelector = new SqlColumnDefinitionExpression(typeof(string), "c", "Name", false);
      var elementSelector = Expression.Constant ("elementSelector");
      var resultOperator = new GroupResultOperator ("itemName", keySelector, elementSelector);

      _stageMock
          .Expect (mock => mock.PrepareResultOperatorItemExpression (keySelector, _context))
          .Return (keySelector);
      _stageMock
          .Expect (mock => mock.PrepareResultOperatorItemExpression (elementSelector, _context))
          .Return (elementSelector);
      _stageMock.Replay();

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

      _stageMock.VerifyAllExpectations();
      Assert.That (_sqlStatementBuilder.GroupByExpression, Is.SameAs (keySelector));
      
      var expectedSelectProjection = new SqlGroupingSelectExpression (
          new NamedExpression ("key", keySelector), 
          new NamedExpression ("element", elementSelector));
      SqlExpressionTreeComparer.CheckAreEqualTrees (expectedSelectProjection, _sqlStatementBuilder.SelectProjection);

      Assert.That (
          _sqlStatementBuilder.DataInfo.DataType,
          Is.EqualTo (typeof (IQueryable<>).MakeGenericType (typeof (IGrouping<,>).MakeGenericType (typeof (string), typeof (string)))));
    }
    public void GenerateSql_VisitSqlColumnDefinitionExpression ()
    {
      var sqlColumnExpression = new SqlColumnDefinitionExpression (typeof (int), "s", "ID", false);
      SqlGeneratingExpressionVisitor.GenerateSql (sqlColumnExpression, _commandBuilder, _stageMock);

      Assert.That (_commandBuilder.GetCommandText(), Is.EqualTo ("[s].[ID]"));
    }
    public void GenerateSql_VisitSqlColumnExpressionWithStar ()
    {
      var sqlColumnExpression = new SqlColumnDefinitionExpression (typeof (Cook), "c", "*", false);
      SqlGeneratingExpressionVisitor.GenerateSql (sqlColumnExpression, _commandBuilder, _stageMock);

      Assert.That (_commandBuilder.GetCommandText(), Is.EqualTo ("[c].*"));
    }
    public void VisitNamedExpression_NameIsNotNull ()
    {
      var columnExpression = new SqlColumnDefinitionExpression (typeof (string), "c", "Name", false);
      var expression = new NamedExpression ("test", columnExpression);

      SqlGeneratingSelectExpressionVisitor.GenerateSql (expression, _commandBuilder, _stageMock);

      Assert.That (_commandBuilder.GetCommandText (), Is.EqualTo ("[c].[Name] AS [test]"));
    }
    public void Update ()
    {
      var columnExpression = new SqlColumnDefinitionExpression (typeof (string), "c", "columnName", false);

      var result = columnExpression.Update (typeof (char), "f", "test", false);

      var expectedResult = new SqlColumnDefinitionExpression (typeof (char), "f", "test", false);

      SqlExpressionTreeComparer.CheckAreEqualTrees (result, expectedResult);
    }
    public void HandleResultOperator_NoConstantExpression ()
    {
      var takeExpression = new SqlColumnDefinitionExpression (typeof (int), "c", "ID", true);
      var resultOperator = new TakeResultOperator (takeExpression);

      _handler.HandleResultOperator (resultOperator, _sqlStatementBuilder, _generator, _stage, _context);

      Assert.That ( _sqlStatementBuilder.TopExpression, Is.SameAs (takeExpression));
      Assert.That (_sqlStatementBuilder.DataInfo, Is.TypeOf (typeof (StreamedSequenceInfo)));
      Assert.That (((StreamedSequenceInfo) _sqlStatementBuilder.DataInfo).DataType, Is.EqualTo (typeof (IQueryable<>).MakeGenericType (typeof (Cook))));
    }
    public void ResolveSubStatementReferenceExpression_CreatesColumnExpression_ForNamedExpression ()
    {
      var referencedExpression = new NamedExpression ("test", Expression.Constant (0));
      var sqlStatement = SqlStatementModelObjectMother.CreateSqlStatement (referencedExpression);

      var tableInfo = new ResolvedSubStatementTableInfo ("q0", sqlStatement);
      var sqlTable = new SqlTable (tableInfo, JoinSemantics.Inner);

      var result = SubStatementReferenceResolver.ResolveSubStatementReferenceExpression (
          referencedExpression, tableInfo, sqlTable, _context);

      var expectedResult = new SqlColumnDefinitionExpression (typeof (int), "q0", "test", false);
      SqlExpressionTreeComparer.CheckAreEqualTrees (expectedResult, result);
    }
    public void SetUp ()
    {
      _stageMock = MockRepository.GenerateStrictMock<ISqlGenerationStage> ();
      _commandBuilder = new SqlCommandBuilder ();
      _visitor = new TestableSqlGeneratingOuterSelectExpressionVisitor (_commandBuilder, _stageMock);

      _namedIntExpression = new NamedExpression ("test", Expression.Constant (0));
      _nameColumnExpression = new SqlColumnDefinitionExpression (typeof (string), "c", "Name", false);
      _entityExpression = new SqlEntityDefinitionExpression (
          typeof (Cook),
          "c",
          "test",
          e => e,
          new SqlColumnDefinitionExpression (typeof (int), "c", "ID", true),
          _nameColumnExpression,
          new SqlColumnDefinitionExpression (typeof (string), "c", "FirstName", false)
          );
    }
    public void ResolveJoinInfo_ForeignKeyOnTheRight()
    {
      var customerTableInfo = new ResolvedSimpleTableInfo (typeof (DataContextTestClass.Customer), "dbo.Customers", "t1");
      var customerPrimaryKey = new SqlColumnDefinitionExpression (typeof (string), customerTableInfo.TableAlias, "CustomerID", true);
      var customerEntity = new SqlEntityDefinitionExpression (customerTableInfo.ItemType, customerTableInfo.TableAlias, null, e => e);

      var ordersMember = customerTableInfo.ItemType.GetProperty ("Orders");
      var unresolvedJoinInfo = new UnresolvedJoinInfo (customerEntity, ordersMember, JoinCardinality.Many);

      var resolvedJoinInfo = _mappingResolver.ResolveJoinInfo (unresolvedJoinInfo, _generator);

      var expectedOrderTableInfo = new ResolvedSimpleTableInfo (typeof (DataContextTestClass.Order), "dbo.Order", "t0");
      var expectedOrderForeignKey = new SqlColumnDefinitionExpression (typeof (string), expectedOrderTableInfo.TableAlias, "CustomerID", false);
      SqlExpressionTreeComparer.CheckAreEqualTrees (Expression.Equal (customerPrimaryKey, expectedOrderForeignKey), resolvedJoinInfo.JoinCondition);

      Assert.That (resolvedJoinInfo.ItemType, Is.EqualTo (expectedOrderTableInfo.ItemType));
      Assert.That (resolvedJoinInfo.ForeignTableInfo.ItemType, Is.EqualTo (expectedOrderTableInfo.ItemType));
      Assert.That (resolvedJoinInfo.ForeignTableInfo.TableAlias, Is.EqualTo (expectedOrderTableInfo.TableAlias));
    }
    public void SetUp ()
    {
      _dataInfo = new StreamedScalarValueInfo (typeof (int));

      _resolvedElementExpressionReference = new SqlColumnDefinitionExpression (typeof (string), "q0", "element", false);
      _resolvedSelectProjection = new NamedExpression (
          null, 
          new AggregationExpression (typeof (int), _resolvedElementExpressionReference, AggregationModifier.Min));

      _associatedGroupingSelectExpression = new SqlGroupingSelectExpression (
          new NamedExpression ("key", Expression.Constant ("k")),
          new NamedExpression ("element", Expression.Constant ("e")));

      _resolvedJoinedGroupingSubStatement = SqlStatementModelObjectMother.CreateSqlStatement (_associatedGroupingSelectExpression);
      _resolvedJoinedGroupingTable = new SqlTable (
          new ResolvedJoinedGroupingTableInfo (
              "q1",
              _resolvedJoinedGroupingSubStatement,
              _associatedGroupingSelectExpression,
              "q0"), JoinSemantics.Inner);

      _simplifiableResolvedSqlStatement = new SqlStatement (
          _dataInfo,
          _resolvedSelectProjection,
          new[] { _resolvedJoinedGroupingTable },
          null,
          null,
          new Ordering[0],
          null,
          false,
          Expression.Constant (0),
          Expression.Constant (0));
      _simplifiableUnresolvedProjection = new AggregationExpression (
          typeof (int),
          new SqlTableReferenceExpression (_resolvedJoinedGroupingTable),
          AggregationModifier.Count);

      _stageMock = MockRepository.GenerateStrictMock<IMappingResolutionStage> ();
      _context = new MappingResolutionContext();

      _groupAggregateSimplifier = new GroupAggregateSimplifier (_stageMock, _context);
    }
    public void ResolveMemberExpression_NonPrimaryKey()
    {
      var sqlEntityExpression = new SqlEntityDefinitionExpression (typeof (DataContextTestClass.Customer), "c", null, e => e);

      var memberInfo = typeof (DataContextTestClass.Customer).GetProperty ("CompanyName");
      Expression result = _mappingResolver.ResolveMemberExpression (sqlEntityExpression, memberInfo);

      var expectedExpression = new SqlColumnDefinitionExpression (typeof (string), "c", "CompanyName", false);
      SqlExpressionTreeComparer.CheckAreEqualTrees (expectedExpression, result);
    }
    public void ResolveSimpleTableInfo_MultiplePrimaryKeyProperties ()
    {
      var simpleTableInfo = new ResolvedSimpleTableInfo (typeof (DataContextTestClass.ClassWithCompoundPrimaryKey), "ClassWithCompoundPrimaryKey", "t0");

      SqlEntityDefinitionExpression resolvedExpr = _mappingResolver.ResolveSimpleTableInfo (simpleTableInfo, _generator);

      SqlColumnExpression expectedPrimaryColumn1 = new SqlColumnDefinitionExpression (typeof (int), simpleTableInfo.TableAlias, "Key1", true);
      SqlColumnExpression expectedPrimaryColumn2 = new SqlColumnDefinitionExpression (typeof (string), simpleTableInfo.TableAlias, "Key2", true);
      var type = typeof (MappingResolver.CompoundIdentityTuple<int, string>);
      var expectedIdentityExpression = Expression.New (
          type.GetConstructors().Single(), 
          new[] { new NamedExpression ("Item1", expectedPrimaryColumn1), new NamedExpression ("Item2", expectedPrimaryColumn2) },
          new[] { type.GetProperty ("Item1"), type.GetProperty ("Item2") });

      Assert.That (resolvedExpr.Type, Is.SameAs (typeof (DataContextTestClass.ClassWithCompoundPrimaryKey)));
      Assert.That (resolvedExpr.TableAlias, Is.EqualTo ("t0"));
      Assert.That (resolvedExpr.Name, Is.Null);
      SqlExpressionTreeComparer.CheckAreEqualTrees (expectedIdentityExpression, resolvedExpr.GetIdentityExpression ());
    }
    public void ResolveMemberExpression()
    {
      var sqlEntityExpression = new SqlEntityDefinitionExpression (typeof (PersonTestClass), "p", null, e => e);

      var memberInfo = typeof (PersonTestClass).GetProperty ("First");
      Expression result = _mappingResolver.ResolveMemberExpression (sqlEntityExpression, memberInfo);

      var expectedExpression = new SqlColumnDefinitionExpression (typeof (string), "p", "FirstName", true);
      SqlExpressionTreeComparer.CheckAreEqualTrees (expectedExpression, result);
    }
    public void ResolveJoinInfo_OneNullableColumn_TheOtherNot_LeadsToConversion ()
    {
      var regionTableInfo = new ResolvedSimpleTableInfo (typeof (DataContextTestClass.Region), "dbo.Regions", "t1");
      var regionEntity = new SqlEntityDefinitionExpression (regionTableInfo.ItemType, regionTableInfo.TableAlias, null, e => e);

      var territoriesMember = regionTableInfo.ItemType.GetProperty ("Territories");
      var unresolvedJoinInfo = new UnresolvedJoinInfo (regionEntity, territoriesMember, JoinCardinality.Many);

      var resolvedJoinInfo = _mappingResolver.ResolveJoinInfo (unresolvedJoinInfo, _generator);

      var regionPrimaryKey = new SqlColumnDefinitionExpression (typeof (int), regionTableInfo.TableAlias, "RegionID", true);
      var expectedTerritoryTableInfo = new ResolvedSimpleTableInfo (typeof (DataContextTestClass.Territory), "dbo.Territories", "t0");
      var expectedTerritoryForeignKey = new SqlColumnDefinitionExpression (typeof (int?), expectedTerritoryTableInfo.TableAlias, "RegionID", false);
      SqlExpressionTreeComparer.CheckAreEqualTrees (
          Expression.Equal (
            Expression.Convert (regionPrimaryKey, typeof (int?)), 
            expectedTerritoryForeignKey), resolvedJoinInfo.JoinCondition);
    }
    public void ResolveSimpleTableInfo ()
    {
      var simpleTableInfo = new ResolvedSimpleTableInfo (typeof (DataContextTestClass.Region), "dbo.Region", "t0");

      SqlEntityDefinitionExpression resolvedExpr = _mappingResolver.ResolveSimpleTableInfo (simpleTableInfo, _generator);

      SqlColumnExpression expectedPrimaryColumn = new SqlColumnDefinitionExpression (typeof (int), simpleTableInfo.TableAlias, "RegionID", true);
      SqlColumnExpression expectedDescriptionColumn =
          new SqlColumnDefinitionExpression (typeof (string), simpleTableInfo.TableAlias, "RegionDescription", false);

      Assert.That (resolvedExpr.Type, Is.SameAs (typeof (DataContextTestClass.Region)));
      Assert.That (resolvedExpr.TableAlias, Is.EqualTo ("t0"));
      Assert.That (resolvedExpr.Name, Is.Null);
      SqlExpressionTreeComparer.CheckAreEqualTrees (expectedPrimaryColumn, resolvedExpr.GetIdentityExpression());
      Assert.That (resolvedExpr.Columns, Has.Count.EqualTo (2));
      SqlExpressionTreeComparer.CheckAreEqualTrees (expectedPrimaryColumn, resolvedExpr.Columns[0]);
      SqlExpressionTreeComparer.CheckAreEqualTrees (expectedDescriptionColumn, resolvedExpr.Columns[1]);
    }
    public void Build_WithOrderByClause ()
    {
      var columnExpression = new SqlColumnDefinitionExpression (typeof (string), "t", "Name", false);
      var orderByClause = new Ordering (columnExpression, OrderingDirection.Asc);

      var sqlStatement = new SqlStatement (
          new TestStreamedValueInfo (typeof (int)),
          _entityExpression,
          new[] { _sqlTable },
          null,
          null,
          new[] { orderByClause },
          null,
          false,
          null,
          null);

      _stageMock.Expect (
          mock => mock.GenerateTextForSelectExpression (_commandBuilder, sqlStatement.SelectProjection))
          .WhenCalled (mi => ((SqlCommandBuilder) mi.Arguments[0]).Append ("[t].[ID],[t].[Name],[t].[City]"));
      _stageMock.Expect (mock => mock.GenerateTextForFromTable (_commandBuilder, sqlStatement.SqlTables[0], true))
          .WhenCalled (mi => ((SqlCommandBuilder) mi.Arguments[0]).Append ("[Table] AS [t]"));
      _stageMock.Expect (mock => mock.GenerateTextForOrdering (_commandBuilder, sqlStatement.Orderings[0]))
          .WhenCalled (mi => ((SqlCommandBuilder) mi.Arguments[0]).Append ("[t].[Name] ASC"));
      _stageMock.Replay();

      _generator.Build (sqlStatement, _commandBuilder, false);

      _commandBuilder.SetInMemoryProjectionBody (Expression.Constant (0));
      var result = _commandBuilder.GetCommand();

      Assert.That (result.CommandText, Is.EqualTo ("SELECT [t].[ID],[t].[Name],[t].[City] FROM [Table] AS [t] ORDER BY [t].[Name] ASC"));
      _stageMock.VerifyAllExpectations();
    }
    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 GenerateTextForJoinKeyExpression ()
    {
      var expression = new SqlColumnDefinitionExpression (typeof (int), "c", "ID", false);

      var stageMock = MockRepository.GeneratePartialMock<DefaultSqlGenerationStage>();
      stageMock
          .Expect (mock => CallGenerateTextForNonSelectExpression (mock, expression))
          .WhenCalled (c => _commandBuilder.Append ("test"));
      stageMock.Replay();

      stageMock.GenerateTextForJoinCondition (_commandBuilder, expression);

      stageMock.VerifyAllExpectations();
      Assert.That (_commandBuilder.GetCommandText(), Is.EqualTo ("test"));
    }
    public void BuildOrderBy_WithMultipleOrderByClauses ()
    {
      var columnExpression1 = new SqlColumnDefinitionExpression (typeof (string), "t", "ID", false);
      var orderByClause1 = new Ordering (columnExpression1, OrderingDirection.Asc);
      var columnExpression2 = new SqlColumnDefinitionExpression (typeof (string), "t", "Name", false);
      var orderByClause2 = new Ordering (columnExpression2, OrderingDirection.Desc);
      var columnExpression3 = new SqlColumnDefinitionExpression (typeof (string), "t", "City", false);
      var orderByClause3 = new Ordering (columnExpression3, OrderingDirection.Desc);

      var sqlStatement = new SqlStatement (
          new TestStreamedValueInfo (typeof (int)),
          _entityExpression,
          new[] { _sqlTable },
          null,
          null,
          new[] { orderByClause1, orderByClause2, orderByClause3 },
          null,
          false,
          null,
          null);

      _stageMock.Expect (mock => mock.GenerateTextForOrdering (_commandBuilder, sqlStatement.Orderings[0]))
          .WhenCalled (mi => ((SqlCommandBuilder) mi.Arguments[0]).Append ("[t].[ID] ASC"));
      _stageMock.Expect (mock => mock.GenerateTextForOrdering (_commandBuilder, sqlStatement.Orderings[1]))
          .WhenCalled (mi => ((SqlCommandBuilder) mi.Arguments[0]).Append ("[t].[Name] DESC"));
      _stageMock.Expect (mock => mock.GenerateTextForOrdering (_commandBuilder, sqlStatement.Orderings[2]))
          .WhenCalled (mi => ((SqlCommandBuilder) mi.Arguments[0]).Append ("[t].[City] DESC"));
      _stageMock.Replay();

      _generator.BuildOrderByPart (sqlStatement, _commandBuilder);

      Assert.That (_commandBuilder.GetCommandText(), Is.EqualTo (" ORDER BY [t].[ID] ASC, [t].[Name] DESC, [t].[City] DESC"));
      _stageMock.VerifyAllExpectations();
    }
    public void VisitChildren_ChangeColumn ()
    {
      var newColumnExpression = new SqlColumnDefinitionExpression (typeof (string), "o", "Test", false);

      var visitorMock = MockRepository.GenerateMock<ExpressionTreeVisitor>();
      var expectedColumns = new[] { _columnExpression1, newColumnExpression, _columnExpression3 };

      visitorMock.Expect (mock => mock.VisitAndConvert (_originalColumnsReadonly, "VisitChildren")).Return (Array.AsReadOnly (expectedColumns));
      visitorMock.Replay();

      var expression = (SqlEntityExpression) ExtensionExpressionTestHelper.CallVisitChildren (_entityExpression, visitorMock);

      Assert.That (expression, Is.Not.SameAs (_entityExpression));
      Assert.That (expression.Type, Is.SameAs (_entityExpression.Type));
      Assert.That (expression.Columns, Is.EqualTo (expectedColumns));
    }
    public void ResolveExpression_OptimizesEntityRefMemberComparisons ()
    {
      // This test proves that the first stage (without resolving SqlEntityRefMemberExpressions) is executed.
      var memberInfo = typeof (Kitchen).GetProperty ("Cook");
      var entityExpression = SqlStatementModelObjectMother.CreateSqlEntityDefinitionExpression (typeof (Kitchen));
      var fakeOptimizedRefIdentity = new SqlColumnDefinitionExpression (typeof (int), "c", "KitchenID", false);
      var entityRefMemberExpression = new SqlEntityRefMemberExpression (entityExpression, memberInfo);
      _resolverMock
          .Stub (stub => stub.TryResolveOptimizedIdentity (entityRefMemberExpression))
          .Return (fakeOptimizedRefIdentity);

      var entity = CreateEntityExpressionWithIdentity (typeof (Cook), typeof (int));
      var binary = Expression.Equal (entityRefMemberExpression, entity);

      var result = ResolvingExpressionVisitor.ResolveExpression (binary, _resolverMock, _stageMock, _mappingResolutionContext, _generator);

      var expected = Expression.Equal (fakeOptimizedRefIdentity, entity.GetIdentityExpression ());
      SqlExpressionTreeComparer.CheckAreEqualTrees (expected, result);
    }
    public void VisitAggregationExpression_Count ()
    {
      var columnExpression = new SqlColumnDefinitionExpression (typeof (string), "c", "Name", false);
      var expression = new AggregationExpression (typeof (int), columnExpression, AggregationModifier.Count);

      SqlGeneratingExpressionVisitor.GenerateSql (expression, _commandBuilder, _stageMock);

      Assert.That (_commandBuilder.GetCommandText(), Is.EqualTo ("COUNT(*)"));
    }
    public void ResolveMemberExpression_WithSqlColumnExpression ()
    {
      var columnExpression = new SqlColumnDefinitionExpression (typeof (string), "c", "CustomerID", true);

      var memberInfo = typeof (DataContextTestClass.Customer).GetProperty ("CustomerID");
      var result = _mappingResolver.ResolveMemberExpression (columnExpression, memberInfo);

      var expectedExpression = columnExpression;

      SqlExpressionTreeComparer.CheckAreEqualTrees (expectedExpression, result);
    }
    public void SimplifyIfPossible_SimplifiableStatement_AddsAggregationAndReturnsReference ()
    {
      Assert.That (_associatedGroupingSelectExpression.AggregationExpressions.Count, Is.EqualTo (0));

      var expression = new SqlSubStatementExpression (_simplifiableResolvedSqlStatement);

      var preparedResolvedAggregate = new AggregationExpression (
          typeof (int), 
          new NamedExpression ("element", Expression.Constant ("e")), 
          AggregationModifier.Count);
      _stageMock
          .Expect (mock => mock.ResolveAggregationExpression(Arg<Expression>.Is.Anything, Arg.Is (_context)))
          .Return (preparedResolvedAggregate)
          .WhenCalled (mi => {
            var expectedReplacedAggregate = new AggregationExpression (
                typeof (int),
                ((NamedExpression) _associatedGroupingSelectExpression.ElementExpression).Expression, 
                AggregationModifier.Count);
            SqlExpressionTreeComparer.CheckAreEqualTrees (expectedReplacedAggregate, (Expression) mi.Arguments[0]);
          });
      _stageMock.Replay();

      var result = _groupAggregateSimplifier.SimplifyIfPossible (expression, _simplifiableUnresolvedProjection);

      _stageMock.VerifyAllExpectations();

      Assert.That (_associatedGroupingSelectExpression.AggregationExpressions.Count, Is.EqualTo (1));
      Assert.That (
          ((NamedExpression) _associatedGroupingSelectExpression.AggregationExpressions[0]).Expression, 
          Is.SameAs (preparedResolvedAggregate));

      var expected = new SqlColumnDefinitionExpression (typeof (int), "q0", "a0", false);
      SqlExpressionTreeComparer.CheckAreEqualTrees (expected, result);
    }
    public void ResolveCollectionSourceExpression ()
    {
      var constantExpression = Expression.Constant (new Cook());
      var expression = Expression.MakeMemberAccess (constantExpression, typeof (Cook).GetProperty ("FirstName"));
      var sqlColumnExpression = new SqlColumnDefinitionExpression (typeof (string), "c", "Name", false);
      var fakeResult = SqlStatementModelObjectMother.CreateSqlEntityDefinitionExpression (typeof (Cook));

      _resolverMock
          .Expect (mock => mock.ResolveConstantExpression (constantExpression))
          .Return (sqlColumnExpression);
      _resolverMock
          .Expect (mock => mock.ResolveMemberExpression (sqlColumnExpression, expression.Member))
          .Return (fakeResult);
      _resolverMock.Replay();

      var result = _stage.ResolveCollectionSourceExpression (expression, _mappingResolutionContext);

      _resolverMock.VerifyAllExpectations();

      Assert.That (result, Is.SameAs (fakeResult));
    }
    public void ResolveMemberAccess ()
    {
      var sourceExpression = new SqlColumnDefinitionExpression (typeof (Cook), "c", "Substitution", false);
      var memberInfo = typeof (Cook).GetProperty ("Name");
      var fakeResolvedExpression = new SqlLiteralExpression ("Hugo");

      _resolverMock
          .Expect (mock => mock.ResolveMemberExpression (sourceExpression, memberInfo))
          .Return (fakeResolvedExpression);
      _resolverMock.Replay();

      var result = _stage.ResolveMemberAccess (sourceExpression, memberInfo, _resolverMock, _mappingResolutionContext);

      _resolverMock.VerifyAllExpectations();
      Assert.That (result, Is.SameAs (fakeResolvedExpression));
    }
    public void TryResolveOptimizedMemberExpression_IdentityMember_ForeignKeyOnTheLeft ()
    {
      var tableInfo = new ResolvedSimpleTableInfo (typeof (DataContextTestClass.ClassWithOneToOneRelation_ForeignKeySide), "X", "x");

      var originatingEntity = new SqlEntityDefinitionExpression (
          tableInfo.ItemType, tableInfo.TableAlias, null, e => e.GetColumn (typeof (int), "ID", true));

      var relationMember = tableInfo.ItemType.GetProperty ("VirtualSide");
      var entityRefMemberExpression = new SqlEntityRefMemberExpression (originatingEntity, relationMember);

      var identityMember = typeof (DataContextTestClass.ClassWithOneToOneRelation_VirtualSide).GetProperty ("ID");
      var result = _mappingResolver.TryResolveOptimizedMemberExpression (entityRefMemberExpression, identityMember);

      var orderForeignKey = new SqlColumnDefinitionExpression (typeof (int), tableInfo.TableAlias, "VirtualSideID", false);
      SqlExpressionTreeComparer.CheckAreEqualTrees (orderForeignKey, result);
    }
 private ResolvedJoinInfo CreateResolvedJoinInfo (
     Type type, string originalTableAlias, string leftSideKeyName, string joinedTableName, string joinedTableAlias, string rightSideKeyName)
 {
   var foreignTableSource = new ResolvedSimpleTableInfo (type, joinedTableName, joinedTableAlias);
   var primaryColumn = new SqlColumnDefinitionExpression (typeof (int), originalTableAlias, leftSideKeyName, false);
   var foreignColumn = new SqlColumnDefinitionExpression (typeof (int), joinedTableAlias, rightSideKeyName, false);
   return new ResolvedJoinInfo (foreignTableSource, Expression.Equal (primaryColumn, foreignColumn));
 }
 public static ResolvedJoinInfo CreateResolvedJoinInfo (Type type)
 {
   var primaryColumn = new SqlColumnDefinitionExpression (typeof (int), "k", "ID", false);
   var foreignColumn = new SqlColumnDefinitionExpression (typeof (int), "s", "ID", false);
   var foreignTableInfo = new ResolvedSimpleTableInfo (type, "Table", "s");
   return new ResolvedJoinInfo (foreignTableInfo, Expression.Equal (primaryColumn, foreignColumn));
 }
    public void VisitJoinConditionExpression ()
    {
      var resolvedTableInfo = new ResolvedSimpleTableInfo (typeof (Cook), "CookTable", "c");
      var leftKey = new SqlColumnDefinitionExpression (typeof (Cook), "c", "ID", false);
      var rightKey = new SqlColumnDefinitionExpression (typeof (Cook), "a", "FK", false);
      var joinInfo = new ResolvedJoinInfo (resolvedTableInfo, Expression.Equal (leftKey, rightKey));
      var sqlTable = new SqlJoinedTable (joinInfo, JoinSemantics.Left);
      var joinConditionExpression = new JoinConditionExpression (sqlTable);

      _entityIdentityResolverMock
          .Expect (
              mock => mock.ResolvePotentialEntityComparison (
                  Arg<BinaryExpression>.Matches (b => b.Left == leftKey && b.Right == rightKey)))
          .Return (null)
          .WhenCalled (mi => mi.ReturnValue = mi.Arguments[0]);
      _compoundComparisonSplitterMock
          .Expect (
              mock => mock.SplitPotentialCompoundComparison (
                  Arg<BinaryExpression>.Matches (b => b.Left == leftKey && b.Right == rightKey)))
          .Return (null)
          .WhenCalled (mi => mi.ReturnValue = mi.Arguments[0]);

      var result = _visitor.VisitExpression (joinConditionExpression);

      _entityIdentityResolverMock.VerifyAllExpectations();
      _compoundComparisonSplitterMock.VerifyAllExpectations();

      var expectedExpression = Expression.Equal (leftKey, rightKey);
      SqlExpressionTreeComparer.CheckAreEqualTrees (expectedExpression, result);
    }