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
102 lines (88 loc) · 3.38 KB
/
NpgsqlParameter`.cs
File metadata and controls
102 lines (88 loc) · 3.38 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
using System;
using System.Data;
using System.Diagnostics.CodeAnalysis;
using System.Threading.Tasks;
using Npgsql.TypeMapping;
using NpgsqlTypes;
namespace Npgsql
{
/// <summary>
/// A generic version of <see cref="NpgsqlParameter"/> which provides more type safety and
/// avoids boxing of value types. Use <see cref="TypedValue"/> instead of <see cref="NpgsqlParameter.Value"/>.
/// </summary>
/// <typeparam name="T">The type of the value that will be stored in the parameter.</typeparam>
public sealed class NpgsqlParameter<T> : NpgsqlParameter
{
/// <summary>
/// Gets or sets the strongly-typed value of the parameter.
/// </summary>
[MaybeNull, AllowNull]
public T TypedValue { get; set; } = default!;
/// <summary>
/// Gets or sets the value of the parameter. This delegates to <see cref="TypedValue"/>.
/// </summary>
#nullable disable
public override object Value
#nullable restore
{
get => TypedValue;
set => TypedValue = (T)value;
}
#region Constructors
/// <summary>
/// Initializes a new instance of <see cref="NpgsqlParameter{T}" />.
/// </summary>
public NpgsqlParameter() {}
/// <summary>
/// Initializes a new instance of <see cref="NpgsqlParameter{T}" /> with a parameter name and value.
/// </summary>
public NpgsqlParameter(string parameterName, T value)
{
ParameterName = parameterName;
TypedValue = value;
}
/// <summary>
/// Initializes a new instance of <see cref="NpgsqlParameter{T}" /> with a parameter name and type.
/// </summary>
public NpgsqlParameter(string parameterName, NpgsqlDbType npgsqlDbType)
{
ParameterName = parameterName;
NpgsqlDbType = npgsqlDbType;
}
/// <summary>
/// Initializes a new instance of <see cref="NpgsqlParameter{T}" /> with a parameter name and type.
/// </summary>
public NpgsqlParameter(string parameterName, DbType dbType)
{
ParameterName = parameterName;
DbType = dbType;
}
#endregion Constructors
internal override void ResolveHandler(ConnectorTypeMapper typeMapper)
{
if (Handler != null)
return;
// TODO: Better exceptions in case of cast failure etc.
if (_npgsqlDbType.HasValue)
Handler = typeMapper.GetByNpgsqlDbType(_npgsqlDbType.Value);
else if (_dataTypeName != null)
Handler = typeMapper.GetByDataTypeName(_dataTypeName);
else
Handler = typeMapper.GetByClrType(typeof(T));
}
internal override int ValidateAndGetLength()
{
if (TypedValue == null)
return 0;
// TODO: Why do it like this rather than a handler?
if (typeof(T) == typeof(DBNull))
return 0;
var lengthCache = LengthCache;
var len = Handler!.ValidateAndGetLength(TypedValue, ref lengthCache, this);
LengthCache = lengthCache;
return len;
}
internal override Task WriteWithLength(NpgsqlWriteBuffer buf, bool async)
=> Handler!.WriteWithLengthInternal(TypedValue, buf, LengthCache, this, async);
}
}