|
11 | 11 | using System.Collections.Generic;
|
12 | 12 | using System.Linq;
|
13 | 13 | using System.Reflection;
|
| 14 | +using NHibernate.Criterion; |
14 | 15 | using NHibernate.DomainModel.Northwind.Entities;
|
15 | 16 | using NHibernate.Engine.Query;
|
16 | 17 | using NHibernate.Linq.Visitors;
|
@@ -269,5 +270,62 @@ public async Task PlansWithNonParameterizedConstantsAreNotCachedAsync()
|
269 | 270 | Has.Count.EqualTo(0),
|
270 | 271 | "Query plan should not be cached.");
|
271 | 272 | }
|
| 273 | + |
| 274 | + [Test] |
| 275 | + public async Task PlansWithNonParameterizedConstantsAreNotCachedForExpandedQueryAsync() |
| 276 | + { |
| 277 | + var queryPlanCacheType = typeof(QueryPlanCache); |
| 278 | + |
| 279 | + var cache = (SoftLimitMRUCache) |
| 280 | + queryPlanCacheType |
| 281 | + .GetField("planCache", BindingFlags.Instance | BindingFlags.NonPublic) |
| 282 | + .GetValue(Sfi.QueryPlanCache); |
| 283 | + cache.Clear(); |
| 284 | + |
| 285 | + var ids = new[] {"ANATR", "UNKNOWN"}.ToList(); |
| 286 | + await (db.Customers.Where(x => ids.Contains(x.CustomerId)).Select( |
| 287 | + c => new {c.CustomerId, c.ContactName, Constant = 1}).FirstAsync()); |
| 288 | + |
| 289 | + Assert.That( |
| 290 | + cache, |
| 291 | + Has.Count.EqualTo(0), |
| 292 | + "Query plan should not be cached."); |
| 293 | + } |
| 294 | + |
| 295 | + //GH-2298 - Different Update queries - same query cache plan |
| 296 | + [Test] |
| 297 | + public async Task DmlPlansForExpandedQueryAsync() |
| 298 | + { |
| 299 | + var queryPlanCacheType = typeof(QueryPlanCache); |
| 300 | + |
| 301 | + var cache = (SoftLimitMRUCache) |
| 302 | + queryPlanCacheType |
| 303 | + .GetField("planCache", BindingFlags.Instance | BindingFlags.NonPublic) |
| 304 | + .GetValue(Sfi.QueryPlanCache); |
| 305 | + cache.Clear(); |
| 306 | + |
| 307 | + using (session.BeginTransaction()) |
| 308 | + { |
| 309 | + var list = new[] {"UNKNOWN", "UNKNOWN2"}.ToList(); |
| 310 | + await (db.Customers.Where(x => list.Contains(x.CustomerId)).UpdateAsync( |
| 311 | + x => new Customer |
| 312 | + { |
| 313 | + CompanyName = "Constant1" |
| 314 | + })); |
| 315 | + |
| 316 | + await (db.Customers.Where(x => list.Contains(x.CustomerId)) |
| 317 | + .UpdateAsync( |
| 318 | + x => new Customer |
| 319 | + { |
| 320 | + ContactName = "Constant1" |
| 321 | + })); |
| 322 | + |
| 323 | + Assert.That( |
| 324 | + cache.Count, |
| 325 | + //2 original queries + 2 expanded queries are expected in cache |
| 326 | + Is.EqualTo(0).Or.EqualTo(4), |
| 327 | + "Query plans should either be cached separately or not cached at all."); |
| 328 | + } |
| 329 | + } |
272 | 330 | }
|
273 | 331 | }
|
0 commit comments