Exemplo n.º 1
0
        /// <summary>
        /// Add on association (one-to-one, many-to-one, or a collection) to a list
        /// of associations to be fetched by outerjoin
        /// </summary>
        private void AddAssociationToJoinTree(IAssociationType type, string[] aliasedLhsColumns, string alias,
                                              string path, int currentDepth, JoinType joinType)
        {
            IJoinable joinable = type.GetAssociatedJoinable(Factory);

            string subalias = GenerateTableAlias(associations.Count + 1, path, joinable);

            OuterJoinableAssociation assoc =
                new OuterJoinableAssociation(type, alias, aliasedLhsColumns, subalias, joinType, GetWithClause(path), Factory, enabledFilters);

            assoc.ValidateJoin(path);
            AddAssociation(subalias, assoc);

            int nextDepth = currentDepth + 1;

            if (!joinable.IsCollection)
            {
                IOuterJoinLoadable pjl = joinable as IOuterJoinLoadable;
                if (pjl != null)
                {
                    WalkEntityTree(pjl, subalias, path, nextDepth);
                }
            }
            else
            {
                IQueryableCollection qc = joinable as IQueryableCollection;
                if (qc != null)
                {
                    WalkCollectionTree(qc, subalias, path, nextDepth);
                }
            }
        }
Exemplo n.º 2
0
        protected override string GenerateTableAlias(int n, string path, IJoinable joinable)
        {
            bool shouldCreateUserAlias = joinable.ConsumesEntityAlias();

            if (shouldCreateUserAlias == false && joinable.IsCollection)
            {
                var elementType = ((ICollectionPersister)joinable).ElementType;
                if (elementType != null)
                {
                    shouldCreateUserAlias = elementType.IsComponentType;
                }
            }
            if (shouldCreateUserAlias)
            {
                ICriteria subcriteria = translator.GetCriteria(path);
                string    sqlAlias    = subcriteria == null ? null : translator.GetSQLAlias(subcriteria);
                if (sqlAlias != null)
                {
                    userAliasList.Add(subcriteria.Alias); //alias may be null
                    return(sqlAlias);                     //EARLY EXIT
                }

                userAliasList.Add(null);
            }
            return(base.GenerateTableAlias(n + translator.SQLAliasCount, path, joinable));
        }
Exemplo n.º 3
0
        private void AddExtraJoins(JoinFragment joinFragment, string alias, IJoinable joinable, bool innerJoin)
        {
            bool include = IsIncluded(alias);

            joinFragment.AddJoins(joinable.FromJoinFragment(alias, innerJoin, include),
                                  joinable.WhereJoinFragment(alias, innerJoin, include));
        }
Exemplo n.º 4
0
        public override string SelectFragment(IJoinable rhs, string rhsAlias, string lhsAlias, string collectionSuffix, bool includeCollectionColumns, EntityLoadInfo entityInfo)
        {
            var buf = new StringBuilder();

            if (includeCollectionColumns)
            {
                buf.Append(SelectFragment(lhsAlias, collectionSuffix)).Append(StringHelper.CommaSpace);
            }

            //6.0 TODO: Remove
#pragma warning disable 618
            if (entityInfo.IncludeLazyProps)
            {
                var selectMode = ReflectHelper.CastOrThrow <ISupportSelectModeJoinable>(ElementPersister, "fetch lazy properties");
                if (selectMode != null)
                {
                    return(buf.Append(selectMode.SelectFragment(null, null, lhsAlias, entityInfo.EntitySuffix, null, false, true)).ToString());
                }
            }
#pragma warning restore 618

            if (entityInfo.LazyProperties?.Count > 0)
            {
                var selectMode = ReflectHelper.CastOrThrow <ISupportLazyPropsJoinable>(ElementPersister, "fetch lazy properties");
                if (selectMode != null)
                {
                    return(buf.Append(selectMode.SelectFragment(null, null, lhsAlias, null, false, entityInfo)).ToString());
                }
            }

            var ojl = (IOuterJoinLoadable)ElementPersister;
            return(buf.Append(ojl.SelectFragment(lhsAlias, entityInfo.EntitySuffix)).ToString());            //use suffix for the entity columns
        }
