context: using System;
  using System.Linq;
  using System.Collections.Generic;
  using System.Text;
  using N2.Collections;
  using N2.Definitions;
  using N2.Definitions.Static;
  using N2.Persistence.Finder;
  using NHibernate;
  using System.Linq.Expressions;
  
  namespace N2.Persistence.NH.Finder
  {
      /// <summary>
      /// The query builder stores query settings and can perform selects on the
      /// database.
      /// </summary>
      [Serializable]
      public class QueryBuilder : IQueryBuilder, IQueryAction
      {
          #region Fields
  
          [NonSerialized] private ISessionProvider sessionProvider;
          [NonSerialized] private DefinitionMap map;
  
          private IList<IHqlProvider> criterias = new List<IHqlProvider>();
          private Operator currentOperator = Operator.None;
          private VersionOption versions = VersionOption.Exclude;
          private bool cachable = true;
          private string sortExpression = null;
          private string orderBy = null;
          private IList<ItemFilter> filters = new List<ItemFilter>();
          private int firstResult = 0;
          private int maxResults = 0;
  
          #endregion
  
          #region Constructor
  
          public QueryBuilder(ISessionProvider sessionProvider, DefinitionMap map)
          {
              this.sessionProvider = sessionProvider;
              this.map = map;
          }
  
          #endregion
  
          #region Properties
  
          /// <summary>Gets or sets the NHibernate session provider.</summary>
          public ISessionProvider SessionProvider
          {
              get { return sessionProvider; }
              set { sessionProvider = value; }
          }
  
          /// <summary>Gets or sets wether the query should be cached.</summary>
          public bool Cachable
          {
              get { return cachable; }
              set { cachable = value; }
          }
  
          /// <summary>Gets or sets wether previous versions should be included in the query.</summary>
          public VersionOption Versions
          {
              get { return versions; }
              set { versions = value; }
          }
  
          /// <summary>Gets or sets the current operator (this is updated as the query is progressed using And or Or).</summary>
          public Operator CurrentOperator
          {
              get { return currentOperator; }
              set { currentOperator = value; }
          }
  
          /// <summary>Gets the current set of criterias.</summary>
          public IList<IHqlProvider> Criterias
          {
              get { return criterias; }
          }
  
          /// <summary>Gets or sets filters applied to the result set after it has been fetched from the database.</summary>
          public IList<ItemFilter> Filters
          {
              get { return filters; }
              set { filters = value; }
          }
  
          /// <summary>Order expression applied to the sql statement.</summary>
          public string OrderBy
          {
              get { return orderBy; }
              set { orderBy = value; }
          }
  
          /// <summary>Gets or sets the sort expression performed on the result list after selection (not in database).</summary>
          public string SortExpression
          {
              get { return sortExpression; }
              set { sortExpression = value; }
          }
  
          /// <summary>Gets or sets the maximum number of results to return.</summary>
          public int MaxResults
          {
              get { return maxResults; }
              set { maxResults = value; }
          }
  
          /// <summary>Gets or sets the first result of the result set to return (skipping those before).</summary>
          public int FirstResult
          {
              get { return firstResult; }
              set { firstResult = value; }
          }
  
          #endregion
  
          #region IQueryBase Members
  
          public IComparisonCriteria<int> ID
          {
              get { return new PropertyCriteria<int>(this, "ID"); }
          }
  
          public IComparisonCriteria<int> ParentID
          {
              get { return new PropertyCriteria<int>(this, "Parent.ID"); }
          }
  
          public IComparisonCriteria<ContentItem> Parent
          {
              get { return new PropertyCriteria<ContentItem>(this, "Parent"); }
          }
  
          public IStringCriteria Title
          {
              get { return new PropertyStringCriteria(this, "Title"); }
          }
  
          public IStringCriteria Name
          {
              get { return new PropertyStringCriteria(this, "Name"); }
          }
  
          public IStringCriteria ZoneName
          {
              get { return new PropertyStringCriteria(this, "ZoneName"); }
          }
  
          public IComparisonCriteria<DateTime> Created
          {
              get { return new PropertyCriteria<DateTime>(this, "Created"); }
          }
  
          public IComparisonCriteria<DateTime> Updated
          {
              get { return new PropertyCriteria<DateTime>(this, "Updated"); }
          }
  
          public INullableComparisonCriteria<DateTime> Published
          {
              get { return new NullablePropertyCriteria<DateTime>(this, "Published"); }
          }
  
          public INullableComparisonCriteria<DateTime> Expires
          {
              get { return new NullablePropertyCriteria<DateTime>(this, "Expires"); }
          }
  
          public IComparisonCriteria<int> SortOrder
          {
              get { return new PropertyCriteria<int>(this, "SortOrder"); }
          }
  
          public IComparisonCriteria<int> VersionIndex
          {
              get { return new PropertyCriteria<int>(this, "VersionIndex"); }
          }
  
          public IComparisonCriteria<ContentState> State
          {
              get { return new PropertyCriteria<ContentState>(this, "State"); }
          }
  
          public ICriteria<bool> Visible
          {
              get { return new PropertyCriteria<bool>(this, "Visible"); }
          }
  
          public ICriteria<ContentItem> VersionOf
          {
              get { return new PropertyVersionOfCriteria(this); }
          }
  
          public IStringCriteria SavedBy
          {
              get { return new PropertyStringCriteria(this, "SavedBy"); }
          }
  
          public IStringCriteria AncestralTrail
          {
              get { return new PropertyStringCriteria(this, "AncestralTrail"); }
          }
  
          public IDetailCriteria Detail(string name)
          {
  
gt:             return new DetailCriteria(this, name);