Esempio n. 1
0
        public CriteriaLoader(IOuterJoinLoadable persister, ISessionFactoryImplementor factory, CriteriaImpl rootCriteria,
                              string rootEntityName, IDictionary <string, IFilter> enabledFilters)
            : base(factory, enabledFilters)
        {
            translator = new CriteriaQueryTranslator(factory, rootCriteria, rootEntityName, CriteriaQueryTranslator.RootSqlAlias);

            querySpaces = translator.GetQuerySpaces();

            CriteriaJoinWalker walker =
                new CriteriaJoinWalker(persister, translator, factory, rootCriteria, rootEntityName, enabledFilters);

            InitFromWalker(walker);

            _uncacheableCollectionPersisters = translator.UncacheableCollectionPersisters;
            userAliases               = walker.UserAliases;
            ResultTypes               = walker.ResultTypes;
            includeInResultRow        = walker.IncludeInResultRow;
            resultRowLength           = ArrayHelper.CountTrue(IncludeInResultRow);
            childFetchEntities        = walker.ChildFetchEntities;
            EntityFetchLazyProperties = walker.EntityFetchLazyProperties;
            // fill caching objects only if there is a projection
            if (translator.HasProjection)
            {
                cachedProjectedColumnAliases = translator.ProjectedColumnAliases;
            }

            PostInstantiate();
        }
Esempio n. 2
0
        public CriteriaJoinWalker(IOuterJoinLoadable persister, CriteriaQueryTranslator translator,
                                  ISessionFactoryImplementor factory, ICriteria criteria, string rootEntityName,
                                  IDictionary <string, IFilter> enabledFilters)
            : base(translator.RootSQLAlias, persister, factory, enabledFilters)
        {
            this.translator = translator;

            querySpaces = translator.GetQuerySpaces();

            if (translator.HasProjection)
            {
                resultTypes = translator.ProjectedTypes;

                InitProjection(
                    translator.GetSelect(enabledFilters),
                    translator.GetWhereCondition(enabledFilters),
                    translator.GetOrderBy(),
                    translator.GetGroupBy(),
                    translator.GetHavingCondition(enabledFilters),
                    enabledFilters,
                    LockMode.None);
            }
            else
            {
                resultTypes = new IType[] { TypeFactory.ManyToOne(persister.EntityName) };

                InitAll(translator.GetWhereCondition(enabledFilters), translator.GetOrderBy(), LockMode.None);
            }

            userAliasList.Add(criteria.Alias);             //root entity comes *last*
            userAliases = userAliasList.ToArray();
        }
		protected void CreateObjects(System.Type rootClass, ISession session)
		{
			criteria = (CriteriaImpl) session.CreateCriteria(rootClass);
			criteriaQuery = new CriteriaQueryTranslator(
				(ISessionFactoryImplementor) factory,
				criteria, criteria.EntityOrClassName, SqlAlias);
		}
		public CriteriaLoader(IOuterJoinLoadable persister, ISessionFactoryImplementor factory, CriteriaImpl rootCriteria,
							  string rootEntityName, IDictionary<string, IFilter> enabledFilters)
			: base(factory, enabledFilters)
		{
			translator = new CriteriaQueryTranslator(factory, rootCriteria, rootEntityName, CriteriaQueryTranslator.RootSqlAlias);

			querySpaces = translator.GetQuerySpaces();

			CriteriaJoinWalker walker =
				new CriteriaJoinWalker(persister, translator, factory, rootCriteria, rootEntityName, enabledFilters);

			InitFromWalker(walker);

			userAliases = walker.UserAliases;
			resultTypes = walker.ResultTypes;
			includeInResultRow = walker.IncludeInResultRow;
			resultRowLength = ArrayHelper.CountTrue(IncludeInResultRow);
			// fill caching objects only if there is a projection
			if (translator.HasProjection)
			{
				cachedProjectedColumnAliases = translator.ProjectedColumnAliases;
			}

			PostInstantiate();
		}
