@@ -74,188 +74,95 @@ public void OrderByPropertiesImplicitlySpecifiedInTheSelect()
74
74
{
75
75
s . CreateQuery ( "select distinct z from Animal a join a.zoo as z order by z.name" ) . List ( ) ;
76
76
}
77
- }
77
+ }
78
78
79
79
[ Test ]
80
80
public void CaseClauseInSelect ( )
81
81
{
82
82
// NH-322
83
- using ( ISession s = OpenSession ( ) )
84
- using ( s . BeginTransaction ( ) )
85
- {
86
- s . Save ( new Animal { BodyWeight = 12 , Description = "Polliwog" } ) ;
87
- s . Transaction . Commit ( ) ;
88
- }
89
-
90
- using ( ISession s = OpenSession ( ) )
83
+ using ( var s = OpenSession ( ) )
91
84
{
92
- var l = s . CreateQuery ( "select a.id, case when a.description = 'Polliwog' then 2 else 0 end from Animal a" ) . List ( ) ;
85
+ var l = s
86
+ . CreateQuery ( "select a.id, case when a.description = 'Polliwog' then 2 else 0 end from Animal a where a.id = :id" )
87
+ . SetInt64 ( "id" , _polliwog . Id )
88
+ . List ( ) ;
93
89
var element = ( IList ) l [ 0 ] ;
94
90
Assert . That ( element [ 1 ] , Is . EqualTo ( 2 ) ) ;
95
91
96
92
// work with alias
97
- l = s . CreateQuery ( "select a.id, case when a.description = 'Polliwog' then 2 else 0 end as value from Animal a" ) . List ( ) ;
93
+ l = s
94
+ . CreateQuery ( "select a.id, case when a.description = 'Polliwog' then 2 else 0 end as value from Animal a where a.id = :id" )
95
+ . SetInt64 ( "id" , _polliwog . Id )
96
+ . List ( ) ;
98
97
element = ( IList ) l [ 0 ] ;
99
98
Assert . That ( element [ 1 ] , Is . EqualTo ( 2 ) ) ;
100
99
}
101
-
102
- using ( ISession s = OpenSession ( ) )
103
- using ( s . BeginTransaction ( ) )
104
- {
105
- s . CreateQuery ( "delete from Animal" ) . ExecuteUpdate ( ) ;
106
- s . Transaction . Commit ( ) ;
107
- }
108
100
}
109
101
110
102
[ Test ]
111
103
public void MultipleParametersInCaseStatement ( )
112
104
{
113
- using ( ISession s = OpenSession ( ) )
114
- using ( s . BeginTransaction ( ) )
115
- {
116
- s . Save ( new Animal { BodyWeight = 12 , Description = "Polliwog" } ) ;
117
- s . Transaction . Commit ( ) ;
118
- }
119
-
120
- try
121
- {
122
- using ( ISession s = OpenSession ( ) )
123
- {
124
- var result = s . CreateQuery ( "select case when 'b' = ? then 2 when 'b' = ? then 1 else 0 end from Animal a" )
125
- . SetParameter ( 0 , "a" )
126
- . SetParameter ( 1 , "b" )
127
- . SetMaxResults ( 1 )
128
- . UniqueResult ( ) ;
129
- Assert . AreEqual ( 1 , result ) ;
130
- }
131
- }
132
- finally
105
+ using ( var s = OpenSession ( ) )
133
106
{
134
- using ( ISession s = OpenSession ( ) )
135
- using ( s . BeginTransaction ( ) )
136
- {
137
- s . CreateQuery ( "delete from Animal" ) . ExecuteUpdate ( ) ;
138
- s . Transaction . Commit ( ) ;
139
- }
107
+ var result = s . CreateQuery ( "select case when 'b' = ? then 2 when 'b' = ? then 1 else 0 end from Animal a" )
108
+ . SetParameter ( 0 , "a" )
109
+ . SetParameter ( 1 , "b" )
110
+ . SetMaxResults ( 1 )
111
+ . UniqueResult ( ) ;
112
+ Assert . AreEqual ( 1 , result ) ;
140
113
}
141
114
}
142
115
143
116
[ Test ]
144
117
public void ParameterInCaseThenClause ( )
145
118
{
146
- using ( ISession s = OpenSession ( ) )
147
- using ( s . BeginTransaction ( ) )
148
- {
149
- s . Save ( new Animal { BodyWeight = 12 , Description = "Polliwog" } ) ;
150
- s . Transaction . Commit ( ) ;
151
- }
152
-
153
- try
154
- {
155
- using ( ISession s = OpenSession ( ) )
156
- {
157
- var result = s . CreateQuery ( "select case when 2=2 then ? else 0 end from Animal a" )
158
- . SetParameter ( 0 , 1 )
159
- . UniqueResult ( ) ;
160
- Assert . AreEqual ( 1 , result ) ;
161
- }
162
- }
163
- finally
119
+ using ( var s = OpenSession ( ) )
164
120
{
165
- using ( ISession s = OpenSession ( ) )
166
- using ( s . BeginTransaction ( ) )
167
- {
168
- s . CreateQuery ( "delete from Animal" ) . ExecuteUpdate ( ) ;
169
- s . Transaction . Commit ( ) ;
170
- }
121
+ var result = s . CreateQuery ( "select case when 2=2 then ? else 0 end from Animal a" )
122
+ . SetParameter ( 0 , 1 )
123
+ . SetMaxResults ( 1 )
124
+ . UniqueResult ( ) ;
125
+ Assert . AreEqual ( 1 , result ) ;
171
126
}
172
127
}
173
128
174
129
[ Test ]
175
130
public void ParameterInCaseThenAndElseClausesWithCast ( )
176
131
{
177
- using ( ISession s = OpenSession ( ) )
178
- using ( s . BeginTransaction ( ) )
179
- {
180
- s . Save ( new Animal { BodyWeight = 12 , Description = "Polliwog" } ) ;
181
- s . Transaction . Commit ( ) ;
182
- }
183
-
184
- try
185
- {
186
- using ( ISession s = OpenSession ( ) )
187
- {
188
- var result = s . CreateQuery ( "select case when 2=2 then cast(? as integer) else ? end from Animal a" )
189
- . SetParameter ( 0 , 1 )
190
- . SetParameter ( 1 , 0 )
191
- . UniqueResult ( ) ;
192
- Assert . AreEqual ( 1 , result ) ;
193
- }
194
- }
195
- finally
132
+ using ( var s = OpenSession ( ) )
196
133
{
197
- using ( ISession s = OpenSession ( ) )
198
- using ( s . BeginTransaction ( ) )
199
- {
200
- s . CreateQuery ( "delete from Animal" ) . ExecuteUpdate ( ) ;
201
- s . Transaction . Commit ( ) ;
202
- }
134
+ var result = s . CreateQuery ( "select case when 2=2 then cast(? as integer) else ? end from Animal a" )
135
+ . SetParameter ( 0 , 1 )
136
+ . SetParameter ( 1 , 0 )
137
+ . SetMaxResults ( 1 )
138
+ . UniqueResult ( ) ;
139
+ Assert . AreEqual ( 1 , result ) ;
203
140
}
204
141
}
205
142
206
143
[ Test ]
207
144
public void SubselectAddition ( )
208
145
{
209
- using ( ISession s = OpenSession ( ) )
210
- using ( s . BeginTransaction ( ) )
211
- {
212
- s . Save ( new Animal { BodyWeight = 12 , Description = "Polliwog" } ) ;
213
- s . Transaction . Commit ( ) ;
214
- }
146
+ if ( ! Dialect . SupportsScalarSubSelects )
147
+ Assert . Ignore ( "Dialect does not support scalar sub-select." ) ;
215
148
216
- try
217
- {
218
- using ( ISession s = OpenSession ( ) )
219
- {
220
- var result = s . CreateQuery ( "select count(a) from Animal a where (select count(a2) from Animal a2) + 1 > 1" )
221
- . UniqueResult ( ) ;
222
- Assert . AreEqual ( 1 , result ) ;
223
- }
224
- }
225
- finally
149
+ using ( var s = OpenSession ( ) )
226
150
{
227
- using ( ISession s = OpenSession ( ) )
228
- using ( s . BeginTransaction ( ) )
229
- {
230
- s . CreateQuery ( "delete from Animal" ) . ExecuteUpdate ( ) ;
231
- s . Transaction . Commit ( ) ;
232
- }
151
+ var result = s . CreateQuery ( "select count(a) from Animal a where (select count(a2) from Animal a2) + 1 > 1" )
152
+ . UniqueResult ( ) ;
153
+ Assert . AreEqual ( 4 , result ) ;
233
154
}
234
155
}
235
156
236
157
[ Test , Ignore ( "Not fixed yet." ) ]
237
158
public void SumShouldReturnDouble ( )
238
159
{
239
160
// NH-1734
240
- using ( ISession s = OpenSession ( ) )
241
- using ( s . BeginTransaction ( ) )
242
- {
243
- s . Save ( new Human { IntValue = 11 , BodyWeight = 12.5f , Description = "Polliwog" } ) ;
244
- s . Transaction . Commit ( ) ;
245
- }
246
-
247
- using ( ISession s = OpenSession ( ) )
161
+ using ( var s = OpenSession ( ) )
248
162
{
249
163
var l = s . CreateQuery ( "select sum(a.intValue * a.bodyWeight) from Animal a group by a.id" ) . List ( ) ;
250
164
Assert . That ( l [ 0 ] , Is . InstanceOf < Double > ( ) ) ;
251
165
}
252
-
253
- using ( ISession s = OpenSession ( ) )
254
- using ( s . BeginTransaction ( ) )
255
- {
256
- s . CreateQuery ( "delete from Animal" ) . ExecuteUpdate ( ) ;
257
- s . Transaction . Commit ( ) ;
258
- }
259
166
}
260
167
261
168
[ Test ]
@@ -290,47 +197,92 @@ public void InvalidJoinOnProperty()
290
197
[ Test ]
291
198
public void InsertIntoFromSelect_WithSelectClauseParameters ( )
292
199
{
200
+ CheckSupportOfBulkInsertionWithGeneratedId < Animal > ( ) ;
201
+
293
202
using ( ISession s = OpenSession ( ) )
294
203
{
295
204
using ( s . BeginTransaction ( ) )
296
205
{
297
- // arrange
298
- s . Save ( new Animal ( ) { Description = "cat1" , BodyWeight = 2.1f } ) ;
299
- s . Save ( new Animal ( ) { Description = "cat2" , BodyWeight = 2.5f } ) ;
300
- s . Save ( new Animal ( ) { Description = "cat3" , BodyWeight = 2.7f } ) ;
301
-
302
- // act
303
206
s . CreateQuery ( "insert into Animal (description, bodyWeight) select a.description, :weight from Animal a where a.bodyWeight < :weight" )
304
- . SetParameter < float > ( "weight" , 5.7f ) . ExecuteUpdate ( ) ;
207
+ . SetParameter ( "weight" , 5.7f ) . ExecuteUpdate ( ) ;
305
208
306
209
// assert
307
- Assert . AreEqual ( 3 , s . CreateCriteria < Animal > ( ) . SetProjection ( Projections . RowCount ( ) )
308
- . Add ( Restrictions . Gt ( "bodyWeight" , 5.5f ) ) . UniqueResult < int > ( ) ) ;
309
-
310
- s . CreateQuery ( "delete from Animal" ) . ExecuteUpdate ( ) ;
210
+ Assert . AreEqual ( 3 , s
211
+ . CreateCriteria < Animal > ( )
212
+ . SetProjection ( Projections . RowCount ( ) )
213
+ . Add ( Restrictions . Gt ( "bodyWeight" , 5.5f ) )
214
+ . Add ( Restrictions . Lt ( "bodyWeight" , 6f ) )
215
+ . UniqueResult < int > ( ) ) ;
311
216
s . Transaction . Commit ( ) ;
312
217
}
313
218
}
314
219
}
315
220
316
-
317
221
[ Test ]
318
222
public void UnaryMinusBeforeParenthesesHandledCorrectly ( )
319
223
{
320
- using ( ISession s = OpenSession ( ) )
321
- using ( ITransaction txn = s . BeginTransaction ( ) )
224
+ using ( var s = OpenSession ( ) )
225
+ using ( var txn = s . BeginTransaction ( ) )
322
226
{
323
- s . Save ( new Animal { Description = "cat1" , BodyWeight = 1 } ) ;
324
-
325
227
// NH-2290: Unary minus before parentheses wasn't handled correctly (this query returned 0).
326
- int actual = s . CreateQuery ( "select -(1+1) from Animal as h" )
327
- . List < int > ( ) . Single ( ) ;
228
+ var actual = s . CreateQuery ( "select -(1+1) from Animal as h" )
229
+ . List < int > ( ) . First ( ) ;
328
230
Assert . That ( actual , Is . EqualTo ( - 2 ) ) ;
329
231
330
232
// This was the workaround, which of course should still work.
331
- int actualWorkaround = s . CreateQuery ( "select -1*(1+1) from Animal as h" )
332
- . List < int > ( ) . Single ( ) ;
233
+ var actualWorkaround = s . CreateQuery ( "select -1*(1+1) from Animal as h" )
234
+ . List < int > ( ) . First ( ) ;
333
235
Assert . That ( actualWorkaround , Is . EqualTo ( - 2 ) ) ;
236
+ txn . Commit ( ) ;
237
+ }
238
+ }
239
+
240
+ private void CheckSupportOfBulkInsertionWithGeneratedId < T > ( )
241
+ {
242
+ // Make sure the env supports bulk inserts with generated ids...
243
+ var persister = Sfi . GetEntityPersister ( typeof ( T ) . FullName ) ;
244
+ var generator = persister . IdentifierGenerator ;
245
+ if ( ! HqlSqlWalker . SupportsIdGenWithBulkInsertion ( generator ) )
246
+ {
247
+ Assert . Ignore ( $ "Identifier generator { generator . GetType ( ) . Name } for entity { typeof ( T ) . FullName } does not support bulk insertions.") ;
248
+ }
249
+ }
250
+
251
+ private Animal _polliwog ;
252
+
253
+ protected override void OnSetUp ( )
254
+ {
255
+ using ( var s = OpenSession ( ) )
256
+ using ( var txn = s . BeginTransaction ( ) )
257
+ {
258
+ _polliwog = new Animal { BodyWeight = 12 , Description = "Polliwog" } ;
259
+ s . Save ( _polliwog ) ;
260
+
261
+ s . Save ( new Animal { Description = "cat1" , BodyWeight = 2.1f } ) ;
262
+ s . Save ( new Animal { Description = "cat2" , BodyWeight = 2.5f } ) ;
263
+ s . Save ( new Animal { Description = "cat3" , BodyWeight = 2.7f } ) ;
264
+
265
+ txn . Commit ( ) ;
266
+ }
267
+ }
268
+
269
+ protected override void OnTearDown ( )
270
+ {
271
+ if ( ! Dialect . SupportsTemporaryTables )
272
+ {
273
+ // Give-up usual cleanup due to TPC: cannot perform multi-table deletes using dialect not supporting temp tables
274
+ DropSchema ( ) ;
275
+ CreateSchema ( ) ;
276
+ return ;
277
+ }
278
+
279
+ using ( var s = OpenSession ( ) )
280
+ using ( var txn = s . BeginTransaction ( ) )
281
+ {
282
+ s . CreateQuery ( "delete from Animal" ) . ExecuteUpdate ( ) ;
283
+
284
+ txn . Commit ( ) ;
285
+ s . Close ( ) ;
334
286
}
335
287
}
336
288
}
0 commit comments