Exemplo n.º 1
0
        public void AddWhereCondition_MultipleWhereCondition()
        {
            var statementBuilder = new SqlStatementBuilder();

            var expression1 = Expression.Constant(true);

            statementBuilder.AddWhereCondition(expression1);
            var expression2 = Expression.Constant(false);

            statementBuilder.AddWhereCondition(expression2);

            Assert.That(((BinaryExpression)statementBuilder.WhereCondition).Left, Is.EqualTo(expression1));
            Assert.That(((BinaryExpression)statementBuilder.WhereCondition).Right, Is.EqualTo(expression2));
            Assert.That(statementBuilder.WhereCondition.NodeType, Is.EqualTo(ExpressionType.AndAlso));
        }
Exemplo n.º 2
0
        public override void HandleResultOperator(
            TakeResultOperator resultOperator,
            SqlStatementBuilder sqlStatementBuilder,
            UniqueIdentifierGenerator generator,
            ISqlPreparationStage stage,
            ISqlPreparationContext context)
        {
            EnsureNoTopExpression(sqlStatementBuilder, generator, stage, context);
            EnsureNoGroupExpression(sqlStatementBuilder, generator, stage, context);
            UpdateDataInfo(resultOperator, sqlStatementBuilder, sqlStatementBuilder.DataInfo);

            var preparedTopExpression = stage.PrepareTopExpression(resultOperator.Count, context);

            if (sqlStatementBuilder.RowNumberSelector != null)
            {
                var whereCondition = Expression.LessThanOrEqual(
                    sqlStatementBuilder.RowNumberSelector,
                    Expression.Add(sqlStatementBuilder.CurrentRowNumberOffset, preparedTopExpression));
                sqlStatementBuilder.AddWhereCondition(whereCondition);
            }
            else
            {
                var topExpressionAsConstantExpression = preparedTopExpression as ConstantExpression;
                if (topExpressionAsConstantExpression != null)
                {
                    sqlStatementBuilder.TopExpression = new SqlLiteralExpression((int)topExpressionAsConstantExpression.Value);
                }
                else
                {
                    sqlStatementBuilder.TopExpression = preparedTopExpression;
                }
            }
        }
