protected JoinExpressionBase([NotNull] TableExpressionBase tableExpression)
     : base(
         Check.NotNull(tableExpression, nameof(tableExpression)).QuerySource,
         tableExpression.Alias)
 {
     _tableExpression = tableExpression;
 }
 public CrossApplyExpression([NotNull] TableExpressionBase tableExpression)
     : base(
         Check.NotNull(tableExpression, nameof(tableExpression)).QuerySource,
         tableExpression.Alias)
 {
     _tableExpression = tableExpression;
 }
Exemple #3
0
 public CrossJoinExpression([NotNull] TableExpressionBase tableExpression)
     : base(
         Check.NotNull(tableExpression, nameof(tableExpression)).QuerySource,
         tableExpression.Alias)
 {
     _tableExpression = tableExpression;
 }
 public LateralJoinExpression([NotNull] TableExpressionBase tableExpression)
     : base(
         Check.NotNull(tableExpression, nameof(tableExpression)).QuerySource,
         tableExpression.Alias)
 {
     _tableExpression = tableExpression;
 }
Exemple #5
0
 public ColumnExpression(
     [NotNull] string name,
     [NotNull] IProperty property,
     [NotNull] TableExpressionBase tableExpression)
     : this(name, Check.NotNull(property, nameof(property)).ClrType, tableExpression)
 {
     _property = property;
 }
Exemple #6
0
        public virtual void AddCrossApply(
            [NotNull] TableExpressionBase tableExpression,
            [NotNull] IEnumerable <Expression> projection)
        {
            Check.NotNull(tableExpression, nameof(tableExpression));
            Check.NotNull(projection, nameof(projection));

            tableExpression.Alias = CreateUniqueTableAlias(tableExpression.Alias);

            _tables.Add(new CrossApplyExpression(tableExpression));
            _projection.AddRange(projection);
        }
Exemple #7
0
        public ColumnExpression(
            [NotNull] string name,
            [NotNull] IProperty property,
            [NotNull] TableExpressionBase tableExpression)
        {
            Check.NotEmpty(name, nameof(name));
            Check.NotNull(tableExpression, nameof(tableExpression));

            Name             = name;
            _property        = property;
            _tableExpression = tableExpression;
        }
        public ColumnExpression(
            [NotNull] string name,
            [NotNull] IProperty property,
            [NotNull] TableExpressionBase tableExpression)
        {
            Check.NotEmpty(name, nameof(name));
            Check.NotNull(tableExpression, nameof(tableExpression));

            Name = name;
            _property = property;
            _tableExpression = tableExpression;
        }
Exemple #9
0
        public virtual JoinExpressionBase AddOuterJoin([NotNull] TableExpressionBase tableExpression)
        {
            Check.NotNull(tableExpression, nameof(tableExpression));

            tableExpression.Alias = CreateUniqueTableAlias(tableExpression.Alias);

            var outerJoinExpression = new LeftOuterJoinExpression(tableExpression);

            _tables.Add(outerJoinExpression);

            return(outerJoinExpression);
        }
Exemple #10
0
        public ColumnExpression(
            [NotNull] string name,
            [NotNull] Type type,
            [NotNull] TableExpressionBase tableExpression)
        {
            Check.NotEmpty(name, nameof(name));
            Check.NotNull(type, nameof(type));
            Check.NotNull(tableExpression, nameof(tableExpression));

            Name             = name;
            Type             = type;
            _tableExpression = tableExpression;
        }
        public ColumnExpression(
         [NotNull] string name,
         [NotNull] Type type,
         [NotNull] TableExpressionBase tableExpression)
        {
            Check.NotEmpty(name, nameof(name));
            Check.NotNull(type, nameof(type));
            Check.NotNull(tableExpression, nameof(tableExpression));

            Name = name;
            Type = type;
            _tableExpression = tableExpression;
        }
