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
;
19 using System
.Text
.RegularExpressions
;
21 using Castle
.ActiveRecord
.Framework
;
22 using Castle
.ActiveRecord
.Queries
.Modifiers
;
25 using NHibernate
.Type
;
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 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 AddSqlReturnDefinition
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
));
196 /// Creates the <see cref="IQuery"/> instance.
198 /// <param name="session"></param>
199 /// <returns></returns>
200 protected override IQuery
CreateQuery(ISession session
)
204 switch (queryLanguage
)
206 case QueryLanguage
.Hql
:
207 nhibQuery
= session
.CreateQuery(Query
);
210 case QueryLanguage
.Sql
:
211 ArrayList queryReturnAliases
= new ArrayList();
212 ArrayList queryReturnTypes
= new ArrayList();
213 if (queryModifiers
!= null)
215 foreach (IQueryModifier mod
in queryModifiers
)
217 SqlQueryReturnDefinition returnDef
= mod
as SqlQueryReturnDefinition
;
219 if (returnDef
== null) continue;
221 queryReturnAliases
.Add(returnDef
.ReturnAlias
);
222 queryReturnTypes
.Add(returnDef
.ReturnType
);
225 ISQLQuery sqlQuery
= session
.CreateSQLQuery(Query
);
226 for (int i
= 0; i
< queryReturnAliases
.Count
; i
++)
228 sqlQuery
.AddEntity((string)queryReturnAliases
[i
], (Type
)queryReturnTypes
[i
]);
230 nhibQuery
= sqlQuery
;
234 throw new ActiveRecordException("Query language not supported: " + queryLanguage
);
237 ApplyModifiers(nhibQuery
);