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
600 lines (508 loc) · 25.3 KB
/
NpgsqlParameterCollection.cs
File metadata and controls
600 lines (508 loc) · 25.3 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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data.Common;
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 DataSet.
/// This class cannot be inherited.
/// </summary>
public sealed class NpgsqlParameterCollection : DbParameterCollection, IList<NpgsqlParameter>
{
readonly List<NpgsqlParameter> _internalList = new List<NpgsqlParameter>(5);
// Dictionary lookups for GetValue to improve performance
[CanBeNull]
Dictionary<string, int> _lookup;
[CanBeNull]
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>
[PublicAPI]
public new NpgsqlParameter this[string parameterName]
{
get
{
var index = IndexOf(parameterName);
if (index == -1)
throw new ArgumentException("Parameter not found");
return _internalList[index];
}
set
{
var index = IndexOf(parameterName);
if (index == -1)
throw new ArgumentException("Parameter not found");
var oldValue = _internalList[index];
if (value.ParameterName != oldValue.ParameterName)
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>
[PublicAPI]
public new NpgsqlParameter this[int index]
{
get => _internalList[index];
set
{
if (value.Collection != null)
throw new InvalidOperationException("The parameter already belongs to a collection");
var oldValue = _internalList[index];
if (oldValue == value)
return;
if (value.ParameterName != oldValue.ParameterName)
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)
{
if (value.Collection != null)
throw new InvalidOperationException("The parameter already belongs to a collection");
_internalList.Add(value);
value.Collection = this;
InvalidateHashLookups();
return value;
}
/// <inheritdoc />
void ICollection<NpgsqlParameter>.Add(NpgsqlParameter item)
=> Add(item);
/// <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)
=> 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)
=> 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)
=> 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)
=> 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)
=> 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)
=> 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)
=> 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)
=> 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)
=> Add(new NpgsqlParameter(parameterName, parameterType, size, sourceColumn));
#endregion
#region IDataParameterCollection Member
/// <inheritdoc />
[PublicAPI]
// ReSharper disable once ImplicitNotNullOverridesUnknownExternalMember
public override void RemoveAt(string parameterName)
{
if (parameterName == null)
throw new ArgumentNullException(nameof(parameterName));
RemoveAt(IndexOf(parameterName));
}
/// <inheritdoc />
public override bool Contains(string parameterName)
{
if (parameterName == null)
throw new ArgumentNullException(nameof(parameterName));
return IndexOf(parameterName) != -1;
}
/// <inheritdoc />
public override int IndexOf([CanBeNull] string parameterName)
{
if (parameterName == null)
return -1;
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 (var i = 0 ; i < _internalList.Count ; i++)
{
var item = _internalList[i];
// Store only the first of each distinct value
if (!_lookup.ContainsKey(item.TrimmedName))
_lookup.Add(item.TrimmedName, i);
}
}
// Try to access the case sensitive parameter name first
if (_lookup.TryGetValue(parameterName, out var retIndex))
return retIndex;
// Case sensitive lookup failed, generate a case insensitive lookup
if (_lookupIgnoreCase == null)
{
_lookupIgnoreCase = new Dictionary<string, int>(PGUtil.InvariantCaseIgnoringStringComparer);
for (var i = 0 ; i < _internalList.Count ; i++)
{
var item = _internalList[i];
// Store only the first of each distinct value
if (!_lookupIgnoreCase.ContainsKey(item.TrimmedName))
_lookupIgnoreCase.Add(item.TrimmedName, i);
}
}
// Then try to access the case insensitive parameter name
if (_lookupIgnoreCase.TryGetValue(parameterName, out retIndex))
return retIndex;
return -1;
}
// First try a case-sensitive match
for (var i = 0; i < _internalList.Count; i++)
if (parameterName == _internalList[i].TrimmedName)
return i;
// If not fond, try a case-insensitive match
for (var i = 0; i < _internalList.Count; i++)
if (string.Equals(parameterName, _internalList[i].TrimmedName, StringComparison.OrdinalIgnoreCase))
return i;
return -1;
}
#endregion
#region IList Member
/// <inheritdoc />
public override bool IsReadOnly => false;
/// <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]);
}
/// <inheritdoc />
public override void Insert(int index, object value)
{
if (value == null)
throw new ArgumentNullException(nameof(value));
if (!(value is NpgsqlParameter param))
throw new InvalidCastException($"{nameof(value)} must be an NpgsqlParameter");
if (param.Collection != null)
throw new InvalidOperationException("The parameter already belongs to a collection");
param.Collection = this;
_internalList.Insert(index, param);
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="value">The <see cref="NpgsqlParameter">NpgsqlParameter</see> to remove from the collection.</param>
public override void Remove(object value)
{
if (value == null)
throw new ArgumentNullException(nameof(value));
if (!(value is NpgsqlParameter param))
throw new InvalidCastException($"{nameof(value)} must be an NpgsqlParameter");
Remove(param);
}
/// <inheritdoc />
public override bool Contains(object value)
=> value is NpgsqlParameter param && _internalList.Contains(param);
/// <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)
{
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()
{
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();
}
/// <inheritdoc />
public override int IndexOf(object value)
{
if (value == null)
throw new ArgumentNullException(nameof(value));
if (!(value is NpgsqlParameter param))
throw new InvalidCastException($"{nameof(value)} must be an NpgsqlParameter");
return _internalList.IndexOf(param);
}
/// <inheritdoc />
public override int Add(object value)
{
if (value == null)
throw new ArgumentNullException(nameof(value));
if (!(value is NpgsqlParameter param))
throw new InvalidCastException($"{nameof(value)} must be an NpgsqlParameter");
Add(param);
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">NpgsqlParameter</see> objects in the collection.
/// </summary>
/// <value>The number of <see cref="NpgsqlParameter">NpgsqlParameter</see> objects in the collection.</value>
public override int Count => _internalList.Count;
/// <inheritdoc />
public override void CopyTo(Array array, int index)
{
if (array == null)
throw new ArgumentNullException(nameof(array));
((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
/// <inheritdoc />
public override void AddRange(Array values)
{
foreach (NpgsqlParameter parameter in values)
Add(parameter);
}
/// <inheritdoc />
protected override DbParameter GetParameter([CanBeNull] string parameterName)
=> parameterName == null
? throw new ArgumentNullException(nameof(parameterName))
: this[parameterName];
/// <inheritdoc />
protected override DbParameter GetParameter(int index)
=> this[index];
/// <inheritdoc />
protected override void SetParameter(string parameterName, DbParameter value)
{
if (parameterName == null)
throw new ArgumentNullException(nameof(parameterName));
this[parameterName] = (NpgsqlParameter) value;
}
/// <inheritdoc />
protected override void SetParameter(int index, DbParameter value)
=> this[index] = (NpgsqlParameter) 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>
[PublicAPI]
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>
[PublicAPI]
public void Insert(int index, NpgsqlParameter item)
{
if (item == null)
throw new ArgumentNullException(nameof(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) => _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));
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() => _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;
}
internal bool HasOutputParameters
{
get
{
foreach (var p in _internalList)
if (p.IsOutputDirection)
return true;
return false;
}
}
}
}