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
18 using System
.Collections
;
20 using NHibernate
.Expressions
;
23 /// Perform a scalar projection ( aggeregate ) type of query:
24 /// avg, max, count(*), etc.
26 /// <typeparam name="ARType">The type of the entity we are querying</typeparam>
27 /// <typeparam name="TResult">The type of the scalar from this query</typeparam>
30 /// ScalarProjectionQuery<Blog, int> proj = new ScalarProjectionQuery<Blog, int>(Projections.RowCount());
31 /// int rowCount = proj.Execute();
34 public class ScalarProjectionQuery
<ARType
, TResult
> : IActiveRecordQuery
<TResult
>
36 private readonly IProjection projection
;
37 private readonly ICriterion
[] criterions
;
38 private readonly DetachedCriteria detachedCriteria
;
41 /// Initializes a new instance of the <see cref="ScalarProjectionQuery{ARType,TResult}"/> class.
43 /// <param name="projection">The projection.</param>
44 /// <param name="criterions">The criterions.</param>
45 public ScalarProjectionQuery(IProjection projection
, params ICriterion
[] criterions
)
47 this.projection
= projection
;
48 this.criterions
= criterions
;
52 /// Initializes a new instance of the <see cref="ScalarProjectionQuery{ARType,TResult}"/> class.
54 /// <param name="projection">The projection.</param>
55 /// <param name="criteria">The detached criteria.</param>
56 public ScalarProjectionQuery(IProjection projection
, DetachedCriteria criteria
)
58 this.projection
= projection
;
59 detachedCriteria
= criteria
;
63 /// Gets the target type of this query
68 get { return typeof(ARType); }
72 /// Executes the specified query and return the results
74 /// <param name="session">The session to execute the query in.</param>
75 /// <returns>the result of the query</returns>
76 object IActiveRecordQuery
.Execute(ISession session
)
78 return Execute(session
);
82 /// Enumerates over the result of the query.
83 /// Always returns a single result
85 public IEnumerable
Enumerate(ISession session
)
87 yield return Execute(session
);
91 /// Executes the specified query and return the results
93 /// <param name="session">The session to execute the query in.</param>
94 /// <returns>the result of the query</returns>
95 public TResult
Execute(ISession session
)
97 if (detachedCriteria
!= null)
99 return detachedCriteria
.GetExecutableCriteria(session
)
100 .SetProjection(projection
)
101 .UniqueResult
<TResult
>();
105 ICriteria criteria
= session
.CreateCriteria(RootType
);
107 CriteriaHelper
.AddCriterionToCriteria(criteria
, criterions
);
109 criteria
.SetProjection(projection
);
110 return criteria
.UniqueResult
<TResult
>();
115 /// Executes the specified query and return the results
117 /// <returns>the result of the query</returns>
118 public TResult
Execute()
120 return (TResult
) ActiveRecordMediator
.ExecuteQuery(this);