1
1
2
+ using System ;
2
3
using NHibernate . Engine ;
3
4
using NHibernate . Type ;
4
5
using NHibernate . Util ;
@@ -21,10 +22,9 @@ public class SqlSelectBuilder : SqlBaseBuilder, ISqlStringBuilder
21
22
private SqlString groupByClause ;
22
23
private SqlString havingClause ;
23
24
private LockMode lockMode ;
24
- private string aliasToLock ;
25
+ private string mainTableAlias ;
25
26
private string comment ;
26
- private bool hasOuterJoin = false ;
27
-
27
+
28
28
public SqlSelectBuilder ( ISessionFactoryImplementor factory )
29
29
: base ( factory . Dialect , factory ) { }
30
30
@@ -42,7 +42,6 @@ public SqlSelectBuilder SetComment(string comment)
42
42
public SqlSelectBuilder SetFromClause ( string fromClause )
43
43
{
44
44
this . fromClause = fromClause ;
45
- hasOuterJoin = hasOuterJoin || fromClause . ToLowerInvariant ( ) . Contains ( "left outer join" ) ;
46
45
return this ;
47
46
}
48
47
@@ -65,8 +64,7 @@ public SqlSelectBuilder SetFromClause(string tableName, string alias)
65
64
/// <returns>The SqlSelectBuilder</returns>
66
65
public SqlSelectBuilder SetFromClause ( SqlString fromClause )
67
66
{
68
- // it is safe to do this because a fromClause will have no
69
- // parameters
67
+ // it is safe to do this because a fromClause will have no parameters
70
68
return SetFromClause ( fromClause . ToString ( ) ) ;
71
69
}
72
70
@@ -113,9 +111,6 @@ public SqlSelectBuilder SetOuterJoins(SqlString outerJoinsAfterFrom, SqlString o
113
111
}
114
112
115
113
this . outerJoinsAfterWhere = tmpOuterJoinsAfterWhere ;
116
-
117
- hasOuterJoin = hasOuterJoin ||
118
- outerJoinsAfterFrom != null && outerJoinsAfterFrom . ToString ( ) . ToLowerInvariant ( ) . Contains ( "outer join" ) ;
119
114
return this ;
120
115
}
121
116
@@ -187,10 +182,18 @@ public SqlSelectBuilder SetHavingClause(SqlString havingSqlString)
187
182
return this ;
188
183
}
189
184
190
- public SqlSelectBuilder SetLockMode ( LockMode lockMode , string alias )
185
+ [ Obsolete ( "For some DBMS's such as PostgreSQL, a lock on query with OUTER JOIN is not possible without specifying the not-null side. " +
186
+ "Use the new method SetLockMode(LockMode, mainTableAlias) instead." ) ]
187
+ public SqlSelectBuilder SetLockMode ( LockMode lockMode )
188
+ {
189
+ this . lockMode = lockMode ;
190
+ return this ;
191
+ }
192
+
193
+ public SqlSelectBuilder SetLockMode ( LockMode lockMode , string mainTableAlias )
191
194
{
192
195
this . lockMode = lockMode ;
193
- aliasToLock = alias ;
196
+ this . mainTableAlias = mainTableAlias ;
194
197
return this ;
195
198
}
196
199
@@ -300,17 +303,23 @@ public SqlString ToSqlString()
300
303
301
304
private string GetForUpdateString ( )
302
305
{
303
- if ( ! Dialect . SupportsOuterJoinForUpdate && hasOuterJoin )
304
- {
305
- if ( Equals ( lockMode , LockMode . Upgrade ) )
306
- return Dialect . GetForUpdateString ( aliasToLock ) ;
307
- if ( Equals ( lockMode , LockMode . UpgradeNoWait ) )
308
- return Dialect . GetForUpdateNowaitString ( aliasToLock ) ;
309
- }
306
+ if ( Dialect . SupportsOuterJoinForUpdate || ! HasOuterJoin ( ) )
307
+ return Dialect . GetForUpdateString ( lockMode ) ;
308
+
309
+ if ( Equals ( lockMode , LockMode . Upgrade ) )
310
+ return Dialect . GetForUpdateString ( mainTableAlias ) ;
311
+ if ( Equals ( lockMode , LockMode . UpgradeNoWait ) )
312
+ return Dialect . GetForUpdateNowaitString ( mainTableAlias ) ;
310
313
311
314
return Dialect . GetForUpdateString ( lockMode ) ;
312
- }
313
315
316
+ bool HasOuterJoin ( )
317
+ {
318
+ return
319
+ StringHelper . ContainsCaseInsensitive ( fromClause , "outer join" ) ||
320
+ ! string . IsNullOrWhiteSpace ( outerJoinsAfterFrom ? . ToString ( ) ) ;
321
+ }
322
+ }
314
323
#endregion
315
324
}
316
325
}
0 commit comments