1 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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
19 using NHibernate
.Criterion
;
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.
26 public class CountQuery
: HqlBasedQuery
28 // constructors will set EITHER criterias OR detachedCriteria
29 private readonly ICriterion
[] criterias
;
30 private readonly DetachedCriteria detachedCriteria
;
33 /// Initializes a new instance of the <see cref="CountQuery"/> class.
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
)
44 /// Initializes a new instance of the <see cref="CountQuery"/> class.
46 /// <param name="targetType">The target type.</param>
47 public CountQuery(Type targetType
) : this(targetType
, "1=1", null)
52 /// Initializes a new instance of the <see cref="CountQuery"/> class.
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
;
62 /// Initializes a new instance of the <see cref="CountQuery"/> class.
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
;
73 /// Executes the query.
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);
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());
104 return Convert
.ToInt32(base.CreateQuery(session
).UniqueResult());