-
Notifications
You must be signed in to change notification settings - Fork 874
Expand file tree
/
Copy pathNpgsqlParameterCollection.cs
More file actions
812 lines (677 loc) · 31.2 KB
/
NpgsqlParameterCollection.cs
File metadata and controls
812 lines (677 loc) · 31.2 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using NpgsqlTypes;
namespace Npgsql;
/// <summary>
/// Represents a collection of parameters relevant to a <see cref="NpgsqlCommand"/> as well as their respective mappings to columns in
/// a <see cref="DataSet"/>.
/// </summary>
public sealed class NpgsqlParameterCollection : DbParameterCollection, IList<NpgsqlParameter>
{
internal const int LookupThreshold = 5;
internal List<NpgsqlParameter> InternalList { get; } = new(5);
#if DEBUG
internal static bool TwoPassCompatMode;
#else
internal static readonly bool TwoPassCompatMode;
#endif
static NpgsqlParameterCollection()
=> TwoPassCompatMode = AppContext.TryGetSwitch("Npgsql.EnableLegacyCaseInsensitiveDbParameters", out var enabled)
&& enabled;
// Dictionary lookups for GetValue to improve performance. _caseSensitiveLookup is only ever used in legacy two-pass mode.
Dictionary<string, int>? _caseInsensitiveLookup;
Dictionary<string, int>? _caseSensitiveLookup;
/// <summary>
/// Initializes a new instance of the NpgsqlParameterCollection class.
/// </summary>
internal NpgsqlParameterCollection() { }
bool LookupEnabled => InternalList.Count >= LookupThreshold;
void LookupClear()
{
_caseInsensitiveLookup?.Clear();
_caseSensitiveLookup?.Clear();
}
void LookupAdd(string name, int index)
{
if (_caseInsensitiveLookup is null)
return;
if (TwoPassCompatMode)
_caseSensitiveLookup!.TryAdd(name, index);
_caseInsensitiveLookup.TryAdd(name, index);
}
void LookupInsert(string name, int index)
{
if (_caseInsensitiveLookup is null)
return;
if (TwoPassCompatMode &&
(!_caseSensitiveLookup!.TryGetValue(name, out var indexCs) || index < indexCs))
{
for (var i = index + 1; i < InternalList.Count; i++)
{
var parameterName = InternalList[i].TrimmedName;
if (_caseSensitiveLookup.TryGetValue(parameterName, out var currentI) && currentI + 1 == i)
_caseSensitiveLookup[parameterName] = i;
}
_caseSensitiveLookup[name] = index;
}
if (!_caseInsensitiveLookup.TryGetValue(name, out var indexCi) || index < indexCi)
{
for (var i = index + 1; i < InternalList.Count; i++)
{
var parameterName = InternalList[i].TrimmedName;
if (_caseInsensitiveLookup.TryGetValue(parameterName, out var currentI) && currentI + 1 == i)
_caseInsensitiveLookup[parameterName] = i;
}
_caseInsensitiveLookup[name] = index;
}
}
void LookupRemove(string name, int index)
{
if (_caseInsensitiveLookup is null)
return;
if (TwoPassCompatMode && _caseSensitiveLookup!.Remove(name))
{
for (var i = index; i < InternalList.Count; i++)
{
var parameterName = InternalList[i].TrimmedName;
if (_caseSensitiveLookup.TryGetValue(parameterName, out var currentI) && currentI - 1 == i)
_caseSensitiveLookup[parameterName] = i;
}
}
if (_caseInsensitiveLookup.Remove(name))
{
for (var i = index; i < InternalList.Count; i++)
{
var parameterName = InternalList[i].TrimmedName;
if (_caseInsensitiveLookup.TryGetValue(parameterName, out var currentI) && currentI - 1 == i)
_caseInsensitiveLookup[parameterName] = i;
}
// Fix-up the case-insensitive lookup to point to the next match, if any.
for (var i = 0; i < InternalList.Count; i++)
{
var value = InternalList[i];
if (string.Equals(name, value.TrimmedName, StringComparison.OrdinalIgnoreCase))
{
_caseInsensitiveLookup[value.TrimmedName] = i;
break;
}
}
}
}
void LookupChangeName(NpgsqlParameter parameter, string oldName, string oldTrimmedName, int index)
{
if (string.Equals(oldTrimmedName, parameter.TrimmedName, StringComparison.OrdinalIgnoreCase))
return;
if (oldName.Length != 0)
LookupRemove(oldTrimmedName, index);
if (!parameter.IsPositional)
LookupAdd(parameter.TrimmedName, index);
}
internal void ChangeParameterName(NpgsqlParameter parameter, string? value)
{
var oldName = parameter.ParameterName;
var oldTrimmedName = parameter.TrimmedName;
parameter.ChangeParameterName(value);
if (_caseInsensitiveLookup is null)
return;
var index = IndexOf(parameter);
if (index == -1) // This would be weird.
return;
LookupChangeName(parameter, oldName, oldTrimmedName, index);
}
#region NpgsqlParameterCollection Member
/// <summary>
/// Gets the <see cref="NpgsqlParameter"/> with the specified name.
/// </summary>
/// <param name="parameterName">The name of the <see cref="NpgsqlParameter"/> to retrieve.</param>
/// <value>
/// The <see cref="NpgsqlParameter"/> with the specified name, or a <see langword="null"/> reference if the parameter is not found.
/// </value>
public new NpgsqlParameter this[string parameterName]
{
get
{
ArgumentNullException.ThrowIfNull(parameterName);
var index = IndexOf(parameterName);
if (index == -1)
ThrowHelper.ThrowArgumentException("Parameter not found");
return InternalList[index];
}
set
{
ArgumentNullException.ThrowIfNull(parameterName);
ArgumentNullException.ThrowIfNull(value);
var index = IndexOf(parameterName);
if (index == -1)
ThrowHelper.ThrowArgumentException("Parameter not found");
if (!string.Equals(parameterName, value.TrimmedName, StringComparison.OrdinalIgnoreCase))
ThrowHelper.ThrowArgumentException("Parameter name must be a case-insensitive match with the property 'ParameterName' on the given NpgsqlParameter", nameof(parameterName));
var oldValue = InternalList[index];
LookupChangeName(value, oldValue.ParameterName, oldValue.TrimmedName, index);
InternalList[index] = value;
}
}
/// <summary>
/// Gets the <see cref="NpgsqlParameter"/> at the specified index.
/// </summary>
/// <param name="index">The zero-based index of the <see cref="NpgsqlParameter"/> to retrieve.</param>
/// <value>The <see cref="NpgsqlParameter"/> at the specified index.</value>
public new NpgsqlParameter this[int index]
{
get => InternalList[index];
set
{
ArgumentNullException.ThrowIfNull(value);
if (value.Collection is not null)
ThrowHelper.ThrowInvalidOperationException("The parameter already belongs to a collection");
var oldValue = InternalList[index];
if (ReferenceEquals(oldValue, value))
return;
LookupChangeName(value, oldValue.ParameterName, oldValue.TrimmedName, index);
InternalList[index] = value;
value.Collection = this;
oldValue.Collection = null;
}
}
/// <summary>
/// Adds the specified <see cref="NpgsqlParameter"/> object to the <see cref="NpgsqlParameterCollection"/>.
/// </summary>
/// <param name="value">The <see cref="NpgsqlParameter"/> to add to the collection.</param>
/// <returns>The parameter that was added.</returns>
public NpgsqlParameter Add(NpgsqlParameter value)
{
ArgumentNullException.ThrowIfNull(value);
if (value.Collection is not null)
ThrowHelper.ThrowInvalidOperationException("The parameter already belongs to a collection");
InternalList.Add(value);
value.Collection = this;
if (!value.IsPositional)
LookupAdd(value.TrimmedName, InternalList.Count - 1);
return value;
}
/// <inheritdoc />
void ICollection<NpgsqlParameter>.Add(NpgsqlParameter item)
=> Add(item);
/// <summary>
/// Adds a <see cref="NpgsqlParameter"/> to the <see cref="NpgsqlParameterCollection"/> given the specified parameter name and
/// value.
/// </summary>
/// <param name="parameterName">The name of the <see cref="NpgsqlParameter"/>.</param>
/// <param name="value">The value of the <see cref="NpgsqlParameter"/> to add to the collection.</param>
/// <returns>The parameter that was added.</returns>
public NpgsqlParameter AddWithValue(string parameterName, object value)
=> Add(new NpgsqlParameter(parameterName, value));
/// <summary>
/// Adds a <see cref="NpgsqlParameter"/> to the <see cref="NpgsqlParameterCollection"/> given the specified parameter name,
/// data type and value.
/// </summary>
/// <param name="parameterName">The name of the <see cref="NpgsqlParameter"/>.</param>
/// <param name="parameterType">One of the NpgsqlDbType values.</param>
/// <param name="value">The value of the <see cref="NpgsqlParameter"/> to add to the collection.</param>
/// <returns>The parameter that was added.</returns>
public NpgsqlParameter AddWithValue(string parameterName, NpgsqlDbType parameterType, object value)
=> Add(new NpgsqlParameter(parameterName, parameterType) { Value = value });
/// <summary>
/// Adds a <see cref="NpgsqlParameter"/> to the <see cref="NpgsqlParameterCollection"/> given the specified parameter name and
/// value.
/// </summary>
/// <param name="parameterName">The name of the <see cref="NpgsqlParameter"/>.</param>
/// <param name="value">The value of the <see cref="NpgsqlParameter"/> to add to the collection.</param>
/// <param name="parameterType">One of the <see cref="NpgsqlDbType"/> values.</param>
/// <param name="size">The length of the column.</param>
/// <returns>The parameter that was added.</returns>
public NpgsqlParameter AddWithValue(string parameterName, NpgsqlDbType parameterType, int size, object value)
=> Add(new NpgsqlParameter(parameterName, parameterType, size) { Value = value });
/// <summary>
/// Adds a <see cref="NpgsqlParameter"/> to the <see cref="NpgsqlParameterCollection"/> given the specified parameter name and
/// value.
/// </summary>
/// <param name="parameterName">The name of the <see cref="NpgsqlParameter"/>.</param>
/// <param name="value">The value of the <see cref="NpgsqlParameter"/> to add to the collection.</param>
/// <param name="parameterType">One of the <see cref="NpgsqlDbType"/> values.</param>
/// <param name="size">The length of the column.</param>
/// <param name="sourceColumn">The name of the source column.</param>
/// <returns>The parameter that was added.</returns>
public NpgsqlParameter AddWithValue(string parameterName, NpgsqlDbType parameterType, int size, string? sourceColumn, object value)
=> Add(new NpgsqlParameter(parameterName, parameterType, size, sourceColumn) { Value = value });
/// <summary>
/// Adds a <see cref="NpgsqlParameter"/> to the <see cref="NpgsqlParameterCollection"/> given the specified value.
/// </summary>
/// <param name="value">The value of the <see cref="NpgsqlParameter"/> to add to the collection.</param>
/// <returns>The parameter that was added.</returns>
public NpgsqlParameter AddWithValue(object value)
=> Add(new NpgsqlParameter { Value = value });
/// <summary>
/// Adds a <see cref="NpgsqlParameter"/> to the <see cref="NpgsqlParameterCollection"/> given the specified data type and value.
/// </summary>
/// <param name="parameterType">One of the <see cref="NpgsqlDbType"/> values.</param>
/// <param name="value">The value of the <see cref="NpgsqlParameter"/> to add to the collection.</param>
/// <returns>The parameter that was added.</returns>
public NpgsqlParameter AddWithValue(NpgsqlDbType parameterType, object value)
=> Add(new NpgsqlParameter { NpgsqlDbType = parameterType, Value = value });
/// <summary>
/// Adds a <see cref="NpgsqlParameter"/> to the <see cref="NpgsqlParameterCollection"/> given the parameter name and the data type.
/// </summary>
/// <param name="parameterName">The name of the parameter.</param>
/// <param name="parameterType">One of the <see cref="DbType"/> values.</param>
/// <returns>The parameter that was added.</returns>
public NpgsqlParameter Add(string parameterName, NpgsqlDbType parameterType)
=> Add(new NpgsqlParameter(parameterName, parameterType));
/// <summary>
/// Adds a <see cref="NpgsqlParameter"/> to the <see cref="NpgsqlParameterCollection"/> with the parameter name, the data type,
/// and the column length.
/// </summary>
/// <param name="parameterName">The name of the parameter.</param>
/// <param name="parameterType">One of the <see cref="DbType"/> values.</param>
/// <param name="size">The length of the column.</param>
/// <returns>The parameter that was added.</returns>
public NpgsqlParameter Add(string parameterName, NpgsqlDbType parameterType, int size)
=> Add(new NpgsqlParameter(parameterName, parameterType, size));
/// <summary>
/// Adds a <see cref="NpgsqlParameter"/> to the <see cref="NpgsqlParameterCollection"/> with the parameter name, the data type, the
/// column length, and the source column name.
/// </summary>
/// <param name="parameterName">The name of the parameter.</param>
/// <param name="parameterType">One of the <see cref="DbType"/> values.</param>
/// <param name="size">The length of the column.</param>
/// <param name="sourceColumn">The name of the source column.</param>
/// <returns>The parameter that was added.</returns>
public NpgsqlParameter Add(string parameterName, NpgsqlDbType parameterType, int size, string sourceColumn)
=> Add(new NpgsqlParameter(parameterName, parameterType, size, sourceColumn));
#endregion
#region IDataParameterCollection Member
/// <inheritdoc />
// ReSharper disable once ImplicitNotNullOverridesUnknownExternalMember
public override void RemoveAt(string parameterName)
=> RemoveAt(IndexOf(parameterName ?? throw new ArgumentNullException(nameof(parameterName))));
/// <inheritdoc />
public override bool Contains(string parameterName)
=> IndexOf(parameterName ?? throw new ArgumentNullException(nameof(parameterName))) != -1;
/// <inheritdoc />
public override int IndexOf(string parameterName)
{
if (parameterName is null)
return -1;
if (parameterName.Length > 0 && (parameterName[0] == ':' || parameterName[0] == '@'))
parameterName = parameterName.Remove(0, 1);
// Using a dictionary is always faster after around 10 items when matched against reference equality.
// For string equality this is the case after ~3 items so we take a decent compromise going with 5.
if (LookupEnabled && parameterName.Length != 0)
{
if (_caseInsensitiveLookup is null)
BuildLookup();
if (TwoPassCompatMode && _caseSensitiveLookup!.TryGetValue(parameterName, out var indexCs))
return indexCs;
if (_caseInsensitiveLookup!.TryGetValue(parameterName, out var indexCi))
return indexCi;
return -1;
}
// Start with case-sensitive search in two pass mode.
if (TwoPassCompatMode)
{
for (var i = 0; i < InternalList.Count; i++)
{
var name = InternalList[i].TrimmedName;
if (string.Equals(parameterName, name))
return i;
}
}
// Then do case-insensitive search.
for (var i = 0; i < InternalList.Count; i++)
{
var name = InternalList[i].TrimmedName;
if (ReferenceEquals(parameterName, name) || string.Equals(parameterName, name, StringComparison.OrdinalIgnoreCase))
return i;
}
return -1;
void BuildLookup()
{
if (TwoPassCompatMode)
_caseSensitiveLookup = new Dictionary<string, int>(InternalList.Count);
_caseInsensitiveLookup = new Dictionary<string, int>(InternalList.Count, StringComparer.OrdinalIgnoreCase);
for (var i = 0; i < InternalList.Count; i++)
{
var item = InternalList[i];
if (!item.IsPositional)
LookupAdd(item.TrimmedName, i);
}
}
}
#endregion
#region IList Member
/// <inheritdoc />
public override bool IsReadOnly => false;
/// <summary>
/// Removes the specified <see cref="NpgsqlParameter"/> from the collection using a specific index.
/// </summary>
/// <param name="index">The zero-based index of the parameter.</param>
public override void RemoveAt(int index)
{
ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual(index, InternalList.Count);
Remove(InternalList[index]);
}
/// <summary>
/// Inserts a parameter into the <see cref="NpgsqlParameterCollection"/> at the specified index.
/// </summary>
/// <param name="index">The zero-based index at which to insert the parameter.</param>
/// <param name="value">The parameter to insert.</param>
/// <remarks>
/// Although this method accepts <see cref="object"/>, only instances of <see cref="NpgsqlParameter"/> are supported.
/// Passing any other type will result in an <see cref="InvalidCastException"/>.
/// </remarks>
public override void Insert(int index, object value)
=> Insert(index, Cast(value));
/// <summary>
/// Removes the <see cref="NpgsqlParameter"/> with the specified name from the collection.
/// </summary>
/// <param name="parameterName">The name of the <see cref="NpgsqlParameter"/> to remove from the collection.</param>
public void Remove(string parameterName)
{
ArgumentNullException.ThrowIfNull(parameterName);
var index = IndexOf(parameterName);
if (index < 0)
ThrowHelper.ThrowInvalidOperationException("No parameter with the specified name exists in the collection");
RemoveAt(index);
}
/// <summary>
/// Removes the specified <see cref="NpgsqlParameter"/> from the collection.
/// </summary>
/// <param name="value">The <see cref="NpgsqlParameter"/> to remove from the collection.</param>
/// <remarks>
/// Although this method accepts <see cref="object"/>, only instances of <see cref="NpgsqlParameter"/> are supported.
/// Passing any other type will result in an <see cref="InvalidCastException"/>.
/// </remarks>
public override void Remove(object value)
=> Remove(Cast(value));
/// <inheritdoc />
public override bool Contains(object value)
=> value is NpgsqlParameter param && InternalList.Contains(param);
/// <summary>
/// Gets a value indicating whether a <see cref="NpgsqlParameter"/> with the specified parameter name exists in the collection.
/// </summary>
/// <param name="parameterName">The name of the <see cref="NpgsqlParameter"/> object to find.</param>
/// <param name="parameter">
/// A reference to the requested parameter is returned in this out param if it is found in the list.
/// This value is <see langword="null"/> if the parameter is not found.
/// </param>
/// <returns>
/// <see langword="true"/> if the collection contains the parameter and param will contain the parameter;
/// otherwise, <see langword="false"/>.
/// </returns>
public bool TryGetValue(string parameterName, [NotNullWhen(true)] out NpgsqlParameter? parameter)
{
ArgumentNullException.ThrowIfNull(parameterName);
var index = IndexOf(parameterName);
if (index != -1)
{
parameter = InternalList[index];
return true;
}
parameter = null;
return false;
}
/// <summary>
/// Removes all items from the collection.
/// </summary>
public override void Clear()
{
// clean up parameters so they can be added to another command if required.
foreach (var toRemove in InternalList)
toRemove.Collection = null;
InternalList.Clear();
LookupClear();
}
/// <summary>
/// Returns the index of the specified parameter in the <see cref="NpgsqlParameterCollection"/>.
/// </summary>
/// <param name="value">The parameter to find.</param>
/// <returns>The index of the parameter if found; otherwise, -1.</returns>
/// <remarks>
/// Although this method accepts <see cref="object"/>, only instances of <see cref="NpgsqlParameter"/> are supported.
/// Passing any other type will result in an <see cref="InvalidCastException"/>.
/// </remarks>
public override int IndexOf(object value)
=> IndexOf(Cast(value));
/// <summary>
/// Adds a parameter to the <see cref="NpgsqlParameterCollection"/>.
/// </summary>
/// <param name="value">The parameter to add.</param>
/// <returns>The zero-based index at which the parameter was added.</returns>
/// <remarks>
/// Although this method accepts <see cref="object"/>, only instances of <see cref="NpgsqlParameter"/> are supported.
/// Passing any other type will result in an <see cref="InvalidCastException"/>.
/// To add a parameter by value, use <see cref="AddWithValue(object)"/>, <see cref="AddWithValue(string, object)"/>,
/// or one of the typed <see cref="Add(string, NpgsqlDbType)"/> overloads.
/// </remarks>
public override int Add(object value)
{
Add(Cast(value));
return Count - 1;
}
/// <inheritdoc />
public override bool IsFixedSize => false;
#endregion
#region ICollection Member
/// <inheritdoc />
public override bool IsSynchronized => (InternalList as ICollection).IsSynchronized;
/// <summary>
/// Gets the number of <see cref="NpgsqlParameter"/> objects in the collection.
/// </summary>
/// <value>The number of <see cref="NpgsqlParameter"/> objects in the collection.</value>
public override int Count => InternalList.Count;
/// <inheritdoc />
public override void CopyTo(Array array, int index)
=> ((ICollection)InternalList).CopyTo(array, index);
/// <inheritdoc />
bool ICollection<NpgsqlParameter>.IsReadOnly => false;
/// <inheritdoc />
public override object SyncRoot => ((ICollection)InternalList).SyncRoot;
#endregion
#region IEnumerable Member
IEnumerator<NpgsqlParameter> IEnumerable<NpgsqlParameter>.GetEnumerator()
=> InternalList.GetEnumerator();
/// <inheritdoc />
public override IEnumerator GetEnumerator() => InternalList.GetEnumerator();
#endregion
/// <summary>
/// Adds the elements of the specified array to the end of the <see cref="NpgsqlParameterCollection"/>.
/// </summary>
/// <param name="values">
/// An array of <see cref="object"/>s to add. Each item must be an instance of <see cref="NpgsqlParameter"/>.
/// Passing any other type will result in an <see cref="InvalidCastException"/>.
/// </param>
public override void AddRange(Array values)
{
ArgumentNullException.ThrowIfNull(values);
foreach (var parameter in values)
Add(Cast(parameter));
}
/// <inheritdoc />
protected override DbParameter GetParameter(string parameterName)
=> this[parameterName];
/// <inheritdoc />
protected override DbParameter GetParameter(int index)
=> this[index];
/// <inheritdoc />
protected override void SetParameter(string parameterName, DbParameter value)
=> this[parameterName] = Cast(value);
/// <inheritdoc />
protected override void SetParameter(int index, DbParameter value)
=> this[index] = Cast(value);
/// <summary>
/// Report the offset within the collection of the given parameter.
/// </summary>
/// <param name="item">Parameter to find.</param>
/// <returns>Index of the parameter, or -1 if the parameter is not present.</returns>
public int IndexOf(NpgsqlParameter item)
=> InternalList.IndexOf(item);
/// <summary>
/// Insert the specified parameter into the collection.
/// </summary>
/// <param name="index">Index of the existing parameter before which to insert the new one.</param>
/// <param name="item">Parameter to insert.</param>
public void Insert(int index, NpgsqlParameter item)
{
ArgumentNullException.ThrowIfNull(item);
if (item.Collection != null)
throw new Exception("The parameter already belongs to a collection");
InternalList.Insert(index, item);
item.Collection = this;
if (!item.IsPositional)
LookupInsert(item.TrimmedName, index);
}
/// <summary>
/// Report whether the specified parameter is present in the collection.
/// </summary>
/// <param name="item">Parameter to find.</param>
/// <returns>True if the parameter was found, otherwise false.</returns>
public bool Contains(NpgsqlParameter item) => InternalList.Contains(item);
/// <summary>
/// Remove the specified parameter from the collection.
/// </summary>
/// <param name="item">Parameter to remove.</param>
/// <returns>True if the parameter was found and removed, otherwise false.</returns>
public bool Remove(NpgsqlParameter item)
{
ArgumentNullException.ThrowIfNull(item);
if (item.Collection != this)
ThrowHelper.ThrowInvalidOperationException("The item does not belong to this collection");
var index = IndexOf(item);
if (index >= 0)
{
InternalList.RemoveAt(index);
if (!LookupEnabled)
LookupClear();
if (!item.IsPositional)
LookupRemove(item.TrimmedName, index);
item.Collection = null;
return true;
}
return false;
}
/// <summary>
/// Convert collection to a System.Array.
/// </summary>
/// <param name="array">Destination array.</param>
/// <param name="arrayIndex">Starting index in destination array.</param>
public void CopyTo(NpgsqlParameter[] array, int arrayIndex)
=> InternalList.CopyTo(array, arrayIndex);
/// <summary>
/// Convert collection to a System.Array.
/// </summary>
/// <returns>NpgsqlParameter[]</returns>
public NpgsqlParameter[] ToArray() => InternalList.ToArray();
internal void CloneTo(NpgsqlParameterCollection other)
{
other.InternalList.Clear();
foreach (var param in InternalList)
{
var newParam = param.Clone();
newParam.Collection = other;
other.InternalList.Add(newParam);
}
if (LookupEnabled && _caseInsensitiveLookup is not null)
{
other._caseInsensitiveLookup = new Dictionary<string, int>(_caseInsensitiveLookup, StringComparer.OrdinalIgnoreCase);
if (TwoPassCompatMode)
{
Debug.Assert(_caseSensitiveLookup is not null);
other._caseSensitiveLookup = new Dictionary<string, int>(_caseSensitiveLookup);
}
}
}
internal void ProcessParameters(NpgsqlDataSource.ReloadableState reloadableState, bool validateValues, CommandType commandType)
{
HasOutputParameters = false;
PlaceholderType = PlaceholderType.NoParameters;
var list = InternalList;
for (var i = 0; i < list.Count; i++)
{
var p = list[i];
switch (PlaceholderType)
{
case PlaceholderType.NoParameters:
PlaceholderType = p.IsPositional ? PlaceholderType.Positional : PlaceholderType.Named;
break;
case PlaceholderType.Named:
if (p.IsPositional)
PlaceholderType = PlaceholderType.Mixed;
break;
case PlaceholderType.Positional:
if (!p.IsPositional)
PlaceholderType = PlaceholderType.Mixed;
break;
case PlaceholderType.Mixed:
break;
default:
ThrowHelper.ThrowArgumentOutOfRangeException(nameof(PlaceholderType), $"Unknown {nameof(PlaceholderType)} value: {{0}}", PlaceholderType);
break;
}
switch (p.Direction)
{
case ParameterDirection.Input:
break;
case ParameterDirection.InputOutput:
if (PlaceholderType == PlaceholderType.Positional && commandType != CommandType.StoredProcedure)
ThrowHelper.ThrowNotSupportedException("Output parameters are not supported in positional mode (unless used with CommandType.StoredProcedure)");
HasOutputParameters = true;
break;
case ParameterDirection.Output:
if (PlaceholderType == PlaceholderType.Positional && commandType != CommandType.StoredProcedure)
ThrowHelper.ThrowNotSupportedException("Output parameters are not supported in positional mode (unless used with CommandType.StoredProcedure)");
HasOutputParameters = true;
continue;
case ParameterDirection.ReturnValue:
// Simply ignored
continue;
default:
ThrowHelper.ThrowArgumentOutOfRangeException(nameof(ParameterDirection),
$"Unhandled {nameof(ParameterDirection)} value: {{0}}", p.Direction);
break;
}
p.ResolveTypeInfo(reloadableState.SerializerOptions, reloadableState.DbTypeResolver);
if (validateValues)
{
p.Bind(out _, out _);
}
}
}
internal bool HasOutputParameters { get; set; }
internal PlaceholderType PlaceholderType { get; set; }
static NpgsqlParameter Cast(object? value)
{
var castedValue = value as NpgsqlParameter;
if (castedValue is null)
ThrowInvalidCastException(value);
return castedValue;
}
[DoesNotReturn]
static void ThrowInvalidCastException(object? value) =>
throw new InvalidCastException(
$"The value \"{value}\" is not of type \"{nameof(NpgsqlParameter)}\" and cannot be used in this parameter collection.");
}
enum PlaceholderType
{
/// <summary>
/// The parameter collection includes no parameters.
/// </summary>
NoParameters,
/// <summary>
/// The parameter collection includes only named parameters.
/// </summary>
Named,
/// <summary>
/// The parameter collection includes only positional parameters.
/// </summary>
Positional,
/// <summary>
/// The parameter collection includes both named and positional parameters.
/// This is only supported when <see cref="NpgsqlCommand.CommandType" /> is set to <see cref="CommandType.StoredProcedure" />.
/// </summary>
Mixed
}