Exemple #1
0
 public OrderBy(DirectVarUse keyVar, DirectVarUse valueVar, Expression expression,
                Ordering ordering, LinqOp next, bool isThenBy)
 {
     this.keyVar     = keyVar;
     this.valueVar   = valueVar;
     this.expression = expression;
     this.ordering   = ordering;
     this.next       = next;
     this.isThenBy   = isThenBy;
 }
Exemple #2
0
        private static LinqOp BuildOrderingOps(DirectVarUse keyVar, DirectVarUse valueVar,
                                               List <OrderingClause> /*!*/ clauses, LinqOp lastOp)
        {
            // PATTERNs:
            //	from x in e orderby k1 , k2, ..., kn [select v | group v by g]
            //
            // TRANSLATIONs:
            //	( e ) . OrderBy ( x => k1 ) . ThenBy ( x => k2 ) ... . ThenBy (x => kn) . Select ( x => v )
            //	( e ) . OrderBy ( x => k1 ) . ThenBy ( x => k2 ) ... . ThenBy (x => kn) . GroupBy ( x => g, x => v )

            Debug.Assert(clauses != null && clauses.Count > 0);

            for (int i = clauses.Count - 1; i >= 0; i--)
            {
                lastOp = new LinqOp.OrderBy(keyVar, valueVar, clauses[i].Expression, clauses[i].Ordering, lastOp, i != 0);
            }

            return(lastOp);
        }
Exemple #3
0
        private static LinqOpChain BuildSelectManyChain(List <Generator> /*!*/ generators,
                                                        GroupByClause gbClause, Expression /*!*/ v, LinqOp lastOp)
        {
            // PATTERNs (Rules #6, #7):
            //	from x1 in e1 from x2 in e3 ... from xk in ek [select v | group v by g]
            //
            // TRANSLATIONs:
            //  ( e1 ) . SelectMany ( x1 =>
            //		( e2 ) . SelectMany ( x2 =>
            //			...
            //				RESPECTIVELY:
            //				( ek ) . Select ( xk => v )
            //				( ek ) . GroupBy ( xk => g, xk => v )
            //			...
            //		)
            //  )
            //	. [lastOp]

            int i = generators.Count - 1;

            LinqOp outer_op;             // outer-most operator in the current chain

            if (gbClause != null)
            {
                outer_op = new LinqOp.GroupBy(generators[i].KeyVar, generators[i].ValueVar, gbClause.ByExpr, v);
            }
            else
            {
                outer_op = new LinqOp.Select(generators[i].KeyVar, generators[i].ValueVar, v);
            }

            // inner-most:
            LinqOpChain inner_chain = new LinqOpChain(generators[i].Expression, outer_op);

            while (--i >= 0)
            {
                inner_chain = new LinqOpChain(
                    generators[i].Expression,
                    outer_op = new LinqOp.SelectMany(generators[i].KeyVar, generators[i].ValueVar, inner_chain)
                    );
            }

            outer_op.Next = lastOp;

            return(inner_chain);
        }
Exemple #4
0
        private void FromWhereToGenerators(List <Generator> /*!*/ generators)
        {
            // query is the inner-most query - it will be converted to OpChain //

            int i = 0;

            for (; ;)
            {
                // convert from clauses to generators if there are any;
                // (the query can start with where-clause if there was an into-clause):
                while (i < fromWhere.Count && fromWhere[i].IsFromClause)
                {
                    FromClause f = (FromClause)fromWhere[i];

                    // each from-clause contains at least one generator:
                    Debug.Assert(f.Generators.Count > 0);

                    // adds generators contained in the from clause (Rule #2):
                    foreach (Generator generator in f.Generators)
                    {
                        generators.Add(generator);
                    }

                    i++;
                }

                // no more clauses:
                if (i == fromWhere.Count)
                {
                    break;
                }

                // where-clause follows; at least one generator has been added:
                Debug.Assert(fromWhere[i].IsWhereClause && generators.Count > 0);

                Generator last_generator = generators[generators.Count - 1];

                DirectVarUse x_key   = last_generator.KeyVar;
                DirectVarUse x_value = last_generator.ValueVar;

                LinqOpChain chain   = null;
                LinqOp      last_op = null;

                // embeds where-clauses imediately preceded by a from-clause to
                // the last generator of the from-clause (Rule #3):
                do
                {
                    LinqOp.Where where_op = new LinqOp.Where(x_key, x_value, ((WhereClause)fromWhere[i]).Expression);

                    if (last_op == null)
                    {
                        chain = new LinqOpChain(last_generator.Expression, where_op);
                    }
                    else
                    {
                        last_op.Next = where_op;
                    }

                    last_op = where_op;
                    i++;
                }while (i < fromWhere.Count && fromWhere[i].IsWhereClause);

                Debug.Assert(chain != null);
                last_generator.Expression = chain;

                // no more clauses:
                if (i == fromWhere.Count)
                {
                    break;
                }
            }
        }
