コード例 #1
0
        public CriteriaLoader(IOuterJoinLoadable persister, ISessionFactoryImplementor factory, CriteriaImpl criteria)
            : base(persister, factory)
        {
            this.criteria = criteria;

            AddAllToPropertySpaces(persister.PropertySpaces);

            resultTypes    = new IType[1];
            resultTypes[0] = NHibernateUtil.Entity(persister.MappedClass);

            SqlStringBuilder condition = new SqlStringBuilder(10);

            bool foundCriterion = false;

            foreach (CriteriaImpl.CriterionEntry ee in criteria.IterateExpressionEntries())
            {
                if (foundCriterion)
                {
                    condition.Add(" and ");
                }

                SqlString sqlString = ee.Criterion.ToSqlString(
                    factory,
                    criteria.GetPersistentClass(ee.Alias),
                    ee.Alias,
                    criteria.AliasClasses);
                condition.Add(sqlString);

                foundCriterion = true;
            }

            if (!foundCriterion)
            {
                condition.Add("1=1");                   // TODO: fix this ugliness
            }

            StringBuilder orderBy    = new StringBuilder(30);
            bool          foundOrder = false;

            foreach (Order ord in criteria.IterateOrderings())
            {
                if (foundOrder)
                {
                    orderBy.Append(StringHelper.CommaSpace);
                }
                orderBy.Append(ord.ToSqlString(factory, criteria.CriteriaClass, Alias));
                foundOrder = true;
            }

            IList associations = WalkTree(persister, Alias, factory);

            InitClassPersisters(associations);
            InitStatementString(associations, condition.ToSqlString(), orderBy.ToString(), factory);

            PostInstantiate();
        }
コード例 #2
0
		public CriteriaLoader( IOuterJoinLoadable persister, ISessionFactoryImplementor factory, CriteriaImpl criteria )
			: base( persister, factory )
		{
			this.criteria = criteria;

			AddAllToPropertySpaces( persister.PropertySpaces );

			resultTypes = new IType[ 1 ];
			resultTypes[ 0 ] = NHibernateUtil.Entity( persister.MappedClass );

			SqlStringBuilder condition = new SqlStringBuilder( 10 );

			bool foundCriterion = false;

			foreach( CriteriaImpl.CriterionEntry ee in criteria.IterateExpressionEntries() )
			{
				if( foundCriterion )
				{
					condition.Add( " and " );
				}

				SqlString sqlString = ee.Criterion.ToSqlString(
					factory,
					criteria.GetPersistentClass( ee.Alias ),
					ee.Alias,
					criteria.AliasClasses );
				condition.Add( sqlString );

				foundCriterion = true;
			}

			if( !foundCriterion )
			{
				condition.Add( "1=1" ); // TODO: fix this ugliness
			}

			StringBuilder orderBy = new StringBuilder( 30 );
			bool foundOrder = false;

			foreach( Order ord in criteria.IterateOrderings() )
			{
				if( foundOrder )
				{
					orderBy.Append( StringHelper.CommaSpace );
				}
				orderBy.Append( ord.ToSqlString( factory, criteria.CriteriaClass, Alias ) );
				foundOrder = true;
			}

			IList associations = WalkTree( persister, Alias, factory );
			InitClassPersisters( associations );
			InitStatementString( associations, condition.ToSqlString(), orderBy.ToString(), factory );

			PostInstantiate();
		}
コード例 #3
0
        public SqlString GetOrderBy()
        {
            SqlStringBuilder orderBy = new SqlStringBuilder(30);

            bool first = true;

            foreach (CriteriaImpl.OrderEntry oe in rootCriteria.IterateOrderings())
            {
                if (!first)
                {
                    orderBy.Add(StringHelper.CommaSpace);
                }
                first = false;
                orderBy.Add(oe.Order.ToSqlString(oe.Criteria, this));
            }
            return(orderBy.ToSqlString());
        }