Esempio n. 5
0
        public static string GetSql(ICriteria criteria)
        {
            var criteriaImpl = (CriteriaImpl)criteria;
            var session = criteriaImpl.Session;
            var factory = session.Factory;

            var translator =
                new CriteriaQueryTranslator(
                    factory,
                    criteriaImpl,
                    criteriaImpl.EntityOrClassName,
                    CriteriaQueryTranslator.RootSqlAlias);

            var implementors = factory.GetImplementors(criteriaImpl.EntityOrClassName);

            var walker = new CriteriaJoinWalker(
                (Persister.Entity.IOuterJoinLoadable)factory.GetEntityPersister(implementors[0]),
                                    translator,
                                    factory,
                                    criteriaImpl,
                                    criteriaImpl.EntityOrClassName,
                                    session.EnabledFilters);

            return walker.SqlString.ToString();
        }
		public CriteriaLoader(
			IOuterJoinLoadable persister,
			ISessionFactoryImplementor factory,
			CriteriaImpl rootCriteria,
			System.Type rootEntityName, // TODO H3: change to string rootEntityName
			IDictionary enabledFilters)
			: base(factory, enabledFilters)
		{
			translator = new CriteriaQueryTranslator(
				factory,
				rootCriteria,
				rootEntityName,
				CriteriaQueryTranslator.RootSqlAlias);

			querySpaces = translator.GetQuerySpaces();

			CriteriaJoinWalker walker = new CriteriaJoinWalker(
				persister,
				translator,
				factory,
				rootCriteria,
				rootEntityName,
				enabledFilters);

			InitFromWalker(walker);

			userAliases = walker.UserAliases;
			resultTypes = walker.ResultTypes;

			PostInstantiate();
		}
Esempio n. 7
0
        public CriteriaJoinWalker(IOuterJoinLoadable persister,CriteriaQueryTranslator translator,
            ISessionFactoryImplementor factory, CriteriaImpl criteria, string rootEntityName,
            IDictionary<string, IFilter> enabledFilters)
            : base(translator.RootSQLAlias, persister, factory, enabledFilters)
        {
            this.translator = translator;

            querySpaces = translator.GetQuerySpaces();

            if (translator.HasProjection)
            {
                resultTypes = translator.ProjectedTypes;

                InitProjection(
                    translator.GetSelect(enabledFilters),
                    translator.GetWhereCondition(enabledFilters),
                    translator.GetOrderBy(),
                    translator.GetGroupBy().ToString(),
                    LockMode.None
                    );
            }
            else
            {
                resultTypes = new IType[] {TypeFactory.ManyToOne(persister.EntityName)};

                InitAll(translator.GetWhereCondition(enabledFilters), translator.GetOrderBy(), LockMode.None);
            }

            userAliasList.Add(criteria.Alias); //root entity comes *last*
            userAliases = ArrayHelper.ToStringArray(userAliasList);
        }
		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)
		{
			InitializeInnerQueryAndParameters(criteriaQuery);

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

			ISessionFactoryImplementor factory = criteriaQuery.Factory;

			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);
			}

			// during CriteriaImpl.Clone we are doing a shallow copy of each criterion.
			// this is not a problem for common criterion but not for SubqueryExpression because here we are holding the state of inner CriteriaTraslator (ICriteriaQuery).
			// After execution (ToSqlString) we have to clean the internal state because the next execution may be performed in a different tree reusing the same istance of SubqueryExpression.
			innerQuery = null;

			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();
		}
		protected void InitProjection(CriteriaQueryTranslator translator, 
			IDictionary<string, IFilter> enabledFilters, LockMode lockMode)
		{
			// the order of the calls here is important, as the join clauses can contain parameter bindings
			SqlString projectionString = translator.GetSelect(enabledFilters);
			WalkEntityTree(persister, Alias);
			SqlString whereString = translator.GetWhereCondition(enabledFilters);
			SqlString orderByString = translator.GetOrderBy();
			SqlString groupByString = translator.GetGroupBy();
			SqlString havingString = translator.GetHavingCondition(enabledFilters);
			
			Persisters = new ILoadable[0];
			InitStatementString(projectionString, whereString, orderByString, groupByString.ToString(), 
                havingString, lockMode);
		}
        private void CreateSubQuerySpaces()
        {
            var subQueries =
                rootCriteria.IterateExpressionEntries()
                .Select(x => x.Criterion)
                .OfType <SubqueryExpression>()
                .Select(x => x.Criteria)
                .OfType <CriteriaImpl>();

            foreach (var criteriaImpl in subQueries)
            {
                //The RootSqlAlias is not relevant, since we're only retreiving the query spaces
                var translator = new CriteriaQueryTranslator(sessionFactory, criteriaImpl, criteriaImpl.EntityOrClassName, RootSqlAlias);
                subQuerySpaces.UnionWith(translator.GetQuerySpaces());
            }
        }
