internal OrderByJoinCollection Clone()
		{
			OrderByJoinCollection clone = new OrderByJoinCollection();
			foreach( OrderByJoin item in this.InnerList )
			{
				clone.Add(item.Clone());
			}
			return clone;
		}
        internal OrderByJoinCollection Clone()
        {
            OrderByJoinCollection clone = new OrderByJoinCollection();

            foreach (OrderByJoin item in this.InnerList)
            {
                clone.Add(item.Clone());
            }
            return(clone);
        }
Esempio n. 3
0
        public OrderBy Parse(Type type, string expression)
        {
            // Example expressions for an Employee query:
            // "FirstName, LastName"
            // "FirstName ASC, LastName DESC"
            // "Company.Name"
            // "Company.Name ASC"
            // "Company.Address.State ASC"
            // "Company.Name ASC, Company.Address.State ASC, FirstName ASC"
            // "Company.Name ASC, HomeAddress.ZipCode, Company.Address.State DESC, ZipCode"

            _expression = expression;

            EntityMap entity = _maps[type];

            if (entity == null)
            {
                throw new Exception("Type " + type + " does not have an entity mapping defined to the database.");
            }

            OrderByItemCollection items = new OrderByItemCollection();
            OrderByJoinCollection joins = new OrderByJoinCollection();

            using (StringReader reader = new StringReader(expression))
            {
                OPathLexer lexer = new OPathLexer(reader);

                lexer.Lex();
                while (lexer.CurrentToken != null)
                {
                    OrderByItem item = ParseItem(lexer, entity, joins, null);
                    items.Add(item);
                }

                if (lexer.LastToken != null && lexer.LastToken.Type == TokenType.COMMA)
                {
                    throw new OPathException("A trailing comma was detected in sort expression '" + expression + "'.");
                }
            }

            return(new OrderBy(null, items, joins));
        }
Esempio n. 4
0
        public OrderBy Parse(Type type, string expression)
        {
            // Example expressions for an Employee query:
            // "FirstName, LastName"
            // "FirstName ASC, LastName DESC"
            // "Company.Name"
            // "Company.Name ASC"
            // "Company.Address.State ASC"
            // "Company.Name ASC, Company.Address.State ASC, FirstName ASC"
            // "Company.Name ASC, HomeAddress.ZipCode, Company.Address.State DESC, ZipCode"

            _expression = expression;

            EntityMap entity = _maps[type];
            if( entity == null )
            {
                throw new Exception("Type " + type + " does not have an entity mapping defined to the database.");
            }

            OrderByItemCollection items = new OrderByItemCollection();
            OrderByJoinCollection joins = new OrderByJoinCollection();

            using( StringReader reader = new StringReader(expression) )
            {
                OPathLexer lexer = new OPathLexer(reader);

                lexer.Lex();
                while( lexer.CurrentToken != null )
                {
                    OrderByItem item = ParseItem(lexer, entity, joins, null);
                    items.Add(item);
                }

                if( lexer.LastToken != null && lexer.LastToken.Type == TokenType.COMMA )
                {
                    throw new OPathException("A trailing comma was detected in sort expression '" + expression + "'.");
                }
            }

            return new OrderBy(null, items, joins);
        }
