예제 #1
0
        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));
        }
        SqlString IProjection.ToSqlString(ICriteria criteria, Int32 position, ICriteriaQuery criteriaQuery, IDictionary <String, IFilter> enabledFilters)
        {
            var classMetadata = criteriaQuery.Factory.GetClassMetadata(criteria.GetRootEntityTypeIfAvailable());
            var propertyNames = this.GetPropertyNames(classMetadata, criteriaQuery);
            var builder       = new SqlStringBuilder();

            for (var i = 0; i < propertyNames.Length; ++i)
            {
                var propertyName = propertyNames[i];
                var columnName   = criteriaQuery.GetColumn(criteria, propertyName);

                builder.Add(columnName);
                builder.Add(" as ");
                builder.Add(propertyName);

                this.aliases.Add(propertyName);

                if (i < propertyNames.Length - 1)
                {
                    builder.Add(", ");
                }
            }

            return(builder.ToSqlString());
        }
예제 #3
0
 public OrmReferenceSelector(Func <ITdiTab> getParrentTab, IUnitOfWork unitOfWork, ICriteria itemsCriteria)
 {
     GetMyTab      = getParrentTab ?? throw new ArgumentNullException(nameof(getParrentTab));
     UoW           = unitOfWork ?? throw new ArgumentNullException(nameof(unitOfWork));
     ItemsCriteria = itemsCriteria ?? throw new ArgumentNullException(nameof(itemsCriteria));
     SubjectType   = itemsCriteria.GetRootEntityTypeIfAvailable();
 }
예제 #4
0
        /// <summary>
        /// Add the Fetch clauses to the query according to the given expand paths, using the ICriteria API
        /// </summary>
        /// <param name="criteria">The query to expand</param>
        /// <param name="expandPaths">The names of the properties to expand.  May include nested paths of the form "Property/SubProperty"</param>
        /// <param name="sessionFactory">Provides the NHibernate metadata for the classes</param>
        /// <param name="expandMap">If provided, will be populated with the names of the expanded properties for each type.</param>
        /// <returns></returns>
        public static ICriteria ApplyExpansions(ICriteria criteria, string[] expandPaths, ISessionFactory sessionFactory, IDictionary <Type, List <string> > expandMap = null)
        {
            if (criteria == null)
            {
                throw new ArgumentException("Criteria cannot be null");
            }

            if (!expandPaths.Any())
            {
                throw new ArgumentException("Expansion Paths cannot be null");
            }

            foreach (var expand in expandPaths)
            {
                // We always start with the resulting element type
                var currentType = criteria.GetRootEntityTypeIfAvailable();
                var dotpath     = expand.Replace('/', '.');
                criteria = criteria.Fetch(SelectMode.JoinOnly, dotpath);

                // Add the types and properties to the expandMap so they will be serialized
                foreach (var seg in expand.Split('/'))
                {
                    if (expandMap != null && !expandMap.ContainsKey(currentType))
                    {
                        expandMap.Add(currentType, new List <string>());
                    }

                    var metadata = sessionFactory.GetClassMetadata(currentType);
                    if (metadata == null)
                    {
                        throw new ArgumentException("Type '" + currentType + "' not recognized as a valid type for this Context");
                    }

                    // Gather information about the property
                    var propInfo = currentType.GetProperty(seg);

                    if (propInfo == null)
                    {
                        throw new ArgumentException("Type '" + currentType.Name + "' does not have property '" + seg + "'");
                    }

                    if (expandMap != null)
                    {
                        expandMap[currentType].Add(seg);
                    }

                    var propType = propInfo.PropertyType;

                    currentType = propType;
                }
            }

            return(criteria);
        }
        IType[] IProjection.GetTypes(ICriteria criteria, ICriteriaQuery criteriaQuery)
        {
            if (this.columnTypes == null)
            {
                var classMetadata = criteriaQuery.Factory.GetClassMetadata(criteria.GetRootEntityTypeIfAvailable());
                var propertyNames = this.GetPropertyNames(classMetadata, criteriaQuery);

                this.columnTypes = propertyNames.Select(x => classMetadata.GetPropertyType(x)).ToArray();
            }

            return(this.columnTypes);
        }
예제 #6
0
        public static INHibernateQueryable <T> Linq <T>(this ISession session, ICriteria criteria)
        {
            if (criteria == null)
            {
                throw new ArgumentNullException("criteria");
            }

            if (criteria.GetRootEntityTypeIfAvailable() != typeof(T))
            {
                throw new ArgumentException("Criteria root type must match type of Linq query.", "criteria");
            }

            return(new Query <T>(new NHibernateQueryProvider(session, criteria), null));
        }
