Пример #1
0
        public IList List(ISessionImplementor session)
        {
            ArrayList values = new ArrayList();
            ArrayList types  = new ArrayList();

            foreach (CriteriaImpl.CriterionEntry ce in criteria.IterateExpressionEntries())
            {
                TypedValue[] tv = ce.Criterion.GetTypedValues(
                    session.Factory,
                    criteria.GetCriteriaClass(ce.Alias),
                    criteria.AliasClasses);

                for (int i = 0; i < tv.Length; i++)
                {
                    values.Add(tv[i].Value);
                    types.Add(tv[i].Type);
                }
            }
            object[] valueArray = values.ToArray();
            IType[]  typeArray  = ( IType[] )types.ToArray(typeof(IType));

            RowSelection selection = new RowSelection();

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

            QueryParameters qp = new QueryParameters(typeArray, valueArray, criteria.LockModes, selection);

            qp.Cacheable   = criteria.Cacheable;
            qp.CacheRegion = criteria.CacheRegion;

            return(List(session, qp, querySpaces, resultTypes));
        }
Пример #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();
        }
		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();
		}
        public SqlString GetWhereCondition(IDictionary <string, IFilter> enabledFilters)
        {
            SqlStringBuilder condition = new SqlStringBuilder(30);

            bool first = true;

            foreach (CriteriaImpl.CriterionEntry entry in rootCriteria.IterateExpressionEntries())
            {
                if (!HasGroupedOrAggregateProjection(entry.Criterion.GetProjections()))
                {
                    if (!first)
                    {
                        condition.Add(" and ");
                    }
                    first = false;
                    SqlString sqlString = entry.Criterion.ToSqlString(entry.Criteria, this, enabledFilters);
                    condition.Add(sqlString);
                }
            }
            return(condition.ToSqlString());
        }
Пример #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));
        }