Ejemplo n.º 1
0
        private void HandleElements(FromReferenceNode collectionNode, String propertyName)
        {
            FromElement          collectionFromElement = collectionNode.FromElement;
            IQueryableCollection queryableCollection   = collectionFromElement.QueryableCollection;

            String path = collectionNode.Path + "[]." + propertyName;

            Log.Debug("Creating elements for {0}", path);

            _fromElement = collectionFromElement;
            if (!collectionFromElement.IsCollectionOfValuesOrComponents)
            {
                Walker.AddQuerySpaces(queryableCollection.ElementPersister);
            }

            DataType       = queryableCollection.ElementType;
            _selectColumns = collectionFromElement.ToColumns(_fromElement.TableAlias, propertyName, _inSelect);
        }
Ejemplo n.º 2
0
        public override void ResolveIndex(IASTNode parent)
        {
            // An ident node can represent an index expression if the ident
            // represents a naked property ref
            //      *Note: this makes the assumption (which is currently the case
            //      in the hql-sql grammar) that the ident is first resolved
            //      itself (addrExpr -> resolve()).  The other option, if that
            //      changes, is to call resolve from here; but it is
            //      currently un-needed overhead.
            if (!(IsResolved && _nakedPropertyRef))
            {
                throw new InvalidOperationException();
            }

            string propertyName = OriginalText;

            if (!DataType.IsCollectionType)
            {
                throw new SemanticException("Collection expected; [" + propertyName + "] does not refer to a collection property");
            }

            // TODO : most of below was taken verbatim from DotNode; should either delegate this logic or super-type it
            CollectionType       type = (CollectionType)DataType;
            string               role = type.Role;
            IQueryableCollection queryableCollection = SessionFactoryHelper.RequireQueryableCollection(role);

            string columnTableAlias = FromElement.TableAlias;

            FromElementFactory factory = new FromElementFactory(
                Walker.CurrentFromClause,
                FromElement,
                propertyName,
                null,
                FromElement.ToColumns(columnTableAlias, propertyName, false),
                true
                );

            FromElement elem = factory.CreateCollection(queryableCollection, role, JoinType.InnerJoin, false, true);

            FromElement = elem;
            Walker.AddQuerySpaces(queryableCollection.CollectionSpaces);                // Always add the collection's query spaces.
        }
Ejemplo n.º 3
0
        private void DereferenceEntityJoin(string classAlias, EntityType propertyType, bool impliedJoin, IASTNode parent)
        {
            _dereferenceType = DerefEntity;
            if (Log.IsDebugEnabled)
            {
                Log.Debug("dereferenceEntityJoin() : generating join for " + _propertyName + " in "
                          + FromElement.ClassName + " "
                          + ((classAlias == null) ? "{no alias}" : "(" + classAlias + ")")
                          + " parent = " + ASTUtil.GetDebugstring(parent)
                          );
            }

            // Create a new FROM node for the referenced class.
            string associatedEntityName = propertyType.GetAssociatedEntityName();
            string tableAlias           = AliasGenerator.CreateName(associatedEntityName);

            string[] joinColumns = GetColumns();
            string   joinPath    = Path;

            if (impliedJoin && Walker.IsInFrom)
            {
                _joinType = Walker.ImpliedJoinType;
            }

            FromClause  currentFromClause = Walker.CurrentFromClause;
            FromElement elem = currentFromClause.FindJoinByPath(joinPath);

            ///////////////////////////////////////////////////////////////////////////////
            //
            // This is the piece which recognizes the condition where an implicit join path
            // resolved earlier in a correlated subquery is now being referenced in the
            // outer query.  For 3.0final, we just let this generate a second join (which
            // is exactly how the old parser handles this).  Eventually we need to add this
            // logic back in and complete the logic in FromClause.promoteJoin; however,
            // FromClause.promoteJoin has its own difficulties (see the comments in
            // FromClause.promoteJoin).
            //
            //		if ( elem == null ) {
            //			// see if this joinPath has been used in a "child" FromClause, and if so
            //			// promote that element to the outer query
            //			FromClause currentNodeOwner = getFromElement().getFromClause();
            //			FromClause currentJoinOwner = currentNodeOwner.locateChildFromClauseWithJoinByPath( joinPath );
            //			if ( currentJoinOwner != null && currentNodeOwner != currentJoinOwner ) {
            //				elem = currentJoinOwner.findJoinByPathLocal( joinPath );
            //				if ( elem != null ) {
            //					currentFromClause.promoteJoin( elem );
            //					// EARLY EXIT!!!
            //					return;
            //				}
            //			}
            //		}
            //
            ///////////////////////////////////////////////////////////////////////////////

            bool found = elem != null;
            // even though we might find a pre-existing element by join path, for FromElements originating in a from-clause
            // we should only ever use the found element if the aliases match (null != null here).  Implied joins are
            // always (?) ok to reuse.
            bool useFoundFromElement = found && (elem.IsImplied || (AreSame(classAlias, elem.ClassAlias)));

            if (!useFoundFromElement)
            {
                // If this is an implied join in a from element, then use the impled join type which is part of the
                // tree parser's state (set by the gramamar actions).
                JoinSequence joinSequence = SessionFactoryHelper
                                            .CreateJoinSequence(impliedJoin, propertyType, tableAlias, _joinType, joinColumns);

                FromElementFactory factory = new FromElementFactory(
                    currentFromClause,
                    GetLhs().FromElement,
                    joinPath,
                    classAlias,
                    joinColumns,
                    impliedJoin
                    );
                elem = factory.CreateEntityJoin(
                    associatedEntityName,
                    tableAlias,
                    joinSequence,
                    _fetch,
                    Walker.IsInFrom,
                    propertyType
                    );
            }
            else
            {
                currentFromClause.AddDuplicateAlias(classAlias, elem);
            }


            SetImpliedJoin(elem);
            Walker.AddQuerySpaces(elem.EntityPersister.QuerySpaces);
            FromElement = elem;                 // This 'dot' expression now refers to the resulting from element.
        }