Exemplo n.º 5
0
        private void AddSubclassJoins(JoinFragment joinFragment, String alias, IJoinable joinable, bool innerJoin, bool includeSubclassJoins)
        {
            bool include = includeSubclassJoins && IsIncluded(alias);

            joinFragment.AddJoins(joinable.FromJoinFragment(alias, innerJoin, include),
                                  joinable.WhereJoinFragment(alias, innerJoin, include));
        }
        private string ManyToManySelectFragment(
            IJoinable rhs,
            string rhsAlias,
            string lhsAlias,
            string collectionSuffix,
            IAssociationType elementType)
        {
            SelectFragment frag = GenerateSelectFragment(lhsAlias, collectionSuffix);

            // We need to select in the associated entity table instead of taking the collection actual element,
            // because filters can be applied to the entity table outer join. In such case, we need to return null
            // for filtered-out elements. (It is tempting to switch to an inner join and just use
            // SelectFragment(lhsAlias, collectionSuffix) for many-to-many too, but this would hinder the proper
            // handling of the not-found feature.)
            var elementColumnNames = string.IsNullOrEmpty(elementType.RHSUniqueKeyPropertyName)
                                ? rhs.KeyColumnNames
                                     // rhs is the entity persister, it does not handle being referenced through an unique key by a
                                     // collection and always yield its identifier columns as KeyColumnNames. We need to resolve the
                                     // key columns instead.
                                     // 6.0 TODO: consider breaking again that IJoinable.SelectFragment interface for transmitting
                                     // the OuterJoinableAssociation instead of its Joinable property. This would allow to get the
                                     // adequate columns directly instead of re-computing them.
                                : ((IPropertyMapping)rhs).ToColumns(elementType.RHSUniqueKeyPropertyName);

            frag.AddColumns(rhsAlias, elementColumnNames, elementColumnAliases);
            AppendIndexColumns(frag, lhsAlias);
            AppendIdentifierColumns(frag, lhsAlias);

            return(frag.ToSqlStringFragment(false));
        }
Exemplo n.º 7
0
        public void Reset()
        {
            obj_to_syncvar.Clear();
            foreach (SafeKeyValuePair <WeakReference, SyncVar> de in initState.obj_to_syncvar)
            {
                obj_to_syncvar.Add(de.Key, de.Value);
            }
            // good bye to exited threads
            foreach (SafeKeyValuePair <ChessTask, bool> de in exited_threads)
            {
                IJoinable t = null;
                if (ctid_to_tid.TryGetValue(de.Key, out t))
                {
                    t.Dispose(this);
                    ctid_to_tid.Remove(de.Key);
                }
            }

            handleToSyncVar.Clear();
            foreach (SafeKeyValuePair <System.IntPtr, SyncVar> de in initState.handleToSyncVar)
            {
                handleToSyncVar.Add(de.Key, de.Value);
            }
            // TODO: we need to keep track of init case, as with initState.obj
            // we need to explicitly release GC addresses
            ReleaseGCAddresses();
            exited_threads.Clear();

            // TODO: initialization as with others?
            uintptr_to_syncvar.Clear();
            // reset wrapper state
            MonitorHelper.Reset();
        }
Exemplo n.º 8
0
 public void AddIJoinableMapping(ChessTask ctid, IJoinable t)
 {
     lock (this)
     {
         // overwrite any previous mapping
         ctid_to_tid[ctid] = t;
     }
 }
Exemplo n.º 9
0
        /// <summary>
        /// Get the columns of the associated table which are to
        /// be used in the join
        /// </summary>
        public static string[] GetRHSColumnNames(IJoinable joinable, IAssociationType type)
        {
            string uniqueKeyPropertyName = type.RHSUniqueKeyPropertyName;

            return(uniqueKeyPropertyName == null
                                ? joinable.KeyColumnNames
                                : ((IOuterJoinLoadable)joinable).GetPropertyColumnNames(uniqueKeyPropertyName));
        }
Exemplo n.º 10
0
 public Join(ISessionFactoryImplementor factory, IAssociationType associationType, string alias, JoinType joinType,
             string[] lhsColumns)
 {
     this.associationType = associationType;
     this.joinable        = associationType.GetAssociatedJoinable(factory);
     this.alias           = alias;
     this.joinType        = joinType;
     this.lhsColumns      = lhsColumns;
 }
        private Persister.Entity.IJoinable GetPathJoinable(string path)
        {
            // start with the root
            IJoinable last = rootPersister;

            var tokens = path.Split(new[] { '.' }, StringSplitOptions.RemoveEmptyEntries);

            if (tokens.Length == 0)
            {
                return(last);
            }

            IPropertyMapping lastEntity = rootPersister;
            int i = 0;

            if (entityJoins.TryGetValue(tokens[0], out var entityJoinInfo))
            {
                last       = entityJoinInfo.Persister;
                lastEntity = (IPropertyMapping)last;
                i++;
            }

            string componentPath = string.Empty;

            for (; i < tokens.Length; i++)
            {
                componentPath += tokens[i];
                IType type = lastEntity.ToType(componentPath);
                if (type.IsAssociationType)
                {
                    if (type.IsCollectionType)
                    {
                        // ignore joinables for composite collections
                        var collectionType = (CollectionType)type;
                        var persister      = Factory.GetCollectionPersister(collectionType.Role);
                        if (persister.ElementType.IsEntityType == false)
                        {
                            return(null);
                        }
                    }
                    IAssociationType atype = (IAssociationType)type;

                    last          = atype.GetAssociatedJoinable(Factory);
                    lastEntity    = (NHibernate_Persister_Entity.IPropertyMapping)Factory.GetEntityPersister(atype.GetAssociatedEntityName(Factory));
                    componentPath = "";
                }
                else if (type.IsComponentType)
                {
                    componentPath += '.';
                }
                else
                {
                    throw new QueryException("not an association: " + componentPath);
                }
            }
            return(last);
        }
