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
20 using NHibernate
.Criterion
;
24 /// Note: This query can not be included in a MultiQuery.
25 /// the problem is that NHibernate does not have a real CriteriaQuery class
27 public class ActiveRecordCriteriaQuery
: HqlBasedQuery
29 // constructors will set EITHER criterias OR detachedCriteria
30 private readonly ICriterion
[] criterias
;
31 private readonly DetachedCriteria detachedCriteria
;
34 /// Initializes a new instance of the <see cref="ActiveRecordCriteriaQuery"/> class.
36 /// <param name="targetType">The target type.</param>
37 /// <param name="criterias">Criteria applied to the query</param>
38 public ActiveRecordCriteriaQuery(Type targetType
, ICriterion
[] criterias
)
39 : base(targetType
, string.Empty
, null)
41 this.criterias
= criterias
;
45 /// Initializes a new instance of the <see cref="ActiveRecordCriteriaQuery"/> class.
47 /// <param name="targetType">The target type.</param>
48 /// <param name="detachedCriteria">Criteria applied to the query</param>
49 public ActiveRecordCriteriaQuery(Type targetType
, DetachedCriteria detachedCriteria
)
50 : base(targetType
, string.Empty
, null)
52 this.detachedCriteria
= detachedCriteria
;
56 /// Executes the query.
58 /// <param name="session">The <c>NHibernate</c>'s <see cref="ISession"/></param>
59 /// <returns><c>ArrayList</c> as an <c>object</c></returns>
60 protected override object InternalExecute(ISession session
)
62 if (detachedCriteria
!= null)
64 ICriteria criteria
= detachedCriteria
.GetExecutableCriteria(session
);
66 return criteria
.List();
68 else if (criterias
!= null)
70 ICriteria criteria
= session
.CreateCriteria(RootType
);
72 CriteriaHelper
.AddCriterionToCriteria(criteria
, criterias
);
74 return criteria
.List();
78 return base.CreateQuery(session
).List();