public virtual Array FindAll(Type type, ICriterion[] criterias, Order[] sortItems)
		{
			return FindAll(type, criterias, sortItems, int.MinValue, int.MinValue);
		}
		public virtual Array FindAll(Type type, ICriterion[] criterias, Order[] sortItems, int firstRow, int maxRows)
		{
			using(ISession session = GetSession())
			{
				try
				{
					ICriteria criteria = session.CreateCriteria(type);

					if (criterias != null)
					{
						foreach(ICriterion cond in criterias)
						{
							criteria.Add(cond);
						}
					}

					if (sortItems != null)
					{
						foreach(Order order in sortItems)
						{
							criteria.AddOrder(order);
						}
					}

					if (firstRow != int.MinValue) criteria.SetFirstResult(firstRow);
					if (maxRows != int.MinValue) criteria.SetMaxResults(maxRows);
					IList result = criteria.List();
					if (result == null || result.Count == 0) return null;

					Array array = Array.CreateInstance(type, result.Count);
					result.CopyTo(array, 0);

					return array;
				}
				catch(Exception ex)
				{
					throw new DataException("Could not perform FindAll for " + type.Name, ex);
				}
			}
		}
		internal static Order[] PropertyNamesToOrderArray(bool asc, params string[] propertyNames) 
		{
			Order[] orders = new Order[propertyNames.Length];

			for (int i = 0; i < propertyNames.Length; i++) 
			{
				orders[i] = new Order(propertyNames[i], asc);
			}
			return orders;
		}
		/// <summary>
		/// Returns all instances found for the specified type 
		/// using sort orders and criteria.
		/// </summary>
		/// <param name="targetType">The The target type.</param>
		/// <param name="orders">An <see cref="Array"/> of <see cref="Order"/> objects.</param>
		/// <param name="criteria">The criteria expression</param>
		/// <returns>The <see cref="Array"/> of results.</returns>
		protected internal static Array FindAll(Type targetType, Order[] orders, params ICriterion[] criteria)
		{
			EnsureInitialized(targetType);

			ISession session = holder.CreateSession(targetType);

			try
			{
				ICriteria sessionCriteria = session.CreateCriteria(targetType);

				foreach(ICriterion cond in criteria)
				{
					sessionCriteria.Add(cond);
				}

				AddOrdersToCriteria(sessionCriteria, orders);

				return SupportingUtils.BuildArray(targetType, sessionCriteria.List());
			}
			catch (ValidationException)
			{
				holder.FailSession(session);

				throw;
			}
			catch (Exception ex)
			{
				holder.FailSession(session);

				throw new ActiveRecordException("Could not perform FindAll for " + targetType.Name, ex);
			}
			finally
			{
				holder.ReleaseSession(session);
			}
		}
		/// <summary>
		/// Returns a portion of the query results (sliced)
		/// </summary>
		/// <param name="targetType">The target type.</param>
		/// <param name="firstResult">The number of the first row to retrieve.</param>
		/// <param name="maxResults">The maximum number of results retrieved.</param>
		/// <param name="orders">An <see cref="Array"/> of <see cref="Order"/> objects.</param>
		/// <param name="criteria">The criteria expression</param>
		/// <returns>The sliced query results.</returns>
		protected internal static Array SlicedFindAll(Type targetType, int firstResult, int maxResults,
		                                              Order[] orders, params ICriterion[] criteria)
		{
			EnsureInitialized(targetType);

			ISession session = holder.CreateSession(targetType);

			try
			{
				ICriteria sessionCriteria = session.CreateCriteria(targetType);

				foreach(ICriterion cond in criteria)
				{
					sessionCriteria.Add(cond);
				}

				if (orders != null)
				{
					foreach (Order order in orders)
					{
						sessionCriteria.AddOrder(order);
					}
				}

				sessionCriteria.SetFirstResult(firstResult);
				sessionCriteria.SetMaxResults(maxResults);

				return SupportingUtils.BuildArray(targetType, sessionCriteria.List());
			}
			catch (ValidationException)
			{
				holder.FailSession(session);

				throw;
			}
			catch (Exception ex)
			{
				holder.FailSession(session);

				throw new ActiveRecordException("Could not perform SlicedFindAll for " + targetType.Name, ex);
			}
			finally
			{
				holder.ReleaseSession(session);
			}
		}
		/// <summary>
		/// Returns a portion of the query results (sliced)
		/// </summary>
		/// <param name="targetType">The target type.</param>
		/// <param name="firstResult">The number of the first row to retrieve.</param>
		/// <param name="maxResults">The maximum number of results retrieved.</param>
		/// <param name="orders">An <see cref="Array"/> of <see cref="Order"/> objects.</param>
		/// <param name="criteria">The criteria expression</param>
		/// <returns>The sliced query results.</returns>
		protected internal static Array SlicedFindAll(Type targetType, int firstResult, int maxResults,
		                                              Order[] orders, DetachedCriteria criteria)
		{
			EnsureInitialized(targetType);

			ISession session = holder.CreateSession(targetType);

			try
			{
				ICriteria executableCriteria = criteria.GetExecutableCriteria(session);
				AddOrdersToCriteria(executableCriteria, orders);
				executableCriteria.SetFirstResult(firstResult);
				executableCriteria.SetMaxResults(maxResults);

				return SupportingUtils.BuildArray(targetType, executableCriteria.List());
			}
			catch(ValidationException)
			{
				holder.FailSession(session);

				throw;
			}
			catch(Exception ex)
			{
				holder.FailSession(session);

				throw new ActiveRecordException("Could not perform SlicedFindAll for " + targetType.Name, ex);
			}
			finally
			{
				holder.ReleaseSession(session);
			}
		}
		public ARPaginableCriteria(Type targetType, Order[] orders, params ICriterion[] criterions)
		{
			this.targetType = targetType;
			this.orders = orders;
			this.criterions = criterions;
		}
		/// <summary>
		/// Searches and returns the first row.
		/// </summary>
		/// <param name="targetType">The target type</param>
		/// <param name="orders">The sort order - used to determine which record is the first one</param>
		/// <param name="criteria">The criteria expression</param>
		/// <returns>A <c>targetType</c> instance or <c>null</c></returns>
		protected internal static object FindFirst(Type targetType, Order[] orders, params ICriterion[] criteria)
		{
			Array result = SlicedFindAll(targetType, 0, 1, orders, criteria);
			return (result != null && result.Length > 0 ? result.GetValue(0) : null);
		}