Exemplo n.º 12
0
			public Join(ISessionFactoryImplementor factory, IAssociationType associationType, string alias, JoinType joinType,
			            string[] lhsColumns)
			{
				this.associationType = associationType;
				this.joinable = associationType.GetAssociatedJoinable(factory);
				this.alias = alias;
				this.joinType = joinType;
				this.lhsColumns = lhsColumns;
			}
Exemplo n.º 13
0
 private bool IsManyToManyRoot(IJoinable joinable)
 {
     if (joinable != null && joinable.IsCollection)
     {
         IQueryableCollection persister = (IQueryableCollection)joinable;
         return(persister.IsManyToMany);
     }
     return(false);
 }
Exemplo n.º 14
0
 public void JoinableTaskEnd(IJoinable t)
 {
     if (!IsJoinableTask(CurrentTid))
     {
         return;
     }
     RemoveJoinableTask(CurrentTid);
     terminatingThread = t;
 }
Exemplo n.º 15
0
        private FromElement CreateFromElement(IEntityPersister entityPersister)
        {
            IJoinable   joinable = (IJoinable)entityPersister;
            string      text     = joinable.TableName;
            IASTNode    ast      = CreateFromElement(text);
            FromElement element  = (FromElement)ast;

            return(element);
        }
Exemplo n.º 16
0
        public override void TaskEnd(ChessTask ctid)
        {
            Debug.Assert(CurrentTid == ctid);
            IJoinable t = syncVarManager.GetJoinableFromChessTask(ctid);

            JoinableTaskEnd(t);
            syncVarManager.TaskEnd(ctid);
            NumThreads--;
        }
Exemplo n.º 17
0
        private static AbstractEntityPersister GetEntityPersister(IJoinable joinable)
        {
            if (!joinable.IsCollection)
            {
                return(joinable as AbstractEntityPersister);
            }

            var collection = (IQueryableCollection)joinable;

            return(collection.ElementType.IsEntityType ? collection.ElementPersister as AbstractEntityPersister : null);
        }
Exemplo n.º 18
0
 private void CreateCriteriaCollectionPersisters()
 {
     foreach (KeyValuePair <string, ICriteria> me in associationPathCriteriaMap)
     {
         IJoinable joinable = GetPathJoinable(me.Key);
         if (joinable != null && joinable.IsCollection)
         {
             criteriaCollectionPersisters.Add((ICollectionPersister)joinable);
         }
     }
 }
Exemplo n.º 19
0
        private string ManyToManySelectFragment(IJoinable rhs, string rhsAlias, string lhsAlias, string collectionSuffix)
        {
            SelectFragment frag = GenerateSelectFragment(lhsAlias, collectionSuffix);

            string[] _elementColumnNames = rhs.KeyColumnNames;
            frag.AddColumns(rhsAlias, _elementColumnNames, elementColumnAliases);
            AppendIndexColumns(frag, lhsAlias);
            AppendIdentifierColumns(frag, lhsAlias);

            return frag.ToSqlStringFragment(false);
        }
