Repository navigation
Expand file tree
/
Copy pathSqlMapper.Identity.cs
More file actions
121 lines (106 loc) · 5.12 KB
/
Copy pathSqlMapper.Identity.cs
File metadata and controls
121 lines (106 loc) · 5.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
using System;
using System.Data;
namespace Dapper
{
public static partial class SqlMapper
{
/// <summary>
/// Identity of a cached query in Dapper, used for extensibility.
/// </summary>
public class Identity : IEquatable<Identity>
{
internal Identity ForGrid(Type primaryType, int gridIndex) =>
new Identity(sql, commandType, connectionString, primaryType, parametersType, null, gridIndex);
internal Identity ForGrid(Type primaryType, Type[] otherTypes, int gridIndex) =>
new Identity(sql, commandType, connectionString, primaryType, parametersType, otherTypes, gridIndex);
/// <summary>
/// Create an identity for use with DynamicParameters, internal use only.
/// </summary>
/// <param name="type">The parameters type to create an <see cref="Identity"/> for.</param>
/// <returns></returns>
public Identity ForDynamicParameters(Type type) =>
new Identity(sql, commandType, connectionString, this.type, type, null, -1);
internal Identity(string sql, CommandType? commandType, IDbConnection connection, Type type, Type parametersType, Type[] otherTypes)
: this(sql, commandType, connection.ConnectionString, type, parametersType, otherTypes, 0) { /* base call */ }
private Identity(string sql, CommandType? commandType, string connectionString, Type type, Type parametersType, Type[] otherTypes, int gridIndex)
{
this.sql = sql;
this.commandType = commandType;
this.connectionString = connectionString;
this.type = type;
this.parametersType = parametersType;
this.gridIndex = gridIndex;
unchecked
{
hashCode = 17; // we *know* we are using this in a dictionary, so pre-compute this
hashCode = (hashCode * 23) + commandType.GetHashCode();
hashCode = (hashCode * 23) + gridIndex.GetHashCode();
hashCode = (hashCode * 23) + (sql?.GetHashCode() ?? 0);
hashCode = (hashCode * 23) + (type?.GetHashCode() ?? 0);
if (otherTypes != null)
{
foreach (var t in otherTypes)
{
hashCode = (hashCode * 23) + (t?.GetHashCode() ?? 0);
}
}
hashCode = (hashCode * 23) + (connectionString == null ? 0 : connectionStringComparer.GetHashCode(connectionString));
hashCode = (hashCode * 23) + (parametersType?.GetHashCode() ?? 0);
}
}
/// <summary>
/// Whether this <see cref="Identity"/> equals another.
/// </summary>
/// <param name="obj">The other <see cref="object"/> to compare to.</param>
public override bool Equals(object obj) => Equals(obj as Identity);
/// <summary>
/// The raw SQL command.
/// </summary>
public readonly string sql;
/// <summary>
/// The SQL command type.
/// </summary>
public readonly CommandType? commandType;
/// <summary>
/// The hash code of this Identity.
/// </summary>
public readonly int hashCode;
/// <summary>
/// The grid index (position in the reader) of this Identity.
/// </summary>
public readonly int gridIndex;
/// <summary>
/// This <see cref="Type"/> of this Identity.
/// </summary>
public readonly Type type;
/// <summary>
/// The connection string for this Identity.
/// </summary>
public readonly string connectionString;
/// <summary>
/// The type of the parameters object for this Identity.
/// </summary>
public readonly Type parametersType;
/// <summary>
/// Gets the hash code for this identity.
/// </summary>
/// <returns></returns>
public override int GetHashCode() => hashCode;
/// <summary>
/// Compare 2 Identity objects
/// </summary>
/// <param name="other">The other <see cref="Identity"/> object to compare.</param>
/// <returns>Whether the two are equal</returns>
public bool Equals(Identity other)
{
return other != null
&& gridIndex == other.gridIndex
&& type == other.type
&& sql == other.sql
&& commandType == other.commandType
&& connectionStringComparer.Equals(connectionString, other.connectionString)
&& parametersType == other.parametersType;
}
}
}
}