forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDetachedNamedQuery.cs
160 lines (139 loc) · 3.74 KB
/
DetachedNamedQuery.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
using System;
using NHibernate.Engine;
namespace NHibernate.Impl
{
/// <summary>
/// Named query in "detached mode" where the NHibernate session is not available.
/// </summary>
/// <seealso cref="AbstractDetachedQuery"/>
/// <seealso cref="IDetachedQuery"/>
/// <seealso cref="IQuery"/>
/// <seealso cref="ISession.GetNamedQuery(string)"/>
[Serializable]
public class DetachedNamedQuery : AbstractDetachedQuery
{
private readonly string queryName;
private bool cacheableWasSet;
private bool cacheModeWasSet;
private bool cacheRegionWasSet;
private bool readOnlyWasSet;
private bool timeoutWasSet;
private bool fetchSizeWasSet;
private bool commentWasSet;
private bool flushModeWasSet;
/// <summary>
/// Create a new instance of <see cref="DetachedNamedQuery"/> for a named query string defined in the mapping file.
/// </summary>
/// <param name="queryName">The name of a query defined externally.</param>
/// <remarks>
/// The query can be either in HQL or SQL format.
/// </remarks>
public DetachedNamedQuery(string queryName)
{
this.queryName = queryName;
}
/// <summary>
/// Get the query name.
/// </summary>
public string QueryName
{
get { return queryName; }
}
/// <summary>
/// Get an executable instance of <see cref="IQuery"/>, to actually run the query.
/// </summary>
public override IQuery GetExecutableQuery(ISession session)
{
IQuery result = session.GetNamedQuery(queryName);
SetDefaultProperties((ISessionFactoryImplementor)session.SessionFactory);
SetQueryProperties(result);
return result;
}
private void SetDefaultProperties(ISessionFactoryImplementor factory)
{
NamedQueryDefinition nqd = factory.GetNamedQuery(queryName) ?? factory.GetNamedSQLQuery(queryName);
if (!cacheableWasSet)
{
cacheable = nqd.IsCacheable;
}
if (!cacheRegionWasSet)
{
cacheRegion = nqd.CacheRegion;
}
if(!timeoutWasSet && nqd.Timeout != -1)
{
selection.Timeout= nqd.Timeout;
}
if (!fetchSizeWasSet && nqd.FetchSize != -1)
{
selection.FetchSize = nqd.FetchSize;
}
if (!cacheModeWasSet && nqd.CacheMode.HasValue)
{
cacheMode = nqd.CacheMode.Value;
}
if (!readOnlyWasSet)
{
readOnly = nqd.IsReadOnly;
}
if (!commentWasSet && nqd.Comment != null)
{
comment = nqd.Comment;
}
if(!flushModeWasSet)
{
flushMode = nqd.FlushMode;
}
}
/// <summary>
/// Creates a new DetachedNamedQuery that is a deep copy of the current instance.
/// </summary>
/// <returns>The clone.</returns>
public DetachedNamedQuery Clone()
{
var result = new DetachedNamedQuery(queryName);
CopyTo(result);
return result;
}
public override IDetachedQuery SetCacheable(bool cacheable)
{
cacheableWasSet = true;
return base.SetCacheable(cacheable);
}
public override IDetachedQuery SetCacheMode(CacheMode cacheMode)
{
cacheModeWasSet = true;
return base.SetCacheMode(cacheMode);
}
public override IDetachedQuery SetCacheRegion(string cacheRegion)
{
cacheRegionWasSet = true;
return base.SetCacheRegion(cacheRegion);
}
public override IDetachedQuery SetReadOnly(bool readOnly)
{
readOnlyWasSet = true;
return base.SetReadOnly(readOnly);
}
public override IDetachedQuery SetTimeout(int timeout)
{
timeoutWasSet = true;
return base.SetTimeout(timeout);
}
public override IDetachedQuery SetFetchSize(int fetchSize)
{
fetchSizeWasSet = true;
return base.SetFetchSize(fetchSize);
}
public override IDetachedQuery SetComment(string comment)
{
commentWasSet = true;
return base.SetComment(comment);
}
public override IDetachedQuery SetFlushMode(FlushMode flushMode)
{
flushModeWasSet = true;
return base.SetFlushMode(flushMode);
}
}
}