示例#1
0
 public CrossJoinExpression([NotNull] TableExpressionBase tableExpression)
     : base(
         Check.NotNull(tableExpression, "tableExpression").QuerySource,
         tableExpression.Alias)
 {
     _tableExpression = tableExpression;
 }
 protected JoinExpressionBase([NotNull] TableExpressionBase tableExpression)
     : base(
         Check.NotNull(tableExpression, "tableExpression").QuerySource,
         tableExpression.Alias)
 {
     _tableExpression = tableExpression;
 }
        private static Expression UpdateColumnExpression(Expression expression, TableExpressionBase 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)
            {
                return(new AliasExpression(aliasExpression.Alias, UpdateColumnExpression(aliasExpression.Expression, tableExpression)));
            }
            switch (expression.NodeType)
            {
            case ExpressionType.Coalesce:
            {
                var binaryExpression = expression as BinaryExpression;
                var left             = UpdateColumnExpression(binaryExpression.Left, tableExpression);
                var right            = UpdateColumnExpression(binaryExpression.Right, tableExpression);
                return(binaryExpression.Update(left, binaryExpression.Conversion, right));
            }

            case ExpressionType.Conditional:
            {
                var conditionalExpression = expression as ConditionalExpression;
                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);
        }
        private static Ordering UpdateColumnExpression(Ordering ordering, TableExpressionBase tableExpression)
        {
            var newExpression = UpdateColumnExpression(ordering.Expression, tableExpression);
            var newOrdering   = new Ordering(newExpression, ordering.OrderingDirection);

            return(newOrdering);
        }
 protected JoinExpressionBase([NotNull] TableExpressionBase tableExpression)
     : base(
         Check.NotNull(tableExpression, nameof(tableExpression)).QuerySource,
         tableExpression.Alias)
 {
     _tableExpression = tableExpression;
 }
示例#6
0
        public ColumnExpression([NotNull] IProperty property, [NotNull] TableExpressionBase tableExpression)
            : base(Check.NotNull(property, "property").PropertyType)
        {
            Check.NotNull(tableExpression, "tableExpression");

            _property        = property;
            _tableExpression = tableExpression;
        }
        public virtual void AddCrossJoin(
            [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 CrossJoinExpression(tableExpression));
            _projection.AddRange(projection);
        }
        public virtual void AddCrossJoin(
            [NotNull] TableExpressionBase tableExpression,
            [NotNull] IEnumerable <ColumnExpression> projection)
        {
            Check.NotNull(tableExpression, "tableExpression");
            Check.NotNull(projection, "projection");

            _tables.Add(new CrossJoinExpression(tableExpression));

            _projection.AddRange(projection);
        }
        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);
        }
        public ColumnExpression(
            [NotNull] string name,
            [NotNull] IProperty property,
            [NotNull] TableExpressionBase tableExpression)
            : base(Check.NotNull(property, "property").PropertyType)
        {
            Check.NotEmpty(name, "name");
            Check.NotNull(tableExpression, "tableExpression");

            _name            = name;
            _property        = property;
            _tableExpression = tableExpression;
        }
        public ColumnExpression(
            [NotNull] string name,
            [NotNull] IProperty property,
            [NotNull] TableExpressionBase tableExpression)
            : base(Check.NotNull(property, nameof(property)).ClrType)
        {
            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)
            : base(Check.NotNull(property, nameof(property)).ClrType)
        {
            Check.NotEmpty(name, nameof(name));
            Check.NotNull(tableExpression, nameof(tableExpression));

            Name             = name;
            _property        = property;
            _tableExpression = tableExpression;
        }
        public virtual JoinExpressionBase AddOuterJoin(
            [NotNull] TableExpressionBase tableExpression,
            [NotNull] IEnumerable <ColumnExpression> projection)
        {
            Check.NotNull(tableExpression, "tableExpression");
            Check.NotNull(projection, "projection");

            var outerJoinExpression = new LeftOuterJoinExpression(tableExpression);

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

            return(outerJoinExpression);
        }
示例#14
0
        public virtual JoinExpressionBase AddInnerJoin(
            [NotNull] TableExpressionBase tableExpression,
            [NotNull] IEnumerable <ColumnExpression> projection)
        {
            Check.NotNull(tableExpression, "tableExpression");
            Check.NotNull(projection, "projection");

            tableExpression.Alias = CreateUniqueTableAlias(tableExpression.Alias);

            var innerJoinExpression = new InnerJoinExpression(tableExpression);

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

            return(innerJoinExpression);
        }
