forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQueryKey.cs
187 lines (167 loc) · 4.33 KB
/
QueryKey.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
using System;
using System.Collections;
using System.Text;
using Iesi.Collections;
using NHibernate.Engine;
using NHibernate.Impl;
using NHibernate.SqlCommand;
using NHibernate.Transform;
using NHibernate.Type;
using NHibernate.Util;
namespace NHibernate.Cache
{
[Serializable]
public class QueryKey
{
private readonly ISessionFactoryImplementor factory;
private readonly SqlString sqlQueryString;
private readonly IType[ ] types;
private readonly object[ ] values;
private readonly int firstRow = RowSelection.NoValue;
private readonly int maxRows = RowSelection.NoValue;
private readonly IDictionary namedParameters;
private readonly ISet filters;
private readonly IResultTransformer customTransformer;
private readonly int hashCode;
/// <param name="factory">the sesion factory for this query key, required to get the identifiers of entities that are used as values.</param>
public QueryKey( ISessionFactoryImplementor factory, SqlString queryString, QueryParameters queryParameters, ISet filters )
{
this.factory = factory;
sqlQueryString = queryString;
types = queryParameters.PositionalParameterTypes;
values = queryParameters.PositionalParameterValues;
RowSelection selection = queryParameters.RowSelection;
if( selection != null )
{
firstRow = selection.FirstRow;
maxRows = selection.MaxRows;
}
else
{
firstRow = RowSelection.NoValue;
maxRows = RowSelection.NoValue;
}
namedParameters = queryParameters.NamedParameters;
this.filters = filters;
this.customTransformer = queryParameters.ResultTransformer;
this.hashCode = ComputeHashCode();
}
public override bool Equals( object other )
{
QueryKey that = ( QueryKey ) other;
if( !sqlQueryString.Equals( that.sqlQueryString ) )
{
return false;
}
if( firstRow != that.firstRow
|| maxRows != that.maxRows )
{
return false;
}
if (!Equals(customTransformer, that.customTransformer))
{
return false;
}
if( types == null )
{
if( that.types != null )
{
return false;
}
}
else
{
if( that.types == null )
{
return false;
}
if( types.Length != that.types.Length )
{
return false;
}
for( int i = 0; i < types.Length; i++ )
{
if( !types[ i ].Equals( that.types[ i ] ) )
{
return false;
}
if( !object.Equals( values[ i ], that.values[ i ] ) )
{
return false;
}
}
}
if( !CollectionHelper.DictionaryEquals( namedParameters, that.namedParameters ) )
{
return false;
}
if (!CollectionHelper.SetEquals(filters, that.filters))
{
return false;
}
return true;
}
public override int GetHashCode()
{
return hashCode;
}
public int ComputeHashCode()
{
unchecked
{
int result = 13;
result = 37 * result + firstRow.GetHashCode();
result = 37 * result + maxRows.GetHashCode();
// NH - commented this out, namedParameters don't have a useful GetHashCode implementations
//result = 37 * result
// + ( namedParameters == null ? 0 : namedParameters.GetHashCode() );
for( int i = 0; i < types.Length; i++ )
{
result = 37 * result + ( types[ i ] == null ? 0 : types[ i ].GetHashCode() );
}
for( int i = 0; i < values.Length; i++ )
{
result = 37 * result + ( values[ i ] == null ? 0 : values[ i ].GetHashCode() );
}
if (filters != null)
{
foreach (object filter in filters)
{
result = 37 * result + filter.GetHashCode();
}
}
result = 37 * result + (customTransformer == null ? 0 : customTransformer.GetHashCode());
result = 37 * result + sqlQueryString.GetHashCode();
return result;
}
}
public override string ToString()
{
StringBuilder buf = new StringBuilder()
.Append( "sql: " )
.Append( sqlQueryString );
Printer print = new Printer(factory);
if( values != null )
{
buf
.Append("; parameters: ")
.Append(print.ToString(types, values));
}
if( namedParameters != null )
{
buf
.Append("; named parameters: ")
.Append(print.ToString(namedParameters));
}
if( firstRow != RowSelection.NoValue )
{
buf.Append( "; first row: " ).Append( firstRow );
}
if( maxRows != RowSelection.NoValue )
{
buf.Append( "; max rows: " ).Append( maxRows );
}
return buf.ToString();
}
}
}