Ejemplo n.º 1
0
        public EntityExpression(Type type, PrimaryKeyExpression externalId, NewExpression externalPeriod, Alias tableAlias, IEnumerable <FieldBinding> bindings, IEnumerable <MixinEntityExpression> mixins, NewExpression tablePeriod, bool avoidExpandOnRetrieving)
            : base(DbExpressionType.Entity, type)
        {
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }

            if (!type.IsEntity())
            {
                throw new ArgumentException("type");
            }
            this.Table      = Schema.Current.Table(type);
            this.ExternalId = externalId ?? throw new ArgumentNullException("externalId");

            this.TableAlias = tableAlias;
            this.Bindings   = bindings.ToReadOnly();
            this.Mixins     = mixins.ToReadOnly();

            this.ExternalPeriod = externalPeriod;
            this.TablePeriod    = tablePeriod;

            this.AvoidExpandOnRetrieving = avoidExpandOnRetrieving;
        }
Ejemplo n.º 2
0
 public MListElementExpression(PrimaryKeyExpression rowId, EntityExpression parent, Expression order, Expression element, NewExpression systemPeriod, TableMList table, Alias alias)
     : base(DbExpressionType.MListElement, typeof(MListElement <,>).MakeGenericType(parent.Type, element.Type))
 {
     this.RowId       = rowId;
     this.Parent      = parent;
     this.Order       = order;
     this.Element     = element;
     this.TablePeriod = systemPeriod;
     this.Table       = table;
     this.Alias       = alias;
 }
Ejemplo n.º 3
0
 internal SetOperatorExpression(SetOperator @operator, SourceWithAliasExpression left, SourceWithAliasExpression right, Alias alias)
     : base(DbExpressionType.SetOperator, alias)
 {
     this.Operator = @operator;
     this.Left     = left ?? throw new ArgumentNullException(nameof(left));
     this.Right    = right ?? throw new ArgumentNullException(nameof(right));
 }
Ejemplo n.º 4
0
        protected internal override Expression VisitProjection(ProjectionExpression proj)
        {
            if (currentSource == null)
            {
                currentSource = WithoutOrder(proj.Select);

                Expression projector = this.Visit(proj.Projector);

                if (projector != proj.Projector)
                {
                    proj = new ProjectionExpression(proj.Select, projector, proj.UniqueFunction, proj.Type);
                }

                currentSource = null;
                return(proj);
            }
            else
            {
                HashSet <ColumnExpression> columns = ExternalColumnGatherer.Gatherer(proj, currentSource.Alias);

                if (columns.Count == 0)
                {
                    Expression projector = Visit(proj.Projector);

                    ConstantExpression key   = Expression.Constant(0);
                    Type            kvpType  = typeof(KeyValuePair <,>).MakeGenericType(key.Type, projector.Type);
                    ConstructorInfo ciKVP    = kvpType.GetConstructor(new[] { key.Type, projector.Type }) !;
                    Type            projType = proj.UniqueFunction == null ? typeof(IEnumerable <>).MakeGenericType(kvpType) : kvpType;

                    var childProj = new ProjectionExpression(proj.Select,
                                                             Expression.New(ciKVP, key, projector), proj.UniqueFunction, projType);

                    return(new ChildProjectionExpression(childProj,
                                                         Expression.Constant(0), inMList != null, inMList ?? proj.Type, new LookupToken()));
                }
                else
                {
                    SelectExpression external;
                    IEnumerable <ColumnExpression> externalColumns;

                    if (!IsKey(currentSource, columns))
                    {
                        Alias           aliasDistinct     = aliasGenerator.GetUniqueAlias(currentSource.Alias.Name + "D");
                        ColumnGenerator generatorDistinct = new ColumnGenerator();

                        List <ColumnDeclaration> columnDistinct = columns.Select(ce => generatorDistinct.MapColumn(ce)).ToList();
                        external = new SelectExpression(aliasDistinct, true, null, columnDistinct, currentSource, null, null, null, 0);


                        Dictionary <ColumnExpression, ColumnExpression> distinctReplacements = columnDistinct.ToDictionary(
                            cd => (ColumnExpression)cd.Expression,
                            cd => cd.GetReference(aliasDistinct));

                        proj = (ProjectionExpression)ColumnReplacer.Replace(proj, distinctReplacements);

                        externalColumns = distinctReplacements.Values.ToHashSet();
                    }
                    else
                    {
                        external        = currentSource;
                        externalColumns = columns;
                    }

                    ColumnGenerator          generatorSM       = new ColumnGenerator();
                    List <ColumnDeclaration> columnsSMExternal = externalColumns.Select(ce => generatorSM.MapColumn(ce)).ToList();
                    List <ColumnDeclaration> columnsSMInternal = proj.Select.Columns.Select(cd => generatorSM.MapColumn(cd.GetReference(proj.Select.Alias))).ToList();

                    SelectExpression @internal = ExtractOrders(proj.Select, out List <OrderExpression>?innerOrders);

                    Alias            aliasSM    = aliasGenerator.GetUniqueAlias(@internal.Alias.Name + "SM");
                    SelectExpression selectMany = new SelectExpression(aliasSM, false, null, columnsSMExternal.Concat(columnsSMInternal),
                                                                       new JoinExpression(JoinType.CrossApply,
                                                                                          external,
                                                                                          @internal, null), null, innerOrders, null, 0);

                    SelectExpression old = currentSource;
                    currentSource = WithoutOrder(selectMany);

                    var selectManyReplacements = selectMany.Columns.ToDictionary(
                        cd => (ColumnExpression)cd.Expression,
                        cd => cd.GetReference(aliasSM));

                    Expression projector = ColumnReplacer.Replace(proj.Projector, selectManyReplacements);

                    projector = Visit(projector);

                    currentSource = old;

                    Expression      key      = TupleReflection.TupleChainConstructor(columnsSMExternal.Select(cd => MakeEquatable(cd.GetReference(aliasSM))));
                    Type            kvpType  = typeof(KeyValuePair <,>).MakeGenericType(key.Type, projector.Type);
                    ConstructorInfo ciKVP    = kvpType.GetConstructor(new[] { key.Type, projector.Type }) !;
                    Type            projType = proj.UniqueFunction == null ? typeof(IEnumerable <>).MakeGenericType(kvpType) : kvpType;

                    var childProj = new ProjectionExpression(selectMany,
                                                             Expression.New(ciKVP, key, projector), proj.UniqueFunction, projType);

                    return(new ChildProjectionExpression(childProj,
                                                         TupleReflection.TupleChainConstructor(columns.Select(a => MakeEquatable(a))), inMList != null, inMList ?? proj.Type, new LookupToken()));
                }
            }
        }