Example #9
0
		protected internal virtual object FindFirst(Order[] orders, params ICriterion[] criterias)
		{
			return FindFirst(GetType(), orders, criterias);
		}
		/// <summary>
		/// Searches and returns the first row.
		/// </summary>
		/// <param name="targetType">The target type</param>
		/// <param name="orders">The sort order - used to determine which record is the first one</param>
		/// <param name="criterias">The criteria expression</param>
		/// <returns>A <c>targetType</c> instance or <c>null</c></returns>
		public static object FindFirst(Type targetType, Order[] orders, params ICriterion[] criterias)
		{
			return ActiveRecordBase.FindFirst(targetType, orders, criterias);
		}
		/// <summary>
		/// Returns all instances found for the specified type 
		/// using sort orders and criterias.
		/// </summary>
		/// <param name="targetType"></param>
		/// <param name="orders"></param>
		/// <param name="criterias"></param>
		/// <returns></returns>
		public static Array FindAll(Type targetType, Order[] orders, params ICriterion[] criterias)
		{
			return ActiveRecordBase.FindAll(targetType, orders, criterias);
		}
		/// <summary>
		/// Returns a portion of the query results (sliced)
		/// </summary>
		public static Array SlicedFindAll(Type targetType, int firstResult, int maxResults,
													  Order[] orders, DetachedCriteria criteria)
		{
			return ActiveRecordBase.SlicedFindAll(targetType, firstResult, maxResults, orders, criteria);
		}
		/// <summary>
		/// Returns a portion of the query results (sliced)
		/// </summary>
		public static Array SlicedFindAll(Type targetType, int firstResult, int maxresults,
		                                  Order[] orders, params ICriterion[] criterias)
		{
			return ActiveRecordBase.SlicedFindAll(targetType, firstResult,
			                                      maxresults, orders, criterias);
		}