|
1 | 1 | using System.Collections.Generic;
|
2 | 2 | using System.Linq;
|
3 | 3 | using System.Reflection;
|
| 4 | +using NHibernate.Criterion; |
4 | 5 | using NHibernate.DomainModel.Northwind.Entities;
|
5 | 6 | using NHibernate.Engine.Query;
|
| 7 | +using NHibernate.Linq; |
6 | 8 | using NHibernate.Linq.Visitors;
|
7 | 9 | using NHibernate.Util;
|
8 | 10 | using NUnit.Framework;
|
@@ -276,5 +278,62 @@ public void PlansWithNonParameterizedConstantsAreNotCached()
|
276 | 278 | Has.Count.EqualTo(0),
|
277 | 279 | "Query plan should not be cached.");
|
278 | 280 | }
|
| 281 | + |
| 282 | + [Test] |
| 283 | + public void PlansWithNonParameterizedConstantsAreNotCachedForExpandedQuery() |
| 284 | + { |
| 285 | + var queryPlanCacheType = typeof(QueryPlanCache); |
| 286 | + |
| 287 | + var cache = (SoftLimitMRUCache) |
| 288 | + queryPlanCacheType |
| 289 | + .GetField("planCache", BindingFlags.Instance | BindingFlags.NonPublic) |
| 290 | + .GetValue(Sfi.QueryPlanCache); |
| 291 | + cache.Clear(); |
| 292 | + |
| 293 | + var ids = new[] {"ANATR", "UNKNOWN"}.ToList(); |
| 294 | + db.Customers.Where(x => ids.Contains(x.CustomerId)).Select( |
| 295 | + c => new {c.CustomerId, c.ContactName, Constant = 1}).First(); |
| 296 | + |
| 297 | + Assert.That( |
| 298 | + cache, |
| 299 | + Has.Count.EqualTo(0), |
| 300 | + "Query plan should not be cached."); |
| 301 | + } |
| 302 | + |
| 303 | + //GH-2298 - Different Update queries - same query cache plan |
| 304 | + [Test] |
| 305 | + public void DmlPlansForExpandedQuery() |
| 306 | + { |
| 307 | + var queryPlanCacheType = typeof(QueryPlanCache); |
| 308 | + |
| 309 | + var cache = (SoftLimitMRUCache) |
| 310 | + queryPlanCacheType |
| 311 | + .GetField("planCache", BindingFlags.Instance | BindingFlags.NonPublic) |
| 312 | + .GetValue(Sfi.QueryPlanCache); |
| 313 | + cache.Clear(); |
| 314 | + |
| 315 | + using (session.BeginTransaction()) |
| 316 | + { |
| 317 | + var list = new[] {"UNKNOWN", "UNKNOWN2"}.ToList(); |
| 318 | + db.Customers.Where(x => list.Contains(x.CustomerId)).Update( |
| 319 | + x => new Customer |
| 320 | + { |
| 321 | + CompanyName = "Constant1" |
| 322 | + }); |
| 323 | + |
| 324 | + db.Customers.Where(x => list.Contains(x.CustomerId)) |
| 325 | + .Update( |
| 326 | + x => new Customer |
| 327 | + { |
| 328 | + ContactName = "Constant1" |
| 329 | + }); |
| 330 | + |
| 331 | + Assert.That( |
| 332 | + cache.Count, |
| 333 | + //2 original queries + 2 expanded queries are expected in cache |
| 334 | + Is.EqualTo(0).Or.EqualTo(4), |
| 335 | + "Query plans should either be cached separately or not cached at all."); |
| 336 | + } |
| 337 | + } |
279 | 338 | }
|
280 | 339 | }
|
0 commit comments