forked from nazarii-piontko/datafusion-sharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataFusionSharpParameterCollection.cs
More file actions
166 lines (133 loc) · 5.4 KB
/
Copy pathDataFusionSharpParameterCollection.cs
File metadata and controls
166 lines (133 loc) · 5.4 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#pragma warning disable CA1010 // Collections should implement generic interface
using System.Collections;
using System.Data.Common;
namespace DataFusionSharp.Data;
/// <summary>
/// A typed, ordered collection of <see cref="DataFusionSharpParameter"/> objects for a <see cref="DataFusionSharpCommand"/>.
/// </summary>
public sealed class DataFusionSharpParameterCollection : DbParameterCollection
{
private readonly List<DataFusionSharpParameter> _items = [];
/// <inheritdoc />
public override int Count => _items.Count;
/// <inheritdoc />
public override object SyncRoot => ((ICollection)_items).SyncRoot;
/// <summary>
/// Gets or sets the parameter at the specified index.
/// </summary>
public new DataFusionSharpParameter this[int index]
{
get => _items[index];
set => _items[index] = value;
}
/// <summary>
/// Gets or sets the parameter with the specified name (prefix-insensitive lookup).
/// </summary>
public new DataFusionSharpParameter this[string parameterName]
{
get => _items[GetIndexByName(parameterName)];
set => _items[GetIndexByName(parameterName)] = value;
}
/// <summary>
/// Adds the given parameter to the collection and returns it.
/// </summary>
public int Add(DataFusionSharpParameter parameter)
{
_items.Add(parameter);
return _items.Count - 1;
}
/// <inheritdoc />
public override int Add(object value)
{
return Add(CastParameter(value));
}
/// <inheritdoc />
public override void AddRange(Array values)
{
ArgumentNullException.ThrowIfNull(values);
foreach (var v in values)
Add(v!);
}
/// <summary>
/// Creates a parameter with the given name and value, adds it to the collection, and returns it.
/// </summary>
public DataFusionSharpParameter AddWithValue(string parameterName, object? value)
{
var p = new DataFusionSharpParameter(parameterName, value);
_items.Add(p);
return p;
}
/// <inheritdoc />
public override void Clear() => _items.Clear();
/// <inheritdoc />
public override bool Contains(object value) => value is DataFusionSharpParameter p && _items.Contains(p);
/// <inheritdoc />
public override bool Contains(string value) => IndexOf(value) >= 0;
/// <inheritdoc />
public override void CopyTo(Array array, int index) => ((ICollection)_items).CopyTo(array, index);
/// <inheritdoc />
public override IEnumerator GetEnumerator() => _items.GetEnumerator();
/// <inheritdoc />
public override int IndexOf(object value) => value is DataFusionSharpParameter p ? _items.IndexOf(p) : -1;
/// <summary>
/// Returns the index of the parameter matching <paramref name="parameterName"/>.
/// Name comparison is case-insensitive and strips any leading prefix characters (<c>@</c>, <c>$</c>, <c>:</c>).
/// </summary>
/// <returns>The index of the parameter with the given name, or -1 if not found.</returns>
public override int IndexOf(string parameterName)
{
ArgumentException.ThrowIfNullOrWhiteSpace(parameterName);
for (var i = 0; i < _items.Count; i++)
{
if (string.Equals(_items[i].ParameterName, parameterName, StringComparison.OrdinalIgnoreCase))
return i;
}
return -1;
}
/// <inheritdoc />
public override void Insert(int index, object value) => _items.Insert(index, CastParameter(value));
/// <inheritdoc />
public override void Remove(object value)
{
if (value is DataFusionSharpParameter p)
_items.Remove(p);
}
/// <inheritdoc />
public override void RemoveAt(int index) => _items.RemoveAt(index);
/// <inheritdoc />
public override void RemoveAt(string parameterName)
{
var i = IndexOf(parameterName);
if (i >= 0)
_items.RemoveAt(i);
}
/// <inheritdoc />
protected override DbParameter GetParameter(int index) => _items[index];
/// <inheritdoc />
protected override DbParameter GetParameter(string parameterName) => this[parameterName];
/// <inheritdoc />
protected override void SetParameter(int index, DbParameter value) => this[index] = CastParameter(value);
/// <inheritdoc />
protected override void SetParameter(string parameterName, DbParameter value) => this[parameterName] = CastParameter(value);
/// <summary>
/// Projects the collection into the <see cref="NamedScalarValueAndMetadata"/> sequence expected by
/// <see cref="SessionContext.SqlAsync(string, System.Collections.Generic.IEnumerable{NamedScalarValueAndMetadata})"/>.
/// </summary>
internal IEnumerable<NamedScalarValueAndMetadata> ToDataFusionParameters()
{
return _items.Select(p => new NamedScalarValueAndMetadata(p.NormalizedName, p.ToScalarValue()));
}
private int GetIndexByName(string parameterName)
{
var i = IndexOf(parameterName);
if (i < 0)
throw new ArgumentException($"Parameter with name '{parameterName}' not found in the collection.", nameof(parameterName));
return i;
}
private static DataFusionSharpParameter CastParameter(object value)
{
if (value is DataFusionSharpParameter p)
return p;
throw new ArgumentException($"Value must be a {nameof(DataFusionSharpParameter)}.", nameof(value));
}
}