예제 #1
0
        public void InitializeInnerQueryAndParameters(ICriteriaQuery criteriaQuery)
        {
            if (innerQuery == null)
            {
                ISessionFactoryImplementor factory = criteriaQuery.Factory;

                innerQuery =
                    new CriteriaQueryTranslator(
                        factory,
                        criteriaImpl,                         //implicit polymorphism not supported (would need a union)
                        criteriaImpl.EntityOrClassName,
                        criteriaQuery.GenerateSQLAlias(),
                        criteriaQuery);

                if (innerQuery.HasProjection)
                {
                    parameters = innerQuery.GetQueryParameters();
                    types      = innerQuery.ProjectedTypes;
                }
                else
                {
                    types = null;
                }
            }
        }
예제 #2
0
        public override SqlString ToSqlString(ICriteria criteria, ICriteriaQuery criteriaQuery, IDictionary <string, IFilter> enabledFilters)
        {
            ISessionFactoryImplementor factory = criteriaQuery.Factory;

            var innerQuery = new CriteriaQueryTranslator(
                factory,
                criteriaImpl,                 //implicit polymorphism not supported (would need a union)
                criteriaImpl.EntityOrClassName,
                criteriaQuery.GenerateSQLAlias(),
                criteriaQuery);

            types = innerQuery.HasProjection ? innerQuery.ProjectedTypes : null;

            if (innerQuery.HasProjection == false)
            {
                throw new QueryException("Cannot use subqueries on a criteria without a projection.");
            }

            IOuterJoinLoadable persister = (IOuterJoinLoadable)factory.GetEntityPersister(criteriaImpl.EntityOrClassName);

            //patch to generate joins on subqueries
            //stolen from CriteriaLoader
            CriteriaJoinWalker walker =
                new CriteriaJoinWalker(persister, innerQuery, factory, criteriaImpl, criteriaImpl.EntityOrClassName, enabledFilters);

            parameters = innerQuery.GetQueryParameters();             // parameters can be inferred only after initialize the walker

            SqlString sql = walker.SqlString;

            if (criteriaImpl.FirstResult != 0 || criteriaImpl.MaxResults != RowSelection.NoValue)
            {
                int?      offset          = Loader.Loader.GetOffsetUsingDialect(parameters.RowSelection, factory.Dialect);
                int?      limit           = Loader.Loader.GetLimitUsingDialect(parameters.RowSelection, factory.Dialect);
                Parameter offsetParameter = offset.HasValue ? innerQuery.CreateSkipParameter(offset.Value) : null;
                Parameter limitParameter  = limit.HasValue ? innerQuery.CreateTakeParameter(limit.Value) : null;
                sql = factory.Dialect.GetLimitString(sql, offset, limit, offsetParameter, limitParameter);
            }

            SqlStringBuilder buf = new SqlStringBuilder().Add(ToLeftSqlString(criteria, criteriaQuery));

            if (op != null)
            {
                buf.Add(" ").Add(op).Add(" ");
            }

            if (quantifier != null && prefixOp)
            {
                buf.Add(quantifier).Add(" ");
            }

            buf.Add("(").Add(sql).Add(")");

            if (quantifier != null && prefixOp == false)
            {
                buf.Add(" ").Add(quantifier);
            }

            return(buf.ToSqlString());
        }
		public override SqlString ToSqlString(ICriteria criteria, ICriteriaQuery criteriaQuery, IDictionary<string, IFilter> enabledFilters)
		{
			ISessionFactoryImplementor factory = criteriaQuery.Factory;

			var innerQuery = new CriteriaQueryTranslator(
				factory,
				criteriaImpl, //implicit polymorphism not supported (would need a union)
				criteriaImpl.EntityOrClassName,
				criteriaQuery.GenerateSQLAlias(),
				criteriaQuery);

			types = innerQuery.HasProjection ? innerQuery.ProjectedTypes : null;

			if (innerQuery.HasProjection == false)
			{
				throw new QueryException("Cannot use subqueries on a criteria without a projection.");
			}

			IOuterJoinLoadable persister = (IOuterJoinLoadable) factory.GetEntityPersister(criteriaImpl.EntityOrClassName);

			//patch to generate joins on subqueries
			//stolen from CriteriaLoader
			CriteriaJoinWalker walker =
				new CriteriaJoinWalker(persister, innerQuery, factory, criteriaImpl, criteriaImpl.EntityOrClassName, enabledFilters);

			parameters = innerQuery.GetQueryParameters(); // parameters can be inferred only after initialize the walker

			SqlString sql = walker.SqlString;

			if (criteriaImpl.FirstResult != 0 || criteriaImpl.MaxResults != RowSelection.NoValue)
			{
				int? offset = Loader.Loader.GetOffsetUsingDialect(parameters.RowSelection, factory.Dialect);
				int? limit = Loader.Loader.GetLimitUsingDialect(parameters.RowSelection, factory.Dialect);
				Parameter offsetParameter = offset.HasValue ? innerQuery.CreateSkipParameter(offset.Value) : null;
				Parameter limitParameter = limit.HasValue ? innerQuery.CreateTakeParameter(limit.Value) : null;
				sql = factory.Dialect.GetLimitString(sql, offset, limit, offsetParameter, limitParameter);
			}

			SqlStringBuilder buf = new SqlStringBuilder().Add(ToLeftSqlString(criteria, criteriaQuery));
			if (op != null)
			{
				buf.Add(" ").Add(op).Add(" ");
			}

			if (quantifier != null && prefixOp)
			{
				buf.Add(quantifier).Add(" ");
			}

			buf.Add("(").Add(sql).Add(")");

			if (quantifier != null && prefixOp == false)
			{
				buf.Add(" ").Add(quantifier);
			}

			return buf.ToSqlString();
		}
		public void InitializeInnerQueryAndParameters(ICriteriaQuery criteriaQuery)
		{
			if (innerQuery == null)
			{
				ISessionFactoryImplementor factory = criteriaQuery.Factory;

				innerQuery = new CriteriaQueryTranslator(
					factory,
					criteriaImpl, //implicit polymorphism not supported (would need a union)
					criteriaImpl.EntityOrClassName,
					criteriaQuery.GenerateSQLAlias(),
					criteriaQuery);

				types = innerQuery.HasProjection ? innerQuery.ProjectedTypes : null;
			}
		}
		// NH: This feels like a hack but I don't understand the code enough yet to code a better solution
		private void InitializeInnerQueryAndParameters(ICriteriaQuery criteriaQuery)
		{
			if (innerQuery != null)
			{
				// Already initialized
				return;
			}

			ISessionFactoryImplementor factory = criteriaQuery.Factory;
			innerQuery = new CriteriaQueryTranslator(factory,
			                                         this.criteriaImpl,
			                                         //implicit polymorphism not supported (would need a union) 
			                                         this.criteriaImpl.CriteriaClass,
			                                         criteriaQuery.GenerateSQLAlias(),
			                                         criteriaQuery);

			parameters = innerQuery.GetQueryParameters();
		}