Exemple #5
0
        private LinqOpChain BuildSingleQueryChain(List <Generator> /*!*/ generators)
        {
            // Applies Rules #2 and #3
            FromWhereToGenerators(generators);

            if (generators.Count > 1)
            {
                // multiple from-clauses (Rules #4 to #7) //

                Expression v = (select != null) ? select.Expression : groupBy.GroupExpr;

                if (orderBy != null)
                {
                    // PATTERNs (Rules #4, #5):
                    //	from x1 in e1 from x2 in e2 ... from xk in ek orderby k1, k2, ..., kn [select v | group v by g]
                    //
                    // TRANSLATIONs:
                    //  ( e1 ) . SelectMany ( x1 =>
                    //		( e2 ) . SelectMany ( x2 =>
                    //			...
                    //				RESPECTIVELY:
                    //				( ek ) . Select ( xk => new Tuple(k1, k2, ..., kn, v) )
                    //				( ek ) . GroupBy ( xk => g, xk => new Tuple(k1, k2, ..., kn, v) )
                    //			...
                    //		)
                    //  )
                    //  . OrderBy ( tuple => tuple[0] ) . ... . ThenBy ( tuple => tuple[n - 1] ) . Select ( tuple => tuple[n] )

                    List <OrderingClause> clauses = orderBy.OrderingClauses;

                    LinqTuple tuple        = new LinqTuple(clauses, v);
                    LinqOp    order_by_ops = BuildTupleOrderingOps(clauses);
                    return(BuildSelectManyChain(generators, groupBy, tuple, order_by_ops));
                }
                else
                {
                    // PATTERNs (Rules #6, #7):
                    //	from x1 in e1 from x2 in e3 ... from xk in ek [select v | group v by g]
                    //
                    // TRANSLATIONs:
                    //  ( e1 ) . SelectMany ( x1 =>
                    //		( e2 ) . SelectMany ( x2 =>
                    //			...
                    //				RESPECTIVELY:
                    //				( ek ) . Select ( xk => v )
                    //				( ek ) . GroupBy ( xk => g, xk => v )
                    //			...
                    //		)
                    //  )

                    return(BuildSelectManyChain(generators, groupBy, v, null));
                }
            }
            else
            {
                // single from-clause (Rules #8 to #11) //

                DirectVarUse x_key   = generators[0].KeyVar;
                DirectVarUse x_value = generators[0].ValueVar;

                LinqOp last_op;
                if (select != null)
                {
                    last_op = new LinqOp.Select(x_key, x_value, select.Expression);
                }
                else
                {
                    last_op = new LinqOp.GroupBy(x_key, x_value, groupBy.GroupExpr, groupBy.ByExpr);
                }

                if (orderBy != null)
                {
                    // PATTERNs (Rules #10, #11):
                    //	from x in e orderby k1 , k2, ..., kn [select v | group v by g]
                    //
                    // TRANSLATIONs:
                    //	( e ) . OrderBy ( x => k1 ) . ThenBy ( x => k2 ) ... . ThenBy (x => kn) . Select ( x => v )
                    //	( e ) . OrderBy ( x => k1 ) . ThenBy ( x => k2 ) ... . ThenBy (x => kn) . GroupBy ( x => g, x => v )

                    return(new LinqOpChain(
                               generators[0].Expression,
                               BuildOrderingOps(x_key, x_value, orderBy.OrderingClauses, last_op)
                               ));
                }
                else
                {
                    // PATTERNs (Rules #8, #9):
                    //	from x in e [select v | group v by g]
                    //
                    // TRANSLATIONs:
                    //	( e ) . Select ( x => v )
                    //  ( e ) . GroupBy ( x => g, x => v )

                    return(new LinqOpChain(generators[0].Expression, last_op));
                }
            }
        }
