Esempio n. 1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="select"></param>
        /// <returns></returns>
        protected override Expression VisitSelect(SelectExpression select)
        {
            switch (_dataType)
            {
            case Nequeo.Data.DataType.ConnectionContext.ConnectionDataType.OracleDataType:
            case Nequeo.Data.DataType.ConnectionContext.ConnectionDataType.SqlDataType:

                select = (SelectExpression)base.VisitSelect(select);
                if (select.Skip != null)
                {
                    SelectExpression newSelect    = select.SetSkip(null).SetTake(null);
                    bool             canAddColumn = !select.IsDistinct && (select.GroupBy == null || select.GroupBy.Count == 0);
                    if (!canAddColumn)
                    {
                        newSelect = newSelect.AddRedundantSelect(new TableAlias());
                    }
                    newSelect = newSelect.AddColumn(new ColumnDeclaration("ROW_NUM", new RowNumberExpression(select.OrderBy)));

                    // add layer for WHERE clause that references new rownum column
                    newSelect = newSelect.AddRedundantSelect(new TableAlias());
                    newSelect = newSelect.RemoveColumn(newSelect.Columns[newSelect.Columns.Count - 1]);

                    var newAlias           = ((SelectExpression)newSelect.From).Alias;
                    ColumnExpression rnCol = new ColumnExpression(typeof(int), newAlias, "ROW_NUM");
                    Expression where;
                    if (select.Take != null)
                    {
                        where = new BetweenExpression(rnCol, Expression.Add(select.Skip, Expression.Constant(1)), Expression.Add(select.Skip, select.Take));
                    }
                    else
                    {
                        where = Expression.GreaterThan(rnCol, select.Skip);
                    }
                    if (newSelect.Where != null)
                    {
                        where = Expression.And(newSelect.Where, where);
                    }
                    newSelect = newSelect.SetWhere(where);

                    select = newSelect;
                }
                return(select);

            default:
                select = (SelectExpression)base.VisitSelect(select);
                if (select.Skip != null)
                {
                    SelectExpression newSelect = select.SetSkip(select.Skip).SetTake(select.Take);
                    select = newSelect;
                }
                return(select);
            }
        }
        protected override Expression VisitSelect(SelectExpression select)
        {
            select = (SelectExpression)base.VisitSelect(select);
            if (select.Skip != null)
            {
                SelectExpression newSelect    = select.SetSkip(null).SetTake(null);
                bool             canAddColumn = !select.IsDistinct && (select.GroupBy == null || select.GroupBy.Count == 0);
                if (!canAddColumn)
                {
                    newSelect = newSelect.AddRedundantSelect(new TableAlias());
                }

                newSelect = newSelect.AddColumn(new ColumnDeclaration(COLUMN_NAME, new RowNumberExpression(select.OrderBy)));

                // add layer for WHERE clause that references new rownum column
                newSelect = newSelect.AddRedundantSelect(new TableAlias());
                newSelect = newSelect.RemoveColumn(newSelect.Columns.Single(c => c.Name == COLUMN_NAME));

                var newAlias           = ((SelectExpression)newSelect.From).Alias;
                ColumnExpression rnCol = new ColumnExpression(typeof(int), newAlias, COLUMN_NAME, null);
                Expression where;

                if (select.Take != null)
                {
                    where = new BetweenExpression(rnCol, Expression.Add(select.Skip, Expression.Constant(1)), Expression.Add(select.Skip, select.Take));
                }
                else
                {
                    where = rnCol.GreaterThan(select.Skip);
                }

                if (newSelect.Where != null)
                {
                    where = newSelect.Where.And(where);
                }

                newSelect = newSelect.SetWhere(where);

                select = newSelect;
            }

            return(select);
        }