Skip to content

Commit da620de

Browse files
committed
Consolidate automatic Parameters and Context handling
1 parent 958c5ee commit da620de

35 files changed

Lines changed: 1216 additions & 442 deletions

BenchmarkTests/CacheCommandTests.cs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ public class NpgsqlCachedCommand : NpgsqlCommand
1111
{
1212
private static readonly NpgsqlCachedCommand _instanceCache = new();
1313

14+
#pragma warning disable CS8603 // Possible null reference return.
1415
private NpgsqlCommand CachedCommandClone() => MemberwiseClone() as NpgsqlCommand;
16+
#pragma warning restore CS8603 // Possible null reference return.
1517

1618
public static NpgsqlCommand Create(NpgsqlConnection connection)
1719
{
@@ -29,7 +31,9 @@ public class NpgsqlCachedParameter : NpgsqlParameter
2931
Value = DBNull.Value,
3032
};
3133

34+
#pragma warning disable CS8603 // Possible null reference return.
3235
public NpgsqlParameter CachedParameterMemberwiseClone() => MemberwiseClone() as NpgsqlParameter;
36+
#pragma warning restore CS8603 // Possible null reference return.
3337

3438
public static NpgsqlParameter CreateTextParam(string? value)
3539
{
@@ -45,17 +49,8 @@ public static NpgsqlParameter CreateTextParam(string? value)
4549
[MemoryDiagnoser]
4650
public class CacheCommandTests
4751
{
48-
private string _connectionStr = "Host=127.0.0.1;Port=5432;Database=postgres;Username=postgres;Password=postgres";
52+
private readonly string _connectionStr = "Host=127.0.0.1;Port=5432;Database=postgres;Username=postgres;Password=postgres";
4953

50-
[GlobalSetup]
51-
public void Setup()
52-
{
53-
}
54-
55-
[GlobalCleanup]
56-
public void Cleanup()
57-
{
58-
}
5954

6055
[Benchmark(Baseline = true)]
6156
public async Task NormalCommand()

NpgsqlRest/Auth/AuthHandler.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ public static async Task HandleLoginAsync(
122122
{
123123
userName = null;
124124
}
125-
else if (string.Equals(claimType, ClaimTypes.NameIdentifier, StringComparison.Ordinal))
125+
else if (string.Equals(claimType, options.AuthenticationOptions.DefaultUserIdClaimType, StringComparison.Ordinal))
126126
{
127127
userId = null;
128128
}
@@ -143,7 +143,7 @@ public static async Task HandleLoginAsync(
143143
{
144144
userName = value;
145145
}
146-
else if (string.Equals(claimType, ClaimTypes.NameIdentifier, StringComparison.Ordinal))
146+
else if (string.Equals(claimType, options.AuthenticationOptions.DefaultUserIdClaimType, StringComparison.Ordinal))
147147
{
148148
userId = value;
149149
}

NpgsqlRest/Auth/NpgsqlRestAuthenticationOptions.cs

Lines changed: 0 additions & 137 deletions
This file was deleted.

NpgsqlRest/Consts.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,7 @@ public static class Consts
3030
public const char At = '@';
3131
public const char Multiply = '*';
3232
public const char Question = '?';
33+
public const string EmptyArray = "[]";
34+
public const string EmptyObj = "{}";
3335
public const string SetContext = "select set_config($1,$2,false)";
3436
}

NpgsqlRest/Defaults/DefaultCommentParser.cs

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,15 @@ internal static class DefaultCommentParser
192192
"user-context",
193193
];
194194

195+
private static readonly string[] userParemetersKey = [
196+
"userparameters",
197+
"userparams",
198+
"user_parameters",
199+
"user_params",
200+
"user-parameters",
201+
"user-params",
202+
];
203+
195204
private const string UploadKey = "upload";
196205

197206
private static readonly string[] parameterKey = [
@@ -818,6 +827,21 @@ internal static class DefaultCommentParser
818827
}
819828
}
820829

830+
// userparameters
831+
// userparams
832+
// user_parameters
833+
// user_params
834+
// user-parameters
835+
// user-params
836+
else if (haveTag is true && StrEqualsToArray(words[0], userParemetersKey))
837+
{
838+
routineEndpoint.UseUserParameters = true;
839+
if (options.LogAnnotationSetInfo)
840+
{
841+
logger?.CommentUserParameters(description);
842+
}
843+
}
844+
821845
// cached
822846
// cached [ param1, param2, param3 [, ...] ]
823847
else if (haveTag is true && StrEquals(words[0], CacheKey))
@@ -904,7 +928,7 @@ internal static class DefaultCommentParser
904928
// upload param_name as metadata
905929
else if (haveTag is true && StrEquals(words[0], UploadKey))
906930
{
907-
if (options.UploadHandlers is null || options.UploadHandlers.Count == 0)
931+
if (options.UploadOptions.UploadHandlers is null || options.UploadOptions.UploadHandlers.Count == 0)
908932
{
909933
logger?.CommentUploadNoHandlers(description);
910934
}
@@ -928,7 +952,7 @@ internal static class DefaultCommentParser
928952
}
929953
if (len >= 3 && StrEquals(words[1], "for"))
930954
{
931-
HashSet<string> existingHandlers = options.UploadHandlers?.Keys.ToHashSet() ?? [];
955+
HashSet<string> existingHandlers = options.UploadOptions.UploadHandlers?.Keys.ToHashSet() ?? [];
932956
var handlers = words[2..]
933957
.Select(w =>
934958
{
@@ -950,7 +974,7 @@ internal static class DefaultCommentParser
950974
{
951975
if (handlers.Length == 0)
952976
{
953-
var first = options.UploadHandlers?.Keys.FirstOrDefault();
977+
var first = options.UploadOptions.UploadHandlers?.Keys.FirstOrDefault();
954978
logger?.CommentUploadFirstAvaialbleHandler(description, first);
955979
}
956980
if (handlers.Length == 1)
@@ -976,7 +1000,7 @@ internal static class DefaultCommentParser
9761000
}
9771001
else
9781002
{
979-
param.UploadMetadata = true;
1003+
param.IsUploadMetadata = true;
9801004
if (options.LogAnnotationSetInfo)
9811005
{
9821006
logger?.CommentUploadMetadataParam(description, paramName);
@@ -1075,7 +1099,7 @@ internal static class DefaultCommentParser
10751099
}
10761100
else
10771101
{
1078-
param.UploadMetadata = true;
1102+
param.IsUploadMetadata = true;
10791103
if (options.LogAnnotationSetInfo)
10801104
{
10811105
logger?.CommentUploadMetadataParam(description, paramName);
@@ -1160,6 +1184,13 @@ public static void SetCustomParameter(RoutineEndpoint endpoint, string name, str
11601184
endpoint.UserContext = parserUserContext;
11611185
}
11621186
}
1187+
else if (StrEqualsToArray(name, userParemetersKey))
1188+
{
1189+
if (bool.TryParse(value, out var parserUserParameters))
1190+
{
1191+
endpoint.UseUserParameters = parserUserParameters;
1192+
}
1193+
}
11631194
}
11641195

11651196
private static bool StrEquals(string str1, string str2) => str1.Equals(str2, StringComparison.OrdinalIgnoreCase);

NpgsqlRest/Defaults/DefaultEndpoint.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ internal static class DefaultEndpoint
3939
bodyParameterName: null,
4040
textResponseNullHandling: options.TextResponseNullHandling,
4141
queryStringNullHandling: options.QueryStringNullHandling,
42-
userContext: options.AuthenticationOptions.UseUserContext);
42+
userContext: options.AuthenticationOptions.UseUserContext,
43+
userParameters: options.AuthenticationOptions.UseUserParameters);
4344

4445
if (options.LogCommands && logger != null)
4546
{

0 commit comments

Comments
 (0)