Ejemplo n.º 1
0
        private static void HandleDuplicates(Select select, MethodCallExpression expression)
        {
            if (select.Duplicates == null)
            {
                select.Duplicates = new Duplicates();
            }
            select.Duplicates.Distinct = ProjectionVisitor <T> .CreateModel(expression.ArgumentAt(2));

            if (expression.Arguments.Count == 2)
            {
                select.Duplicates.OrderBy.Add(new OrderBy {
                    Type       = OrderBy.SourceType.Projection,
                    Projection = select.Duplicates.Distinct,
                    Order      = Order.Ascending
                });
            }
            else
            {
                Enumerable.Range(3, expression.Arguments.Count - 2)
                .Where(x => x % 2 == 1)
                .Select(x => new { Index = x, IsProjection = ProjectionVisitor <T> .IsProjection(expression.ArgumentAt(x).GetLambdaBody()) })
                .ToList().ForEach(x => select.Duplicates.OrderBy.Add(new OrderBy {
                    Type       = x.IsProjection ? OrderBy.SourceType.Projection : OrderBy.SourceType.Operator,
                    Projection = x.IsProjection ? ProjectionVisitor <T> .CreateModel(expression.ArgumentAt(x.Index)) : null,
                    Operator   = !x.IsProjection ? WhereVisitor <T> .CreateModel(expression.ArgumentAt(x.Index), select.From.Alias) : null,
                    Order      = expression.ConstantArgumentAt <Order>(x.Index + 1)
                }));
            }
        }
Ejemplo n.º 2
0
        public static Projection CreateModel(Expression expression, string tableAlias = null)
        {
            Projection projection = null;
            var        visitor    = new ProjectionVisitor <T>(tableAlias);

            visitor.Visit(expression, x => projection = x);
            return(projection);
        }
Ejemplo n.º 3
0
 private static void HandleOrderBy(Select select, MethodCallExpression expression, bool descending)
 {
     if (select.OrderBy == null)
     {
         select.OrderBy = new List <OrderBy>();
     }
     select.OrderBy.Insert(0, new OrderBy {
         Order      = descending ? Order.Descending : Order.Ascending,
         Projection = ProjectionVisitor <T> .CreateModel(expression.ArgumentAt(2)),
         Type       = OrderBy.SourceType.Projection
     });
 }
Ejemplo n.º 4
0
 private static void HandleDistinct(Select select, MethodCallExpression expression)
 {
     if (select.Distinct == null)
     {
         select.Distinct = new List <Distinct>();
     }
     select.Distinct.Insert(0, new Distinct {
         Projection = ProjectionVisitor <T> .CreateModel(expression.ArgumentAt(2)),
         Order      = expression.HasArguments(4) ? new OrderBy {
             Type       = OrderBy.SourceType.Projection,
             Projection = ProjectionVisitor <T> .CreateModel(expression.ArgumentAt(3)),
             Order      = expression.ConstantArgumentAt <Order>(4)
         } : null
     });
 }
Ejemplo n.º 5
0
        private void HandleExpression(Context context, Expression expression)
        {
            Operand operand;

            if (!context.HasParent || context.Parent.IsBinaryLogicalOperator())
            {
                operand = Operand.Create.Operator(
                    Operator.Create.Equal(
                        Operand.Create.Projection(ProjectionVisitor <T> .CreateModel(expression, _tableAlias)),
                        Operand.Create.Projection(Projection.Create.Constant(true)
                                                  )
                        )
                    );
            }
            else
            {
                operand = Operand.Create.Projection(ProjectionVisitor <T> .CreateModel(expression, _tableAlias));
            }

            context.State(operand);
        }
Ejemplo n.º 6
0
        private void HandleSync(Query target, MethodCallExpression expression)
        {
            target.Operation = Query.OperationType.SyncWith;
            var fields  = expression.ArgumentAt <NewArrayExpression>(5).Expressions.Select(x => x.GetLambdaBody().StripConversion());
            var exclude = expression.ConstantArgumentAt <SyncFields>(4) == SyncFields.Exclude;
            var source  = CreateModel(expression.ArgumentAt(2), _getTableName).Select;

            if (!exclude)
            {
                source.Projection = fields.Select(x => new SelectProjection {
                    Projection = ProjectionVisitor <T> .CreateModel(x, source.From.Alias)
                }).ToList();
                target.Select.Projection = fields.Select(x => new SelectProjection {
                    Projection = ProjectionVisitor <T> .CreateModel(x, target.Select.From.Alias)
                }).ToList();
            }
            target.SyncWith = new Sync {
                Target         = target.Select,
                Source         = source,
                SourceKey      = ProjectionVisitor <T> .CreateModel(expression.ArgumentAt(3), source.From.Alias),
                TargetKey      = ProjectionVisitor <T> .CreateModel(expression.ArgumentAt(3), target.Select.From.Alias),
                ExcludedFields = exclude ? fields.Select(x => ProjectionVisitor <T> .CreateModel(x).Field).ToList() : new List <Field>()
            };
        }
Ejemplo n.º 7
0
        private static void HandleSetOperation(Select select, MethodCallExpression expression, bool intersect, Func <IQueryable <T>, string> getTableName)
        {
            if (select.SetOperatons == null)
            {
                select.SetOperatons = new List <SetOperation>();
            }
            var setOperationSource = CreateModel(expression.ArgumentAt(2), getTableName).Select;

            setOperationSource.Top     = 1;
            setOperationSource.TopType = Select.TopValueType.Count;
            var operatorExpressions = expression.ArgumentAt <NewArrayExpression>(3).
                                      Expressions.
                                      Select(x => x.NodeType == ExpressionType.Quote ? ((UnaryExpression)x).Operand : x).
                                      Select(x => ((LambdaExpression)x).Body).
                                      Select(x => x.NodeType == ExpressionType.Convert ? ((UnaryExpression)x).Operand : x);
            var operators = operatorExpressions.Select(x => Operator.Create.Equal(Operand.Create.Projection(ProjectionVisitor <T> .CreateModel(x)),
                                                                                  Operand.Create.Projection(ProjectionVisitor <T> .CreateModel(x, select.From.Alias))));

            foreach (var @operator in operators)
            {
                setOperationSource.Where = setOperationSource.HasWhere ? Operator.Create.Operators(setOperationSource.Where, Operator.OperatorType.And, @operator) : @operator;
            }
            setOperationSource.Projection = operatorExpressions.Select(x => SelectProjection.Create(ProjectionVisitor <T> .CreateModel(x))).ToList();
            select.SetOperatons.Add(new SetOperation {
                Type   = intersect ? SetOperation.OperationType.Intersect : SetOperation.OperationType.Compliment,
                Select = setOperationSource
            });
        }