-
Notifications
You must be signed in to change notification settings - Fork 934
/
Copy pathQueryParameters.cs
232 lines (190 loc) · 8.82 KB
/
QueryParameters.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using NHibernate.Impl;
using NHibernate.Param;
using NHibernate.SqlCommand;
using NHibernate.Transform;
using NHibernate.Type;
namespace NHibernate.Engine
{
/// <summary>
/// Container for data that is used during the NHibernate query/load process.
/// </summary>
[Serializable]
public sealed class QueryParameters
{
private static readonly INHibernateLogger log = NHibernateLogger.For(typeof (QueryParameters));
private bool readOnly;
public QueryParameters() : this(TypeHelper.EmptyTypeArray, Array.Empty<object>()) {}
public QueryParameters(IType[] positionalParameterTypes, object[] postionalParameterValues, object optionalObject, string optionalEntityName, object optionalObjectId)
: this(positionalParameterTypes, postionalParameterValues)
{
OptionalObject = optionalObject;
OptionalId = optionalObjectId;
OptionalEntityName = optionalEntityName;
}
public QueryParameters(IType[] positionalParameterTypes, object[] postionalParameterValues)
: this(positionalParameterTypes, postionalParameterValues, null, null, false, false, false, null, null, false, null) {}
public QueryParameters(IType[] positionalParameterTypes, object[] postionalParameterValues, object[] collectionKeys)
: this(positionalParameterTypes, postionalParameterValues, null, collectionKeys) {}
public QueryParameters(IType[] positionalParameterTypes, object[] postionalParameterValues, IDictionary<string, TypedValue> namedParameters, object[] collectionKeys)
: this(positionalParameterTypes, postionalParameterValues, namedParameters, null, null, false, false, false, null, null, collectionKeys, null) {}
public QueryParameters(IType[] positionalParameterTypes, object[] positionalParameterValues, IDictionary<string, LockMode> lockModes, RowSelection rowSelection,
bool isReadOnlyInitialized, bool readOnly, bool cacheable, string cacheRegion, string comment, bool isLookupByNaturalKey, IResultTransformer transformer)
: this(positionalParameterTypes, positionalParameterValues, null, lockModes, rowSelection, isReadOnlyInitialized, readOnly, cacheable, cacheRegion, comment, null, transformer)
{
NaturalKeyLookup = isLookupByNaturalKey;
}
public QueryParameters(IDictionary<string, TypedValue> namedParameters, IDictionary<string, LockMode> lockModes, RowSelection rowSelection, bool isReadOnlyInitialized,
bool readOnly, bool cacheable, string cacheRegion, string comment, bool isLookupByNaturalKey, IResultTransformer transformer)
: this(
TypeHelper.EmptyTypeArray, Array.Empty<object>(), namedParameters, lockModes, rowSelection, isReadOnlyInitialized, readOnly, cacheable, cacheRegion, comment, null,
transformer)
{
// used by CriteriaTranslator
NaturalKeyLookup = isLookupByNaturalKey;
}
public QueryParameters(IType[] positionalParameterTypes, object[] positionalParameterValues, IDictionary<string, TypedValue> namedParameters,
IDictionary<string, LockMode> lockModes, RowSelection rowSelection, bool isReadOnlyInitialized, bool readOnly, bool cacheable, string cacheRegion,
string comment, object[] collectionKeys, IResultTransformer transformer)
{
PositionalParameterTypes = positionalParameterTypes ?? Array.Empty<IType>();
PositionalParameterValues = positionalParameterValues ?? Array.Empty<object>();
NamedParameters = namedParameters ?? new Dictionary<string, TypedValue>(1);
LockModes = lockModes;
RowSelection = rowSelection;
Cacheable = cacheable;
CacheRegion = cacheRegion;
Comment = comment;
CollectionKeys = collectionKeys;
IsReadOnlyInitialized = isReadOnlyInitialized;
this.readOnly = readOnly;
ResultTransformer = transformer;
}
public QueryParameters(IType[] positionalParameterTypes, object[] positionalParameterValues, IDictionary<string, TypedValue> namedParameters,
IDictionary<string, LockMode> lockModes, RowSelection rowSelection, bool isReadOnlyInitialized, bool readOnly, bool cacheable, string cacheRegion,
string comment, object[] collectionKeys, object optionalObject, string optionalEntityName, object optionalId, IResultTransformer transformer)
: this(
positionalParameterTypes, positionalParameterValues, namedParameters, lockModes, rowSelection, isReadOnlyInitialized, readOnly, cacheable, cacheRegion, comment, collectionKeys,
transformer)
{
OptionalEntityName = optionalEntityName;
OptionalId = optionalId;
OptionalObject = optionalObject;
}
public bool HasRowSelection
{
get { return RowSelection != null; }
}
public IDictionary<string, TypedValue> NamedParameters { get; internal set; }
/// <summary>
/// Gets or sets an array of <see cref="IType"/> objects that is stored at the index
/// of the Parameter.
/// </summary>
public IType[] PositionalParameterTypes { get; set; }
/// <summary>
/// Gets or sets an array of <see cref="object"/> objects that is stored at the index
/// of the Parameter.
/// </summary>
public object[] PositionalParameterValues { get; set; }
/// <summary>
/// Gets or sets the <see cref="RowSelection"/> for the Query.
/// </summary>
public RowSelection RowSelection { get; set; }
/// <summary>
/// Gets or sets an <see cref="IDictionary"/> that contains the alias name of the
/// object from hql as the key and the <see cref="LockMode"/> as the value.
/// </summary>
/// <value>An <see cref="IDictionary"/> of lock modes.</value>
public IDictionary<string, LockMode> LockModes { get; set; }
public bool IsReadOnlyInitialized { get; private set; }
public bool Cacheable { get; set; }
public string CacheRegion { get; set; }
public CacheMode? CacheMode { get; set; }
public string Comment { get; set; }
public bool ForceCacheRefresh { get; set; }
public string OptionalEntityName { get; set; }
public object OptionalId { get; set; }
public object OptionalObject { get; set; }
public object[] CollectionKeys { get; set; }
public bool Callable { get; set; }
public bool ReadOnly
{
get
{
if (!IsReadOnlyInitialized)
{
throw new InvalidOperationException("cannot call ReadOnly when IsReadOnlyInitialized returns false");
}
return readOnly;
}
set
{
readOnly = value;
IsReadOnlyInitialized = true;
}
}
public SqlString ProcessedSql { get; internal set; }
public IEnumerable<IParameterSpecification> ProcessedSqlParameters { get; internal set; }
public RowSelection ProcessedRowSelection { get; internal set; }
public bool NaturalKeyLookup { get; set; }
public IResultTransformer ResultTransformer { get; private set; }
public bool HasAutoDiscoverScalarTypes { get; set; }
public void LogParameters(ISessionFactoryImplementor factory)
{
var print = new Printer(factory);
if (PositionalParameterValues.Length != 0)
{
log.Debug("parameters: {0}", print.ToString(PositionalParameterTypes, PositionalParameterValues));
}
if (NamedParameters != null)
{
log.Debug("named parameters: {0}", print.ToString(NamedParameters));
}
}
/// <summary>
/// Ensure the Types and Values are the same length.
/// </summary>
/// <exception cref="QueryException">
/// If the Lengths of <see cref="PositionalParameterTypes"/> and
/// <see cref="PositionalParameterValues"/> are not equal.
/// </exception>
public void ValidateParameters()
{
int typesLength = PositionalParameterTypes == null ? 0 : PositionalParameterTypes.Length;
int valuesLength = PositionalParameterValues == null ? 0 : PositionalParameterValues.Length;
if (typesLength != valuesLength)
{
throw new QueryException("Number of positional parameter types (" + typesLength
+ ") does not match number of positional parameter values (" + valuesLength + ")");
}
}
public QueryParameters CreateCopyUsing(RowSelection selection)
{
var copy = new QueryParameters(
PositionalParameterTypes, PositionalParameterValues, NamedParameters, LockModes, selection,
IsReadOnlyInitialized, readOnly, Cacheable, CacheRegion, Comment, CollectionKeys, OptionalObject,
OptionalEntityName, OptionalId, ResultTransformer)
{
ProcessedSql = ProcessedSql,
ProcessedSqlParameters = ProcessedSqlParameters != null ? ProcessedSqlParameters.ToList() : null,
CacheMode = CacheMode
};
return copy;
}
public bool IsReadOnly(ISessionImplementor session)
{
return IsReadOnlyInitialized ? ReadOnly : session.PersistenceContext.DefaultReadOnly;
}
public bool CanGetFromCache(ISessionImplementor session)
{
return !ForceCacheRefresh && (CacheMode ?? session.CacheMode).HasFlag(NHibernate.CacheMode.Get);
}
public bool CanPutToCache(ISessionImplementor session)
{
return (CacheMode ?? session.CacheMode).HasFlag(NHibernate.CacheMode.Put);
}
}
}