@@ -23,6 +23,38 @@ where NHibernate.Linq.SqlMethods.Like(e.FirstName, "Ma%et")
23
23
Assert . That ( query [ 0 ] . FirstName , Is . EqualTo ( "Margaret" ) ) ;
24
24
}
25
25
26
+ [ Test ]
27
+ public void LikeFunctionWithEscapeCharacter ( )
28
+ {
29
+ using ( var tx = session . BeginTransaction ( ) )
30
+ {
31
+ var employeeName = "Mar%aret" ;
32
+ var escapeChar = '\\ ' ;
33
+ var employeeNameEscaped = employeeName . Replace ( "%" , escapeChar + "%" ) ;
34
+
35
+ //This entity will be flushed to the db, but rolled back when the test completes
36
+
37
+ session . Save ( new Employee { FirstName = employeeName , LastName = "" } ) ;
38
+ session . Flush ( ) ;
39
+
40
+
41
+ var query = ( from e in db . Employees
42
+ where NHibernate . Linq . SqlMethods . Like ( e . FirstName , employeeNameEscaped , escapeChar )
43
+ select e ) . ToList ( ) ;
44
+
45
+ Assert . That ( query . Count , Is . EqualTo ( 1 ) ) ;
46
+ Assert . That ( query [ 0 ] . FirstName , Is . EqualTo ( employeeName ) ) ;
47
+
48
+ Assert . Throws < ArgumentException > ( ( ) =>
49
+ {
50
+ ( from e in db . Employees
51
+ where NHibernate . Linq . SqlMethods . Like ( e . FirstName , employeeNameEscaped , e . FirstName . First ( ) )
52
+ select e ) . ToList ( ) ;
53
+ } ) ;
54
+ tx . Rollback ( ) ;
55
+ }
56
+ }
57
+
26
58
private static class SqlMethods
27
59
{
28
60
public static bool Like ( string expression , string pattern )
@@ -48,8 +80,8 @@ where NHibernate.Test.Linq.FunctionTests.SqlMethods.Like(e.FirstName, "Ma%et")
48
80
public void SubstringFunction2 ( )
49
81
{
50
82
var query = ( from e in db . Employees
51
- where e . FirstName . Substring ( 0 , 2 ) == "An"
52
- select e ) . ToList ( ) ;
83
+ where e . FirstName . Substring ( 0 , 2 ) == "An"
84
+ select e ) . ToList ( ) ;
53
85
54
86
Assert . That ( query . Count , Is . EqualTo ( 2 ) ) ;
55
87
}
@@ -58,8 +90,8 @@ where e.FirstName.Substring(0, 2) == "An"
58
90
public void SubstringFunction1 ( )
59
91
{
60
92
var query = ( from e in db . Employees
61
- where e . FirstName . Substring ( 3 ) == "rew"
62
- select e ) . ToList ( ) ;
93
+ where e . FirstName . Substring ( 3 ) == "rew"
94
+ select e ) . ToList ( ) ;
63
95
64
96
Assert . That ( query . Count , Is . EqualTo ( 1 ) ) ;
65
97
Assert . That ( query [ 0 ] . FirstName , Is . EqualTo ( "Andrew" ) ) ;
@@ -83,12 +115,12 @@ public void ReplaceFunction()
83
115
var query = from e in db . Employees
84
116
where e . FirstName . StartsWith ( "An" )
85
117
select new
86
- {
87
- Before = e . FirstName ,
88
- AfterMethod = e . FirstName . Replace ( "An" , "Zan" ) ,
89
- AfterExtension = ExtensionMethods . Replace ( e . FirstName , "An" , "Zan" ) ,
90
- AfterExtension2 = e . FirstName . ReplaceExtension ( "An" , "Zan" )
91
- } ;
118
+ {
119
+ Before = e . FirstName ,
120
+ AfterMethod = e . FirstName . Replace ( "An" , "Zan" ) ,
121
+ AfterExtension = ExtensionMethods . Replace ( e . FirstName , "An" , "Zan" ) ,
122
+ AfterExtension2 = e . FirstName . ReplaceExtension ( "An" , "Zan" )
123
+ } ;
92
124
93
125
var s = ObjectDumper . Write ( query ) ;
94
126
}
@@ -124,7 +156,7 @@ public void IndexOfFunctionProjection()
124
156
{
125
157
if ( ! TestDialect . SupportsLocate )
126
158
Assert . Ignore ( "Locate function not supported." ) ;
127
-
159
+
128
160
var query = from e in db . Employees
129
161
where e . FirstName . Contains ( "a" )
130
162
select e . FirstName . IndexOf ( 'A' , 3 ) ;
@@ -139,7 +171,7 @@ public void TwoFunctionExpression()
139
171
Assert . Ignore ( "Locate function not supported." ) ;
140
172
141
173
var query = from e in db . Employees
142
- where e . FirstName . IndexOf ( "A" ) == e . BirthDate . Value . Month
174
+ where e . FirstName . IndexOf ( "A" ) == e . BirthDate . Value . Month
143
175
select e . FirstName ;
144
176
145
177
ObjectDumper . Write ( query ) ;
@@ -176,9 +208,9 @@ public void Trim()
176
208
{
177
209
using ( session . BeginTransaction ( ) )
178
210
{
179
- AnotherEntity ae1 = new AnotherEntity { Input = " hi " } ;
180
- AnotherEntity ae2 = new AnotherEntity { Input = "hi" } ;
181
- AnotherEntity ae3 = new AnotherEntity { Input = "heh" } ;
211
+ AnotherEntity ae1 = new AnotherEntity { Input = " hi " } ;
212
+ AnotherEntity ae2 = new AnotherEntity { Input = "hi" } ;
213
+ AnotherEntity ae3 = new AnotherEntity { Input = "heh" } ;
182
214
session . Save ( ae1 ) ;
183
215
session . Save ( ae2 ) ;
184
216
session . Save ( ae3 ) ;
@@ -201,9 +233,9 @@ public void TrimTrailingWhitespace()
201
233
{
202
234
using ( session . BeginTransaction ( ) )
203
235
{
204
- session . Save ( new AnotherEntity { Input = " hi " } ) ;
205
- session . Save ( new AnotherEntity { Input = "hi" } ) ;
206
- session . Save ( new AnotherEntity { Input = "heh" } ) ;
236
+ session . Save ( new AnotherEntity { Input = " hi " } ) ;
237
+ session . Save ( new AnotherEntity { Input = "hi" } ) ;
238
+ session . Save ( new AnotherEntity { Input = "heh" } ) ;
207
239
session . Flush ( ) ;
208
240
209
241
Assert . AreEqual ( TestDialect . IgnoresTrailingWhitespace ? 2 : 1 , session . Query < AnotherEntity > ( ) . Where ( e => e . Input . TrimStart ( ) == "hi " ) . Count ( ) ) ;
@@ -256,7 +288,7 @@ public void WhereBoolConstantEqual()
256
288
var query = from item in db . Role
257
289
where item . IsActive . Equals ( true )
258
290
select item ;
259
-
291
+
260
292
ObjectDumper . Write ( query ) ;
261
293
}
262
294
@@ -266,7 +298,7 @@ public void WhereBoolParameterEqual()
266
298
var query = from item in db . Role
267
299
where item . IsActive . Equals ( 1 == 1 )
268
300
select item ;
269
-
301
+
270
302
ObjectDumper . Write ( query ) ;
271
303
}
272
304
@@ -286,8 +318,8 @@ where item.IsActive.Equals(f())
286
318
public void WhereLongEqual ( )
287
319
{
288
320
var query = from item in db . PatientRecords
289
- where item . Id . Equals ( - 1 )
290
- select item ;
321
+ where item . Id . Equals ( - 1 )
322
+ select item ;
291
323
292
324
ObjectDumper . Write ( query ) ;
293
325
}
@@ -301,7 +333,7 @@ where item.RegisteredAt.Equals(DateTime.Today)
301
333
302
334
ObjectDumper . Write ( query ) ;
303
335
}
304
-
336
+
305
337
[ Test ]
306
338
public void WhereGuidEqual ( )
307
339
{
@@ -310,7 +342,7 @@ where item.Reference.Equals(Guid.Empty)
310
342
select item ;
311
343
312
344
ObjectDumper . Write ( query ) ;
313
- }
345
+ }
314
346
315
347
[ Test ]
316
348
public void WhereDoubleEqual ( )
@@ -320,8 +352,8 @@ where item.BodyWeight.Equals(-1)
320
352
select item ;
321
353
322
354
ObjectDumper . Write ( query ) ;
323
- }
324
-
355
+ }
356
+
325
357
[ Test ]
326
358
public void WhereFloatEqual ( )
327
359
{
@@ -330,7 +362,7 @@ where item.Float.Equals(-1)
330
362
select item ;
331
363
332
364
ObjectDumper . Write ( query ) ;
333
- }
365
+ }
334
366
335
367
[ Test ]
336
368
public void WhereCharEqual ( )
@@ -362,4 +394,4 @@ where item.Discount.Equals(-1)
362
394
ObjectDumper . Write ( query ) ;
363
395
}
364
396
}
365
- }
397
+ }
0 commit comments