示例#15
0
        public virtual ColumnExpression AddToOrderBy(
            [NotNull] string column,
            [NotNull] IProperty property,
            [NotNull] TableExpressionBase table,
            OrderingDirection orderingDirection)
        {
            Check.NotEmpty(column, "column");
            Check.NotNull(property, "property");
            Check.NotNull(table, "table");

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

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

            return(columnExpression);
        }
        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);
        }
示例#17
0
 public InnerJoinExpression([NotNull] TableExpressionBase tableExpression)
     : base(tableExpression)
 {
 }
        public virtual JoinExpressionBase AddInnerJoin([NotNull] TableExpressionBase tableExpression)
        {
            Check.NotNull(tableExpression, nameof(tableExpression));

            return(AddInnerJoin(tableExpression, Enumerable.Empty <AliasExpression>()));
        }
示例#19
0
 public LeftOuterJoinExpression([NotNull] TableExpressionBase tableExpression)
     : base(Check.NotNull(tableExpression, "tableExpression"))
 {
 }
        private Expression BuildColumnExpression(
            IReadOnlyCollection<Expression> projections,
            TableExpressionBase tableExpression,
            IProperty property)
        {
            Check.NotNull(property, nameof(property));

            if (projections.Count == 0)
            {
                return new ColumnExpression(
                    _queryCompilationContext.GetColumnName(property),
                    property,
                    tableExpression);
            }

            var matchingColumnExpression
                = projections
                    .OfType<AliasExpression>()
                    .Last(p => p.TryGetColumnExpression()?.Property == property);

            return new ColumnExpression(
                matchingColumnExpression.Alias ?? matchingColumnExpression.TryGetColumnExpression().Name,
                property,
                tableExpression);
        }
        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 static Expression UpdateColumnExpression(Expression expression, TableExpressionBase 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)
            {
                return new AliasExpression(aliasExpression.Alias, UpdateColumnExpression(aliasExpression.Expression, tableExpression));
            }
            switch (expression.NodeType)
            {
                case ExpressionType.Coalesce:
                {
                    var binaryExpression = expression as BinaryExpression;
                    var left = UpdateColumnExpression(binaryExpression.Left, tableExpression);
                    var right = UpdateColumnExpression(binaryExpression.Right, tableExpression);
                    return binaryExpression.Update(left, binaryExpression.Conversion, right);
                }
                case ExpressionType.Conditional:
                {
                    var conditionalExpression = expression as ConditionalExpression;
                    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;
        }
        private Expression BuildJoinEqualityExpression(
            INavigation navigation,
            IReadOnlyList<IProperty> primaryKeyProperties,
            TableExpressionBase targetTableExpression,
            TableExpressionBase joinExpression)
        {
            Expression joinPredicateExpression = null;

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

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

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

                var primaryKeyColumnExpression
                    = BuildColumnExpression(joinTableProjections, joinExpression, primaryKeyProperty);

                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 Ordering UpdateColumnExpression(Ordering ordering, TableExpressionBase tableExpression)
        {
            var newExpression = UpdateColumnExpression(ordering.Expression, tableExpression);
            var newOrdering = new Ordering(newExpression, ordering.OrderingDirection);

            return newOrdering;
        }
        public virtual void AddTable([NotNull] TableExpressionBase tableExpression)
        {
            Check.NotNull(tableExpression, "tableExpression");

            _tables.Add(tableExpression);
        }
        public virtual void RemoveTable([NotNull] TableExpressionBase tableExpression)
        {
            Check.NotNull(tableExpression, "tableExpression");

            _tables.Remove(tableExpression);
        }
 public virtual void AddTable([NotNull] TableExpressionBase tableExpression)
 => _tables.Add(Check.NotNull(tableExpression, nameof(tableExpression)));
        public virtual JoinExpressionBase AddInnerJoin([NotNull] TableExpressionBase tableExpression)
        {
            Check.NotNull(tableExpression, "tableExpression");

            return(AddInnerJoin(tableExpression, Enumerable.Empty <ColumnExpression>()));
        }