Esempio n. 5
0
        private OrderByItem ParseItem(OPathLexer lexer, EntityMap entity, OrderByJoinCollection joins, OrderByJoin parentJoin)
        {
            Yytoken token = lexer.CurrentToken;

            if (token.Type != TokenType.IDENT)
            {
                throw new OPathException("'" + token.Text + "' encountered where a property or relationship was expected in sort expression '" + _expression + "'.");
            }

            string propertyName = token.Text;

            token = lexer.Lex();
            if (token == null)
            {
                FieldMap field;
                try
                {
                    field = entity.GetFieldMap(propertyName);
                }
                catch
                {
                    throw new OPathException(string.Format("The specified property '{0}' in the sort '{1}' is not defined in the entity map for type '{2}'.", propertyName, _expression, entity.Type));
                }

                return(new OrderByItem(propertyName, field, true, parentJoin));
            }
            if (token.Type != TokenType.PERIOD)
            {
                if (token.Type != TokenType.ASCEND && token.Type != TokenType.DESCEND && token.Type != TokenType.COMMA)
                {
                    throw new OPathException("'" + token.Text + "' is not valid in sort expression '" + _expression + "'.");
                }

                FieldMap field;
                try
                {
                    field = entity.GetFieldMap(propertyName);
                }
                catch
                {
                    throw new OPathException(string.Format("The specified property '{0}' in the sort '{1}' is not defined in the entity map for type '{2}'.", propertyName, _expression, entity.Type));
                }

                bool ascending = (token.Type != TokenType.DESCEND);

                if (token.Type != TokenType.COMMA)
                {
                    lexer.MoveToNext();
                }
                lexer.MoveToNext();

                return(new OrderByItem(propertyName, field, ascending, parentJoin));
            }
            else             // dot operator (.)
            {
                token = lexer.Lex();
                if (token == null)
                {
                    throw new OPathException("End of expression encountered where a property or relationship was expected in sort expression '" + _expression + "'.");
                }
                if (token.Type != TokenType.IDENT)
                {
                    throw new OPathException("'" + token.Text + "' encountered where a property or relationship was expected in sort expression '" + _expression + "'.");
                }

                RelationMap relation = entity.Relation(propertyName);
                if (relation == null)
                {
                    throw new OPathException(string.Format("The specified relationship '{0}' in the sort expression '{1}' is not defined in the entity map for type '{2}'.", propertyName, _expression, entity.Type));
                }
                if (relation.Relationship != Relationship.Parent)
                {
                    throw new Exception("Relationship '" + relation.Alias + "' is not a ManyToOne relation.  Only ManyToOne relations are allowed in sort expressions.");
                }

                EntityMap relEntity = _maps[relation.Type];

                OrderByJoin join = joins[propertyName];
                if (join == null)
                {
                    join = new OrderByJoin(relation);
                    joins.Add(join);
                }

                // recursive call
                return(ParseItem(lexer, relEntity, join.NestedJoins, join));
            }
        }
Esempio n. 6
0
		public OrderBy(Expression source, OrderByItemCollection orderByItems, OrderByJoinCollection orderByJoins)
		{
			this.Source = source;
			_items = orderByItems;
			_joins = orderByJoins;
		}
Esempio n. 7
0
        private OrderByItem ParseItem(OPathLexer lexer, EntityMap entity, OrderByJoinCollection joins, OrderByJoin parentJoin)
        {
            Yytoken token = lexer.CurrentToken;
            if( token.Type != TokenType.IDENT )
            {
                throw new OPathException("'" + token.Text + "' encountered where a property or relationship was expected in sort expression '" + _expression + "'.");
            }

            string propertyName = token.Text;

            token = lexer.Lex();
            if( token == null )
            {
                FieldMap field;
                try
                {
                    field = entity.GetFieldMap(propertyName);
                }
                catch
                {
                    throw new OPathException(string.Format("The specified property '{0}' in the sort '{1}' is not defined in the entity map for type '{2}'.", propertyName, _expression, entity.Type));
                }

                return new OrderByItem(propertyName, field, true, parentJoin);
            }
            if( token.Type != TokenType.PERIOD )
            {
                if( token.Type != TokenType.ASCEND && token.Type != TokenType.DESCEND && token.Type != TokenType.COMMA )
                {
                    throw new OPathException("'" + token.Text + "' is not valid in sort expression '" + _expression + "'.");
                }

                FieldMap field;
                try
                {
                    field = entity.GetFieldMap(propertyName);
                }
                catch
                {
                    throw new OPathException(string.Format("The specified property '{0}' in the sort '{1}' is not defined in the entity map for type '{2}'.", propertyName, _expression, entity.Type));
                }

                bool ascending = (token.Type != TokenType.DESCEND);

                if( token.Type != TokenType.COMMA )
                {
                    lexer.MoveToNext();
                }
                lexer.MoveToNext();

                return new OrderByItem(propertyName, field, ascending, parentJoin);
            }
            else // dot operator (.)
            {
                token = lexer.Lex();
                if( token == null )
                {
                    throw new OPathException("End of expression encountered where a property or relationship was expected in sort expression '" + _expression + "'.");
                }
                if( token.Type != TokenType.IDENT )
                {
                    throw new OPathException("'" + token.Text + "' encountered where a property or relationship was expected in sort expression '" + _expression + "'.");
                }

                RelationMap relation = entity.Relation(propertyName);
                if( relation == null )
                {
                    throw new OPathException(string.Format("The specified relationship '{0}' in the sort expression '{1}' is not defined in the entity map for type '{2}'.", propertyName, _expression, entity.Type));
                }
                if( relation.Relationship != Relationship.Parent )
                {
                    throw new Exception("Relationship '" + relation.Alias + "' is not a ManyToOne relation.  Only ManyToOne relations are allowed in sort expressions.");
                }

                EntityMap relEntity = _maps[relation.Type];

                OrderByJoin join = joins[propertyName];
                if( join == null )
                {
                    join = new OrderByJoin(relation);
                    joins.Add(join);
                }

                // recursive call
                return ParseItem(lexer, relEntity, join.NestedJoins, join);
            }
        }