Exemple #12
0
        // TODO: Make polymorphic
        public virtual Expression UpdateColumnExpression(
            [NotNull] Expression expression,
            [NotNull] TableExpressionBase tableExpression)
        {
            Check.NotNull(expression, nameof(expression));
            Check.NotNull(tableExpression, nameof(tableExpression));

            var aliasExpression  = expression as AliasExpression;
            var columnExpression = expression as ColumnExpression;

            if (columnExpression != null)
            {
                return(new ColumnExpression(columnExpression.Name, columnExpression.Property, tableExpression));
            }

            if (aliasExpression != null)
            {
                var newAliasExpression
                    = new AliasExpression(
                          aliasExpression.Alias,
                          UpdateColumnExpression(aliasExpression.Expression, tableExpression))
                    {
                    SourceMember = aliasExpression.SourceMember
                    };

                return(newAliasExpression);
            }

            switch (expression.NodeType)
            {
            case ExpressionType.Coalesce:
            {
                var binaryExpression = (BinaryExpression)expression;
                var left             = UpdateColumnExpression(binaryExpression.Left, tableExpression);
                var right            = UpdateColumnExpression(binaryExpression.Right, tableExpression);
                return(binaryExpression.Update(left, binaryExpression.Conversion, right));
            }

            case ExpressionType.Conditional:
            {
                var conditionalExpression = (ConditionalExpression)expression;
                var test    = UpdateColumnExpression(conditionalExpression.Test, tableExpression);
                var ifTrue  = UpdateColumnExpression(conditionalExpression.IfTrue, tableExpression);
                var ifFalse = UpdateColumnExpression(conditionalExpression.IfFalse, tableExpression);
                return(conditionalExpression.Update(test, ifTrue, ifFalse));
            }
            }

            return(expression);
        }
Exemple #13
0
        public virtual JoinExpressionBase AddOuterJoin(
            [NotNull] TableExpressionBase tableExpression,
            [NotNull] IEnumerable <Expression> projection)
        {
            Check.NotNull(tableExpression, nameof(tableExpression));
            Check.NotNull(projection, nameof(projection));

            tableExpression.Alias = CreateUniqueTableAlias(tableExpression.Alias);

            var outerJoinExpression = new LeftOuterJoinExpression(tableExpression);

            _tables.Add(outerJoinExpression);
            _projection.AddRange(projection);

            return(outerJoinExpression);
        }
