forked from npgsql/npgsql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNpgsqlParameterCollection.cs
More file actions
848 lines (740 loc) · 34.6 KB
/
NpgsqlParameterCollection.cs
File metadata and controls
848 lines (740 loc) · 34.6 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
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
#region License
// The PostgreSQL License
//
// Copyright (C) 2015 The Npgsql Development Team
//
// Permission to use, copy, modify, and distribute this software and its
// documentation for any purpose, without fee, and without a written
// agreement is hereby granted, provided that the above copyright notice
// and this paragraph and the following two paragraphs appear in all copies.
//
// IN NO EVENT SHALL THE NPGSQL DEVELOPMENT TEAM BE LIABLE TO ANY PARTY
// FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES,
// INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS
// DOCUMENTATION, EVEN IF THE NPGSQL DEVELOPMENT TEAM HAS BEEN ADVISED OF
// THE POSSIBILITY OF SUCH DAMAGE.
//
// THE NPGSQL DEVELOPMENT TEAM SPECIFICALLY DISCLAIMS ANY WARRANTIES,
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
// ON AN "AS IS" BASIS, AND THE NPGSQL DEVELOPMENT TEAM HAS NO OBLIGATIONS
// TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
#endregion
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data.Common;
using System.Diagnostics.Contracts;
using JetBrains.Annotations;
using NpgsqlTypes;
namespace Npgsql
{
/// <summary>
/// Represents a collection of parameters relevant to a <see cref="NpgsqlCommand">NpgsqlCommand</see>
/// as well as their respective mappings to columns in a <see cref="System.Data.DataSet">DataSet</see>.
/// This class cannot be inherited.
/// </summary>
#if WITHDESIGN
[ListBindable(false)]
[Editor(typeof(NpgsqlParametersEditor), typeof(System.Drawing.Design.UITypeEditor))]
#endif
public sealed class NpgsqlParameterCollection : DbParameterCollection, IEnumerable<NpgsqlParameter>
{
readonly List<NpgsqlParameter> _internalList = new List<NpgsqlParameter>();
// Dictionary lookups for GetValue to improve performance
Dictionary<string, int> _lookup;
Dictionary<string, int> _lookupIgnoreCase;
/// <summary>
/// Initializes a new instance of the NpgsqlParameterCollection class.
/// </summary>
internal NpgsqlParameterCollection()
{
InvalidateHashLookups();
}
/// <summary>
/// Invalidate the hash lookup tables. This should be done any time a change
/// may throw the lookups out of sync with the list.
/// </summary>
internal void InvalidateHashLookups()
{
_lookup = null;
_lookupIgnoreCase = null;
}
#region NpgsqlParameterCollection Member
/// <summary>
/// Gets the <see cref="NpgsqlParameter">NpgsqlParameter</see> with the specified name.
/// </summary>
/// <param name="parameterName">The name of the <see cref="NpgsqlParameter">NpgsqlParameter</see> to retrieve.</param>
/// <value>The <see cref="NpgsqlParameter">NpgsqlParameter</see> with the specified name, or a null reference if the parameter is not found.</value>
#if WITHDESIGN
[Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
#endif
[PublicAPI]
public new NpgsqlParameter this[string parameterName]
{
get
{
var index = IndexOf(parameterName);
if (index == -1)
{
throw new IndexOutOfRangeException("Parameter not found");
}
return _internalList[index];
}
set
{
int index = IndexOf(parameterName);
if (index == -1)
{
throw new IndexOutOfRangeException("Parameter not found");
}
NpgsqlParameter oldValue = _internalList[index];
if (value.CleanName != oldValue.CleanName)
{
InvalidateHashLookups();
}
_internalList[index] = value;
}
}
/// <summary>
/// Gets the <see cref="NpgsqlParameter">NpgsqlParameter</see> at the specified index.
/// </summary>
/// <param name="index">The zero-based index of the <see cref="NpgsqlParameter">NpgsqlParameter</see> to retrieve.</param>
/// <value>The <see cref="NpgsqlParameter">NpgsqlParameter</see> at the specified index.</value>
#if WITHDESIGN
[Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
#endif
[PublicAPI]
public new NpgsqlParameter this[int index]
{
get { return _internalList[index]; }
set
{
var oldValue = _internalList[index];
if (oldValue == value)
{
// Reasigning the same value is a non-op.
return;
}
if (value.Collection != null)
{
throw new InvalidOperationException("The parameter already belongs to a collection");
}
if (value.CleanName != oldValue.CleanName)
{
InvalidateHashLookups();
}
_internalList[index] = value;
value.Collection = this;
oldValue.Collection = null;
}
}
/// <summary>
/// Adds the specified <see cref="NpgsqlParameter">NpgsqlParameter</see> object to the <see cref="NpgsqlParameterCollection">NpgsqlParameterCollection</see>.
/// </summary>
/// <param name="value">The <see cref="NpgsqlParameter">NpgsqlParameter</see> to add to the collection.</param>
/// <returns>The index of the new <see cref="NpgsqlParameter">NpgsqlParameter</see> object.</returns>
public NpgsqlParameter Add(NpgsqlParameter value)
{
// Do not allow parameters without name.
if (value.Collection != null)
{
throw new InvalidOperationException("The parameter already belongs to a collection");
}
_internalList.Add(value);
value.Collection = this;
InvalidateHashLookups();
// Check if there is a name. If not, add a name based of the index+1 of the parameter.
if (value.ParameterName.Trim() == string.Empty || (value.ParameterName.Length == 1 && value.ParameterName[0] == ':'))
{
value.ParameterName = ":" + "Parameter" + _internalList.Count;
value.AutoAssignedName = true;
}
return value;
}
/// <summary>
/// Adds a <see cref="NpgsqlParameter">NpgsqlParameter</see> to the <see cref="NpgsqlParameterCollection">NpgsqlParameterCollection</see> given the specified parameter name and value.
/// </summary>
/// <param name="parameterName">The name of the <see cref="NpgsqlParameter">NpgsqlParameter</see>.</param>
/// <param name="value">The Value of the <see cref="NpgsqlParameter">NpgsqlParameter</see> to add to the collection.</param>
/// <returns>The paramater that was added.</returns>
[PublicAPI]
public NpgsqlParameter AddWithValue(string parameterName, object value)
{
return Add(new NpgsqlParameter(parameterName, value));
}
/// <summary>
/// Adds a <see cref="NpgsqlParameter">NpgsqlParameter</see> to the <see cref="NpgsqlParameterCollection">NpgsqlParameterCollection</see> given the specified parameter name, data type and value.
/// </summary>
/// <param name="parameterName">The name of the <see cref="NpgsqlParameter">NpgsqlParameter</see>.</param>
/// <param name="parameterType">One of the NpgsqlDbType values.</param>
/// <param name="value">The Value of the <see cref="NpgsqlParameter">NpgsqlParameter</see> to add to the collection.</param>
/// <returns>The paramater that was added.</returns>
[PublicAPI]
public NpgsqlParameter AddWithValue(string parameterName, NpgsqlDbType parameterType, object value)
{
return Add(new NpgsqlParameter(parameterName, parameterType) { Value = value });
}
/// <summary>
/// Adds a <see cref="NpgsqlParameter">NpgsqlParameter</see> to the <see cref="NpgsqlParameterCollection">NpgsqlParameterCollection</see> given the specified parameter name and value.
/// </summary>
/// <param name="parameterName">The name of the <see cref="NpgsqlParameter">NpgsqlParameter</see>.</param>
/// <param name="value">The Value of the <see cref="NpgsqlParameter">NpgsqlParameter</see> to add to the collection.</param>
/// <param name="parameterType">One of the NpgsqlDbType values.</param>
/// <param name="size">The length of the column.</param>
/// <returns>The paramater that was added.</returns>
[PublicAPI]
public NpgsqlParameter AddWithValue(string parameterName, NpgsqlDbType parameterType, int size, object value)
{
return Add(new NpgsqlParameter(parameterName, parameterType, size) { Value = value });
}
/// <summary>
/// Adds a <see cref="NpgsqlParameter">NpgsqlParameter</see> to the <see cref="NpgsqlParameterCollection">NpgsqlParameterCollection</see> given the specified parameter name and value.
/// </summary>
/// <param name="parameterName">The name of the <see cref="NpgsqlParameter">NpgsqlParameter</see>.</param>
/// <param name="value">The Value of the <see cref="NpgsqlParameter">NpgsqlParameter</see> to add to the collection.</param>
/// <param name="parameterType">One of the NpgsqlDbType values.</param>
/// <param name="size">The length of the column.</param>
/// <param name="sourceColumn">The name of the source column.</param>
/// <returns>The paramater that was added.</returns>
[PublicAPI]
public NpgsqlParameter AddWithValue(string parameterName, NpgsqlDbType parameterType, int size, string sourceColumn, object value)
{
return Add(new NpgsqlParameter(parameterName, parameterType, size, sourceColumn) { Value = value });
}
/// <summary>
/// Adds a <see cref="NpgsqlParameter">NpgsqlParameter</see> to the <see cref="NpgsqlParameterCollection">NpgsqlParameterCollection</see> given the specified value.
/// </summary>
/// <param name="value">The Value of the <see cref="NpgsqlParameter">NpgsqlParameter</see> to add to the collection.</param>
/// <returns>The paramater that was added.</returns>
[PublicAPI]
public NpgsqlParameter AddWithValue(object value)
{
return Add(new NpgsqlParameter() { Value = value });
}
/// <summary>
/// Adds a <see cref="NpgsqlParameter">NpgsqlParameter</see> to the <see cref="NpgsqlParameterCollection">NpgsqlParameterCollection</see> given the specified data type and value.
/// </summary>
/// <param name="parameterType">One of the NpgsqlDbType values.</param>
/// <param name="value">The Value of the <see cref="NpgsqlParameter">NpgsqlParameter</see> to add to the collection.</param>
/// <returns>The paramater that was added.</returns>
[PublicAPI]
public NpgsqlParameter AddWithValue(NpgsqlDbType parameterType, object value)
{
return Add(new NpgsqlParameter {
NpgsqlDbType = parameterType,
Value = value
});
}
/// <summary>
/// Adds a <see cref="NpgsqlParameter">NpgsqlParameter</see> to the <see cref="NpgsqlParameterCollection">NpgsqlParameterCollection</see> given the parameter name and the data type.
/// </summary>
/// <param name="parameterName">The name of the parameter.</param>
/// <param name="parameterType">One of the DbType values.</param>
/// <returns>The index of the new <see cref="NpgsqlParameter">NpgsqlParameter</see> object.</returns>
[PublicAPI]
public NpgsqlParameter Add(string parameterName, NpgsqlDbType parameterType)
{
return Add(new NpgsqlParameter(parameterName, parameterType));
}
/// <summary>
/// Adds a <see cref="NpgsqlParameter">NpgsqlParameter</see> to the <see cref="NpgsqlParameterCollection">NpgsqlParameterCollection</see> 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 DbType values.</param>
/// <param name="size">The length of the column.</param>
/// <returns>The index of the new <see cref="NpgsqlParameter">NpgsqlParameter</see> object.</returns>
[PublicAPI]
public NpgsqlParameter Add(string parameterName, NpgsqlDbType parameterType, int size)
{
return Add(new NpgsqlParameter(parameterName, parameterType, size));
}
/// <summary>
/// Adds a <see cref="NpgsqlParameter">NpgsqlParameter</see> to the <see cref="NpgsqlParameterCollection">NpgsqlParameterCollection</see> 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 DbType values.</param>
/// <param name="size">The length of the column.</param>
/// <param name="sourceColumn">The name of the source column.</param>
/// <returns>The index of the new <see cref="NpgsqlParameter">NpgsqlParameter</see> object.</returns>
[PublicAPI]
public NpgsqlParameter Add(string parameterName, NpgsqlDbType parameterType, int size, string sourceColumn)
{
return Add(new NpgsqlParameter(parameterName, parameterType, size, sourceColumn));
}
#endregion
#region IDataParameterCollection Member
/// <summary>
/// Removes the specified <see cref="NpgsqlParameter">NpgsqlParameter</see> from the collection using the parameter name.
/// </summary>
/// <param name="parameterName">The name of the <see cref="NpgsqlParameter">NpgsqlParameter</see> object to retrieve.</param>
[PublicAPI]
// ReSharper disable once ImplicitNotNullOverridesUnknownExternalMember
public override void RemoveAt(string parameterName)
{
if (parameterName == null)
throw new ArgumentNullException(nameof(parameterName));
Contract.EndContractBlock();
RemoveAt(IndexOf(parameterName));
}
/// <summary>
/// Gets a value indicating whether a <see cref="NpgsqlParameter">NpgsqlParameter</see> with the specified parameter name exists in the collection.
/// </summary>
/// <param name="parameterName">The name of the <see cref="NpgsqlParameter">NpgsqlParameter</see> object to find.</param>
/// <returns><b>true</b> if the collection contains the parameter; otherwise, <b>false</b>.</returns>
// ReSharper disable once ImplicitNotNullOverridesUnknownExternalMember
public override bool Contains(string parameterName)
{
if (parameterName == null)
throw new ArgumentNullException(nameof(parameterName));
Contract.EndContractBlock();
return (IndexOf(parameterName) != -1);
}
/// <summary>
/// Gets the location of the <see cref="NpgsqlParameter">NpgsqlParameter</see> in the collection with a specific parameter name.
/// </summary>
/// <param name="parameterName">The name of the <see cref="NpgsqlParameter">NpgsqlParameter</see> object to find.</param>
/// <returns>The zero-based location of the <see cref="NpgsqlParameter">NpgsqlParameter</see> in the collection.</returns>
public override int IndexOf([CanBeNull] string parameterName)
{
if (parameterName == null)
{
return -1;
}
int retIndex;
int scanIndex;
if (parameterName.Length > 0 && ((parameterName[0] == ':') || (parameterName[0] == '@')))
{
parameterName = parameterName.Remove(0, 1);
}
// Using a dictionary is much faster for 5 or more items
if (_internalList.Count >= 5)
{
if (_lookup == null)
{
_lookup = new Dictionary<string, int>();
for (scanIndex = 0 ; scanIndex < _internalList.Count ; scanIndex++)
{
var item = _internalList[scanIndex];
// Store only the first of each distinct value
if (! _lookup.ContainsKey(item.CleanName))
{
_lookup.Add(item.CleanName, scanIndex);
}
}
}
// Try to access the case sensitive parameter name first
if (_lookup.TryGetValue(parameterName, out retIndex))
{
return retIndex;
}
// Case sensitive lookup failed, generate a case insensitive lookup
if (_lookupIgnoreCase == null)
{
_lookupIgnoreCase = new Dictionary<string, int>(PGUtil.InvariantCaseIgnoringStringComparer);
for (scanIndex = 0 ; scanIndex < _internalList.Count ; scanIndex++)
{
var item = _internalList[scanIndex];
// Store only the first of each distinct value
if (!_lookupIgnoreCase.ContainsKey(item.CleanName))
{
_lookupIgnoreCase.Add(item.CleanName, scanIndex);
}
}
}
// Then try to access the case insensitive parameter name
if (_lookupIgnoreCase.TryGetValue(parameterName, out retIndex))
{
return retIndex;
}
return -1;
}
retIndex = -1;
// Scan until a case insensitive match is found, and save its index for possible return.
// Items that don't match loosely cannot possibly match exactly.
for (scanIndex = 0 ; scanIndex < _internalList.Count ; scanIndex++)
{
var item = _internalList[scanIndex];
var comparer = PGUtil.InvariantCaseIgnoringStringComparer;
if (comparer.Compare(parameterName, item.CleanName) == 0)
{
retIndex = scanIndex;
break;
}
}
// Then continue the scan until a case sensitive match is found, and return it.
// If a case insensitive match was found, it will be re-checked for an exact match.
for ( ; scanIndex < _internalList.Count ; scanIndex++)
{
var item = _internalList[scanIndex];
if (item.CleanName == parameterName)
{
return scanIndex;
}
}
// If a case insensitive match was found, it will be returned here.
return retIndex;
}
#endregion
#region IList Member
#if NET45 || NET452 || DNX452
/// <summary>
/// Report whether the collection is read only. Always false.
/// </summary>
public override bool IsReadOnly => false;
#endif
/// <summary>
/// Removes the specified <see cref="NpgsqlParameter">NpgsqlParameter</see> 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)
{
if (_internalList.Count - 1 < index)
{
throw new IndexOutOfRangeException();
}
Remove(_internalList[index]);
}
/// <summary>
/// Inserts a <see cref="NpgsqlParameter">NpgsqlParameter</see> into the collection at the specified index.
/// </summary>
/// <param name="index">The zero-based index where the parameter is to be inserted within the collection.</param>
/// <param name="oValue">The <see cref="NpgsqlParameter">NpgsqlParameter</see> to add to the collection.</param>
public override void Insert(int index, object oValue)
{
if (oValue == null)
throw new ArgumentNullException(nameof(oValue));
Contract.EndContractBlock();
CheckType(oValue);
NpgsqlParameter value = oValue as NpgsqlParameter;
Contract.Assert(value != null);
if (value.Collection != null)
{
throw new InvalidOperationException("The parameter already belongs to a collection");
}
value.Collection = this;
_internalList.Insert(index, value);
InvalidateHashLookups();
}
/// <summary>
/// Removes the specified <see cref="NpgsqlParameter">NpgsqlParameter</see> from the collection.
/// </summary>
/// <param name="parameterName">The name of the <see cref="NpgsqlParameter">NpgsqlParameter</see> to remove from the collection.</param>
[PublicAPI]
public void Remove(string parameterName)
{
var index = IndexOf(parameterName);
if (index < 0)
{
throw new InvalidOperationException("No parameter with the specified name exists in the collection");
}
RemoveAt(index);
}
/// <summary>
/// Removes the specified <see cref="NpgsqlParameter">NpgsqlParameter</see> from the collection.
/// </summary>
/// <param name="oValue">The <see cref="NpgsqlParameter">NpgsqlParameter</see> to remove from the collection.</param>
public override void Remove(object oValue)
{
if (oValue == null)
throw new ArgumentNullException(nameof(oValue));
Contract.EndContractBlock();
CheckType(oValue);
var p = oValue as NpgsqlParameter;
Contract.Assert(p != null);
Remove(p);
}
/// <summary>
/// Gets a value indicating whether a <see cref="NpgsqlParameter">NpgsqlParameter</see> exists in the collection.
/// </summary>
/// <param name="value">The value of the <see cref="NpgsqlParameter">NpgsqlParameter</see> object to find.</param>
/// <returns>true if the collection contains the <see cref="NpgsqlParameter">NpgsqlParameter</see> object; otherwise, false.</returns>
// ReSharper disable once AnnotationRedundancyInHierarchy
public override bool Contains([CanBeNull] object value)
{
if (!(value is NpgsqlParameter))
{
return false;
}
return _internalList.Contains((NpgsqlParameter) value);
}
/// <summary>
/// Gets a value indicating whether a <see cref="NpgsqlParameter">NpgsqlParameter</see> with the specified parameter name exists in the collection.
/// </summary>
/// <param name="parameterName">The name of the <see cref="NpgsqlParameter">NpgsqlParameter</see> 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 null if the parameter is not found.</param>
/// <returns><b>true</b> if the collection contains the parameter and param will contain the parameter; otherwise, <b>false</b>.</returns>
[ContractAnnotation("=>true,parameter:notnull; =>false,parameter:null")]
public bool TryGetValue(string parameterName, [CanBeNull] out NpgsqlParameter parameter)
{
int index = IndexOf(parameterName);
if (index != -1)
{
parameter = _internalList[index];
return true;
}
else
{
parameter = null;
return false;
}
}
/// <summary>
/// Removes all items from the collection.
/// </summary>
public override void Clear()
{
foreach (var toRemove in _internalList)
{
// clean up the parameter so it can be added to another command if required.
toRemove.Collection = null;
}
_internalList.Clear();
InvalidateHashLookups();
}
/// <summary>
/// Gets the location of a <see cref="NpgsqlParameter">NpgsqlParameter</see> in the collection.
/// </summary>
/// <param name="value">The value of the <see cref="NpgsqlParameter">NpgsqlParameter</see> object to find.</param>
/// <returns>The zero-based index of the <see cref="NpgsqlParameter">NpgsqlParameter</see> object in the collection.</returns>
// ReSharper disable once ImplicitNotNullConflictInHierarchy
public override int IndexOf(object value)
{
if (value == null)
throw new ArgumentNullException(nameof(value));
Contract.EndContractBlock();
CheckType(value);
return _internalList.IndexOf((NpgsqlParameter) value);
}
/// <summary>
/// Adds the specified <see cref="NpgsqlParameter">NpgsqlParameter</see> object to the <see cref="NpgsqlParameterCollection">NpgsqlParameterCollection</see>.
/// </summary>
/// <param name="value">The <see cref="NpgsqlParameter">NpgsqlParameter</see> to add to the collection.</param>
/// <returns>The zero-based index of the new <see cref="NpgsqlParameter">NpgsqlParameter</see> object.</returns>
public override int Add(object value)
{
CheckType(value);
Add((NpgsqlParameter) value);
return Count - 1;
}
#if NET45 || NET452 || DNX452
/// <summary>
/// Report whether the collection is fixed size. Always false.
/// </summary>
public override bool IsFixedSize => false;
#endif
#endregion
#region ICollection Member
#if NET45 || NET452 || DNX452
/// <summary>
/// Report whether the collection is synchronized.
/// </summary>
public override bool IsSynchronized => (_internalList as ICollection).IsSynchronized;
#endif
/// <summary>
/// Gets the number of <see cref="NpgsqlParameter">NpgsqlParameter</see> objects in the collection.
/// </summary>
/// <value>The number of <see cref="NpgsqlParameter">NpgsqlParameter</see> objects in the collection.</value>
#if WITHDESIGN
[Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
#endif
public override int Count => _internalList.Count;
/// <summary>
/// Copies <see cref="NpgsqlParameter">NpgsqlParameter</see> objects from the <see cref="NpgsqlParameterCollection">NpgsqlParameterCollection</see> to the specified array.
/// </summary>
/// <param name="array">An <see cref="System.Array">Array</see> to which to copy the <see cref="NpgsqlParameter">NpgsqlParameter</see> objects in the collection.</param>
/// <param name="index">The starting index of the array.</param>
// ReSharper disable once ImplicitNotNullConflictInHierarchy
public override void CopyTo(Array array, int index)
{
if (array == null)
throw new ArgumentNullException(nameof(array));
Contract.EndContractBlock();
(_internalList as ICollection).CopyTo(array, index);
}
/// <summary>
/// Sync root.
/// </summary>
public override object SyncRoot => (_internalList as ICollection).SyncRoot;
#endregion
#region IEnumerable Member
IEnumerator<NpgsqlParameter> IEnumerable<NpgsqlParameter>.GetEnumerator()
{
return _internalList.GetEnumerator();
}
/// <summary>
/// Returns an enumerator that can iterate through the collection.
/// </summary>
/// <returns>An <see cref="System.Collections.IEnumerator">IEnumerator</see> that can be used to iterate through the collection.</returns>
public override IEnumerator GetEnumerator()
{
return _internalList.GetEnumerator();
}
#endregion
/// <summary>
/// Add an Array of parameters to the collection.
/// </summary>
/// <param name="values">Parameters to add.</param>
public override void AddRange(Array values)
{
foreach (NpgsqlParameter parameter in values)
{
Add(parameter);
}
}
/// <summary>
/// Get parameter.
/// </summary>
/// <param name="parameterName"></param>
/// <returns></returns>
// ReSharper disable once ImplicitNotNullOverridesUnknownExternalMember
protected override DbParameter GetParameter(string parameterName)
{
if (parameterName == null) throw new ArgumentNullException(nameof(parameterName));
Contract.EndContractBlock();
return this[parameterName];
}
/// <summary>
/// Get parameter.
/// </summary>
/// <param name="index"></param>
/// <returns></returns>
protected override DbParameter GetParameter(int index)
{
return this[index];
}
/// <summary>
/// Set parameter.
/// </summary>
/// <param name="parameterName"></param>
/// <param name="value"></param>
// ReSharper disable once ImplicitNotNullOverridesUnknownExternalMember
protected override void SetParameter(string parameterName, DbParameter value)
{
if (parameterName == null) throw new ArgumentNullException(nameof(parameterName));
Contract.EndContractBlock();
this[parameterName] = (NpgsqlParameter) value;
}
/// <summary>
/// Set parameter.
/// </summary>
/// <param name="index"></param>
/// <param name="value"></param>
protected override void SetParameter(int index, DbParameter value)
{
this[index] = (NpgsqlParameter) value;
}
void CheckType(object o)
{
if (!(o is NpgsqlParameter))
{
throw new InvalidCastException($"Can't cast {o.GetType()} into NpgsqlParameter");
}
}
/*
/// <summary>
/// In methods taking an array as argument this method is used to verify
/// that the argument has the type <see cref="NpgsqlParameter">NpgsqlParameter</see>[]
/// </summary>
/// <param name="array">The array to verify</param>
private void CheckType(Array array)
{
NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "CheckType", array);
if (array.GetType() != typeof (NpgsqlParameter[]))
{
throw new InvalidCastException(
String.Format(this.resman.GetString("Exception_WrongType"), array.GetType().ToString()));
}
}
*/
/// <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>
[PublicAPI]
public int IndexOf(NpgsqlParameter item)
{
return _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>
[PublicAPI]
public void Insert(int index, NpgsqlParameter item)
{
if (item.Collection != null)
{
throw new Exception("The parameter already belongs to a collection");
}
_internalList.Insert(index, item);
item.Collection = this;
InvalidateHashLookups();
}
/// <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>
[PublicAPI]
public bool Contains(NpgsqlParameter item)
{
return _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>
[PublicAPI]
public bool Remove(NpgsqlParameter item)
{
if (item == null) throw new ArgumentNullException(nameof(item));
Contract.EndContractBlock();
if (item.Collection != this)
{
throw new InvalidOperationException("The item does not belong to this collection");
}
if (_internalList.Remove(item))
{
item.Collection = null;
InvalidateHashLookups();
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>
[PublicAPI]
public void CopyTo(NpgsqlParameter[] array, int arrayIndex)
{
_internalList.CopyTo(array, arrayIndex);
}
/// <summary>
/// Convert collection to a System.Array.
/// </summary>
/// <returns>NpgsqlParameter[]</returns>
[PublicAPI]
public NpgsqlParameter[] ToArray()
{
return _internalList.ToArray();
}
internal void CloneTo(NpgsqlParameterCollection other)
{
other._internalList.Clear();
foreach (var param in _internalList)
{
var newParam = param.Clone();
newParam.Collection = this;
other._internalList.Add(newParam);
}
other._lookup = _lookup;
other._lookupIgnoreCase = _lookupIgnoreCase;
}
}
}