예제 #7
0
        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);
        }
예제 #8
0
        /// <summary>
        /// Add the Fetch clauses to the query according to the given expand paths, using the ICriteria API
        /// </summary>
        /// <param name="criteria">The query to expand</param>
        /// <param name="expandPaths">The names of the properties to expand.  May include nested paths of the form "Property/SubProperty"</param>
        /// <param name="sessionFactory">Provides the NHibernate metadata for the classes</param>
        /// <param name="expandMap">If provided, will be populated with the names of the expanded properties for each type.</param>
        /// <returns></returns>
        public static ICriteria ApplyExpansions(ICriteria criteria, string[] expandPaths, ISessionFactory sessionFactory, IDictionary<Type, List<string>> expandMap = null)
        {
            if (criteria == null) throw new ArgumentException("Criteria cannot be null");

            if (!expandPaths.Any()) throw new ArgumentException("Expansion Paths cannot be null");

            foreach (string expand in expandPaths)
            {
                // We always start with the resulting element type
                var currentType = criteria.GetRootEntityTypeIfAvailable();
                var dotpath = expand.Replace('/', '.');
                criteria.SetFetchMode(dotpath, FetchMode.Eager);

                // Add the types and properties to the expandMap so they will be serialized
                foreach (string seg in expand.Split('/'))
                {
                    if (expandMap != null && !expandMap.ContainsKey(currentType))
                        expandMap.Add(currentType, new List<string>());

                    IClassMetadata metadata = sessionFactory.GetClassMetadata(currentType);
                    if (metadata == null)
                    {
                        throw new ArgumentException("Type '" + currentType + "' not recognized as a valid type for this Context");
                    }

                    // Gather information about the property
                    var propInfo = currentType.GetProperty(seg);

                    if (propInfo == null)
                    {
                        throw new ArgumentException("Type '" + currentType.Name + "' does not have property '" + seg + "'");
                    }
                    if (expandMap != null) expandMap[currentType].Add(seg);
                    var propType = propInfo.PropertyType;

                    currentType = propType;
                }
            }

            return criteria;
        }
        /// <summary>
        /// To the future.
        /// </summary>
        /// <param name="criteria">The criteria.</param>
        /// <returns>IEnumerable.</returns>
        public static IEnumerable ToFuture(ICriteria criteria)
        {
            Type collectionType;
            try
            {
                collectionType = criteria.GetRootEntityTypeIfAvailable() ?? typeof(object);
            }
            catch (Exception)
            {
                collectionType = typeof(object);
            }

            FutureFunction current = Functions.FirstOrDefault(n => n.CollectionType == collectionType);
            if (current == null)
            {
                current = new FutureFunction(collectionType);
                Functions.Add(current);
            }

            return current.ExecuteFuture(criteria);
        }
예제 #10
0
        /// <summary>
        /// Get the entity type in criteria
        /// </summary>
        /// <param name="criteria"></param>
        /// <returns></returns>
        /// <exception cref="InvalidOperationException">criteria의 session이 null일 경우</exception>
        public static Type GetRootType(this ICriteria criteria)
        {
            criteria.ShouldNotBeNull("criteria");

            var rootType = criteria.GetRootEntityTypeIfAvailable();

            if (rootType != null)
            {
                return(rootType);
            }

            var impl = GetRootCriteria(criteria);

            impl.ShouldNotBeNull("impl");
            impl.Session.ShouldNotBeNull("impl.Session");

            var factory   = impl.Session.Factory;
            var persister = factory.GetEntityPersister(impl.EntityOrClassName);

            persister.ShouldNotBeNull("Could not find entity named: " + impl.EntityOrClassName);

            return(persister.GetMappedClass(EntityMode.Poco));
        }
예제 #11
0
 /// <summary>
 /// Gets the root entity type if available, throws otherwise
 /// </summary>
 /// <remarks>
 /// This is an NHibernate specific method, used by several dependent
 /// frameworks for advance integration with NHibernate.
 /// </remarks>
 public System.Type GetRootEntityTypeIfAvailable()
 {
     return(criteria.GetRootEntityTypeIfAvailable());
 }
예제 #12
0
        private string GetSecurityKeyProperty(ICriteria criteria)
        {
            Type rootType = criteria.GetRootEntityTypeIfAvailable();

            return(criteria.Alias + "." + Security.GetSecurityKeyProperty(rootType));
        }
 private string GetSecurityKeyProperty(ICriteria criteria)
 {
     Type rootType = criteria.GetRootEntityTypeIfAvailable();
     return criteria.Alias + "." + Security.GetSecurityKeyProperty(rootType);
 }