forked from NpgsqlRest/NpgsqlRest
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNpgsqlRestParameter.cs
More file actions
109 lines (94 loc) · 3.78 KB
/
Copy pathNpgsqlRestParameter.cs
File metadata and controls
109 lines (94 loc) · 3.78 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
using System.Text.Json.Nodes;
using Microsoft.Extensions.Primitives;
using Npgsql;
namespace NpgsqlRest;
public class NpgsqlRestParameter : NpgsqlParameter
{
public int Ordinal { get; private set; }
public string ConvertedName { get; private set; }
public string ActualName { get; private set; }
public TypeDescriptor TypeDescriptor { get; init; }
public ParamType ParamType { get; set; } = default!;
public StringValues? QueryStringValues { get; set; } = null;
public JsonNode? JsonBodyNode { get; set; } = null;
public NpgsqlRestParameter? HashOf { get; set; } = null;
public bool IsUploadMetadata { get; set; } = false;
public bool IsIpAddress { get; set; } = false;
public string? UserClaim { get; set; } = null;
public bool IsUserClaims { get; set; } = false;
public bool IsFromUserClaims => UserClaim is not null || IsUserClaims is true;
public NpgsqlRestParameter(
NpgsqlRestOptions options,
int ordinal,
string convertedName,
string actualName,
TypeDescriptor typeDescriptor)
{
Ordinal = ordinal;
ConvertedName = convertedName;
ActualName = actualName;
TypeDescriptor = typeDescriptor;
NpgsqlDbType = typeDescriptor.ActualDbType;
if (actualName is not null &&
options.AuthenticationOptions.ParameterNameClaimsMapping.TryGetValue(actualName, out var claimName))
{
UserClaim = claimName;
}
if (actualName is not null &&
string.Equals(options.AuthenticationOptions.IpAddressParameterName, actualName, StringComparison.OrdinalIgnoreCase))
{
IsIpAddress = true;
}
if (actualName is not null &&
string.Equals(options.AuthenticationOptions.ClaimsJsonParameterName, actualName, StringComparison.OrdinalIgnoreCase))
{
IsUserClaims = true;
}
if (options.UploadOptions.UseDefaultUploadMetadataParameter is true)
{
if (string.Equals(options.UploadOptions.DefaultUploadMetadataParameterName, actualName, StringComparison.OrdinalIgnoreCase))
{
IsUploadMetadata = true;
}
}
}
internal string GetCacheStringValue()
{
if (Value == DBNull.Value)
{
return string.Empty;
}
if (TypeDescriptor.IsArray)
{
return string.Join(",", Value as object[] ?? []);
}
return Value?.ToString() ?? string.Empty;
}
#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring as nullable.
#pragma warning disable CS8603 // Possible null reference return.
public NpgsqlRestParameter NpgsqlRestParameterMemberwiseClone() => MemberwiseClone() as NpgsqlRestParameter;
private NpgsqlRestParameter() { }
#pragma warning restore CS8603 // Possible null reference return.
#pragma warning restore CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring as nullable.
private static readonly NpgsqlRestParameter _textParam = new()
{
NpgsqlDbType = NpgsqlTypes.NpgsqlDbType.Text,
Value = DBNull.Value,
};
public static NpgsqlParameter CreateParamWithType(NpgsqlTypes.NpgsqlDbType type)
{
var result = _textParam.NpgsqlRestParameterMemberwiseClone();
result.NpgsqlDbType = type;
return result;
}
public static NpgsqlParameter CreateTextParam(object? value)
{
var result = _textParam.NpgsqlRestParameterMemberwiseClone();
if (value is null)
{
return result;
}
result.Value = value;
return result;
}
}