/// <summary> /// Clones this item. /// </summary> /// <param name="cloneContext">The clones of all query source clauses are registered with this <see cref="CloneContext"/>.</param> /// <returns>A clone of this item.</returns> virtual public Ordering Clone (CloneContext cloneContext) { ArgumentUtility.CheckNotNull ("cloneContext", cloneContext); var clone = new Ordering (Expression, OrderingDirection); return clone; }
public SqlRowNumberExpression (Ordering[] orderings) : base (typeof (int)) { ArgumentUtility.CheckNotNull ("orderings", orderings); ArgumentUtility.CheckNotEmpty ("orderings", orderings); _orderings = Array.AsReadOnly (orderings); }
public void InitializeWithExpressionAndOrderDirectionAsc() { var expression = ExpressionHelper.CreateExpression (); var ordering = new Ordering (expression, OrderingDirection.Asc); Assert.That (ordering.Expression, Is.SameAs (expression)); Assert.That (ordering.OrderingDirection, Is.EqualTo (OrderingDirection.Asc)); }
public FromExpressionInfo (SqlTable sqlTable, Ordering[] extractedOrderings, Expression itemSelector, Expression whereCondition) { ArgumentUtility.CheckNotNull ("sqlTable", sqlTable); ArgumentUtility.CheckNotNull ("extractedOrderings", extractedOrderings); ArgumentUtility.CheckNotNull ("itemSelector", itemSelector); SqlTable = sqlTable; ExtractedOrderings = Array.AsReadOnly(extractedOrderings); ItemSelector = itemSelector; WhereCondition = whereCondition; }
public void HandleResultOperator_WithOrderingsAndTopExpression () { _sqlStatementBuilder.TopExpression = Expression.Constant ("top"); var ordering = new Ordering (Expression.Constant ("order"), OrderingDirection.Asc); _sqlStatementBuilder.Orderings.Add (ordering); var averageResultOperator = new MaxResultOperator (); _handler.HandleResultOperator (averageResultOperator, _sqlStatementBuilder, _generator, _stage, _context); Assert.That (_sqlStatementBuilder.Orderings.Count, Is.EqualTo (0)); Assert.That ( ((ResolvedSubStatementTableInfo) ((SqlTable) _sqlStatementBuilder.SqlTables[0]).TableInfo).SqlStatement.Orderings.Count, Is.EqualTo (1)); Assert.That ( ((ResolvedSubStatementTableInfo) ((SqlTable) _sqlStatementBuilder.SqlTables[0]).TableInfo).SqlStatement.Orderings[0], Is.SameAs(ordering)); }
public void SetUp () { _mockRepository = new MockRepository(); _visitorMock = _mockRepository.StrictMock<TestQueryModelVisitor>(); _bodyClauseMock1 = _mockRepository.StrictMock<IBodyClause>(); _bodyClauseMock2 = _mockRepository.StrictMock<IBodyClause>(); _ordering1 = new Ordering (ExpressionHelper.CreateExpression(), OrderingDirection.Asc); _ordering2 = new Ordering (ExpressionHelper.CreateExpression(), OrderingDirection.Asc); _resultOperator1 = new TestResultOperator(); _resultOperator2 = new TestResultOperator(); _queryModel = ExpressionHelper.CreateQueryModel<Cook>(); _orderByClause = ExpressionHelper.CreateOrderByClause(); _groupJoinClause = ExpressionHelper.CreateGroupJoinClause<Cook>(); }
public void SetUp () { _mockRepository = new MockRepository(); _visitorMock = _mockRepository.StrictMock<QueryModelVisitorBase>(); _testVisitor = new TestQueryModelVisitor (); _bodyClauseMock1 = _mockRepository.StrictMock<WhereClause> (ExpressionHelper.CreateExpression()); _bodyClauseMock2 = _mockRepository.StrictMock<WhereClause> (ExpressionHelper.CreateExpression()); _orderingMock1 = _mockRepository.StrictMock<Ordering> (ExpressionHelper.CreateExpression(), OrderingDirection.Asc); _orderingMock2 = _mockRepository.StrictMock<Ordering> (ExpressionHelper.CreateExpression(), OrderingDirection.Asc); _resultOperatorMock1 = _mockRepository.StrictMock<ResultOperatorBase> (); _resultOperatorMock2 = _mockRepository.StrictMock<ResultOperatorBase> (); _queryModel = ExpressionHelper.CreateQueryModel<Cook> (); _orderByClause = ExpressionHelper.CreateOrderByClause (); _groupJoinClause = ExpressionHelper.CreateGroupJoinClause<Cook> (); }
public void SetUp () { _stageMock = MockRepository.GenerateMock<ISqlPreparationStage> (); _generator = new UniqueIdentifierGenerator (); _context = SqlStatementModelObjectMother.CreateSqlPreparationContext (); _handler = new SkipResultOperatorHandler (); _sqlTable = new SqlTable (new UnresolvedTableInfo (typeof (Cook)), JoinSemantics.Inner); _selectProjection = new SqlTableReferenceExpression (_sqlTable); _ordering = new Ordering (Expression.Constant (7), OrderingDirection.Asc); _sqlStatementBuilder = new SqlStatementBuilder { SelectProjection = _selectProjection, DataInfo = new StreamedSequenceInfo (typeof (Cook[]), Expression.Constant (new Cook ())), SqlTables = { _sqlTable }, }; _tupleCtor = _tupleCtor = typeof (KeyValuePair<Cook, int>).GetConstructor (new[] { typeof (Cook), typeof (int) }); }
protected override Expression VisitChildren(ExpressionVisitor visitor) { var newColumnExpression = visitor.Visit(ColumnExpression) as ColumnExpression; var newOrderings = new List<Ordering>(); var recreate = false; foreach (var ordering in _orderings) { var newOrdering = new Ordering(visitor.Visit(ordering.Expression), ordering.OrderingDirection); newOrderings.Add(newOrdering); recreate |= newOrdering.Expression != ordering.Expression; } if (recreate || (newColumnExpression != null && ColumnExpression != newColumnExpression)) { return new RowNumberExpression(newColumnExpression, newOrderings); } return this; }
public void Equals_EqualStatementsWithAllMembers () { var dataInfo = new TestStreamedValueInfo (typeof (int)); var isDistinctQuery = BooleanObjectMother.GetRandomBoolean(); var selectProjection = Expression.Constant (1); var sqlTable = new SqlTable (new ResolvedSimpleTableInfo (typeof (Cook), "CookTable", "c"), JoinSemantics.Inner); var ordering = new Ordering (Expression.Constant ("ordering"), OrderingDirection.Asc); var whereCondition = Expression.Constant (true); var topExpression = Expression.Constant ("top"); var rowNumberSelector = Expression.Constant ("selector"); var currentRowNumberOffset = Expression.Constant (1); var groupByExpression = Expression.Constant ("group"); var sqlStatement1 = new SqlStatement ( dataInfo, selectProjection, new[] { sqlTable }, whereCondition, groupByExpression, new[] { ordering }, topExpression, isDistinctQuery, rowNumberSelector, currentRowNumberOffset); var sqlStatement2 = new SqlStatement ( dataInfo, selectProjection, new[] { sqlTable }, whereCondition, groupByExpression, new[] { ordering }, topExpression, isDistinctQuery, rowNumberSelector, currentRowNumberOffset); Assert.That (sqlStatement1.Equals (sqlStatement2), Is.True); }
public override void VisitOrdering(Ordering ordering, QueryModel queryModel, OrderByClause orderByClause, int index) { if (_query.OrderByPredicate == null) { _query.OrderByPredicate = ODataQueryPart.OrderBy(); } if (ordering.Expression.NodeType == ExpressionType.MemberAccess) { var memberExpression = (MemberExpression)ordering.Expression; var direction = (ordering.OrderingDirection == OrderingDirection.Asc) ? ODataOrderingDirection.Asc : ODataOrderingDirection.Desc; var o = ODataQueryPart.Ordering(memberExpression.Member.Name, direction); _query.OrderByPredicate.AddOrdering(o); } base.VisitOrdering(ordering, queryModel, orderByClause, index); }
public void Initialization_WithExistingSqlStatement () { var isDistinctQuery = BooleanObjectMother.GetRandomBoolean(); var selectProjection = Expression.Constant ("select"); var whereCondition = Expression.Constant (true); var topExpression = Expression.Constant ("top"); var sqlTable = new SqlTable (new ResolvedSimpleTableInfo (typeof (Cook), "CookTable", "c"), JoinSemantics.Inner); var ordering = new Ordering (Expression.Constant ("order"), OrderingDirection.Desc); var rowNumberSelector = Expression.Constant("selector"); var currentRowNumberOffset = Expression.Constant(1); var groupExpression = Expression.Constant ("group"); var sqlStatement = new SqlStatement ( new TestStreamedValueInfo (typeof (int)), selectProjection, new[] { sqlTable }, whereCondition, groupExpression, new[] { ordering }, topExpression, isDistinctQuery, rowNumberSelector, currentRowNumberOffset); var testedBuilder = new SqlStatementBuilder (sqlStatement); Assert.That (testedBuilder.SelectProjection, Is.SameAs (selectProjection)); Assert.That (testedBuilder.TopExpression, Is.SameAs (topExpression)); Assert.That (testedBuilder.SqlTables[0], Is.SameAs (sqlTable)); Assert.That (testedBuilder.Orderings[0], Is.SameAs (ordering)); Assert.That (testedBuilder.WhereCondition, Is.EqualTo (whereCondition)); Assert.That (testedBuilder.IsDistinctQuery, Is.EqualTo (isDistinctQuery)); Assert.That (testedBuilder.DataInfo, Is.SameAs (sqlStatement.DataInfo)); Assert.That (testedBuilder.RowNumberSelector, Is.SameAs (sqlStatement.RowNumberSelector)); Assert.That (testedBuilder.CurrentRowNumberOffset, Is.SameAs (currentRowNumberOffset)); Assert.That (testedBuilder.GroupByExpression, Is.SameAs (groupExpression)); }
private void ExpandCompositeOrderings(OrderByClause orderByClause) { var orderings = orderByClause.Orderings; var copy = new Ordering[orderings.Count]; orderings.CopyTo(copy, 0); copy.Apply(o => orderings.Remove(o)); foreach (var o in copy) { if (o.Expression is LuceneCompositeOrderingExpression) { var ex = (LuceneCompositeOrderingExpression) o.Expression; ex.Fields.Apply(f => orderings.Add(new Ordering(f, o.OrderingDirection))); } else { orderings.Add(o); } } }
public void ToString_AllProperties () { var dataInfo = new TestStreamedValueInfo (typeof (int)); var selectProjection = Expression.Constant (1); var sqlTable1 = new SqlTable (new ResolvedSimpleTableInfo (typeof (Cook), "CookTable", "c"), JoinSemantics.Inner); var sqlTable2 = new SqlTable (new ResolvedSimpleTableInfo (typeof (Kitchen), "KitchenTable", "k"), JoinSemantics.Left); var ordering = new Ordering (Expression.Constant ("ordering"), OrderingDirection.Asc); var whereCondition = Expression.Constant (true); var topExpression = Expression.Constant (10); var groupExpression = Expression.Constant ("group"); var builder = new SqlStatementBuilder { DataInfo = dataInfo, SelectProjection = selectProjection, SqlTables = { sqlTable1, sqlTable2 }, Orderings = { ordering }, WhereCondition = whereCondition, TopExpression = topExpression, IsDistinctQuery = true, GroupByExpression = groupExpression }; var result = builder.ToString(); Assert.That ( result, Is.EqualTo ( "SELECT DISTINCT TOP (10) 1 FROM [CookTable] [c], [KitchenTable] [k] WHERE True GROUP BY \"group\" ORDER BY \"ordering\" ASC")); }
public void GetSqlStatement_CheckProperties () { var dataInfo = new TestStreamedValueInfo (typeof (Cook)); var topExpression = ExpressionHelper.CreateExpression(); var isDistinctQuery = BooleanObjectMother.GetRandomBoolean(); var selectProjection = new AggregationExpression (typeof (int), Expression.Constant (1), AggregationModifier.Min); var whereCondition = Expression.Constant (true); var sqlTable = new SqlTable (new ResolvedSimpleTableInfo (typeof (Cook), "CookTable", "c"), JoinSemantics.Inner); var ordering = new Ordering (Expression.Constant ("order"), OrderingDirection.Desc); var rowNumberSelector = Expression.Constant ("selector"); var currentRowNumberOffset = Expression.Constant (1); var groupExpression = Expression.Constant ("group"); var statementBuilder = new SqlStatementBuilder { DataInfo = dataInfo, TopExpression = topExpression, IsDistinctQuery = isDistinctQuery, SelectProjection = selectProjection, SqlTables = { sqlTable }, WhereCondition = whereCondition, RowNumberSelector = rowNumberSelector, CurrentRowNumberOffset = currentRowNumberOffset, GroupByExpression = groupExpression, Orderings = { ordering } }; var sqlStatement = statementBuilder.GetSqlStatement(); Assert.That (sqlStatement.DataInfo, Is.SameAs (dataInfo)); Assert.That (sqlStatement.TopExpression, Is.SameAs (topExpression)); Assert.That (sqlStatement.IsDistinctQuery, Is.EqualTo (isDistinctQuery)); Assert.That (sqlStatement.SelectProjection, Is.SameAs (selectProjection)); Assert.That (sqlStatement.SqlTables[0], Is.SameAs (sqlTable)); Assert.That (sqlStatement.Orderings[0], Is.SameAs (ordering)); Assert.That (sqlStatement.WhereCondition, Is.SameAs (whereCondition)); Assert.That (sqlStatement.RowNumberSelector, Is.SameAs (rowNumberSelector)); Assert.That (sqlStatement.CurrentRowNumberOffset, Is.SameAs (currentRowNumberOffset)); Assert.That (sqlStatement.GroupByExpression, Is.SameAs (groupExpression)); }
public override void VisitOrdering(Ordering ordering, QueryModel queryModel, OrderByClause orderByClause, int index) { ordering.TransformExpressions(e => MergeAggregatingResultsInExpressionRewriter.Rewrite(e, new NameGenerator(queryModel))); }
public void Transform(Ordering ordering) { ordering.TransformExpressions(VisitExpression); }
/// <summary> /// This API supports the Entity Framework Core infrastructure and is not intended to be used /// directly from your code. This API may change or be removed in future releases. /// </summary> public override void VisitOrdering(Ordering ordering, QueryModel queryModel, OrderByClause orderByClause, int index) => ordering.TransformExpressions(TransformingVisitor.Visit);
public string ToOrderClause(Ordering clause) { var locator = _parser.JsonLocator(_mapping, clause.Expression); return clause.OrderingDirection == OrderingDirection.Asc ? locator : locator + " desc"; }
public void GenerateTextForOrdering (ISqlCommandBuilder commandBuilder, Ordering ordering) { ArgumentUtility.CheckNotNull ("commandBuilder", commandBuilder); ArgumentUtility.CheckNotNull ("ordering", ordering); if (ordering.Expression.NodeType == ExpressionType.Constant || ordering.Expression is SqlLiteralExpression) { commandBuilder.Append ("(SELECT "); GenerateTextForOrderByExpression (commandBuilder, ordering.Expression); commandBuilder.Append (")"); } else GenerateTextForOrderByExpression (commandBuilder, ordering.Expression); commandBuilder.AppendFormat (string.Format (" {0}", ordering.OrderingDirection.ToString ().ToUpper ())); }
public void VisitSqlStatement_EverthingIsCopiedIfSelectionProjectionHasChanged () { var selectProjection = Expression.Constant("select"); var whereCondition = Expression.Constant(true); var topExpression = Expression.Constant("top"); var dataInfo = new StreamedSequenceInfo(typeof(Cook[]), Expression.Constant(new Cook())); var builder = new SqlStatementBuilder () { SelectProjection = selectProjection, WhereCondition = whereCondition, TopExpression = topExpression, IsDistinctQuery = true, DataInfo = dataInfo }; var sqlTable = new SqlTable (new ResolvedSimpleTableInfo (typeof (Cook), "CookTable", "c"), JoinSemantics.Inner); builder.SqlTables.Add (sqlTable); var ordering = new Ordering (Expression.Constant ("order"),OrderingDirection.Asc); builder.Orderings.Add (ordering); var sqlStatement = builder.GetSqlStatement(); var fakeResult = Expression.Constant ("fake"); _stageMock .Expect (mock => mock.ApplyContext (sqlStatement.SelectProjection, SqlExpressionContext.ValueRequired, _mappingResolutionContext)) .Return (fakeResult); _stageMock.Replay (); var result = SqlContextSelectionAdjuster.ApplyContext (sqlStatement, SqlExpressionContext.ValueRequired, _stageMock, _mappingResolutionContext); _stageMock.VerifyAllExpectations(); Assert.That (result.SelectProjection, Is.SameAs (fakeResult)); Assert.That (result.DataInfo, Is.SameAs (dataInfo)); Assert.That (result.WhereCondition, Is.SameAs (whereCondition)); Assert.That (result.TopExpression, Is.SameAs (topExpression)); Assert.That (result.SqlTables[0], Is.SameAs (sqlTable)); Assert.That (result.Orderings[0], Is.SameAs (ordering)); Assert.That (result.IsDistinctQuery, Is.True); }
public void NumberExpression () { var ordering1 = new Ordering (Expression.Constant ("order1"), OrderingDirection.Asc); var ordering2 = new Ordering (Expression.Constant ("order2"), OrderingDirection.Desc); var sqlRowNumberRÉxpression = new SqlRowNumberExpression ( new[] { ordering1, ordering2 }); _stageMock .Expect (mock => mock.GenerateTextForOrdering (_commandBuilder, ordering1)) .WhenCalled (mi => ((SqlCommandBuilder) mi.Arguments[0]).Append ("order1 ASC")); _stageMock .Expect (mock => mock.GenerateTextForOrdering (_commandBuilder, ordering2)) .WhenCalled (mi => ((SqlCommandBuilder) mi.Arguments[0]).Append ("order2 DESC")); _stageMock.Replay(); SqlGeneratingExpressionVisitor.GenerateSql (sqlRowNumberRÉxpression, _commandBuilder, _stageMock); _stageMock.VerifyAllExpectations(); Assert.That (_commandBuilder.GetCommandText(), Is.EqualTo ("ROW_NUMBER() OVER (ORDER BY order1 ASC, order2 DESC)")); }
public override void VisitOrdering( Ordering ordering, QueryModel queryModel, OrderByClause orderByClause, int index) { QueryBuildResult result = QueryBuildingVisitor.Build(ordering.Expression); PropertyExpression propExpr = result.Expression as PropertyExpression; OrderDirection direction = ordering.OrderingDirection == OrderingDirection.Asc ? OrderDirection.Ascending : OrderDirection.Descending; _orderByAggregator.Add(new OrderData(propExpr, direction)); _returnProperties.AddRange(result.PropertiesUsed); base.VisitOrdering(ordering, queryModel, orderByClause, index); }
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(); }
private static Ordering UpdateColumnExpression(Ordering ordering, TableExpressionBase tableExpression) { var newExpression = UpdateColumnExpression(ordering.Expression, tableExpression); var newOrdering = new Ordering(newExpression, ordering.OrderingDirection); return newOrdering; }
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 virtual void VisitOrdering(Ordering ordering, QueryModel queryModel, OrderByClause orderByClause, int index) { ArgumentUtility.CheckNotNull ("ordering", ordering); ArgumentUtility.CheckNotNull ("queryModel", queryModel); ArgumentUtility.CheckNotNull ("orderByClause", orderByClause); // nothing to do here }
private FromExpressionInfo CreateFakeFromExpressionInfo (Ordering[] extractedOrderings) { return new FromExpressionInfo ( new SqlTable (new ResolvedSubStatementTableInfo("sc", _statementBuilder.GetSqlStatement()), JoinSemantics.Inner), extractedOrderings, Expression.Constant (0), null); }
public override void VisitOrdering(Ordering ordering, QueryModel queryModel, OrderByClause orderByClause, int index) { _resultOperatorAndOrderByJoinDetector.Transform(ordering); }
public void MoveCurrentStatementToSqlTable_WithOrderings () { var ordering = new Ordering (Expression.Constant ("order1"), OrderingDirection.Desc); var fakeFromExpressionInfo = CreateFakeFromExpressionInfo (new[] { ordering }); _stageMock .Expect ( mock => mock.PrepareFromExpression (Arg<Expression>.Is.Anything, Arg<ISqlPreparationContext>.Is.Anything, Arg<Func<ITableInfo, SqlTable>>.Is.Anything)) .Return (fakeFromExpressionInfo); _stageMock.Replay (); _handler.MoveCurrentStatementToSqlTable (_statementBuilder, _generator, _context, info => new SqlTable (info, JoinSemantics.Inner), _stageMock); Assert.That (_statementBuilder.Orderings[0], Is.SameAs (ordering)); }