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
.Modifiers
18 using System
.Collections
;
21 using NHibernate
.Type
;
24 /// Represents a query parameter.
26 public class QueryParameter
: IQueryModifier
28 private readonly String name
;
29 private readonly int position
= -1;
30 private readonly Object
value;
31 private readonly IType type
;
32 private readonly ParameterFlags flags
;
34 #region Constructors for Named Parameters
36 /// Initializes a new instance of the <see cref="QueryParameter"/> class.
38 /// <param name="name">The name.</param>
39 /// <param name="value">The value.</param>
40 public QueryParameter(string name
, object value)
43 throw new ArgumentNullException("name");
45 flags
= ParameterFlags
.Named
;
51 /// Initializes a new instance of the <see cref="QueryParameter"/> class.
53 /// <param name="name">The name.</param>
54 /// <param name="value">The value.</param>
55 /// <param name="type">The type.</param>
56 public QueryParameter(String name
, Object
value, IType type
)
63 #region Constructors for Positional Parameters
65 /// Initializes a new instance of the <see cref="QueryParameter"/> class.
67 /// <param name="position">The position.</param>
68 /// <param name="value">The value.</param>
69 public QueryParameter(int position
, object value)
72 throw new ArgumentException("Position must be equal or greater than 0", "position");
74 flags
= ParameterFlags
.Positional
;
75 this.position
= position
;
80 /// Initializes a new instance of the <see cref="QueryParameter"/> class.
82 /// <param name="position">The position.</param>
83 /// <param name="value">The value.</param>
84 /// <param name="type">The type.</param>
85 public QueryParameter(int position
, object value, IType type
)
86 : this(position
, value)
92 #region Constructors for Named List Parameters
94 /// Initializes a new instance of the <see cref="QueryParameter"/> class.
96 /// <param name="name">The name.</param>
97 /// <param name="value">The value.</param>
98 /// <param name="type">The type.</param>
99 public QueryParameter(String name
, ICollection
value, IType type
)
100 : this(name
, (object) value, type
)
102 flags
|= ParameterFlags
.List
;
106 /// Initializes a new instance of the <see cref="QueryParameter"/> class.
108 /// <param name="name">The name.</param>
109 /// <param name="value">The value.</param>
110 public QueryParameter(String name
, ICollection
value)
111 : this(name
, (object) value)
113 flags
|= ParameterFlags
.List
;
117 #region Constructors for Positional List Parameters (throws exceptions)
119 /// It is important to keep this constructor as is, to avoid
120 /// confusion with the <see cref="QueryParameter(int, object, IType)"/>
123 public QueryParameter(int position
, ICollection
value, IType type
)
124 : this(position
, (object) value, type
)
126 throw new InvalidOperationException("Parameter lists can not be positional");
130 /// It is important to keep this constructor as is, to avoid
131 /// confusion with the <see cref="QueryParameter(int, object)"/>
134 public QueryParameter(int position
, ICollection
value)
135 : this(position
, (object) value)
137 throw new InvalidOperationException("Parameter lists can not be positional");
141 #region Public Read-Only Properties
143 /// The position of the positional parameter, or <c>-1</c>
144 /// if this is a named parameter.
148 get { return position; }
152 /// The name of the named parameter, or <c>null</c>
153 /// if this is a positional parameter.
161 /// The parameter value.
165 get { return value; }
169 /// The NHibernate type.
177 #region "Apply" method
179 /// Add this parameter to the <paramref name="query"/>.
181 /// <param name="query">The query</param>
183 /// Is there a cleaner way to do this, without reflection or complex
186 public void Apply(IQuery query
)
188 if (IsFlagged(ParameterFlags
.Named
))
190 if (IsFlagged(ParameterFlags
.List
))
193 query
.SetParameterList(Name
, (ICollection
) Value
, Type
);
195 query
.SetParameterList(Name
, (ICollection
) Value
);
200 query
.SetParameter(Name
, Value
, Type
);
202 query
.SetParameter(Name
, Value
);
205 else if (IsFlagged(ParameterFlags
.Positional
))
207 if (IsFlagged(ParameterFlags
.List
))
209 throw new InvalidOperationException("Parameter lists can not be positional");
214 query
.SetParameter(Position
, Value
, Type
);
216 query
.SetParameter(Position
, Value
);
223 private bool IsFlagged(ParameterFlags flag
)
225 return ((flags
& flag
) == flag
);
229 private enum ParameterFlags