Ejemplo n.º 4
0
        private void DereferenceCollection(CollectionType collectionType, bool implicitJoin, bool indexed, string classAlias)
        {
            _dereferenceType = DerefCollection;
            string role = collectionType.Role;

            //foo.bars.size (also handles deprecated stuff like foo.bars.maxelement for backwardness)
            IASTNode sibling        = NextSibling;
            bool     isSizeProperty = sibling != null && CollectionProperties.IsAnyCollectionProperty(sibling.Text);

            if (isSizeProperty)
            {
                indexed = true;                 //yuck!}
            }

            IQueryableCollection queryableCollection = SessionFactoryHelper.RequireQueryableCollection(role);
            string     propName          = Path;
            FromClause currentFromClause = Walker.CurrentFromClause;

            if (Walker.StatementType != HqlSqlWalker.SELECT && indexed && classAlias == null)
            {
                // should indicate that we are processing an INSERT/UPDATE/DELETE
                // query with a subquery implied via a collection property
                // function. Here, we need to use the table name itself as the
                // qualification alias.
                // TODO : verify this works for all databases...
                // TODO : is this also the case in non-"indexed" scenarios?
                string alias = GetLhs().FromElement.Queryable.TableName;
                _columns = FromElement.ToColumns(alias, _propertyPath, false, true);
            }

            //We do not look for an existing join on the same path, because
            //it makes sense to join twice on the same collection role
            FromElementFactory factory = new FromElementFactory(
                currentFromClause,
                GetLhs().FromElement,
                propName,
                classAlias,
                GetColumns(),
                implicitJoin
                );
            FromElement elem = factory.CreateCollection(queryableCollection, role, _joinType, _fetch, indexed);

            if (Log.IsDebugEnabled)
            {
                Log.Debug("dereferenceCollection() : Created new FROM element for " + propName + " : " + elem);
            }

            SetImpliedJoin(elem);
            FromElement = elem;                 // This 'dot' expression now refers to the resulting from element.

            if (isSizeProperty)
            {
                elem.Text             = "";
                elem.UseWhereFragment = false;
            }

            if (!implicitJoin)
            {
                IEntityPersister entityPersister = elem.EntityPersister;
                if (entityPersister != null)
                {
                    Walker.AddQuerySpaces(entityPersister.QuerySpaces);
                }
            }
            Walker.AddQuerySpaces(queryableCollection.CollectionSpaces);                // Always add the collection's query spaces.
        }