Removed untyped contructor from ComponentRegistration and add a protected setter.
[castle.git] / ActiveRecord / Castle.ActiveRecord / Framework / Queries / SimpleQuery.Generic.cs
blob6e67ec367bc6a74fc128f33e3cae4436dba5ed9b
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 System.Collections.Generic;
21 using Castle.ActiveRecord.Framework;
23 using NHibernate;
25 /// <summary>
26 /// Represents a query that can result in an array of
27 /// objects of the type <typeparamref name="T"/>.
28 /// </summary>
29 /// <typeparam name="T">The resulting object type</typeparam>
30 public class SimpleQuery<T> : HqlBasedQuery, IActiveRecordQuery<T[]>
32 #region Constructors
34 /// <summary>
35 /// Creates a new <c>SimpleQuery</c> for the giving <paramref name="query"/>,
36 /// using the specified positional <paramref name="positionalParameters"/>.
37 /// The target ActiveRecord type is <typeparamref name="T"/>.
38 /// </summary>
39 /// <param name="query">The query</param>
40 /// <param name="positionalParameters">The positional parameters</param>
41 public SimpleQuery(String query, params Object[] positionalParameters)
42 : base(typeof(T), query, positionalParameters)
46 /// <summary>
47 /// Creates a new <c>SimpleQuery</c> for the giving <paramref name="query"/>,
48 /// using the specified positional <paramref name="positionalParameters"/>.
49 /// The target ActiveRecord type is <typeparamref name="T"/>.
50 /// </summary>
51 /// <param name="query">The query</param>
52 /// <param name="queryLanguage">The query language</param>
53 /// <param name="positionalParameters">The positional parameters</param>
54 public SimpleQuery(QueryLanguage queryLanguage, String query, params Object[] positionalParameters)
55 : base(typeof(T), queryLanguage, query, positionalParameters)
59 /// <summary>
60 /// Creates a new <c>SimpleQuery</c> for the giving <paramref name="query"/>,
61 /// using the specified positional <paramref name="positionalParameters"/> and
62 /// the target ActiveRecord type specified in <paramref name="targetType"/>.
63 /// </summary>
64 /// <param name="targetType">The target ActiveRecord type</param>
65 /// <param name="query">The query</param>
66 /// <param name="positionalParameters">The positional parameters</param>
67 public SimpleQuery(Type targetType, String query, params Object[] positionalParameters)
68 : base(targetType, query, positionalParameters)
72 /// <summary>
73 /// Creates a new <c>SimpleQuery</c> for the giving <paramref name="query"/>,
74 /// using the specified positional <paramref name="positionalParameters"/> and
75 /// the target ActiveRecord type specified in <paramref name="targetType"/>.
76 /// </summary>
77 /// <param name="targetType">The target ActiveRecord type</param>
78 /// <param name="queryLanguage">The query language</param>
79 /// <param name="query">The query</param>
80 /// <param name="positionalParameters">The positional parameters</param>
81 public SimpleQuery(Type targetType, QueryLanguage queryLanguage, String query, params Object[] positionalParameters)
82 : base(targetType, queryLanguage, query, positionalParameters)
86 #endregion
88 #region IActiveRecordQuery<T[]> implementation
90 T[] IActiveRecordQuery<T[]>.Execute(ISession session)
92 return (T[]) InternalExecute(session);
95 #endregion
97 #region Public "Execute" and "Enumerate" Methods
99 /// <summary>
100 /// Executes the query and gets the results.
101 /// </summary>
102 public T[] Execute()
104 return ActiveRecordBase<T>.ExecuteQuery2(this);
107 /// <summary>
108 /// Enumerates the query results. Better suited for queries
109 /// which might return large results.
110 /// <seealso cref="IQuery.Enumerable" />
111 /// </summary>
112 /// <remarks>
113 /// It might not look obvious at first, but
114 /// <see cref="ActiveRecordMediator"/> will call our
115 /// <see cref="InternalEnumerate"/>, which will call our
116 /// <see cref="GenericEnumerate"/>, which will convert
117 /// the <c>NHibernate</c>'s <see cref="IQuery.Enumerable"/> result
118 /// returned by <see cref="ActiveRecordBaseQuery.InternalEnumerate"/>
119 /// into a generic <see cref="IEnumerable{T}"/>.
120 /// So, all we need to do is to cast it back to <see cref="IEnumerable{T}"/>.
121 /// </remarks>
122 public IEnumerable<T> Enumerate()
124 return (IEnumerable<T>) ActiveRecordMediator.EnumerateQuery(this);
127 #endregion
129 /// <summary>
130 /// Simply creates the query and then call its <see cref="IQuery.Enumerable()"/> method.
131 /// Note: Only use when you expect most of the results to be in the second level cache
132 /// </summary>
133 /// <param name="session">The <c>NHibernate</c>'s <see cref="ISession"/></param>
134 /// <returns></returns>
135 protected override IEnumerable InternalEnumerate(ISession session)
137 return GenericEnumerate(session);
140 private IEnumerable<T> GenericEnumerate(ISession session)
142 IEnumerable en = InternalEnumerateFromBase(session);
144 foreach(T item in en)
146 yield return item;
150 /// <summary>
151 /// Needed to avoid <c>CS1911</c>.
152 /// </summary>
153 private IEnumerable InternalEnumerateFromBase(ISession session)
155 return base.InternalEnumerate(session);
158 /// <summary>
159 /// Executes the query and converts the results into a strongly-typed
160 /// array of <typeparamref name="T"/>.
161 /// </summary>
162 /// <param name="session">The <c>NHibernate</c>'s <see cref="ISession"/></param>
163 protected override object InternalExecute(ISession session)
165 IList results = (IList) base.InternalExecute(session);
166 return SupportingUtils.BuildArray<T>(results, false);