Esempio n. 8
0
		private void AddOrderByJoinsToList(ArrayList joinList, EntityMap sourceEntity, string sourceAlias, OrderByJoinCollection joins, int indentDepth)
		{
			foreach( OrderByJoin join in joins )
			{
				RelationMap relation = join.RelationMap;
				if( relation.Relationship != Relationship.Parent )
				{
					throw new Exception("Relationship '" + relation.Alias + "' is not a ManyToOne relation.  Only ManyToOne relations are allowed in sort expressions.");
				}

				EntityMap joinEntity = _maps[relation.Type];
				join.Alias = GetNextAlias();
				
				string relationConstraint = ReplaceTableNameWithAlias(relation.Filter, sourceEntity.Table, sourceAlias);
				relationConstraint = ReplaceTableNameWithAlias(relationConstraint, joinEntity.Table, join.Alias);

				StringWriter joinCondition = new StringWriter();
				bool whereStarted = false;
				WriteJoinCondition(joinCondition, sourceAlias, relation.Fields, join.Alias, GetFieldNames(joinEntity.KeyFields), relationConstraint, false, ref whereStarted);

				joinList.Add( new Join(joinEntity.Table, join.Alias, joinCondition.ToString(), indentDepth));

				// traverse inner joins using a recursive call
				if( join.NestedJoins.Count > 0 )
				{
					AddOrderByJoinsToList(joinList, joinEntity, join.Alias, join.NestedJoins, indentDepth + 1);
				}
			}
		}
Esempio n. 9
0
 public OrderBy(Expression source, OrderByItemCollection orderByItems, OrderByJoinCollection orderByJoins)
 {
     this.Source = source;
     _items      = orderByItems;
     _joins      = orderByJoins;
 }
Esempio n. 10
0
        private void AddOrderByJoinsToList(ArrayList joinList, EntityMap sourceEntity, string sourceAlias, OrderByJoinCollection joins, int indentDepth)
        {
            foreach (OrderByJoin join in joins)
            {
                RelationMap relation = join.RelationMap;
                if (relation.Relationship != Relationship.Parent)
                {
                    throw new Exception("Relationship '" + relation.Alias + "' is not a ManyToOne relation.  Only ManyToOne relations are allowed in sort expressions.");
                }

                EntityMap joinEntity = _maps[relation.Type];
                join.Alias = GetNextAlias();

                string relationConstraint = ReplaceTableNameWithAlias(relation.Filter, sourceEntity.Table, sourceAlias);
                relationConstraint = ReplaceTableNameWithAlias(relationConstraint, joinEntity.Table, join.Alias);

                StringWriter joinCondition = new StringWriter();
                bool         whereStarted  = false;
                WriteJoinCondition(joinCondition, sourceAlias, relation.Fields, join.Alias, GetFieldNames(joinEntity.KeyFields), relationConstraint, false, ref whereStarted);

                joinList.Add(new Join(joinEntity.Table, join.Alias, joinCondition.ToString(), indentDepth));

                // traverse inner joins using a recursive call
                if (join.NestedJoins.Count > 0)
                {
                    AddOrderByJoinsToList(joinList, joinEntity, join.Alias, join.NestedJoins, indentDepth + 1);
                }
            }
        }