1
1
using System . Collections . Generic ;
2
-
2
+ using System . Linq ;
3
3
using NHibernate . Engine ;
4
4
using NHibernate . SqlTypes ;
5
5
using NHibernate . Type ;
@@ -20,9 +20,7 @@ public class SqlUpdateBuilder : SqlBaseBuilder, ISqlStringBuilder
20
20
// columns-> (ColumnName, Value) or (ColumnName, SqlType) for parametrized column
21
21
private readonly LinkedHashMap < string , object > columns = new LinkedHashMap < string , object > ( ) ;
22
22
23
- private SqlString join = null ;
24
-
25
- private List < SqlString > whereStrings = new List < SqlString > ( ) ;
23
+ private List < SqlString > whereStrings = new List < SqlString > ( ) ;
26
24
private readonly List < SqlType > whereParameterTypes = new List < SqlType > ( ) ;
27
25
private SqlString assignments ;
28
26
@@ -130,25 +128,30 @@ public SqlUpdateBuilder AppendAssignmentFragment(SqlString fragment)
130
128
return this ;
131
129
}
132
130
133
- public SqlUpdateBuilder SetJoin ( string joinTableName , string [ ] lhsColumnNames , string [ ] rhsColumnNames )
131
+ public SqlUpdateBuilder SetJoin ( string joinTableName , string [ ] keyColumnNames , IType identityType , string [ ] lhsColumnNames , string [ ] rhsColumnNames )
134
132
{
135
- SqlStringBuilder joinStringBuilder = new SqlStringBuilder ( ) ;
133
+ var sqlBuilder = new SqlStringBuilder ( )
134
+ . Add ( "EXISTS (SELECT * FROM " )
135
+ . Add ( joinTableName )
136
+ . Add ( " WHERE " )
137
+ . Add ( ToWhereString ( joinTableName , keyColumnNames ) ) ;
136
138
137
- joinStringBuilder . Add ( " INNER JOIN " ) ;
138
- joinStringBuilder . Add ( joinTableName ) ;
139
- joinStringBuilder . Add ( " ON " ) ;
140
- bool andNeeded = false ;
141
139
for ( int columnIndex = 0 ; columnIndex < lhsColumnNames . Length ; columnIndex ++ )
142
140
{
143
- if ( andNeeded )
144
- {
145
- joinStringBuilder . Add ( " AND " ) ;
146
- }
147
- joinStringBuilder . Add ( tableName + StringHelper . Dot + lhsColumnNames [ columnIndex ] + " = " + joinTableName + StringHelper . Dot + rhsColumnNames [ columnIndex ] ) ;
148
- andNeeded = true ;
141
+ sqlBuilder . Add ( " AND " )
142
+ . Add ( tableName )
143
+ . Add ( StringHelper . Dot . ToString ( ) )
144
+ . Add ( lhsColumnNames [ columnIndex ] )
145
+ . Add ( "=" )
146
+ . Add ( joinTableName )
147
+ . Add ( StringHelper . Dot . ToString ( ) )
148
+ . Add ( rhsColumnNames [ columnIndex ] ) ;
149
149
}
150
+ sqlBuilder . Add ( ")" ) ;
151
+
152
+ whereStrings . Add ( sqlBuilder . ToSqlString ( ) ) ;
153
+ whereParameterTypes . AddRange ( identityType . SqlTypes ( Mapping ) ) ;
150
154
151
- join = joinStringBuilder . ToSqlString ( ) ;
152
155
return this ;
153
156
}
154
157
@@ -174,20 +177,6 @@ public SqlUpdateBuilder SetIdentityColumn(string[] columnNames, IType identityTy
174
177
return this ;
175
178
}
176
179
177
- /// <summary>
178
- /// Sets the IdentityColumn for the <c>UPDATE</c> sql to use.
179
- /// </summary>
180
- /// <param name="tableName">Table name to prepend to the columns.</param>
181
- /// <param name="columnNames">An array of the column names for the Property</param>
182
- /// <param name="identityType">The IType of the Identity Property.</param>
183
- /// <returns>The SqlUpdateBuilder.</returns>
184
- public SqlUpdateBuilder SetIdentityColumn ( string tableName , string [ ] columnNames , IType identityType )
185
- {
186
- whereStrings . Add ( ToWhereString ( tableName , columnNames ) ) ;
187
- whereParameterTypes . AddRange ( identityType . SqlTypes ( Mapping ) ) ;
188
- return this ;
189
- }
190
-
191
180
/// <summary>
192
181
/// Sets the VersionColumn for the <c>UPDATE</c> sql to use.
193
182
/// </summary>
@@ -271,19 +260,14 @@ public SqlString ToSqlString()
271
260
initialCapacity += ( columns . Count - 1 ) + ( columns . Count * 3 ) ;
272
261
}
273
262
274
- // 3 - JOIN
275
- if ( join != null )
276
- initialCapacity += 3 ;
277
-
278
263
// 1 = "WHERE"
279
264
initialCapacity ++ ;
280
265
281
266
// the "AND" before all but the first whereString
282
267
if ( whereStrings . Count > 0 )
283
268
{
284
269
initialCapacity += ( whereStrings . Count - 1 ) ;
285
- foreach ( SqlString whereString in whereStrings )
286
- initialCapacity += whereString . Count ;
270
+ initialCapacity += whereStrings . Sum ( x => x . Count ) ;
287
271
}
288
272
289
273
if ( ! string . IsNullOrEmpty ( comment ) )
@@ -325,12 +309,6 @@ public SqlString ToSqlString()
325
309
sqlBuilder . Add ( assignments ) ;
326
310
}
327
311
328
- if ( join != null )
329
- {
330
- sqlBuilder . Add ( " FROM " ) ;
331
- sqlBuilder . Add ( tableName ) ;
332
- sqlBuilder . Add ( join ) ;
333
- }
334
312
335
313
sqlBuilder . Add ( " WHERE " ) ;
336
314
bool andNeeded = false ;
0 commit comments