Exemple #14
0
        public virtual AliasExpression AddToOrderBy(
            [NotNull] string column,
            [NotNull] IProperty property,
            [NotNull] TableExpressionBase table,
            OrderingDirection orderingDirection)
        {
            Check.NotEmpty(column, nameof(column));
            Check.NotNull(property, nameof(property));
            Check.NotNull(table, nameof(table));

            var columnExpression = new ColumnExpression(column, property, table);
            var aliasExpression  = new AliasExpression(columnExpression);

            if (_orderBy.FindIndex(o => o.Expression.TryGetColumnExpression()?.Equals(columnExpression) ?? false) == -1)
            {
                _orderBy.Add(new Ordering(aliasExpression, orderingDirection));
            }

            return(aliasExpression);
        }
        private static IEnumerable<Expression> ExtractProjections(TableExpressionBase tableExpression)
        {
            var selectExpression = tableExpression as SelectExpression;

            if (selectExpression != null)
            {
                return selectExpression.Projection.ToList();
            }

            var joinExpression = tableExpression as JoinExpressionBase;

            return joinExpression != null
                ? ExtractProjections(joinExpression.TableExpression)
                : Enumerable.Empty<Expression>();
        }
        private Expression BuildColumnExpression(
            IReadOnlyCollection<Expression> projections,
            TableExpressionBase tableExpression,
            IProperty property,
            IQuerySource querySource)
        {
            Check.NotNull(property, nameof(property));

            if (projections.Count == 0)
            {
                return new ColumnExpression(
                    _relationalAnnotationProvider.For(property).ColumnName,
                    property,
                    tableExpression);
            }

            var aliasExpressions
                = projections
                    .OfType<AliasExpression>()
                    .Where(p => p.TryGetColumnExpression()?.Property == property)
                    .ToList();

            var aliasExpression
                = aliasExpressions.Count == 1
                    ? aliasExpressions[0]
                    : aliasExpressions.Last(ae => ae.TryGetColumnExpression().Table.QuerySource == querySource);

            return new ColumnExpression(
                aliasExpression.Alias ?? aliasExpression.TryGetColumnExpression().Name,
                property,
                tableExpression);
        }
        private Expression BuildJoinEqualityExpression(
            INavigation navigation,
            TableExpressionBase targetTableExpression,
            TableExpressionBase joinExpression,
            IQuerySource querySource)
        {
            Expression joinPredicateExpression = null;

            var targetTableProjections = ExtractProjections(targetTableExpression).ToList();
            var joinTableProjections = ExtractProjections(joinExpression).ToList();

            for (var i = 0; i < navigation.ForeignKey.Properties.Count; i++)
            {
                var principalKeyProperty = navigation.ForeignKey.PrincipalKey.Properties[i];
                var foreignKeyProperty = navigation.ForeignKey.Properties[i];

                var foreignKeyColumnExpression
                    = BuildColumnExpression(targetTableProjections, targetTableExpression, foreignKeyProperty, querySource);

                var primaryKeyColumnExpression
                    = BuildColumnExpression(joinTableProjections, joinExpression, principalKeyProperty, querySource);

                var primaryKeyExpression = primaryKeyColumnExpression;

                if (foreignKeyColumnExpression.Type != primaryKeyExpression.Type)
                {
                    if (foreignKeyColumnExpression.Type.IsNullableType()
                        && !primaryKeyExpression.Type.IsNullableType())
                    {
                        primaryKeyExpression
                            = Expression.Convert(primaryKeyExpression, foreignKeyColumnExpression.Type);
                    }
                    else if (primaryKeyExpression.Type.IsNullableType()
                             && !foreignKeyColumnExpression.Type.IsNullableType())
                    {
                        foreignKeyColumnExpression
                            = Expression.Convert(foreignKeyColumnExpression, primaryKeyColumnExpression.Type);
                    }
                }

                var equalExpression
                    = Expression.Equal(foreignKeyColumnExpression, primaryKeyExpression);

                joinPredicateExpression
                    = joinPredicateExpression == null
                        ? equalExpression
                        : Expression.AndAlso(joinPredicateExpression, equalExpression);
            }

            return joinPredicateExpression;
        }
        private static void LiftOrderBy(
            SelectExpression innerJoinSelectExpression,
            SelectExpression targetSelectExpression,
            TableExpressionBase innerJoinExpression)
        {
            foreach (var ordering in innerJoinSelectExpression.OrderBy)
            {
                var orderingExpression = ordering.Expression;

                var aliasExpression = ordering.Expression as AliasExpression;

                if (aliasExpression?.Alias != null)
                {
                    var columnExpression = aliasExpression.TryGetColumnExpression();

                    if (columnExpression != null)
                    {
                        orderingExpression
                            = new ColumnExpression(
                                aliasExpression.Alias,
                                columnExpression.Property,
                                columnExpression.Table);
                    }
                }

                var index = innerJoinSelectExpression.AddToProjection(orderingExpression);

                var expression = innerJoinSelectExpression.Projection[index];

                var newExpression
                    = targetSelectExpression.UpdateColumnExpression(expression, innerJoinExpression);

                targetSelectExpression.AddToOrderBy(new Ordering(newExpression, ordering.OrderingDirection));
            }

            innerJoinSelectExpression.ClearOrderBy();
        }
Exemple #19
0
 public virtual void AddTable([NotNull] TableExpressionBase tableExpression)
 => _tables.Add(Check.NotNull(tableExpression, nameof(tableExpression)));
Exemple #20
0
        public virtual void RemoveTable([NotNull] TableExpressionBase tableExpression)
        {
            Check.NotNull(tableExpression, nameof(tableExpression));

            _tables.Remove(tableExpression);
        }
Exemple #21
0
        public virtual JoinExpressionBase AddOuterJoin([NotNull] TableExpressionBase tableExpression)
        {
            Check.NotNull(tableExpression, nameof(tableExpression));

            return(AddOuterJoin(tableExpression, Enumerable.Empty <AliasExpression>()));
        }
 public LeftOuterJoinExpression([NotNull] TableExpressionBase tableExpression)
     : base(Check.NotNull(tableExpression, nameof(tableExpression)))
 {
 }
Exemple #23
0
 public InnerJoinExpression([NotNull] TableExpressionBase tableExpression)
     : base(tableExpression)
 {
 }