forked from npgsql/npgsql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNpgsqlParameter.cs
More file actions
572 lines (507 loc) · 22.8 KB
/
NpgsqlParameter.cs
File metadata and controls
572 lines (507 loc) · 22.8 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
using System;
using System.ComponentModel;
using System.Data;
using System.Data.Common;
using System.Threading.Tasks;
using JetBrains.Annotations;
using Npgsql.PostgresTypes;
using Npgsql.TypeHandling;
using Npgsql.TypeMapping;
using Npgsql.Util;
using NpgsqlTypes;
namespace Npgsql
{
///<summary>
/// This class represents a parameter to a command that will be sent to server
///</summary>
public class NpgsqlParameter : DbParameter, IDbDataParameter, ICloneable
{
#region Fields and Properties
byte _precision;
byte _scale;
int _size;
// ReSharper disable InconsistentNaming
private protected NpgsqlDbType? _npgsqlDbType;
private protected string? _dataTypeName;
// ReSharper restore InconsistentNaming
DbType? _cachedDbType;
string _name = string.Empty;
object? _value;
string _sourceColumn;
internal string TrimmedName { get; private set; } = string.Empty;
/// <summary>
/// Can be used to communicate a value from the validation phase to the writing phase.
/// To be used by type handlers only.
/// </summary>
public object? ConvertedValue { get; set; }
internal NpgsqlLengthCache? LengthCache { get; set; }
internal NpgsqlTypeHandler? Handler { get; set; }
internal FormatCode FormatCode { get; private set; }
#endregion
#region Constructors
/// <summary>
/// Initializes a new instance of the <see cref="NpgsqlParameter">NpgsqlParameter</see> class.
/// </summary>
public NpgsqlParameter()
{
_sourceColumn = string.Empty;
Direction = ParameterDirection.Input;
SourceVersion = DataRowVersion.Current;
}
#nullable disable
/// <summary>
/// Initializes a new instance of the <see cref="NpgsqlParameter">NpgsqlParameter</see>
/// class with the parameter name and a value of the new <b>NpgsqlParameter</b>.
/// </summary>
/// <param name="parameterName">The name of the parameter to map.</param>
/// <param name="value">An <see cref="System.Object">Object</see> that is the value of the <see cref="NpgsqlParameter">NpgsqlParameter</see>.</param>
/// <remarks>
/// <p>When you specify an <see cref="System.Object">Object</see>
/// in the value parameter, the <see cref="System.Data.DbType">DbType</see> is
/// inferred from the .NET Framework type of the <b>Object</b>.</p>
/// <p>When using this constructor, you must be aware of a possible misuse of the constructor which takes a DbType parameter.
/// This happens when calling this constructor passing an int 0 and the compiler thinks you are passing a value of DbType.
/// Use <code> Convert.ToInt32(value) </code> for example to have compiler calling the correct constructor.</p>
/// </remarks>
public NpgsqlParameter(string parameterName, object value)
: this()
{
ParameterName = parameterName;
// ReSharper disable once VirtualMemberCallInConstructor
Value = value;
}
/// <summary>
/// Initializes a new instance of the <see cref="NpgsqlParameter">NpgsqlParameter</see>
/// class with the parameter name and the data type.
/// </summary>
/// <param name="parameterName">The name of the parameter to map.</param>
/// <param name="parameterType">One of the <see cref="NpgsqlTypes.NpgsqlDbType">NpgsqlDbType</see> values.</param>
public NpgsqlParameter(string parameterName, NpgsqlDbType parameterType)
: this(parameterName, parameterType, 0, string.Empty)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="NpgsqlParameter">NpgsqlParameter</see>.
/// </summary>
/// <param name="parameterName">The name of the parameter to map.</param>
/// <param name="parameterType">One of the <see cref="System.Data.DbType">DbType</see> values.</param>
public NpgsqlParameter(string parameterName, DbType parameterType)
: this(parameterName, parameterType, 0, string.Empty)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="NpgsqlParameter">NpgsqlParameter</see>.
/// </summary>
/// <param name="parameterName">The name of the parameter to map.</param>
/// <param name="parameterType">One of the <see cref="NpgsqlTypes.NpgsqlDbType">NpgsqlDbType</see> values.</param>
/// <param name="size">The length of the parameter.</param>
public NpgsqlParameter(string parameterName, NpgsqlDbType parameterType, int size)
: this(parameterName, parameterType, size, string.Empty)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="NpgsqlParameter">NpgsqlParameter</see>.
/// </summary>
/// <param name="parameterName">The name of the parameter to map.</param>
/// <param name="parameterType">One of the <see cref="System.Data.DbType">DbType</see> values.</param>
/// <param name="size">The length of the parameter.</param>
public NpgsqlParameter(string parameterName, DbType parameterType, int size)
: this(parameterName, parameterType, size, string.Empty)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="NpgsqlParameter">NpgsqlParameter</see>
/// </summary>
/// <param name="parameterName">The name of the parameter to map.</param>
/// <param name="parameterType">One of the <see cref="NpgsqlTypes.NpgsqlDbType">NpgsqlDbType</see> values.</param>
/// <param name="size">The length of the parameter.</param>
/// <param name="sourceColumn">The name of the source column.</param>
public NpgsqlParameter(string parameterName, NpgsqlDbType parameterType, int size, string sourceColumn)
{
ParameterName = parameterName;
NpgsqlDbType = parameterType;
_size = size;
SourceColumn = sourceColumn;
Direction = ParameterDirection.Input;
SourceVersion = DataRowVersion.Current;
}
/// <summary>
/// Initializes a new instance of the <see cref="NpgsqlParameter">NpgsqlParameter</see>.
/// </summary>
/// <param name="parameterName">The name of the parameter to map.</param>
/// <param name="parameterType">One of the <see cref="System.Data.DbType">DbType</see> values.</param>
/// <param name="size">The length of the parameter.</param>
/// <param name="sourceColumn">The name of the source column.</param>
public NpgsqlParameter(string parameterName, DbType parameterType, int size, string sourceColumn)
{
ParameterName = parameterName;
DbType = parameterType;
_size = size;
_sourceColumn = sourceColumn;
Direction = ParameterDirection.Input;
SourceVersion = DataRowVersion.Current;
}
/// <summary>
/// Initializes a new instance of the <see cref="NpgsqlParameter">NpgsqlParameter</see>.
/// </summary>
/// <param name="parameterName">The name of the parameter to map.</param>
/// <param name="parameterType">One of the <see cref="NpgsqlTypes.NpgsqlDbType">NpgsqlDbType</see> values.</param>
/// <param name="size">The length of the parameter.</param>
/// <param name="sourceColumn">The name of the source column.</param>
/// <param name="direction">One of the <see cref="System.Data.ParameterDirection">ParameterDirection</see> values.</param>
/// <param name="isNullable"><b>true</b> if the value of the field can be null, otherwise <b>false</b>.</param>
/// <param name="precision">The total number of digits to the left and right of the decimal point to which
/// <see cref="NpgsqlParameter.Value">Value</see> is resolved.</param>
/// <param name="scale">The total number of decimal places to which
/// <see cref="NpgsqlParameter.Value">Value</see> is resolved.</param>
/// <param name="sourceVersion">One of the <see cref="System.Data.DataRowVersion">DataRowVersion</see> values.</param>
/// <param name="value">An <see cref="System.Object">Object</see> that is the value
/// of the <see cref="NpgsqlParameter">NpgsqlParameter</see>.</param>
public NpgsqlParameter(string parameterName, NpgsqlDbType parameterType, int size, string sourceColumn,
ParameterDirection direction, bool isNullable, byte precision, byte scale,
DataRowVersion sourceVersion, object value)
{
ParameterName = parameterName;
Size = size;
_sourceColumn = sourceColumn;
Direction = direction;
IsNullable = isNullable;
Precision = precision;
Scale = scale;
SourceVersion = sourceVersion;
// ReSharper disable once VirtualMemberCallInConstructor
Value = value;
NpgsqlDbType = parameterType;
}
/// <summary>
/// Initializes a new instance of the <see cref="NpgsqlParameter">NpgsqlParameter</see>.
/// </summary>
/// <param name="parameterName">The name of the parameter to map.</param>
/// <param name="parameterType">One of the <see cref="System.Data.DbType">DbType</see> values.</param>
/// <param name="size">The length of the parameter.</param>
/// <param name="sourceColumn">The name of the source column.</param>
/// <param name="direction">One of the <see cref="System.Data.ParameterDirection">ParameterDirection</see> values.</param>
/// <param name="isNullable"><b>true</b> if the value of the field can be null, otherwise <b>false</b>.</param>
/// <param name="precision">The total number of digits to the left and right of the decimal point to which
/// <see cref="NpgsqlParameter.Value">Value</see> is resolved.</param>
/// <param name="scale">The total number of decimal places to which
/// <see cref="NpgsqlParameter.Value">Value</see> is resolved.</param>
/// <param name="sourceVersion">One of the <see cref="System.Data.DataRowVersion">DataRowVersion</see> values.</param>
/// <param name="value">An <see cref="System.Object">Object</see> that is the value
/// of the <see cref="NpgsqlParameter">NpgsqlParameter</see>.</param>
public NpgsqlParameter(string parameterName, DbType parameterType, int size, string sourceColumn,
ParameterDirection direction, bool isNullable, byte precision, byte scale,
DataRowVersion sourceVersion, object value)
{
ParameterName = parameterName;
Size = size;
_sourceColumn = sourceColumn;
Direction = direction;
IsNullable = isNullable;
Precision = precision;
Scale = scale;
SourceVersion = sourceVersion;
// ReSharper disable once VirtualMemberCallInConstructor
Value = value;
DbType = parameterType;
}
#nullable restore
#endregion
#region Name
/// <summary>
/// Gets or sets The name of the <see cref="NpgsqlParameter">NpgsqlParameter</see>.
/// </summary>
/// <value>The name of the <see cref="NpgsqlParameter">NpgsqlParameter</see>.
/// The default is an empty string.</value>
[DefaultValue("")]
#nullable disable
public sealed override string ParameterName
#nullable restore
{
get => _name;
set
{
// ReSharper disable once ConditionIsAlwaysTrueOrFalse
if (value == null)
_name = TrimmedName = string.Empty;
else if (value.Length > 0 && (value[0] == ':' || value[0] == '@'))
TrimmedName = (_name = value).Substring(1);
else
_name = TrimmedName = value;
Collection?.InvalidateHashLookups();
}
}
#endregion Name
#region Value
/// <inheritdoc />
[TypeConverter(typeof(StringConverter)), Category("Data")]
#nullable disable
public override object Value
#nullable restore
{
get => _value;
set
{
if (_value == null || value == null || _value.GetType() != value.GetType())
Handler = null;
_value = value;
ConvertedValue = null;
}
}
/// <summary>
/// Gets or sets the value of the parameter.
/// </summary>
/// <value>An <see cref="System.Object">Object</see> that is the value of the parameter.
/// The default value is null.</value>
[Category("Data")]
[TypeConverter(typeof(StringConverter))]
#nullable disable
public object NpgsqlValue
#nullable restore
{
get => Value;
set => Value = value;
}
#endregion Value
#region Type
/// <summary>
/// Gets or sets the <see cref="System.Data.DbType">DbType</see> of the parameter.
/// </summary>
/// <value>One of the <see cref="System.Data.DbType">DbType</see> values. The default is <b>Object</b>.</value>
[DefaultValue(DbType.Object)]
[Category("Data"), RefreshProperties(RefreshProperties.All)]
public sealed override DbType DbType
{
get
{
if (_cachedDbType.HasValue)
return _cachedDbType.Value;
if (_npgsqlDbType.HasValue)
return _cachedDbType ??= GlobalTypeMapper.Instance.ToDbType(_npgsqlDbType.Value);
if (_value != null) // Infer from value but don't cache
return GlobalTypeMapper.Instance.ToDbType(_value.GetType());
return DbType.Object;
}
set
{
Handler = null;
if (value == DbType.Object)
{
_cachedDbType = null;
_npgsqlDbType = null;
}
else
{
_cachedDbType = value;
_npgsqlDbType = GlobalTypeMapper.Instance.ToNpgsqlDbType(value);
}
}
}
/// <summary>
/// Gets or sets the <see cref="NpgsqlTypes.NpgsqlDbType">NpgsqlDbType</see> of the parameter.
/// </summary>
/// <value>One of the <see cref="NpgsqlTypes.NpgsqlDbType">NpgsqlDbType</see> values. The default is <b>Unknown</b>.</value>
[DefaultValue(NpgsqlDbType.Unknown)]
[Category("Data"), RefreshProperties(RefreshProperties.All)]
[DbProviderSpecificTypeProperty(true)]
public NpgsqlDbType NpgsqlDbType
{
get
{
if (_npgsqlDbType.HasValue)
return _npgsqlDbType.Value;
if (_value != null) // Infer from value
return GlobalTypeMapper.Instance.ToNpgsqlDbType(_value.GetType());
return NpgsqlDbType.Unknown;
}
set
{
if (value == NpgsqlDbType.Array)
throw new ArgumentOutOfRangeException(nameof(value), "Cannot set NpgsqlDbType to just Array, Binary-Or with the element type (e.g. Array of Box is NpgsqlDbType.Array | NpgsqlDbType.Box).");
if (value == NpgsqlDbType.Range)
throw new ArgumentOutOfRangeException(nameof(value), "Cannot set NpgsqlDbType to just Range, Binary-Or with the element type (e.g. Range of integer is NpgsqlDbType.Range | NpgsqlDbType.Integer)");
Handler = null;
_npgsqlDbType = value;
_cachedDbType = null;
}
}
/// <summary>
/// Used to specify which PostgreSQL type will be sent to the database for this parameter.
/// </summary>
[PublicAPI]
public string? DataTypeName
{
get
{
if (_dataTypeName != null)
return _dataTypeName;
throw new NotImplementedException("Infer from others");
}
set
{
_dataTypeName = value;
Handler = null;
}
}
#endregion Type
#region Other Properties
/// <inheritdoc />
public sealed override bool IsNullable { get; set; }
/// <inheritdoc />
[DefaultValue(ParameterDirection.Input)]
[Category("Data")]
public sealed override ParameterDirection Direction { get; set; }
#pragma warning disable CS0109
/// <summary>
/// Gets or sets the maximum number of digits used to represent the
/// <see cref="NpgsqlParameter.Value">Value</see> property.
/// </summary>
/// <value>The maximum number of digits used to represent the
/// <see cref="NpgsqlParameter.Value">Value</see> property.
/// The default value is 0, which indicates that the data provider
/// sets the precision for <b>Value</b>.</value>
[DefaultValue((byte)0)]
[Category("Data")]
public new byte Precision
{
get => _precision;
set
{
_precision = value;
Handler = null;
}
}
/// <summary>
/// Gets or sets the number of decimal places to which
/// <see cref="NpgsqlParameter.Value">Value</see> is resolved.
/// </summary>
/// <value>The number of decimal places to which
/// <see cref="NpgsqlParameter.Value">Value</see> is resolved. The default is 0.</value>
[DefaultValue((byte)0)]
[Category("Data")]
public new byte Scale
{
get => _scale;
set
{
_scale = value;
Handler = null;
}
}
#pragma warning restore CS0109
/// <inheritdoc />
[DefaultValue(0)]
[Category("Data")]
public sealed override int Size
{
get => _size;
set
{
if (value < -1)
throw new ArgumentException($"Invalid parameter Size value '{value}'. The value must be greater than or equal to 0.");
_size = value;
Handler = null;
}
}
/// <inheritdoc />
[DefaultValue("")]
[Category("Data")]
public sealed override string? SourceColumn
{
get => _sourceColumn;
set => _sourceColumn = value ?? string.Empty;
}
/// <inheritdoc />
[Category("Data"), DefaultValue(DataRowVersion.Current)]
public sealed override DataRowVersion SourceVersion { get; set; }
/// <inheritdoc />
public sealed override bool SourceColumnNullMapping { get; set; }
#pragma warning disable CA2227
/// <summary>
/// The collection to which this parameter belongs, if any.
/// </summary>
public NpgsqlParameterCollection? Collection { get; set; }
#pragma warning restore CA2227
/// <summary>
/// The PostgreSQL data type, such as int4 or text, as discovered from pg_type.
/// This property is automatically set if parameters have been derived via
/// <see cref="NpgsqlCommandBuilder.DeriveParameters"/> and can be used to
/// acquire additional information about the parameters' data type.
/// </summary>
public PostgresType? PostgresType { get; internal set; }
#endregion Other Properties
#region Internals
internal virtual void ResolveHandler(ConnectorTypeMapper typeMapper)
{
if (Handler != null)
return;
if (_npgsqlDbType.HasValue)
Handler = typeMapper.GetByNpgsqlDbType(_npgsqlDbType.Value);
else if (_dataTypeName != null)
Handler = typeMapper.GetByDataTypeName(_dataTypeName);
else if (_value != null)
Handler = typeMapper.GetByClrType(_value.GetType());
else
throw new InvalidOperationException($"Parameter '{ParameterName}' must have its value set");
}
internal void Bind(ConnectorTypeMapper typeMapper)
{
ResolveHandler(typeMapper);
FormatCode = Handler!.PreferTextWrite ? FormatCode.Text : FormatCode.Binary;
}
internal virtual int ValidateAndGetLength()
{
if (_value == null)
throw new InvalidCastException($"Parameter {ParameterName} must be set");
if (_value is DBNull)
return 0;
var lengthCache = LengthCache;
var len = Handler!.ValidateObjectAndGetLength(_value, ref lengthCache, this);
LengthCache = lengthCache;
return len;
}
internal virtual Task WriteWithLength(NpgsqlWriteBuffer buf, bool async)
=> Handler!.WriteObjectWithLength(_value!, buf, LengthCache, this, async);
/// <inheritdoc />
public override void ResetDbType()
{
_cachedDbType = null;
_npgsqlDbType = null;
_dataTypeName = null;
Handler = null;
}
internal bool IsInputDirection => Direction == ParameterDirection.InputOutput || Direction == ParameterDirection.Input;
internal bool IsOutputDirection => Direction == ParameterDirection.InputOutput || Direction == ParameterDirection.Output;
#endregion
#region Clone
/// <summary>
/// Creates a new <see cref="NpgsqlParameter">NpgsqlParameter</see> that
/// is a copy of the current instance.
/// </summary>
/// <returns>A new <see cref="NpgsqlParameter">NpgsqlParameter</see> that is a copy of this instance.</returns>
public NpgsqlParameter Clone()
{
// use fields instead of properties
// to avoid auto-initializing something like type_info
var clone = new NpgsqlParameter
{
_precision = _precision,
_scale = _scale,
_size = _size,
_cachedDbType = _cachedDbType,
_npgsqlDbType = _npgsqlDbType,
Direction = Direction,
IsNullable = IsNullable,
_name = _name,
TrimmedName = TrimmedName,
SourceColumn = SourceColumn,
SourceVersion = SourceVersion,
_value = _value,
SourceColumnNullMapping = SourceColumnNullMapping,
};
return clone;
}
object ICloneable.Clone() => Clone();
#endregion
}
}