Exemplo n.º 20
0
        protected override string GenerateTableAlias(int n, string path, IJoinable joinable)
        {
            // TODO: deal with side-effects (changes to includeInSelectList, userAliasList, resultTypeList)!!!

            // for collection-of-entity, we are called twice for given "path"
            // once for the collection Joinable, once for the entity Joinable.
            // the second call will/must "consume" the alias + perform side effects according to consumesEntityAlias()
            // for collection-of-other, however, there is only one call
            // it must "consume" the alias + perform side effects, despite what consumeEntityAlias() return says
            //
            // note: the logic for adding to the userAliasList is still strictly based on consumesEntityAlias return value

            bool shouldCreateUserAlias = joinable.ConsumesEntityAlias();

            if (!shouldCreateUserAlias && joinable.IsCollection)
            {
                // is it a collection-of-other (component or value) ?
                var elementType = ((ICollectionPersister)joinable).ElementType;
                if (elementType != null)
                {
                    shouldCreateUserAlias = elementType.IsComponentType || !elementType.IsEntityType;
                }
            }

            string sqlAlias = null;

            if (shouldCreateUserAlias)
            {
                ICriteria subcriteria = translator.GetCriteria(path);
                sqlAlias = subcriteria == null ? null : translator.GetSQLAlias(subcriteria);

                if (joinable.ConsumesEntityAlias() && !translator.HasProjection)
                {
                    includeInResultRowList.Add(subcriteria != null && subcriteria.Alias != null);

                    if (sqlAlias != null)
                    {
                        if (subcriteria.Alias != null)
                        {
                            userAliasList.Add(subcriteria.Alias);                             //alias may be null
                            resultTypeList.Add(translator.ResultType(subcriteria));
                        }
                    }
                }
            }

            if (sqlAlias == null)
            {
                sqlAlias = base.GenerateTableAlias(n + translator.SQLAliasCount, path, joinable);
            }

            return(sqlAlias);
        }
Exemplo n.º 21
0
 public Join(ISessionFactoryImplementor factory, IAssociationType associationType, string alias, JoinType joinType,
             string[] lhsColumns)
 {
     this.associationType = associationType;
     this.joinable        = associationType.GetAssociatedJoinable(factory);
     this.alias           = alias;
     this.joinType        = joinType;
     this.lhsColumns      = lhsColumns;
     this.rhsColumns      = lhsColumns.Length > 0
                             ? JoinHelper.GetRHSColumnNames(joinable, associationType)
                             : Array.Empty <string>();
 }
Exemplo n.º 22
0
        protected override string FilterFragment(string alias)
        {
            string    result = base.FilterFragment(alias);
            IJoinable j      = ElementPersister as IJoinable;

            if (j != null)
            {
                result += j.OneToManyFilterFragment(alias);
            }

            return(result);
        }
Exemplo n.º 23
0
        public override string SelectFragment(IJoinable rhs, string rhsAlias, string lhsAlias, string entitySuffix, string collectionSuffix, bool includeCollectionColumns)
        {
            var buf = new StringBuilder();

            if (includeCollectionColumns)
            {
                buf.Append(SelectFragment(lhsAlias, collectionSuffix)).Append(StringHelper.CommaSpace);
            }

            var ojl = (IOuterJoinLoadable)ElementPersister;

            return(buf.Append(ojl.SelectFragment(lhsAlias, entitySuffix)).ToString());            //use suffix for the entity columns
        }
Exemplo n.º 24
0
 public OuterJoinableAssociation(IAssociationType joinableType, String lhsAlias, String[] lhsColumns, String rhsAlias,
                                 JoinType joinType, ISessionFactoryImplementor factory,
                                 IDictionary <string, IFilter> enabledFilters)
 {
     this.joinableType = joinableType;
     this.lhsAlias     = lhsAlias;
     this.lhsColumns   = lhsColumns;
     this.rhsAlias     = rhsAlias;
     this.joinType     = joinType;
     joinable          = joinableType.GetAssociatedJoinable(factory);
     rhsColumns        = JoinHelper.GetRHSColumnNames(joinableType, factory);
     on = joinableType.GetOnCondition(rhsAlias, factory, enabledFilters);
     this.enabledFilters = enabledFilters;             // needed later for many-to-many/filter application
 }
		public OuterJoinableAssociation(IAssociationType joinableType, String lhsAlias, String[] lhsColumns, String rhsAlias,
		                                JoinType joinType, ISessionFactoryImplementor factory,
		                                IDictionary<string, IFilter> enabledFilters)
		{
			this.joinableType = joinableType;
			this.lhsAlias = lhsAlias;
			this.lhsColumns = lhsColumns;
			this.rhsAlias = rhsAlias;
			this.joinType = joinType;
			joinable = joinableType.GetAssociatedJoinable(factory);
			rhsColumns = JoinHelper.GetRHSColumnNames(joinableType, factory);
			on = joinableType.GetOnCondition(rhsAlias, factory, enabledFilters);
			this.enabledFilters = enabledFilters; // needed later for many-to-many/filter application
		}
 public override string SelectFragment(IJoinable rhs, string rhsAlias, string lhsAlias, string collectionSuffix, bool includeCollectionColumns, EntityLoadInfo entityInfo)
 {
     // we need to determine the best way to know that two joinables
     // represent a single many-to-many...
     if (rhs != null && IsManyToMany && !rhs.IsCollection)
     {
         IAssociationType elementType = (IAssociationType)ElementType;
         if (rhs.Equals(elementType.GetAssociatedJoinable(Factory)))
         {
             return(ManyToManySelectFragment(rhs, rhsAlias, lhsAlias, collectionSuffix, elementType));
         }
     }
     return(includeCollectionColumns ? SelectFragment(lhsAlias, collectionSuffix) : string.Empty);
 }
