电子药箱通讯服务端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Identity.cs 3.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace RDH.Data
  8. {
  9. public class Identity : IEquatable<Identity>
  10. {
  11. internal Identity ForGrid(Type primaryType, int gridIndex)
  12. {
  13. return new Identity(sql, commandType, connectionString, primaryType, parametersType, null, gridIndex);
  14. }
  15. internal Identity ForGrid(Type primaryType, Type[] otherTypes, int gridIndex)
  16. {
  17. return new Identity(sql, commandType, connectionString, primaryType, parametersType, otherTypes, gridIndex);
  18. }
  19. public Identity ForDynamicParameters(Type type)
  20. {
  21. return new Identity(sql, commandType, connectionString, this.type, type, null, -1);
  22. }
  23. internal Identity(string sql, CommandType? commandType, IDbConnection connection, Type type, Type parametersType, Type[] otherTypes)
  24. : this(sql, commandType, connection.ConnectionString, type, parametersType, otherTypes, 0)
  25. { }
  26. private Identity(string sql, CommandType? commandType, string connectionString, Type type, Type parametersType, Type[] otherTypes, int gridIndex)
  27. {
  28. this.sql = sql;
  29. this.commandType = commandType;
  30. this.connectionString = connectionString;
  31. this.type = type;
  32. this.parametersType = parametersType;
  33. this.gridIndex = gridIndex;
  34. unchecked
  35. {
  36. hashCode = 17; // we *know* we are using this in a dictionary, so pre-compute this
  37. hashCode = hashCode * 23 + commandType.GetHashCode();
  38. hashCode = hashCode * 23 + gridIndex.GetHashCode();
  39. hashCode = hashCode * 23 + (sql == null ? 0 : sql.GetHashCode());
  40. hashCode = hashCode * 23 + (type == null ? 0 : type.GetHashCode());
  41. if (otherTypes != null)
  42. {
  43. foreach (var t in otherTypes)
  44. {
  45. hashCode = hashCode * 23 + (t == null ? 0 : t.GetHashCode());
  46. }
  47. }
  48. hashCode = hashCode * 23 + (connectionString == null ? 0 : connectionString.GetHashCode());
  49. hashCode = hashCode * 23 + (parametersType == null ? 0 : parametersType.GetHashCode());
  50. }
  51. }
  52. public override bool Equals(object obj)
  53. {
  54. return Equals(obj as Identity);
  55. }
  56. public readonly string sql;
  57. public readonly CommandType? commandType;
  58. public readonly int hashCode, gridIndex;
  59. private readonly Type type;
  60. public readonly string connectionString;
  61. public readonly Type parametersType;
  62. public override int GetHashCode()
  63. {
  64. return hashCode;
  65. }
  66. public bool Equals(Identity other)
  67. {
  68. return
  69. other != null &&
  70. gridIndex == other.gridIndex &&
  71. type == other.type &&
  72. sql == other.sql &&
  73. commandType == other.commandType &&
  74. connectionString == other.connectionString &&
  75. parametersType == other.parametersType;
  76. }
  77. }
  78. }