Exemplo n.º 3
0
        public void AddWhereCondition_SingleWhereCondition()
        {
            var statementBuilder = new SqlStatementBuilder();

            var expression = Expression.Constant("whereTest");

            statementBuilder.AddWhereCondition(expression);

            Assert.That(statementBuilder.WhereCondition, Is.EqualTo(expression));
        }
        public ITableInfo VisitUnresolvedGroupReferenceTableInfo(UnresolvedGroupReferenceTableInfo tableInfo)
        {
            var groupSourceSubStatementTableInfo = tableInfo.ReferencedGroupSource.GetResolvedTableInfo() as ResolvedSubStatementTableInfo;

            if (groupSourceSubStatementTableInfo == null)
            {
                var message = string.Format(
                    "This SQL generator only supports sequences in from expressions if they are members of an entity or if they come from a GroupBy operator. "
                    + "Sequence: '{0}'", tableInfo);
                throw new NotSupportedException(message);
            }

            var groupingSelectExpression = groupSourceSubStatementTableInfo.SqlStatement.SelectProjection as SqlGroupingSelectExpression;

            if (groupingSelectExpression == null)
            {
                throw new NotSupportedException(
                          "When a sequence retrieved by a subquery is used in a from expression, the subquery must end with a GroupBy operator.");
            }

            var elementSelectingStatementBuilder = new SqlStatementBuilder(groupSourceSubStatementTableInfo.SqlStatement)
            {
                GroupByExpression = null
            };

            var currentKeyExpression = Expression.MakeMemberAccess(
                new SqlTableReferenceExpression(tableInfo.ReferencedGroupSource),
                groupingSelectExpression.Type.GetProperty("Key"));

            var groupKeyJoinCondition = _stage.ResolveWhereExpression(
                Expression.OrElse(
                    Expression.AndAlso(new SqlIsNullExpression(groupingSelectExpression.KeyExpression), new SqlIsNullExpression(currentKeyExpression)),
                    Expression.AndAlso(
                        Expression.AndAlso(
                            new SqlIsNotNullExpression(groupingSelectExpression.KeyExpression),
                            new SqlIsNotNullExpression(currentKeyExpression)),
                        Expression.Equal(groupingSelectExpression.KeyExpression, currentKeyExpression))),
                _context);

            elementSelectingStatementBuilder.AddWhereCondition(groupKeyJoinCondition);

            elementSelectingStatementBuilder.SelectProjection = groupingSelectExpression.ElementExpression;
            elementSelectingStatementBuilder.RecalculateDataInfo(groupingSelectExpression);

            return(new ResolvedJoinedGroupingTableInfo(
                       _generator.GetUniqueIdentifier("q"),
                       elementSelectingStatementBuilder.GetSqlStatement(),
                       groupingSelectExpression,
                       groupSourceSubStatementTableInfo.TableAlias));
        }
        public override void HandleResultOperator(OfTypeResultOperator 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);

            EnsureNoGroupExpression(sqlStatementBuilder, generator, stage, context);
            UpdateDataInfo(resultOperator, sqlStatementBuilder, sqlStatementBuilder.DataInfo);
            var typeCheckExpression = Expression.TypeIs(sqlStatementBuilder.SelectProjection, resultOperator.SearchedItemType);

            sqlStatementBuilder.AddWhereCondition(typeCheckExpression);
            sqlStatementBuilder.SelectProjection = Expression.Convert(sqlStatementBuilder.SelectProjection, resultOperator.SearchedItemType);
        }
        public override void HandleResultOperator(AllResultOperator 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);

            EnsureNoGroupExpression(sqlStatementBuilder, generator, stage, context);

            sqlStatementBuilder.AddWhereCondition(stage.PrepareWhereExpression(Expression.Not(resultOperator.Predicate), 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(Expression.Not(sqlExistsExpression), context);
            UpdateDataInfo(resultOperator, sqlStatementBuilder, sqlSubStatement.DataInfo);
        }
        public override void HandleResultOperator(
            SkipResultOperator 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);

            EnsureNoSetOperations(sqlStatementBuilder, generator, stage, context);

            // We move the statement into a subquery and change it to return the row number in addition to the original projection. Then, we use that
            // row number from the outer statement to skip the desired amount of rows. We also add an order by clause to the outer statement to ensure
            // that the rows come in the correct order.
            // E.g., (from c in Cooks orderby c.FirstName select c.LastName).Skip (20)
            // becomes
            // from x in
            //   (from c in Cooks select new { Key = c.LastName, Value = ROW_NUMBER() OVER (c.FirstName) })
            // where x.Value > 20
            // orderby x.Value
            // select x.Key

            var originalDataInfo = sqlStatementBuilder.DataInfo;

            var subStatementWithRowNumber = CreateSubStatementWithRowNumber(sqlStatementBuilder, generator, stage, context);

            sqlStatementBuilder.SelectProjection = subStatementWithRowNumber.OriginalProjectionSelector;
            sqlStatementBuilder.SqlTables.Add(subStatementWithRowNumber.SubStatementTable);
            sqlStatementBuilder.AddWhereCondition(Expression.GreaterThan(subStatementWithRowNumber.RowNumberSelector, resultOperator.Count));
            sqlStatementBuilder.Orderings.Add(new Ordering(subStatementWithRowNumber.RowNumberSelector, OrderingDirection.Asc));
            sqlStatementBuilder.DataInfo               = originalDataInfo;
            sqlStatementBuilder.RowNumberSelector      = subStatementWithRowNumber.RowNumberSelector;
            sqlStatementBuilder.CurrentRowNumberOffset = resultOperator.Count;

            AddMappingForItemExpression(context, originalDataInfo, subStatementWithRowNumber.OriginalProjectionSelector);
        }