Updates Castle to the latest NHibernate Trunk libs. This required namespace changes...
[castle.git] / ActiveRecord / Castle.ActiveRecord / Framework / Queries / CountQuery.cs
blob238715bd7a2df757ebc703e8bb292258b6b761ad
1 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 namespace Castle.ActiveRecord.Queries
17 using System;
18 using NHibernate;
19 using NHibernate.Criterion;
21 /// <summary>
22 /// Query the database for a count (using COUNT(*) ) of all the entites of the specified type.
23 /// Optionally using a where clause;
24 /// Note: If Criteria are used, this query can not be included in a MultiQuery.
25 /// </summary>
26 public class CountQuery : HqlBasedQuery
28 // constructors will set EITHER criterias OR detachedCriteria
29 private readonly ICriterion[] criterias;
30 private readonly DetachedCriteria detachedCriteria;
32 /// <summary>
33 /// Initializes a new instance of the <see cref="CountQuery"/> class.
34 /// </summary>
35 /// <param name="targetType">The target type.</param>
36 /// <param name="filter">The filter.</param>
37 /// <param name="parameters">The parameters.</param>
38 public CountQuery(Type targetType, string filter, params object[] parameters)
39 : base(targetType, "SELECT COUNT(*) FROM " + targetType.Name + " WHERE " + filter, parameters)
43 /// <summary>
44 /// Initializes a new instance of the <see cref="CountQuery"/> class.
45 /// </summary>
46 /// <param name="targetType">The target type.</param>
47 public CountQuery(Type targetType) : this(targetType, "1=1", null)
51 /// <summary>
52 /// Initializes a new instance of the <see cref="CountQuery"/> class.
53 /// </summary>
54 /// <param name="targetType">The target type.</param>
55 /// <param name="criterias">Criteria applied to the query</param>
56 public CountQuery(Type targetType, ICriterion[] criterias) : this(targetType, string.Empty, null)
58 this.criterias = criterias;
61 /// <summary>
62 /// Initializes a new instance of the <see cref="CountQuery"/> class.
63 /// </summary>
64 /// <param name="targetType">The target type.</param>
65 /// <param name="detachedCriteria">Criteria applied to the query</param>
66 public CountQuery(Type targetType, DetachedCriteria detachedCriteria)
67 : this(targetType, string.Empty, null)
69 this.detachedCriteria = detachedCriteria;
72 /// <summary>
73 /// Executes the query.
74 /// </summary>
75 /// <param name="session">The <c>NHibernate</c>'s <see cref="ISession"/></param>
76 /// <returns><c>System.Int32</c> as object</returns>
77 protected override object InternalExecute(ISession session)
79 if (detachedCriteria != null)
81 ICriteria criteria = detachedCriteria.GetExecutableCriteria(session);
83 criteria.SetProjection(Projections.RowCount());
85 Int32 count = Convert.ToInt32(criteria.UniqueResult());
87 // clear the projection, so our caller can re-use the DetachedCriteria
88 criteria.SetProjection(null);
90 return count;
92 else if (criterias != null)
94 ICriteria criteria = session.CreateCriteria(RootType);
96 CriteriaHelper.AddCriterionToCriteria(criteria, criterias);
98 criteria.SetProjection(Projections.RowCount());
100 return Convert.ToInt32(criteria.UniqueResult());
102 else
104 return Convert.ToInt32(base.CreateQuery(session).UniqueResult());