Esempio n. 12
0
        public CriteriaLoader(IOuterJoinLoadable persister, ISessionFactoryImplementor factory, CriteriaImpl rootCriteria,
                              string rootEntityName, IDictionary <string, IFilter> enabledFilters)
            : base(factory, enabledFilters)
        {
            translator = new CriteriaQueryTranslator(factory, rootCriteria, rootEntityName, CriteriaQueryTranslator.RootSqlAlias);

            querySpaces = translator.GetQuerySpaces();

            CriteriaJoinWalker walker =
                new CriteriaJoinWalker(persister, translator, factory, rootCriteria, rootEntityName, enabledFilters);

            InitFromWalker(walker);

            userAliases = walker.UserAliases;
            resultTypes = walker.ResultTypes;

            PostInstantiate();
        }
        public void EqOrNullTest()
        {
            using (ISession session = OpenSession())
            {
                CriteriaImpl criteria = (CriteriaImpl) session.CreateCriteria(typeof (Simple));
                CriteriaQueryTranslator criteriaQuery =
                    new CriteriaQueryTranslator((ISessionFactoryImplementor) sessions, criteria,
                                                criteria.CriteriaClass.FullName, "sql_alias");

                ICriterion exp = Criterions.Criterion.EqOrNull("Name", "foo");
                SqlString sqlString =
                    exp.ToSqlString(criteria, criteriaQuery, new CollectionHelper.EmptyMapClass<string, IFilter>());

                string expectedSql = "sql_alias.Name = ?";

                Assert.AreEqual(expectedSql, sqlString.ToString());
                Assert.AreEqual(1, sqlString.GetParameterCount());

                exp = Criterions.Criterion.EqOrNull("Name", null);
                sqlString =
                    exp.ToSqlString(criteria, criteriaQuery, new CollectionHelper.EmptyMapClass<string, IFilter>());

                expectedSql = "sql_alias.Name is null";

                Assert.AreEqual(expectedSql, sqlString.ToString());
                Assert.AreEqual(0, sqlString.GetParameterCount());

                // Check that the result is the same than using official Expression
                ICriterion orExpExpected = Expression.Or(Expression.IsNull("Name"), Expression.Eq("Name", "foo"));
                ICriterion orExpActual =
                    Expression.Or(Criterions.Criterion.EqOrNull("Name", null),
                                  Criterions.Criterion.EqOrNull("Name", "foo"));

                SqlString sqlStringExpected =
                    orExpExpected.ToSqlString(criteria, criteriaQuery,
                                              new CollectionHelper.EmptyMapClass<string, IFilter>());
                SqlString sqlStringActual =
                    orExpActual.ToSqlString(criteria, criteriaQuery,
                                            new CollectionHelper.EmptyMapClass<string, IFilter>());
                Assert.AreEqual(sqlStringExpected.ToString(), sqlStringActual.ToString());
            }
        }
