Fixing an issue with output parameters that are of type IntPtr
[castle.git] / ActiveRecord / Castle.ActiveRecord / Framework / Queries / HqlBasedQuery.cs
blob1d696dbc4e80aa22c12b5bde18ec737f6ed82ee2
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;
20 using Castle.ActiveRecord.Framework;
21 using Castle.ActiveRecord.Queries.Modifiers;
23 using NHibernate;
24 using NHibernate.Type;
25 using NHibernate.Transform;
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 readonly 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 SqlQuery Modifiers
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 /// <summary>
194 /// Adds a SQL query join definition.
195 /// See <see cref="NHibernate.ISession.CreateSQLQuery(string,string[],Type[])"/> for more information.
196 /// </summary>
197 public void AddSqlJoinDefinition(String associationPath, String associationAlias)
199 AddModifier(new SqlQueryJoinDefinition(associationPath, associationAlias));
202 /// <summary>
203 /// Adds a SQL query scalar definition.
204 /// See <see cref="NHibernate.ISession.CreateSQLQuery(string,string[],Type[])"/> for more information.
205 /// </summary>
206 public void AddSqlScalarDefinition(IType scalarType, String columnAlias)
208 AddModifier(new SqlQueryScalarDefinition(scalarType, columnAlias));
211 #endregion
213 #region SetResultTransformer
215 /// <summary>
216 /// Adds a query result transformer.
217 /// See <see cref="IResultTransformer"/> for more information.
218 /// </summary>
219 public void SetResultTransformer(IResultTransformer transformer)
221 AddModifier(new QueryResultTransformer(transformer));
224 #endregion
226 /// <summary>
227 /// Creates the <see cref="IQuery"/> instance.
228 /// </summary>
229 /// <param name="session"></param>
230 /// <returns></returns>
231 protected override IQuery CreateQuery(ISession session)
233 IQuery nhibQuery;
235 switch (queryLanguage)
237 case QueryLanguage.Hql:
238 nhibQuery = session.CreateQuery(Query);
239 break;
241 case QueryLanguage.Sql:
242 nhibQuery = session.CreateSQLQuery(Query);
243 break;
245 default:
246 throw new ActiveRecordException("Query language not supported: " + queryLanguage);
249 ApplyModifiers(nhibQuery);
251 return nhibQuery;