예제 #1
0
        /// <summary>
        /// Get the order by string required for collection fetching
        /// </summary>
        protected SqlString OrderBy(IList <OuterJoinableAssociation> associations)
        {
            SqlStringBuilder buf = new SqlStringBuilder();

            OuterJoinableAssociation last = null;

            foreach (OuterJoinableAssociation oj in associations)
            {
                if (oj.JoinType == JoinType.LeftOuterJoin)
                {
                    if (oj.Joinable.IsCollection)
                    {
                        IQueryableCollection queryableCollection = (IQueryableCollection)oj.Joinable;
                        if (queryableCollection.HasOrdering)
                        {
                            string orderByString = queryableCollection.GetSQLOrderByString(oj.RHSAlias);
                            buf.Add(orderByString).Add(StringHelper.CommaSpace);
                        }
                    }
                    else
                    {
                        // it might still need to apply a collection ordering based on a
                        // many-to-many defined order-by...
                        if (last != null && last.Joinable.IsCollection)
                        {
                            IQueryableCollection queryableCollection = (IQueryableCollection)last.Joinable;
                            if (queryableCollection.IsManyToMany && last.IsManyToManyWith(oj))
                            {
                                if (queryableCollection.HasManyToManyOrdering)
                                {
                                    string orderByString = queryableCollection.GetManyToManyOrderByString(oj.RHSAlias);
                                    buf.Add(orderByString).Add(StringHelper.CommaSpace);
                                }
                            }
                        }
                    }
                }
                last = oj;
            }

            if (buf.Count > 0)
            {
                buf.RemoveAt(buf.Count - 1);
            }

            return(buf.ToSqlString());
        }
        private void InitStatementString(IOuterJoinLoadable elementPersister, string alias, int batchSize, SqlString subquery)
        {
            int joins = CountEntityPersisters(associations);

            Suffixes = BasicLoader.GenerateSuffixes(joins + 1);

            int collectionJoins = CountCollectionPersisters(associations) + 1;

            CollectionSuffixes = BasicLoader.GenerateSuffixes(joins + 1, collectionJoins);

            SqlStringBuilder whereString = WhereString(oneToManyPersister.GenerateTableAliasForKeyColumns(alias), oneToManyPersister.KeyColumnNames, subquery, batchSize);
            string           filter      = oneToManyPersister.FilterFragment(alias, EnabledFilters);

            whereString.Insert(0, StringHelper.MoveAndToBeginning(filter));

            JoinFragment     ojf    = MergeOuterJoins(associations);
            SqlSelectBuilder select =
                new SqlSelectBuilder(Factory)
                .SetSelectClause(
#pragma warning disable 618
                    oneToManyPersister.SelectFragment(null, null, alias, Suffixes[joins], CollectionSuffixes[0], true) +
#pragma warning restore 618
                    SelectString(associations))
                .SetFromClause(
                    elementPersister.FromTableFragment(alias) + oneToManyPersister.FromJoinFragment(alias, true, true))
                .SetWhereClause(whereString.ToSqlString())
                .SetOuterJoins(ojf.ToFromFragmentString,
                               ojf.ToWhereFragmentString + elementPersister.WhereJoinFragment(alias, true, true));

            select.SetOrderByClause(OrderBy(associations, oneToManyPersister.GetSQLOrderByString(alias)));

            if (Factory.Settings.IsCommentsEnabled)
            {
                select.SetComment("load one-to-many " + oneToManyPersister.Role);
            }

            SqlString = select.ToSqlString();
        }
		private void InitStatementString( IQueryableCollection persister, string alias, IList associations, int batchSize, ISessionFactoryImplementor factory )
		{
			Suffixes = GenerateSuffixes( associations.Count );

			SqlStringBuilder whereString = WhereString( factory, alias, persister.KeyColumnNames, persister.KeyType, batchSize );
			if( persister.HasWhere )
			{
				whereString
					.Add( " and " )
					.Add( persister.GetSQLWhereString( alias ) );
			}

			JoinFragment ojf = MergeOuterJoins( associations );
			SqlSelectBuilder select = new SqlSelectBuilder( factory )
				.SetSelectClause(
				persister.SelectFragment( alias ).Append(
					SelectString( associations, factory ) ).ToString()
				)
				.SetFromClause( persister.TableName, alias )
				.SetWhereClause( whereString.ToSqlString() )
				.SetOuterJoins(
				ojf.ToFromFragmentString,
				ojf.ToWhereFragmentString
				);

			if( persister.HasOrdering )
			{
				select.SetOrderByClause( persister.GetSQLOrderByString( alias ) );
			}
			SqlString = select.ToSqlString();
		}