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
;
19 using System
.Collections
.Generic
;
21 using Castle
.ActiveRecord
.Framework
;
26 /// Represents a query that can result in an array of
27 /// objects of the type <typeparamref name="T"/>.
29 /// <typeparam name="T">The resulting object type</typeparam>
30 public class SimpleQuery
<T
> : HqlBasedQuery
, IActiveRecordQuery
<T
[]>
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"/>.
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
)
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"/>.
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
)
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"/>.
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
)
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"/>.
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
)
88 #region IActiveRecordQuery<T[]> implementation
90 T
[] IActiveRecordQuery
<T
[]>.Execute(ISession session
)
92 return (T
[]) InternalExecute(session
);
97 #region Public "Execute" and "Enumerate" Methods
100 /// Executes the query and gets the results.
104 return ActiveRecordBase
<T
>.ExecuteQuery2(this);
108 /// Enumerates the query results. Better suited for queries
109 /// which might return large results.
110 /// <seealso cref="IQuery.Enumerable" />
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}"/>.
122 public IEnumerable
<T
> Enumerate()
124 return (IEnumerable
<T
>) ActiveRecordMediator
.EnumerateQuery(this);
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
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
)
151 /// Needed to avoid <c>CS1911</c>.
153 private IEnumerable
InternalEnumerateFromBase(ISession session
)
155 return base.InternalEnumerate(session
);
159 /// Executes the query and converts the results into a strongly-typed
160 /// array of <typeparamref name="T"/>.
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);