Ejemplo n.º 5
0
 public SourceWithAliasExpression(DbExpressionType nodeType, Alias alias)
     : base(nodeType)
 {
     this.Alias = alias;
 }
Ejemplo n.º 6
0
 public ColumnExpression GetReference(Alias alias)
 {
     return(new ColumnExpression(Expression.Type, alias, Name));
 }
Ejemplo n.º 7
0
        private ReadOnlyCollection <ColumnDeclaration> AnswerAndExpand(ReadOnlyCollection <ColumnDeclaration> columns, Alias currentAlias, Dictionary <ColumnExpression, ColumnExpression> askedColumns)
        {
            ColumnGenerator cg = new ColumnGenerator(columns);

            foreach (var col in askedColumns.Keys.ToArray())
            {
                if (col.Alias == currentAlias)
                {
                    //Expression expr = columns.SingleEx(cd => (cd.Name ?? "-") == col.Name).Expression;
                    //askedColumns[col] = expr is SqlConstantExpression ? expr : col;
                    askedColumns[col] = col;
                }
                else
                {
                    ColumnExpression colExp = CurrentScope[col];
                    //if (expr is ColumnExpression colExp)
                    //{
                    ColumnDeclaration cd = cg.Columns.FirstOrDefault(c => c.Expression.Equals(colExp));
                    if (cd == null)
                    {
                        cd = cg.MapColumn(colExp);
                    }

                    askedColumns[col] = new ColumnExpression(col.Type, currentAlias, cd.Name);
                    //}
                    //else
                    //{
                    //    askedColumns[col] = expr;
                    //}
                }
            }


            if (columns.Count != cg.Columns.Count())
            {
                return(cg.Columns.ToReadOnly());
            }

            return(columns);
        }
Ejemplo n.º 8
0
 public AggregateRequestsExpression(Alias groupByAlias, AggregateExpression aggregate)
     : base(DbExpressionType.AggregateRequest, aggregate.Type)
 {
     this.Aggregate    = aggregate;
     this.GroupByAlias = groupByAlias;
 }
Ejemplo n.º 9
0
        public SqlTableValuedFunctionExpression(ObjectName sqlFunction, Table?viewTable, Type?singleColumnType, Alias alias, IEnumerable <Expression> arguments)
            : base(DbExpressionType.SqlTableValuedFunction, alias)
        {
            if ((viewTable == null) == (singleColumnType == null))
            {
                throw new ArgumentException("Either viewTable or singleColumn should be set");
            }

            this.SqlFunction      = sqlFunction;
            this.ViewTable        = viewTable;
            this.SingleColumnType = singleColumnType;
            this.Arguments        = arguments.ToReadOnly();
        }
Ejemplo n.º 10
0
 static internal ColumnExpression SingleProjection(ColumnDeclaration declaration, Alias newAlias, Type columnType)
 {
     return(new ColumnExpression(columnType, newAlias, declaration.Name));
 }
Ejemplo n.º 11
0
 public ColumnProjector(HashSet <Expression> candidates, Alias newAlias, bool projectTrivialColumns)
 {
     this.candidates            = candidates;
     this.newAlias              = newAlias;
     this.projectTrivialColumns = projectTrivialColumns;
 }
Ejemplo n.º 12
0
        internal SetOperatorExpression(SetOperator @operator, SourceWithAliasExpression left, SourceWithAliasExpression right, Alias alias)
            : base(DbExpressionType.SetOperator, alias)
        {
            if (left == null)
            {
                throw new ArgumentNullException("left");
            }

            if (right == null)
            {
                throw new ArgumentNullException("right");
            }

            this.Operator = @operator;
            this.Left     = left;
            this.Right    = right;
        }
Ejemplo n.º 13
0
 internal TableExpression(Alias alias, ITable table, string withHint)
     : base(DbExpressionType.Table, alias)
 {
     this.Table    = table;
     this.WithHint = withHint;
 }