Exemple #6
0
 public LinqOpChain(Expression /*!*/ expression, LinqOp firstOp)
     : base(Position.Invalid)
 {
     this.expression = expression;
     this.firstOp    = firstOp;
 }
Exemple #7
0
		public LinqOpChain(Expression/*!*/ expression, LinqOp firstOp)
			: base(Position.Invalid)
		{
			this.expression = expression;
			this.firstOp = firstOp;
		}
Exemple #8
0
			public OrderBy(DirectVarUse keyVar, DirectVarUse valueVar, Expression expression,
				Ordering ordering, LinqOp next, bool isThenBy)
			{
				this.keyVar = keyVar;
				this.valueVar = valueVar;
				this.expression = expression;
				this.ordering = ordering;
				this.next = next;
				this.isThenBy = isThenBy;
			}
Exemple #9
0
		private static LinqOp BuildOrderingOps(DirectVarUse keyVar, DirectVarUse valueVar,
			List<OrderingClause>/*!*/ clauses, LinqOp lastOp)
		{
			// PATTERNs: 
			//	from x in e orderby k1 , k2, ..., kn [select v | group v by g]
			//
			// TRANSLATIONs:
			//	( e ) . OrderBy ( x => k1 ) . ThenBy ( x => k2 ) ... . ThenBy (x => kn) . Select ( x => v )
			//	( e ) . OrderBy ( x => k1 ) . ThenBy ( x => k2 ) ... . ThenBy (x => kn) . GroupBy ( x => g, x => v )

			Debug.Assert(clauses != null && clauses.Count > 0);

			for (int i = clauses.Count - 1; i >= 0; i--)
				lastOp = new LinqOp.OrderBy(keyVar, valueVar, clauses[i].Expression, clauses[i].Ordering, lastOp, i != 0);

			return lastOp;
		}
Exemple #10
0
		private static LinqOpChain BuildSelectManyChain(List<Generator>/*!*/ generators,
			GroupByClause gbClause, Expression/*!*/ v, LinqOp lastOp)
		{
			// PATTERNs (Rules #6, #7): 
			//	from x1 in e1 from x2 in e3 ... from xk in ek [select v | group v by g]
			//
			// TRANSLATIONs: 
			//  ( e1 ) . SelectMany ( x1 => 
			//		( e2 ) . SelectMany ( x2 =>
			//			...
			//				RESPECTIVELY:
			//				( ek ) . Select ( xk => v )       
			//				( ek ) . GroupBy ( xk => g, xk => v )
			//			... 
			//		)
			//  ) 
			//	. [lastOp]

			int i = generators.Count - 1;

			LinqOp outer_op; // outer-most operator in the current chain

			if (gbClause != null)
				outer_op = new LinqOp.GroupBy(generators[i].KeyVar, generators[i].ValueVar, gbClause.ByExpr, v);
			else
				outer_op = new LinqOp.Select(generators[i].KeyVar, generators[i].ValueVar, v);

			// inner-most:
			LinqOpChain inner_chain = new LinqOpChain(generators[i].Expression, outer_op);

			while (--i >= 0)
			{
				inner_chain = new LinqOpChain(
					generators[i].Expression,
					outer_op = new LinqOp.SelectMany(generators[i].KeyVar, generators[i].ValueVar, inner_chain)
				);
			}

			outer_op.Next = lastOp;

			return inner_chain;
		}