Updating to NHibernate CR1
[castle.git] / ActiveRecord / Castle.ActiveRecord / Framework / Queries / HqlBasedQuery.cs
blob2c00edaa6c6ed8f86378e9d74c61fcc87940dbff
1 // Copyright 2004-2007 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.Text.RegularExpressions;
21 using Castle.ActiveRecord.Framework;
22 using Castle.ActiveRecord.Queries.Modifiers;
24 using NHibernate;
25 using NHibernate.Type;
27 /// <summary>
28 /// defines the possible query langauges
29 /// </summary>
30 public enum QueryLanguage
32 /// <summary>
33 /// Hibernate Query Language
34 /// </summary>
35 Hql = 0,
36 /// <summary>
37 /// Structured Query Language
38 /// </summary>
39 Sql = 1,
42 /// <summary>
43 /// Base class for all HQL or SQL-based queries.
44 /// </summary>
45 public class HqlBasedQuery : ActiveRecordBaseQuery
47 private QueryLanguage queryLanguage;
48 private String query;
50 /// <summary>
51 /// Initializes a new instance of the <see cref="HqlBasedQuery"/> class.
52 /// </summary>
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)
60 /// <summary>
61 /// Initializes a new instance of the <see cref="HqlBasedQuery"/> class.
62 /// </summary>
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)
71 /// <summary>
72 /// Initializes a new instance of the <see cref="HqlBasedQuery"/> class.
73 /// </summary>
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)
78 : base(targetType)
80 this.query = query;
81 this.queryLanguage = queryLanguage;
84 /// <summary>
85 /// Initializes a new instance of the <see cref="HqlBasedQuery"/> class.
86 /// </summary>
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)
96 int i = 0;
97 foreach (object value in positionalParameters)
99 AddModifier(new QueryParameter(i++, value));
104 /// <summary>
105 /// The query text.
106 /// </summary>
107 public string Query
109 get { return query; }
110 set { query = value; }
113 #region SetParameter and SetParameterList
115 /// <summary>
116 /// Sets a parameter with the given name.
117 /// </summary>
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));
125 /// <summary>
126 /// Sets a parameter with the given name and type
127 /// </summary>
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));
136 /// <summary>
137 /// Sets a parameter with the given name with a list of values
138 /// </summary>
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));
146 /// <summary>
147 /// Sets a parameter with the given name with a list of values and type
148 /// </summary>
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));
157 #endregion
159 #region SetQueryRange
161 /// <summary>
162 /// Sets the query range (paging)
163 /// </summary>
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));
171 /// <summary>
172 /// Sets the query range (maximum number of items returned)
173 /// </summary>
174 /// <param name="maxResults">The maximum number of results.</param>
175 public void SetQueryRange(int maxResults)
177 AddModifier(new QueryRange(maxResults));
180 #endregion
182 #region AddSqlReturnDefinition
184 /// <summary>
185 /// Adds a SQL query return definition.
186 /// See <see cref="NHibernate.ISession.CreateSQLQuery(string,string[],Type[])"/> for more information.
187 /// </summary>
188 public void AddSqlReturnDefinition(Type returnType, string returnAlias)
190 AddModifier(new SqlQueryReturnDefinition(returnType, returnAlias));
193 #endregion
195 /// <summary>
196 /// Creates the <see cref="IQuery"/> instance.
197 /// </summary>
198 /// <param name="session"></param>
199 /// <returns></returns>
200 protected override IQuery CreateQuery(ISession session)
202 IQuery nhibQuery;
204 switch (queryLanguage)
206 case QueryLanguage.Hql:
207 nhibQuery = session.CreateQuery(Query);
208 break;
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;
231 break;
233 default:
234 throw new ActiveRecordException("Query language not supported: " + queryLanguage);
237 ApplyModifiers(nhibQuery);
239 return nhibQuery;