Exemplo n.º 27
0
        public override string GetTaskName(int ctid)
        {
            IJoinable j = syncVarManager.GetJoinableFromChessTask(ctid);

            if (j != null)
            {
                string name = j.Name();
                if (name != null)
                {
                    return(name);
                }
            }
            return(base.GetTaskName(ctid));
        }
Exemplo n.º 28
0
        /// <summary>
        /// Get the columns of the associated table which are to
        /// be used in the join
        /// </summary>
        public static string[] GetRHSColumnNames(IAssociationType type, ISessionFactoryImplementor factory)
        {
            string    uniqueKeyPropertyName = type.RHSUniqueKeyPropertyName;
            IJoinable joinable = type.GetAssociatedJoinable(factory);

            if (uniqueKeyPropertyName == null)
            {
                return(joinable.JoinColumnNames);
            }
            else
            {
                return(((IOuterJoinLoadable)joinable).GetPropertyColumnNames(uniqueKeyPropertyName));
            }
        }
Exemplo n.º 29
0
        public string[] GetReferencedColumns(ISessionFactoryImplementor factory)
        {
            //I really, really don't like the fact that a Type now knows about column mappings!
            //bad seperation of concerns ... could we move this somehow to Joinable interface??
            IJoinable joinable = GetJoinable(factory);

            if (uniqueKeyPropertyName == null)
            {
                return(joinable.JoinKeyColumnNames);
            }
            else
            {
                return((( IUniqueKeyLoadable )joinable).GetUniqueKeyColumnNames(uniqueKeyPropertyName));
            }
        }
Exemplo n.º 30
0
 public override string SelectFragment(IJoinable rhs, string rhsAlias, string lhsAlias,
     string entitySuffix, string collectionSuffix, bool includeCollectionColumns)
 {
     // we need to determine the best way to know that two joinables
     // represent a single many-to-many...
     if (rhs != null && IsManyToMany && !rhs.IsCollection)
     {
         IAssociationType elementType = (IAssociationType) ElementType;
         if (rhs.Equals(elementType.GetAssociatedJoinable(Factory)))
         {
             return ManyToManySelectFragment(rhs, rhsAlias, lhsAlias, collectionSuffix);
         }
     }
     return includeCollectionColumns ? SelectFragment(lhsAlias, collectionSuffix) : string.Empty;
 }
Exemplo n.º 31
0
        /// <summary>
        /// Generate a select list of columns containing all properties of the entity classes
        /// </summary>
        public string SelectString(IList <OuterJoinableAssociation> associations)
        {
            if (associations.Count == 0)
            {
                return(string.Empty);
            }
            else
            {
                SqlStringBuilder buf = new SqlStringBuilder(associations.Count * 3);

                int entityAliasCount     = 0;
                int collectionAliasCount = 0;

                for (int i = 0; i < associations.Count; i++)
                {
                    OuterJoinableAssociation join = associations[i];
                    OuterJoinableAssociation next = (i == associations.Count - 1) ? null : associations[i + 1];

                    IJoinable joinable     = join.Joinable;
                    string    entitySuffix = (suffixes == null || entityAliasCount >= suffixes.Length) ? null : suffixes[entityAliasCount];

                    string collectionSuffix = (collectionSuffixes == null || collectionAliasCount >= collectionSuffixes.Length)
                                                                                                                                                        ? null
                                                                                                                                                        : collectionSuffixes[collectionAliasCount];

                    string selectFragment =
                        joinable.SelectFragment(next == null ? null : next.Joinable, next == null ? null : next.RHSAlias, join.RHSAlias,
                                                entitySuffix, collectionSuffix, join.JoinType == JoinType.LeftOuterJoin);

                    if (selectFragment.Trim().Length > 0)
                    {
                        buf.Add(StringHelper.CommaSpace)
                        .Add(selectFragment);
                    }
                    if (joinable.ConsumesEntityAlias())
                    {
                        entityAliasCount++;
                    }

                    if (joinable.ConsumesCollectionAlias() && join.JoinType == JoinType.LeftOuterJoin)
                    {
                        collectionAliasCount++;
                    }
                }

                return(buf.ToSqlString().ToString());
            }
        }
		public OuterJoinableAssociation(IAssociationType joinableType, String lhsAlias, String[] lhsColumns, String rhsAlias,
		                                JoinType joinType, SqlString withClause, ISessionFactoryImplementor factory,
		                                IDictionary<string, IFilter> enabledFilters)
		{
			this.joinableType = joinableType;
			this.lhsAlias = lhsAlias;
			this.lhsColumns = lhsColumns;
			this.rhsAlias = rhsAlias;
			this.joinType = joinType;
			joinable = joinableType.GetAssociatedJoinable(factory);
			rhsColumns = JoinHelper.GetRHSColumnNames(joinableType, factory);
			on = new SqlString(joinableType.GetOnCondition(rhsAlias, factory, enabledFilters));
			if (StringHelper.IsNotEmpty(withClause))
				on = on.Append(" and ( ").Append(withClause).Append(" )");
			this.enabledFilters = enabledFilters; // needed later for many-to-many/filter application
		}
