1 // Copyright 2004-2007 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 Castle
.ActiveRecord
.Framework
;
21 using Castle
.ActiveRecord
.Queries
.Modifiers
;
24 using NHibernate
.Type
;
25 using NHibernate
.Transform
;
28 /// defines the possible query langauges
30 public enum QueryLanguage
33 /// Hibernate Query Language
37 /// Structured Query Language
43 /// Base class for all HQL or SQL-based queries.
45 public class HqlBasedQuery
: ActiveRecordBaseQuery
47 private readonly QueryLanguage queryLanguage
;
51 /// Initializes a new instance of the <see cref="HqlBasedQuery"/> class.
53 /// <param name="targetType">Type of the target.</param>
54 /// <param name="query">The query.</param>
55 public HqlBasedQuery(Type targetType
, string query
)
56 : this(targetType
, QueryLanguage
.Hql
, query
)
61 /// Initializes a new instance of the <see cref="HqlBasedQuery"/> class.
63 /// <param name="targetType">Type of the target.</param>
64 /// <param name="query">The query.</param>
65 /// <param name="positionalParameters">The positional parameters.</param>
66 public HqlBasedQuery(Type targetType
, string query
, params object[] positionalParameters
)
67 : this(targetType
, QueryLanguage
.Hql
, query
, positionalParameters
)
72 /// Initializes a new instance of the <see cref="HqlBasedQuery"/> class.
74 /// <param name="targetType">Type of the target.</param>
75 /// <param name="queryLanguage">The query language.</param>
76 /// <param name="query">The query.</param>
77 public HqlBasedQuery(Type targetType
, QueryLanguage queryLanguage
, string query
)
81 this.queryLanguage
= queryLanguage
;
85 /// Initializes a new instance of the <see cref="HqlBasedQuery"/> class.
87 /// <param name="targetType">Type of the target.</param>
88 /// <param name="queryLanguage">The query language.</param>
89 /// <param name="query">The query.</param>
90 /// <param name="positionalParameters">The positional parameters.</param>
91 public HqlBasedQuery(Type targetType
, QueryLanguage queryLanguage
, string query
, params object[] positionalParameters
)
92 : this(targetType
, queryLanguage
, query
)
94 if (positionalParameters
!= null && positionalParameters
.Length
> 0)
97 foreach (object value in positionalParameters
)
99 AddModifier(new QueryParameter(i
++, value));
109 get { return query; }
110 set { query = value; }
113 #region SetParameter and SetParameterList
116 /// Sets a parameter with the given name.
118 /// <param name="parameterName">Name of the parameter.</param>
119 /// <param name="value">The value.</param>
120 public void SetParameter(string parameterName
, object value)
122 AddModifier(new QueryParameter(parameterName
, value));
126 /// Sets a parameter with the given name and type
128 /// <param name="parameterName">Name of the parameter.</param>
129 /// <param name="value">The value.</param>
130 /// <param name="type">The type.</param>
131 public void SetParameter(string parameterName
, object value, IType type
)
133 AddModifier(new QueryParameter(parameterName
, value, type
));
137 /// Sets a parameter with the given name with a list of values
139 /// <param name="parameterName">Name of the parameter.</param>
140 /// <param name="list">The list.</param>
141 public void SetParameterList(string parameterName
, ICollection list
)
143 AddModifier(new QueryParameter(parameterName
, list
));
147 /// Sets a parameter with the given name with a list of values and type
149 /// <param name="parameterName">Name of the parameter.</param>
150 /// <param name="list">The list.</param>
151 /// <param name="type">The type.</param>
152 public void SetParameterList(string parameterName
, ICollection list
, IType type
)
154 AddModifier(new QueryParameter(parameterName
, list
, type
));
159 #region SetQueryRange
162 /// Sets the query range (paging)
164 /// <param name="firstResult">The first result.</param>
165 /// <param name="maxResults">The maximum number of results returned (page size)</param>
166 public void SetQueryRange(int firstResult
, int maxResults
)
168 AddModifier(new QueryRange(firstResult
, maxResults
));
172 /// Sets the query range (maximum number of items returned)
174 /// <param name="maxResults">The maximum number of results.</param>
175 public void SetQueryRange(int maxResults
)
177 AddModifier(new QueryRange(maxResults
));
182 #region SqlQuery Modifiers
185 /// Adds a SQL query return definition.
186 /// See <see cref="NHibernate.ISession.CreateSQLQuery(string,string[],Type[])"/> for more information.
188 public void AddSqlReturnDefinition(Type returnType
, String returnAlias
)
190 AddModifier(new SqlQueryReturnDefinition(returnType
, returnAlias
));
194 /// Adds a SQL query join definition.
195 /// See <see cref="NHibernate.ISession.CreateSQLQuery(string,string[],Type[])"/> for more information.
197 public void AddSqlJoinDefinition(String associationPath
, String associationAlias
)
199 AddModifier(new SqlQueryJoinDefinition(associationPath
, associationAlias
));
203 /// Adds a SQL query scalar definition.
204 /// See <see cref="NHibernate.ISession.CreateSQLQuery(string,string[],Type[])"/> for more information.
206 public void AddSqlScalarDefinition(IType scalarType
, String columnAlias
)
208 AddModifier(new SqlQueryScalarDefinition(scalarType
, columnAlias
));
213 #region SetResultTransformer
216 /// Adds a query result transformer.
217 /// See <see cref="IResultTransformer"/> for more information.
219 public void SetResultTransformer(IResultTransformer transformer
)
221 AddModifier(new QueryResultTransformer(transformer
));
227 /// Creates the <see cref="IQuery"/> instance.
229 /// <param name="session"></param>
230 /// <returns></returns>
231 protected override IQuery
CreateQuery(ISession session
)
235 switch (queryLanguage
)
237 case QueryLanguage
.Hql
:
238 nhibQuery
= session
.CreateQuery(Query
);
241 case QueryLanguage
.Sql
:
242 nhibQuery
= session
.CreateSQLQuery(Query
);
246 throw new ActiveRecordException("Query language not supported: " + queryLanguage
);
249 ApplyModifiers(nhibQuery
);