Esempio n. 14
0
        public CriteriaJoinWalker(IOuterJoinLoadable persister, CriteriaQueryTranslator translator,
                                  ISessionFactoryImplementor factory, ICriteria criteria, string rootEntityName,
                                  IDictionary <string, IFilter> enabledFilters)
            : base(translator.RootSQLAlias, persister, factory, enabledFilters)
        {
            this.translator = translator;

            querySpaces = translator.GetQuerySpaces();

            if (translator.HasProjection)
            {
                InitProjection(
                    translator.GetSelect(),
                    translator.GetWhereCondition(),
                    translator.GetOrderBy(),
                    translator.GetGroupBy(),
                    translator.GetHavingCondition(),
                    enabledFilters,
                    LockMode.None,
                    translator.GetEntityProjections());

                resultTypes        = translator.ProjectedTypes;
                userAliases        = translator.ProjectedAliases;
                includeInResultRow = new bool[resultTypes.Length];
                ArrayHelper.Fill(IncludeInResultRow, true);
            }
            else
            {
                InitAll(translator.GetWhereCondition(), translator.GetOrderBy(), LockMode.None);

                resultTypes = new IType[] { TypeFactory.ManyToOne(persister.EntityName) };

                // root entity comes last
                userAliasList.Add(criteria.Alias);                 //root entity comes *last*
                resultTypeList.Add(translator.ResultType(criteria));
                includeInResultRowList.Add(true);
                userAliases        = userAliasList.ToArray();
                resultTypes        = resultTypeList.ToArray();
                includeInResultRow = includeInResultRowList.ToArray();
            }
        }
		public CriteriaJoinWalker(IOuterJoinLoadable persister, CriteriaQueryTranslator translator,
		                          ISessionFactoryImplementor factory, ICriteria criteria, string rootEntityName,
		                          IDictionary<string, IFilter> enabledFilters)
			: base(translator.RootSQLAlias, persister, factory, enabledFilters)
		{
			this.translator = translator;

			querySpaces = translator.GetQuerySpaces();

			if (translator.HasProjection)
			{
				InitProjection(
					translator.GetSelect(enabledFilters),
					translator.GetWhereCondition(enabledFilters),
					translator.GetOrderBy(),
					translator.GetGroupBy(),
					translator.GetHavingCondition(enabledFilters),
					enabledFilters, 
					LockMode.None);

				resultTypes = translator.ProjectedTypes;
				userAliases = translator.ProjectedAliases;
				includeInResultRow = new bool[resultTypes.Length];
				ArrayHelper.Fill(IncludeInResultRow, true);
			}
			else
			{
				InitAll(translator.GetWhereCondition(enabledFilters), translator.GetOrderBy(), LockMode.None);

				resultTypes = new IType[] { TypeFactory.ManyToOne(persister.EntityName) };

				// root entity comes last
				userAliasList.Add(criteria.Alias); //root entity comes *last*
				resultTypeList.Add(translator.ResultType(criteria));
				includeInResultRowList.Add(true);
				userAliases = userAliasList.ToArray();
				resultTypes = resultTypeList.ToArray();
				includeInResultRow = includeInResultRowList.ToArray();
			}
		}
		private void CreateSubQuerySpaces()
		{

			var subQueries =
				rootCriteria.IterateExpressionEntries()
				            .Select(x => x.Criterion)
				            .OfType<SubqueryExpression>()
				            .Select(x => x.Criteria)
				            .OfType<CriteriaImpl>();

			foreach (var criteriaImpl in subQueries)
			{
				//The RootSqlAlias is not relevant, since we're only retreiving the query spaces
				var translator = new CriteriaQueryTranslator(sessionFactory, criteriaImpl, criteriaImpl.EntityOrClassName, RootSqlAlias);
				subQuerySpaces.UnionWith(translator.GetQuerySpaces());
			}

		}	
		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();
		}