Exemplo n.º 33
0
        private void IncludeInResultIfNeeded(IJoinable joinable, ICriteria subcriteria, string sqlAlias)
        {
            if (joinable.ConsumesEntityAlias() && !translator.HasProjection)
            {
                includeInResultRowList.Add(subcriteria != null && subcriteria.Alias != null);

                if (sqlAlias != null)
                {
                    if (subcriteria.Alias != null)
                    {
                        userAliasList.Add(subcriteria.Alias);                         //alias may be null
                        resultTypeList.Add(translator.ResultType(subcriteria));
                    }
                }
            }
        }
Exemplo n.º 34
0
        /// <summary>
        /// Generate a select list of columns containing all properties of the entity classes
        /// </summary>
        public string SelectString(IList <OuterJoinableAssociation> associations)
        {
            if (associations.Count == 0)
            {
                return(string.Empty);
            }
            else
            {
                SqlStringBuilder buf = new SqlStringBuilder(associations.Count * 3);

                int entityAliasCount     = 0;
                int collectionAliasCount = 0;

                for (int i = 0; i < associations.Count; i++)
                {
                    OuterJoinableAssociation join = associations[i];
                    OuterJoinableAssociation next = (i == associations.Count - 1) ? null : associations[i + 1];

                    IJoinable joinable     = join.Joinable;
                    string    entitySuffix = (suffixes == null || entityAliasCount >= suffixes.Length) ? null : suffixes[entityAliasCount];

                    string collectionSuffix = (collectionSuffixes == null || collectionAliasCount >= collectionSuffixes.Length)
                                                                                                                                                        ? null
                                                                                                                                                        : collectionSuffixes[collectionAliasCount];

                    string selectFragment =
                        GetSelectFragment(join, entitySuffix, collectionSuffix, next);

                    if (!string.IsNullOrWhiteSpace(selectFragment))
                    {
                        buf.Add(StringHelper.CommaSpace)
                        .Add(selectFragment);
                    }
                    if (joinable.ConsumesEntityAlias() && join.SelectMode != SelectMode.JoinOnly)
                    {
                        entityAliasCount++;
                    }

                    if (joinable.ConsumesCollectionAlias() && join.ShouldFetchCollectionPersister())
                    {
                        collectionAliasCount++;
                    }
                }

                return(buf.ToSqlString().ToString());
            }
        }
Exemplo n.º 35
0
 protected override string GenerateTableAlias(int n, string path, IJoinable joinable)
 {
     if (joinable.ConsumesEntityAlias())
     {
         ICriteria subcriteria = translator.GetCriteria(path);
         String sqlAlias = subcriteria == null ? null : translator.GetSQLAlias(subcriteria);
         if (sqlAlias != null)
         {
             userAliasList.Add(subcriteria.Alias); //alias may be null
             return sqlAlias; //EARLY EXIT
         }
         else
         {
             userAliasList.Add(null);
         }
     }
     return base.GenerateTableAlias(n + translator.SQLAliasCount, path, joinable);
 }
Exemplo n.º 36
0
		public JoinSequence SetRoot(IJoinable joinable, string alias)
		{
			this.rootAlias = alias;
			this.rootJoinable = joinable;
			return this;
		}