コード例 #4
0
ファイル: CriteriaTransformer.cs プロジェクト: zibler/zibler
        private static void CloneSubcriteriaAndOrders(CriteriaImpl clone, CriteriaImpl original)
        {
            //we need to preserve the parent criteria, we rely on the orderring when creating the
            //subcriterias initially here, so we don't need to make more than a single pass
            Hashtable newParents = new Hashtable();
            newParents[original] = clone;

            foreach (CriteriaImpl.Subcriteria subcriteria in original.IterateSubcriteria())
            {
                ICriteria currentParent = (ICriteria)newParents[subcriteria.Parent];
                if (currentParent == null)
                {
                    throw new AssertionFailure(
                        "Could not find parent for subcriteria in the previous subcriteria. If you see this error, it is a bug");
                }
                CriteriaImpl.Subcriteria clonedSubCriteria =
                    new CriteriaImpl.Subcriteria(clone, currentParent, subcriteria.Path, subcriteria.Alias, subcriteria.JoinType);
                clonedSubCriteria.SetLockMode(subcriteria.LockMode);
                newParents[subcriteria] = clonedSubCriteria;
            }

            // remap the orders
            foreach (CriteriaImpl.OrderEntry orderEntry in original.IterateOrderings())
            {
                ICriteria currentParent = (ICriteria)newParents[orderEntry.Criteria];
                if (currentParent == null)
                {
                    throw new AssertionFailure(
                        "Could not find parent for order in the previous criteria. If you see this error, it is a bug");
                }
                currentParent.AddOrder(orderEntry.Order);
            }

            // remap the restrictions to appropriate criterias
            foreach (CriteriaImpl.CriterionEntry criterionEntry in original.Restrictions)
            {
                ICriteria currentParent = (ICriteria) newParents[criterionEntry.Criteria];
                if (currentParent == null)
                {
                    throw new AssertionFailure(
                        "Could not find parent for restriction in the previous criteria. If you see this error, it is a bug.");
                }

                currentParent.Add(criterionEntry.Criterion);
            }
        }
コード例 #5
0
        private static ICriteria ToShardedCriteria(CriteriaImpl other, IShardedSessionImplementor shardedSession)
        {
            Preconditions.CheckNotNull(other);
            Preconditions.CheckNotNull(shardedSession);

            var entityName = other.EntityOrClassName;
            var alias      = other.Alias;

            ICriteria CriteriaFactory(ISession s) =>
            alias != null
                                        ? s.CreateCriteria(entityName, alias)
                                        : s.CreateCriteria(entityName);

            var result = new ShardedCriteriaImpl(shardedSession, other.EntityOrClassName, CriteriaFactory);

            foreach (var entry in other.IterateSubcriteria())
            {
                result.CreateCriteria(entry.Path, entry.Alias, entry.JoinType, entry.WithClause);
            }
            foreach (var entry in other.IterateExpressionEntries())
            {
                result.Add(entry.Criterion);
            }
            foreach (var entry in other.LockModes)
            {
                result.SetLockMode(entry.Key, entry.Value);
            }
            foreach (var entry in other.IterateOrderings())
            {
                result.AddOrder(entry.Order);
            }

            if (other.Cacheable)
            {
                result.SetCacheable(true);
                if (other.CacheMode != null)
                {
                    result.SetCacheMode(other.CacheMode.Value);
                }
                if (other.CacheRegion != null)
                {
                    result.SetCacheRegion(other.CacheRegion);
                }
            }

            if (other.Comment != null)
            {
                result.SetComment(other.Comment);
            }
            if (other.FetchSize > 0)
            {
                result.SetFetchSize(other.FetchSize);
            }
            if (other.FirstResult > 0)
            {
                result.SetFirstResult(other.FirstResult);
            }
            if (other.MaxResults > 0)
            {
                result.SetMaxResults(other.MaxResults);
            }
            if (other.Projection != null)
            {
                result.SetProjection(other.Projection);
            }
            if (other.ResultTransformer != null)
            {
                result.SetResultTransformer(other.ResultTransformer);
            }
            if (other.Timeout > 0)
            {
                result.SetTimeout(other.Timeout);
            }
            if (other.IsReadOnlyInitialized)
            {
                result.SetReadOnly(other.IsReadOnly);
            }

            return(result);
        }
