Removed untyped contructor from ComponentRegistration and add a protected setter.
[castle.git] / ActiveRecord / Castle.ActiveRecord / Framework / Queries / ScalarProjectionQuery.cs
bloba51137b091f2e02b0234445bc53ad4dd476efd84
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 System.Collections;
19 using NHibernate;
20 using NHibernate.Expressions;
22 /// <summary>
23 /// Perform a scalar projection ( aggeregate ) type of query:
24 /// avg, max, count(*), etc.
25 /// </summary>
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>
28 /// <example>
29 /// <code>
30 /// ScalarProjectionQuery&lt;Blog, int&gt; proj = new ScalarProjectionQuery&lt;Blog, int&gt;(Projections.RowCount());
31 /// int rowCount = proj.Execute();
32 /// </code>
33 /// </example>
34 public class ScalarProjectionQuery<ARType, TResult> : IActiveRecordQuery<TResult>
36 private readonly IProjection projection;
37 private readonly ICriterion[] criterions;
38 private readonly DetachedCriteria detachedCriteria;
40 /// <summary>
41 /// Initializes a new instance of the <see cref="ScalarProjectionQuery{ARType,TResult}"/> class.
42 /// </summary>
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;
51 /// <summary>
52 /// Initializes a new instance of the <see cref="ScalarProjectionQuery{ARType,TResult}"/> class.
53 /// </summary>
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;
62 /// <summary>
63 /// Gets the target type of this query
64 /// </summary>
65 /// <value></value>
66 public Type RootType
68 get { return typeof(ARType); }
71 /// <summary>
72 /// Executes the specified query and return the results
73 /// </summary>
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);
81 /// <summary>
82 /// Enumerates over the result of the query.
83 /// Always returns a single result
84 /// </summary>
85 public IEnumerable Enumerate(ISession session)
87 yield return Execute(session);
90 /// <summary>
91 /// Executes the specified query and return the results
92 /// </summary>
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>();
103 else
105 ICriteria criteria = session.CreateCriteria(RootType);
107 CriteriaHelper.AddCriterionToCriteria(criteria, criterions);
109 criteria.SetProjection(projection);
110 return criteria.UniqueResult<TResult>();
114 /// <summary>
115 /// Executes the specified query and return the results
116 /// </summary>
117 /// <returns>the result of the query</returns>
118 public TResult Execute()
120 return (TResult) ActiveRecordMediator.ExecuteQuery(this);