public void GetStatementAndResetBuilder() { var statementBuilder = new SqlStatementBuilder { DataInfo = new TestStreamedValueInfo(typeof(Cook)), TopExpression = ExpressionHelper.CreateExpression(), IsDistinctQuery = true, SelectProjection = new AggregationExpression(typeof(int), Expression.Constant(1), AggregationModifier.Min), SqlTables = { new SqlTable(new ResolvedSimpleTableInfo(typeof(Cook), "CookTable", "c"), JoinSemantics.Inner) }, WhereCondition = Expression.Constant(true), RowNumberSelector = Expression.Constant("selector"), CurrentRowNumberOffset = Expression.Constant(1), GroupByExpression = Expression.Constant("group"), Orderings = { new Ordering(Expression.Constant("order"), OrderingDirection.Desc) } }; var sqlStatement = statementBuilder.GetSqlStatement(); var result = statementBuilder.GetStatementAndResetBuilder(); Assert.That(result, Is.Not.SameAs(sqlStatement)); Assert.That(result, Is.EqualTo(sqlStatement)); Assert.That(statementBuilder.DataInfo, Is.Null); Assert.That(statementBuilder.TopExpression, Is.Null); Assert.That(statementBuilder.IsDistinctQuery, Is.False); Assert.That(statementBuilder.SelectProjection, Is.Null); Assert.That(statementBuilder.SqlTables, Is.Empty); Assert.That(statementBuilder.Orderings, Is.Empty); Assert.That(statementBuilder.WhereCondition, Is.Null); Assert.That(statementBuilder.RowNumberSelector, Is.Null); Assert.That(statementBuilder.CurrentRowNumberOffset, Is.Null); Assert.That(statementBuilder.GroupByExpression, Is.Null); }
private SubStatementWithRowNumber CreateSubStatementWithRowNumber( SqlStatementBuilder sqlStatementBuilder, UniqueIdentifierGenerator generator, ISqlPreparationStage stage, ISqlPreparationContext context) { var originalSelectProjection = sqlStatementBuilder.SelectProjection; IncludeRowNumberInSelectProjection(sqlStatementBuilder, stage, context); // Orderings are not allowed in SQL substatements unless a TOP expression is present if (sqlStatementBuilder.TopExpression == null) { sqlStatementBuilder.Orderings.Clear(); } sqlStatementBuilder.RecalculateDataInfo(originalSelectProjection); // No NamedExpression required here because the row number tuple's items already have named members var newSqlStatement = sqlStatementBuilder.GetStatementAndResetBuilder(); var tableInfo = new ResolvedSubStatementTableInfo(generator.GetUniqueIdentifier("q"), newSqlStatement); var sqlTable = new SqlTable(tableInfo, JoinSemantics.Inner); var originalProjectionSelector = Expression.MakeMemberAccess( new SqlTableReferenceExpression(sqlTable), newSqlStatement.SelectProjection.Type.GetProperty("Key")); var rowNumberSelector = Expression.MakeMemberAccess( new SqlTableReferenceExpression(sqlTable), newSqlStatement.SelectProjection.Type.GetProperty("Value")); return(new SubStatementWithRowNumber(sqlTable, originalProjectionSelector, rowNumberSelector)); }
protected void MoveCurrentStatementToSqlTable( SqlStatementBuilder sqlStatementBuilder, ISqlPreparationContext context, Func <ITableInfo, SqlTable> tableGenerator, ISqlPreparationStage stage, OrderingExtractionPolicy orderingExtractionPolicy = OrderingExtractionPolicy.ExtractOrderingsIntoProjection) { // Ensure that select clause is named - usually SqlPreparationQueryModelVisitor would do this, but it hasn't done it yet sqlStatementBuilder.SelectProjection = new NamedExpression(null, sqlStatementBuilder.SelectProjection); var oldStatement = sqlStatementBuilder.GetStatementAndResetBuilder(); var fromExpressionInfo = stage.PrepareFromExpression( new SqlSubStatementExpression(oldStatement), context, tableGenerator, orderingExtractionPolicy); sqlStatementBuilder.SqlTables.Add(fromExpressionInfo.SqlTable); sqlStatementBuilder.SelectProjection = fromExpressionInfo.ItemSelector; sqlStatementBuilder.Orderings.AddRange(fromExpressionInfo.ExtractedOrderings); Assertion.DebugAssert(fromExpressionInfo.WhereCondition == null); // the new statement is an identity query that selects the result of its subquery, so it starts with the same data type sqlStatementBuilder.DataInfo = oldStatement.DataInfo; AddMappingForItemExpression(context, oldStatement.DataInfo, fromExpressionInfo.ItemSelector); }
public override void HandleResultOperator(AnyResultOperator resultOperator, SqlStatementBuilder sqlStatementBuilder, UniqueIdentifierGenerator generator, ISqlPreparationStage stage, ISqlPreparationContext context) { ArgumentUtility.CheckNotNull("resultOperator", resultOperator); ArgumentUtility.CheckNotNull("sqlStatementBuilder", sqlStatementBuilder); ArgumentUtility.CheckNotNull("generator", generator); ArgumentUtility.CheckNotNull("stage", stage); ArgumentUtility.CheckNotNull("context", context); // No name required for the select projection inside of an EXISTS expression var sqlSubStatement = sqlStatementBuilder.GetStatementAndResetBuilder(); var subStatementExpression = new SqlSubStatementExpression(sqlSubStatement); var sqlExistsExpression = new SqlExistsExpression(subStatementExpression); sqlStatementBuilder.SelectProjection = stage.PrepareSelectExpression(sqlExistsExpression, context); UpdateDataInfo(resultOperator, sqlStatementBuilder, sqlSubStatement.DataInfo); }
public override void HandleResultOperator(ContainsResultOperator resultOperator, SqlStatementBuilder sqlStatementBuilder, UniqueIdentifierGenerator generator, ISqlPreparationStage stage, ISqlPreparationContext context) { ArgumentUtility.CheckNotNull("resultOperator", resultOperator); ArgumentUtility.CheckNotNull("sqlStatementBuilder", sqlStatementBuilder); ArgumentUtility.CheckNotNull("generator", generator); ArgumentUtility.CheckNotNull("stage", stage); ArgumentUtility.CheckNotNull("context", context); var dataInfo = sqlStatementBuilder.DataInfo; var preparedItemExpression = stage.PrepareResultOperatorItemExpression(resultOperator.Item, context); // No name required for the select projection inside of an IN expression // (If the expression is a constant collection, a name would even be fatal.) var sqlSubStatement = sqlStatementBuilder.GetStatementAndResetBuilder(); var subStatementExpression = sqlSubStatement.CreateExpression(); sqlStatementBuilder.SelectProjection = new SqlInExpression(preparedItemExpression, subStatementExpression); UpdateDataInfo(resultOperator, sqlStatementBuilder, dataInfo); }