Exemplo n.º 37
0
		private void AddExtraJoins(JoinFragment joinFragment, string alias, IJoinable joinable, bool innerJoin)
		{
			bool include = IsIncluded(alias);
			joinFragment.AddJoins(joinable.FromJoinFragment(alias, innerJoin, include),
			                      joinable.WhereJoinFragment(alias, innerJoin, include));
		}
		/// <summary>
		///  Add on association (one-to-one or many-to-one) to a list of associations be fetched by outerjoin (if necessary)
		/// </summary>
		/// <param name="type"></param>
		/// <param name="aliasedForeignKeyColumns"></param>
		/// <param name="persister"></param>
		/// <param name="alias"></param>
		/// <param name="associations"></param>
		/// <param name="visitedPersisters"></param>
		/// <param name="path"></param>
		/// <param name="currentDepth"></param>
		/// <param name="joinType"></param>
		/// <param name="factory"></param>
		private void WalkAssociationTree(
			IAssociationType type,
			string[ ] aliasedForeignKeyColumns,
			IJoinable persister,
			string alias,
			IList associations,
			ISet visitedPersisters,
			string path,
			int currentDepth,
			JoinType joinType,
			ISessionFactoryImplementor factory )
		{
			IJoinable joinable = type.GetJoinable( factory );

			int maxFetchDepth = factory.MaximumFetchDepth;

			bool enabled = ( joinType == JoinType.InnerJoin ) || (
				( maxFetchDepth <= 0 || currentDepth < maxFetchDepth ) &&
				!visitedPersisters.Contains( joinable ) &&
				( !joinable.IsCollection || !ContainsCollectionPersister( associations ) )
				);

			if ( enabled )
			{
				visitedPersisters.Add( persister );
				OuterJoinableAssociation assoc = new OuterJoinableAssociation();
				associations.Add( assoc );

				// After adding to collection!!
				string subalias = GenerateTableAlias(
					joinable.Name,
					associations.Count,
					path,
					joinable.IsManyToMany );

				assoc.Joinable = joinable;
				assoc.TableName = joinable.TableName;
				assoc.PrimaryKeyColumns = type.GetReferencedColumns( factory );
				assoc.ForeignKeyColumns = aliasedForeignKeyColumns;
				assoc.Subalias = subalias;
				assoc.Owner = GetPosition( alias, associations );
				assoc.IsOneToOne = type.IsEntityType &&
					( (EntityType) type ).IsOneToOne &&
					!( (EntityType) type ).IsUniqueKeyReference;
				assoc.JoinType = joinType;

				if ( assoc.ForeignKeyColumns.Length != assoc.PrimaryKeyColumns.Length ||
					assoc.ForeignKeyColumns.Length == 0 )
				{
					throw new MappingException( string.Format( "Invalid join columns for association: {0}", path ) );
				}

				int nextDepth = currentDepth + 1;
				if ( !joinable.IsCollection )
				{
					if ( joinable is IOuterJoinLoadable )
					{
						WalkClassTree( (IOuterJoinLoadable) joinable, subalias, associations, visitedPersisters, path, nextDepth, factory );
					}
				}
				else
				{
					if ( joinable is IQueryableCollection )
					{
						WalkCollectionTree( (IQueryableCollection) joinable, subalias, associations, visitedPersisters, path, nextDepth, factory ) ;
					}
				}
			}
		}
Exemplo n.º 39
0
		private bool IsManyToManyRoot(IJoinable joinable)
		{
			if (joinable != null && joinable.IsCollection)
			{
				IQueryableCollection persister = (IQueryableCollection) joinable;
				return persister.IsManyToMany;
			}
			return false;
		}
Exemplo n.º 40
0
		public override string SelectFragment(IJoinable rhs, string rhsAlias, string lhsAlias, string entitySuffix, string collectionSuffix, bool includeCollectionColumns)
		{
			var buf = new StringBuilder();

			if (includeCollectionColumns)
			{
				buf.Append(SelectFragment(lhsAlias, collectionSuffix)).Append(StringHelper.CommaSpace);
			}

			var ojl = (IOuterJoinLoadable)ElementPersister;
			return buf.Append(ojl.SelectFragment(lhsAlias, entitySuffix)).ToString(); //use suffix for the entity columns
		}
		public abstract string SelectFragment(IJoinable rhs, string rhsAlias, string lhsAlias, string currentEntitySuffix,
		                                      string currentCollectionSuffix, bool includeCollectionColumns);
		private string ManyToManySelectFragment(IJoinable rhs, string rhsAlias, string lhsAlias, string collectionSuffix)
		{
			SelectFragment frag = GenerateSelectFragment(lhsAlias, collectionSuffix);

			string[] _elementColumnNames = rhs.KeyColumnNames;
			frag.AddColumns(rhsAlias, _elementColumnNames, elementColumnAliases);
			AppendIndexColumns(frag, lhsAlias);
			AppendIdentifierColumns(frag, lhsAlias);

			return frag.ToSqlStringFragment(false);
		}