コード例 #6
0
        public QueryParameters GetQueryParameters()
        {
            RowSelection selection = new RowSelection();

            selection.FirstRow  = rootCriteria.FirstResult;
            selection.MaxRows   = rootCriteria.MaxResults;
            selection.Timeout   = rootCriteria.Timeout;
            selection.FetchSize = rootCriteria.FetchSize;

            Dictionary <string, LockMode> lockModes = new Dictionary <string, LockMode>();

            foreach (KeyValuePair <string, LockMode> me in rootCriteria.LockModes)
            {
                ICriteria subcriteria = GetAliasedCriteria(me.Key);
                lockModes[GetSQLAlias(subcriteria)] = me.Value;
            }

            List <TypedValue> typedValues = new List <TypedValue>();

            // NH-specific: Get parameters for projections first
            if (this.HasProjection)
            {
                typedValues.AddRange(rootCriteria.Projection.GetTypedValues(rootCriteria, this));
            }

            foreach (CriteriaImpl.Subcriteria subcriteria in rootCriteria.IterateSubcriteria())
            {
                LockMode lm = subcriteria.LockMode;
                if (lm != null)
                {
                    lockModes[GetSQLAlias(subcriteria)] = lm;
                }
                // Get parameters that may be used in JOINs
                if (subcriteria.WithClause != null)
                {
                    typedValues.AddRange(subcriteria.WithClause.GetTypedValues(subcriteria, this));
                }
            }

            List <TypedValue> groupedTypedValues = new List <TypedValue>();

            // Type and value gathering for the WHERE clause needs to come AFTER lock mode gathering,
            // because the lock mode gathering loop now contains join clauses which can contain
            // parameter bindings (as in the HQL WITH clause).
            foreach (CriteriaImpl.CriterionEntry ce in rootCriteria.IterateExpressionEntries())
            {
                bool          criteriaContainsGroupedProjections = false;
                IProjection[] projections = ce.Criterion.GetProjections();

                if (projections != null)
                {
                    foreach (IProjection projection in projections)
                    {
                        if (projection.IsGrouped)
                        {
                            criteriaContainsGroupedProjections = true;
                            break;
                        }
                    }
                }

                if (criteriaContainsGroupedProjections)
                {
                    // GROUP BY/HAVING parameters need to be added after WHERE parameters - so don't add them
                    // to typedValues yet
                    groupedTypedValues.AddRange(ce.Criterion.GetTypedValues(ce.Criteria, this));
                }
                else
                {
                    typedValues.AddRange(ce.Criterion.GetTypedValues(ce.Criteria, this));
                }
            }

            // NH-specific: GROUP BY/HAVING parameters need to appear after WHERE parameters
            if (groupedTypedValues.Count > 0)
            {
                typedValues.AddRange(groupedTypedValues);
            }

            // NH-specific: To support expressions/projections used in ORDER BY
            foreach (CriteriaImpl.OrderEntry oe in rootCriteria.IterateOrderings())
            {
                typedValues.AddRange(oe.Order.GetTypedValues(oe.Criteria, this));
            }

            return
                (new QueryParameters(
                     typedValues.Select(tv => tv.Type).ToArray(),
                     typedValues.Select(tv => tv.Value).ToArray(),
                     lockModes,
                     selection,
                     rootCriteria.IsReadOnlyInitialized,
                     rootCriteria.IsReadOnlyInitialized ? rootCriteria.IsReadOnly : false,
                     rootCriteria.Cacheable,
                     rootCriteria.CacheRegion,
                     rootCriteria.Comment,
                     rootCriteria.LookupByNaturalKey,
                     rootCriteria.ResultTransformer,
                     _tempPagingParameterIndexes));
        }