-
Notifications
You must be signed in to change notification settings - Fork 0
/
SqlMapper.DapperTable.cs
49 lines (41 loc) · 1.89 KB
/
SqlMapper.DapperTable.cs
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
using System;
using System.Collections.Generic;
namespace Dapper
{
public static partial class SqlMapper
{
private sealed class DapperTable
{
private string[] fieldNames;
private readonly Dictionary<string, int> fieldNameLookup;
internal string[] FieldNames => fieldNames;
public DapperTable(string[] fieldNames)
{
this.fieldNames = fieldNames ?? throw new ArgumentNullException(nameof(fieldNames));
fieldNameLookup = new Dictionary<string, int>(fieldNames.Length, StringComparer.Ordinal);
// if there are dups, we want the **first** key to be the "winner" - so iterate backwards
for (int i = fieldNames.Length - 1; i >= 0; i--)
{
string key = fieldNames[i];
if (key != null) fieldNameLookup[key] = i;
}
}
internal int IndexOfName(string name)
{
return (name != null && fieldNameLookup.TryGetValue(name, out int result)) ? result : -1;
}
internal int AddField(string name)
{
if (name == null) throw new ArgumentNullException(nameof(name));
if (fieldNameLookup.ContainsKey(name)) throw new InvalidOperationException("Field already exists: " + name);
int oldLen = fieldNames.Length;
Array.Resize(ref fieldNames, oldLen + 1); // yes, this is sub-optimal, but this is not the expected common case
fieldNames[oldLen] = name;
fieldNameLookup[name] = oldLen;
return oldLen;
}
internal bool FieldExists(string key) => key != null && fieldNameLookup.ContainsKey(key);
public int FieldCount => fieldNames.Length;
}
}
}