Exemplo n.º 43
0
		protected override string GenerateTableAlias(int n, string path, IJoinable joinable)
		{
			// TODO: deal with side-effects (changes to includeInSelectList, userAliasList, resultTypeList)!!!

			// for collection-of-entity, we are called twice for given "path"
			// once for the collection Joinable, once for the entity Joinable.
			// the second call will/must "consume" the alias + perform side effects according to consumesEntityAlias()
			// for collection-of-other, however, there is only one call 
			// it must "consume" the alias + perform side effects, despite what consumeEntityAlias() return says
			// 
			// note: the logic for adding to the userAliasList is still strictly based on consumesEntityAlias return value

			bool shouldCreateUserAlias = joinable.ConsumesEntityAlias();
			if (!shouldCreateUserAlias && joinable.IsCollection)
			{
				// is it a collection-of-other (component or value) ?
				var elementType = ((ICollectionPersister) joinable).ElementType;
				if (elementType != null)
					shouldCreateUserAlias = elementType.IsComponentType || !elementType.IsEntityType;
			}

			string sqlAlias = null;

			if (shouldCreateUserAlias)
			{
				ICriteria subcriteria = translator.GetCriteria(path);
				sqlAlias = subcriteria == null ? null : translator.GetSQLAlias(subcriteria);

				if (joinable.ConsumesEntityAlias() && !translator.HasProjection)
				{
					includeInResultRowList.Add(subcriteria != null && subcriteria.Alias != null);

					if (sqlAlias != null)
					{
						if (subcriteria.Alias != null)
						{
							userAliasList.Add(subcriteria.Alias); //alias may be null
							resultTypeList.Add(translator.ResultType(subcriteria));
						}
					}
				}
			}

			if (sqlAlias == null)
				sqlAlias = base.GenerateTableAlias(n + translator.SQLAliasCount, path, joinable);

			return sqlAlias;
		}
		protected override string GenerateTableAlias(int n, string path, IJoinable joinable)
		{
			// TODO: deal with side-effects (changes to includeInSelectList, userAliasList, resultTypeList)!!!
			bool shouldCreateUserAlias = joinable.ConsumesEntityAlias(); 
			if(shouldCreateUserAlias == false  && joinable.IsCollection)
			{
				var elementType = ((ICollectionPersister)joinable).ElementType;
				if (elementType != null)
					shouldCreateUserAlias = elementType.IsComponentType;
			}
			if (shouldCreateUserAlias)
			{
				ICriteria subcriteria = translator.GetCriteria(path);
				string sqlAlias = subcriteria == null ? null : translator.GetSQLAlias(subcriteria);
				if (sqlAlias != null)
				{
					if (!translator.HasProjection)
					{
						includeInResultRowList.Add(subcriteria.Alias != null);
						if (subcriteria.Alias!=null)
						{
							userAliasList.Add(subcriteria.Alias); //alias may be null
							resultTypeList.Add(translator.ResultType(subcriteria));
						}
					}
					return sqlAlias; //EARLY EXIT
				}
				else
				{
					if (!translator.HasProjection)
						includeInResultRowList.Add(false);
				}
			}
			return base.GenerateTableAlias(n + translator.SQLAliasCount, path, joinable);
		}
		public string SelectFragment(IJoinable rhs, string rhsAlias, string lhsAlias,
			string entitySuffix, string collectionSuffix, bool includeCollectionColumns)
		{
			return SelectFragment(lhsAlias, entitySuffix);
		}
		private static SqlString GetWhereJoinFragment(IJoinable persister, string tableAlias)
		{
			SqlString whereJoinFragment = persister.WhereJoinFragment(tableAlias, true, false);
			if (whereJoinFragment == null)
			{
				whereJoinFragment = SqlString.Empty;
			}
			else
			{
				whereJoinFragment = whereJoinFragment.Trim();
				if (whereJoinFragment.StartsWithCaseInsensitive("and "))
				{
					whereJoinFragment = whereJoinFragment.Substring(4);
				}
			}
			return whereJoinFragment;
		}
Exemplo n.º 47
0
		protected virtual string GenerateTableAlias(int n, string path, IJoinable joinable)
		{
			return StringHelper.GenerateAlias(joinable.Name, n);
		}
		protected override string GenerateTableAlias(int n, string path, IJoinable joinable)
		{
			bool shouldCreateUserAlias = joinable.ConsumesEntityAlias(); 
			if(shouldCreateUserAlias == false  && joinable.IsCollection)
			{
				var elementType = ((ICollectionPersister)joinable).ElementType;
				if (elementType != null)
					shouldCreateUserAlias = elementType.IsComponentType;
			}
			if (shouldCreateUserAlias)
			{
				ICriteria subcriteria = translator.GetCriteria(path);
				string sqlAlias = subcriteria == null ? null : translator.GetSQLAlias(subcriteria);
				if (sqlAlias != null)
				{
					userAliasList.Add(subcriteria.Alias); //alias may be null
					return sqlAlias; //EARLY EXIT
				}

				userAliasList.Add(null);
			}
			return base.GenerateTableAlias(n + translator.SQLAliasCount, path, joinable);
		}