PageRenderTime 43ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/mcs/class/System.Data.Linq/src/DbLinq/Util/CacheChecker.cs

https://github.com/ekovalenko-softheme/mono
C# | 111 lines | 83 code | 11 blank | 17 comment | 38 complexity | f57a8117abd21fd261f1c8c7e5e38b82 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, CC-BY-SA-3.0, GPL-2.0, Unlicense
  1. using System;
  2. using System.Reflection;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Linq.Expressions;
  6. using System.Data.Linq.Mapping;
  7. using System.Text;
  8. #if MONO_STRICT
  9. using System.Data.Linq;
  10. using System.Data.Linq.Identity;
  11. #else
  12. using DbLinq.Data.Linq;
  13. using DbLinq.Data.Linq.Identity;
  14. #endif
  15. using DbLinq.Linq;
  16. using DbLinq.Util;
  17. namespace DbLinq.Util
  18. {
  19. internal class CacheChecker
  20. {
  21. /// <summary>
  22. /// Quote from MSDN:
  23. /// If the object requested by the query is easily identifiable as one
  24. /// already retrieved, no query is executed. The identity table acts as a cache
  25. /// of all previously retrieved objects
  26. /// From Matt Warren: http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=345635&SiteID=1
  27. /// The cache is checked when the query is a simple table.Where(pred) or table.First(pred) where the
  28. /// predicate refers only to the primary key. Otherwise the query is always sent and the cache only checked
  29. /// after the results are retrieved.
  30. /// The DLINQ cache is not distributed or shared, it is local and contained within the context. It is only a
  31. /// referential identity cache used to guarantee that two reads of the same entity return the same instance.
  32. /// You are not expected to hold the cache for an extended duration (except possibly for a client scenario),
  33. /// or share it across threads, processes, or machines in a cluster.
  34. /// </summary>
  35. public static bool TryRetrieveFromCache<S>(SessionVarsParsed vars, Expression scalarExpr, out S cachedRow)
  36. {
  37. cachedRow = default(S);
  38. int count1 = vars.ExpressionChain.Count;
  39. MethodCallExpression scalarMethodCall = scalarExpr.XMethodCall();
  40. bool isSingleWhere = false;
  41. LambdaExpression whereLambda = null;
  42. if (count1 == 1 && scalarMethodCall.Arguments.Count == 1)
  43. {
  44. MethodCallExpression call0 = vars.ExpressionChain[0];
  45. if (call0.Method.Name == "Where" && call0.Arguments.Count == 2)
  46. {
  47. //db.Customers.Where(id==1).Single()
  48. isSingleWhere = true;
  49. whereLambda = call0.Arguments[1].XLambda();
  50. }
  51. }
  52. else if (count1 == 0 && scalarMethodCall.Arguments.Count == 2)
  53. {
  54. //db.Customers.Single(id==1)
  55. isSingleWhere = true;
  56. whereLambda = scalarMethodCall.Arguments[1].XLambda();
  57. }
  58. if ((!isSingleWhere) || whereLambda == null)
  59. return false;
  60. if (whereLambda.Parameters.Count != 1 || whereLambda.Parameters[0].NodeType != ExpressionType.Parameter)
  61. return false;
  62. if (whereLambda.Body.NodeType != ExpressionType.Equal)
  63. return false;
  64. BinaryExpression equals = (BinaryExpression)whereLambda.Body;
  65. Expression left = equals.Left;
  66. Expression right = equals.Right;
  67. MemberExpression member;
  68. ConstantExpression consta;
  69. if (left.NodeType == ExpressionType.MemberAccess && right.NodeType == ExpressionType.Constant)
  70. {
  71. member = left.XMember();
  72. consta = right.XConstant();
  73. }
  74. else if (left.NodeType == ExpressionType.Constant && right.NodeType == ExpressionType.Parameter)
  75. {
  76. member = right.XMember();
  77. consta = left.XConstant();
  78. }
  79. else
  80. {
  81. return false;
  82. }
  83. if (member.Expression.NodeType != ExpressionType.Parameter)
  84. return false;
  85. //check that it's a primary key field
  86. ColumnAttribute colAttrib = AttribHelper.GetColumnAttribute(member.Member);
  87. if (colAttrib == null)
  88. return false;
  89. if (!colAttrib.IsPrimaryKey)
  90. return false;
  91. IdentityKey key = new IdentityKey(typeof(S), consta.Value);
  92. DataContext context = vars.Context;
  93. object cachedEntity = context.GetRegisteredEntityByKey(key);
  94. if (cachedEntity == null)
  95. return false;
  96. cachedRow = (S)cachedEntity;
  97. return true;
  98. }
  99. }
  100. }