protected void CreateObjects(System.Type rootClass, ISession session) { criteria = (CriteriaImpl)session.CreateCriteria(rootClass); criteriaQuery = new CriteriaQueryTranslator( (ISessionFactoryImplementor)factory, criteria, criteria.EntityOrClassName, SqlAlias); }
protected SubqueryExpression(String op, String quantifier, DetachedCriteria dc, bool prefixOp) { criteriaImpl = dc.GetCriteriaImpl(); this.quantifier = quantifier; this.prefixOp = prefixOp; this.op = op; }
/// <summary> /// Creates an exact clone of the criteria /// </summary> /// <returns></returns> public static ICriteria Clone(ICriteria criteria) { CriteriaImpl root = GetRootCriteria(criteria); CriteriaImpl clone = new CriteriaImpl(root.CriteriaClass, root.Alias, root.Session); CloneSubcriteriaAndOrders(clone, root); foreach (DictionaryEntry de in root.FetchModes) { clone.SetFetchMode((string) de.Key, (FetchMode) de.Value); } foreach (DictionaryEntry de in root.LockModes) { clone.SetLockMode((string) de.Key, (LockMode) de.Value); } clone.SetMaxResults(root.MaxResults); clone.SetFirstResult(root.FirstResult); clone.SetTimeout(root.Timeout); clone.SetFetchSize(root.FetchSize); clone.SetCacheable(root.Cacheable); if (string.IsNullOrEmpty(root.CacheRegion) == false) clone.SetCacheRegion(root.CacheRegion); clone.SetProjection(root.Projection); CloneProjectCrtieria(clone, root); clone.SetResultTransformer(root.ResultTransformer); return clone; }
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(); }
public static Type GetRootType(ICriteria criteria) { Type rootType = criteria.GetRootEntityTypeIfAvailable(); if (rootType != null) { return(rootType); } CriteriaImpl impl = GetRootCriteria(criteria); if (impl.Session == null) { throw new InvalidOperationException("Could not get root type on criteria that is not attached to a session"); } ISessionFactoryImplementor factory = impl.Session.Factory; IEntityPersister persister = factory.GetEntityPersister(impl.EntityOrClassName); if (persister == null) { throw new InvalidOperationException("Could not find entity named: " + impl.EntityOrClassName); } return(persister.GetMappedClass(EntityMode.Poco)); }
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(); }
protected void CreateObjects(System.Type rootClass, ISession session) { criteria = (CriteriaImpl) session.CreateCriteria(rootClass); criteriaQuery = new CriteriaQueryTranslator( (ISessionFactoryImplementor) factory, criteria, criteria.EntityOrClassName, SqlAlias); }
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 CriteriaQueryTranslator(ISessionFactoryImplementor factory, CriteriaImpl criteria, string rootEntityName, string rootSQLAlias, ICriteriaQuery outerQuery) : this(factory, criteria, rootEntityName, rootSQLAlias) { outerQueryTranslator = outerQuery; collectedParameterSpecifications = outerQuery.CollectedParameterSpecifications; namedParameters = outerQuery.CollectedParameters; }
private static System.Type GetCriteriaClass(ICriteria criteria) { CriteriaImpl impl = criteria as CriteriaImpl; return(impl != null? impl.GetRootEntityTypeIfAvailable() : GetCriteriaClass(((CriteriaImpl.Subcriteria)criteria).Parent)); }
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 void SetCacheRegionOnInnerCriteria() { CriteriaImpl criteria = null; query.QueryOptions .RegisterCustomAction(x => criteria = x as CriteriaImpl) .SetCacheRegion("someRegion"); query.ToList(); Assert.AreEqual("someRegion", criteria.CacheRegion); }
public CriteriaQueryTranslator( ISessionFactoryImplementor factory, CriteriaImpl criteria, string rootEntityName, string rootSQLAlias, ICriteriaQuery outerQuery) : this(factory, criteria, rootEntityName, rootSQLAlias) { outerQueryTranslator = outerQuery; }
private static CriteriaImpl GetRootCriteria(ICriteria criteria) { CriteriaImpl impl = criteria as CriteriaImpl; if (impl != null) { return(impl); } return(GetRootCriteria(((CriteriaImpl.Subcriteria)criteria).Parent)); }
public static IProjection GetProjection(this ICriteria criteria) { CriteriaImpl impl = criteria as CriteriaImpl; if (impl != null) { return(impl.Projection); } return(null); }
public void SetCacheableActsOnInnerCriteria() { CriteriaImpl criteria = null; query.QueryOptions .RegisterCustomAction(x => criteria = x as CriteriaImpl) .SetCachable(true); query.ToList(); Assert.AreEqual(criteria.Cacheable, true); }
public void SetCommentActsOnInnerCriteria() { CriteriaImpl criteria = null; query.QueryOptions .RegisterCustomAction(x => criteria = x as CriteriaImpl) .SetComment("something"); query.ToList(); Assert.AreEqual("something", criteria.Comment); }
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(); }
protected DetachedCriteria ToDetachedCriteria <T>(QueryOver <T> actual) { ICriteria criteria = actual.UnderlyingCriteria; CriteriaImpl criteriaImpl = (CriteriaImpl) typeof(QueryOver <T>).GetField("impl", BindingFlags.NonPublic | BindingFlags.Instance) .GetValue(actual); return((DetachedCriteria) typeof(DetachedCriteria).GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance, null, new System.Type[] { typeof(CriteriaImpl), typeof(ICriteria) }, null) .Invoke(new object[] { criteriaImpl, criteria })); }
private ICriteria _crearCriteria <S>() where S : T { var entry = this.Session.PersistenceContext.GetCollectionEntry(this); var persister = entry.LoadedPersister as OneToManyPersister; var criteria = new CriteriaImpl(typeof(S), Session); foreach (string columnName in persister.KeyColumnNames) { criteria.Add(NHibernate.Criterion.Expression.Sql(string.Format("this_.{0} = ?", columnName), Key, NHibernateUtil.Guid)); } return(criteria); }
public void SetCacheModeActsOnInnerCriteria() { CriteriaImpl criteria = null; query.QueryOptions .RegisterCustomAction(x => criteria = x as CriteriaImpl) .SetCacheMode(CacheMode.Ignore); query.ToList(); object value = criteria.GetType().GetField("cacheMode", BindingFlags.Instance | BindingFlags.NonPublic) .GetValue(criteria); Assert.AreEqual(value, CacheMode.Ignore); }
public CriteriaQueryTranslator(ISessionFactoryImplementor factory, CriteriaImpl criteria, string rootEntityName, string rootSQLAlias) { rootCriteria = criteria; this.rootEntityName = rootEntityName; sessionFactory = factory; this.rootSQLAlias = rootSQLAlias; helper = new SessionFactoryHelper(factory); CreateAliasCriteriaMap(); CreateAssociationPathCriteriaMap(); CreateCriteriaEntityNameMap(); CreateCriteriaCollectionPersisters(); CreateCriteriaSQLAliasMap(); }
private static void CloneProjectCrtieria(CriteriaImpl clone, ICriteria original) { if (original.ProjectionCriteria != null) { if (original.ProjectionCriteria == original) { clone.SetProjectionCriteria(clone); } else { ICriteria clonedProjectionCriteria = Clone(original.ProjectionCriteria); clone.SetProjectionCriteria(clonedProjectionCriteria); } } }
public void ActionIsInvokedForImmediateResult() { CriteriaImpl crit = null; bool customActionWasCalled = false; query.QueryOptions .SetCachable(true) .RegisterCustomAction(delegate(ICriteria criteria) { Assert.IsNotNull(criteria); crit = criteria as CriteriaImpl; customActionWasCalled = true; }); query.FirstOrDefault(); Assert.IsTrue(crit.Cacheable); Assert.IsTrue(customActionWasCalled); }
private static System.Type GetRootType(CriteriaImpl criteria) { if (criteria.Session == null) { throw new InvalidOperationException("Could not get root type on criteria that is not attached to a session"); } ISessionFactoryImplementor factory = criteria.Session.Factory; //TODO: need to cache the entityName meta data var entityNames = factory.GetEntityNameMetaData(); if (!entityNames.ContainsKey(criteria.EntityOrClassName)) { throw new InvalidOperationException("Could not find entity named: " + criteria.EntityOrClassName); } return(entityNames[criteria.EntityOrClassName]); }
public CriteriaQueryTranslator(ISessionFactoryImplementor factory, CriteriaImpl criteria, string rootEntityName, string rootSQLAlias) { rootCriteria = criteria; this.rootEntityName = rootEntityName; sessionFactory = factory; this.rootSQLAlias = rootSQLAlias; helper = new SessionFactoryHelper(factory); collectedParameterSpecifications = new List <IParameterSpecification>(); namedParameters = new List <NamedParameter>(); CreateAliasCriteriaMap(); CreateAssociationPathCriteriaMap(); CreateCriteriaEntityNameMap(); CreateCriteriaCollectionPersisters(); CreateCriteriaSQLAliasMap(); CreateSubQuerySpaces(); }
public CriteriaQueryTranslator(ISessionFactoryImplementor factory, CriteriaImpl criteria, string rootEntityName, string rootSQLAlias) { rootCriteria = criteria; this.rootEntityName = rootEntityName; sessionFactory = factory; this.rootSQLAlias = rootSQLAlias; helper = new SessionFactoryHelper(factory); collectedParameterSpecifications = new List<IParameterSpecification>(); namedParameters = new List<NamedParameter>(); CreateAliasCriteriaMap(); CreateAssociationPathCriteriaMap(); CreateCriteriaEntityNameMap(); CreateCriteriaCollectionPersisters(); CreateCriteriaSQLAliasMap(); CreateSubQuerySpaces(); }
public CriteriaQueryTranslator(ISessionFactoryImplementor factory, CriteriaImpl criteria, string rootEntityName, string rootSQLAlias) { rootCriteria = criteria; sessionFactory = factory; rootPersister = GetQueryablePersister(rootEntityName); this.rootSQLAlias = rootSQLAlias; helper = new SessionFactoryHelper(factory); collectedParameterSpecifications = new List <IParameterSpecification>(); namedParameters = new List <NamedParameter>(); CreateAliasCriteriaMap(); CreateAssociationPathCriteriaMap(); CreateEntityJoinMap(); CreateCriteriaEntityNameMap(); CreateCriteriaCollectionPersisters(); CreateCriteriaSQLAliasMap(); CreateSubQuerySpaces(); supportsQueryCache = GetPersisters().All(o => o.SupportsQueryCache); }
private void CopyCriteriaValues(CriteriaImpl criteria, IQueryOver <TRoot, TRoot> query) { if (!string.IsNullOrEmpty(criteria.CacheRegion)) { query.CacheRegion(criteria.CacheRegion); } if (criteria.Cacheable) { query.Cacheable(); } if (criteria.IsReadOnly) { query.ReadOnly(); } foreach (var pair in criteria.LockModes) { query.UnderlyingCriteria.SetLockMode(pair.Key, pair.Value); } if (criteria.Timeout != RowSelection.NoValue) { query.UnderlyingCriteria.SetTimeout(criteria.Timeout); } }
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(); }
private static System.Type GetRootType(CriteriaImpl criteria) { if (criteria.Session == null) throw new InvalidOperationException("Could not get root type on criteria that is not attached to a session"); ISessionFactoryImplementor factory = criteria.Session.Factory; //TODO: need to cache the entityName meta data var entityNames = factory.GetEntityNameMetaData(); if (!entityNames.ContainsKey(criteria.EntityOrClassName)) throw new InvalidOperationException("Could not find entity named: " + criteria.EntityOrClassName); return entityNames[criteria.EntityOrClassName]; }
private string GetWholeAssociationPath(CriteriaImpl.Subcriteria subcriteria) { string path = subcriteria.Path; // some messy, complex stuff here, since createCriteria() can take an // aliased path, or a path rooted at the creating criteria instance ICriteria parent = null; if (path.IndexOf('.') > 0) { // if it is a compound path string testAlias = StringHelper.Root(path); if (!testAlias.Equals(subcriteria.Alias)) { // and the qualifier is not the alias of this criteria // -> check to see if we belong to some criteria other // than the one that created us aliasCriteriaMap.TryGetValue(testAlias, out parent); } } if (parent == null) { // otherwise assume the parent is the the criteria that created us parent = subcriteria.Parent; } else { path = StringHelper.Unroot(path); } if (parent.Equals(rootCriteria)) { // if its the root criteria, we are done return path; } else { // otherwise, recurse return GetWholeAssociationPath((CriteriaImpl.Subcriteria)parent) + '.' + path; } }
private static CriteriaImpl TransformToRowCount(CriteriaImpl criteria) { criteria.ClearOrders(); criteria.SetFirstResult(0).SetMaxResults(RowSelection.NoValue).SetProjection(Projections.RowCount()); return criteria; }
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); } }
public CriteriaBatchItem(ICriteria query) { _criteria = (CriteriaImpl)query ?? throw new ArgumentNullException(nameof(query)); }
protected DetachedCriteria(System.Type entityType, string alias) { impl = new CriteriaImpl(entityType, alias, null); criteria = impl; }
public ISession GetSession(ICriteria criteria) { CriteriaImpl impl = criteria as CriteriaImpl; return(impl != null ? (ISession)impl.Session : this.GetSession(((CriteriaImpl.Subcriteria)criteria).Parent)); }
protected DetachedCriteria(CriteriaImpl impl, ICriteria criteria) { this.impl = impl; this.criteria = criteria; }
protected DetachedCriteria(System.Type entityType) { impl = new CriteriaImpl(entityType, null); criteria = impl; }
public Task <IList> ListAsync(CriteriaImpl criteria, CancellationToken cancellationToken) { return(_session.ListAsync(criteria, cancellationToken)); }
protected DetachedCriteria(string entityName, string alias) { impl = new CriteriaImpl(entityName, alias, null); criteria = impl; }
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(); }
internal DetachedCriteria(CriteriaImpl impl) { this.impl = impl; criteria = impl; }
/// <summary> /// /// </summary> /// <param name="criteria"></param> /// <returns></returns> public IList Find( CriteriaImpl criteria ) { // The body of this method is modified from H2.1 version, because the Java version // used factory.GetImplementors which returns a list of implementor class names // obtained from IClassPersister.ClassName property. // // In Java, calling ReflectHelper.ClassForName( IClassPersister.ClassName ) // works, but in .NET it does not, because the class name does not include the assembly // name. .NET tries to load the class from NHibernate assembly and fails. // // The solution was to add SessionFactoryImpl.GetImplementorClasses method // which returns an array of System.Types instead of just class names. System.Type[ ] implementors = factory.GetImplementorClasses( criteria.CriteriaClass ); int size = implementors.Length; CriteriaLoader[ ] loaders = new CriteriaLoader[size]; ISet spaces = new HashedSet(); for( int i = 0; i < size; i++ ) { System.Type newCriteriaClazz = implementors[ i ]; loaders[ i ] = new CriteriaLoader( GetOuterJoinLoadable( newCriteriaClazz ), factory, new CriteriaImpl( newCriteriaClazz, criteria ) ); spaces.AddAll( loaders[ i ].QuerySpaces ); } AutoFlushIfRequired( spaces ); IList results = new ArrayList(); dontFlushFromFind++; try { for( int i = 0; i < size; i++ ) { IList currentResults; try { currentResults = loaders[ i ].List( this ); } catch( HibernateException ) { // Do not call Convert on HibernateExceptions throw; } catch( Exception sqle ) { throw Convert( sqle, "Unable to perform find" ); } foreach( object result in results ) { currentResults.Add( result ); } results = currentResults; } } finally { dontFlushFromFind--; } return results; }