Skip to content

Commit 7ae757a

Browse files
committed
refresh ednpoint
1 parent 5836702 commit 7ae757a

26 files changed

Lines changed: 1959 additions & 1387 deletions

.github/workflows/build-test-publish.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ jobs:
4343
env:
4444
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
4545
with:
46-
tag_name: v2.14.0-client-v2.5.0
47-
release_name: "AOT Client v2.5.0 NpgsqlRest v2.14.0"
46+
tag_name: v2.15.0-client-v2.6.0
47+
release_name: "AOT Client v2.6.0 NpgsqlRest v2.15.0"
4848
draft: true
4949
prerelease: true
5050

BenchmarkTests/BenchmarkTests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
<ItemGroup>
1212
<PackageReference Include="BenchmarkDotNet" Version="0.14.0" />
13-
<PackageReference Include="Npgsql" Version="9.0.1" />
13+
<PackageReference Include="Npgsql" Version="9.0.2" />
1414
<PackageReference Include="System.Text.Json" Version="9.0.0" />
1515
</ItemGroup>
1616

NpgsqlRest/Interfaces.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,9 @@ public interface IRoutineSource
104104
/// Yield all routines with the formatters from the current source.
105105
/// </summary>
106106
/// <param name="options">Current options</param>
107+
/// <param name="serviceProvider">Service provider</param>
107108
/// <returns></returns>
108-
IEnumerable<(Routine, IRoutineSourceParameterFormatter)> Read(NpgsqlRestOptions options);
109+
IEnumerable<(Routine, IRoutineSourceParameterFormatter)> Read(NpgsqlRestOptions options, IServiceProvider? serviceProvider);
109110

110111
/// <summary>
111112
/// SQL Query that returns data source.

NpgsqlRest/NpgsqlRest.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141

4242
<ItemGroup>
4343
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0" PrivateAssets="All" />
44-
<PackageReference Include="Npgsql" Version="9.0.1" />
44+
<PackageReference Include="Npgsql" Version="9.0.2" />
4545
<PackageReferenceFiles Include="bin\$(Configuration)\$(AssemblyName).xml" />
4646
</ItemGroup>
4747

NpgsqlRest/NpgsqlRestMetadata.cs

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
using System.Security.Cryptography.X509Certificates;
2+
using Npgsql;
3+
using NpgsqlRest.Defaults;
4+
5+
namespace NpgsqlRest;
6+
7+
public class NpgsqlRestMetadataEntry
8+
{
9+
internal NpgsqlRestMetadataEntry(
10+
Routine routine,
11+
RoutineEndpoint endpoint,
12+
IRoutineSourceParameterFormatter formatter,
13+
string key)
14+
{
15+
Routine = routine;
16+
Endpoint = endpoint;
17+
Formatter = formatter;
18+
Key = key;
19+
}
20+
21+
public Routine Routine { get; }
22+
public RoutineEndpoint Endpoint { get; }
23+
public IRoutineSourceParameterFormatter Formatter { get; }
24+
public string Key { get; }
25+
}
26+
27+
public class NpgsqlRestMetadata
28+
{
29+
internal NpgsqlRestMetadata(
30+
Dictionary<string, NpgsqlRestMetadataEntry> entries,
31+
Dictionary<string, NpgsqlRestMetadataEntry> overloads)
32+
{
33+
Entries = entries;
34+
Overloads = overloads;
35+
}
36+
37+
public Dictionary<string, NpgsqlRestMetadataEntry> Entries { get; }
38+
public Dictionary<string, NpgsqlRestMetadataEntry> Overloads { get; }
39+
}
40+
41+
public static class NpgsqlRestMetadataBuilder
42+
{
43+
public const int MaxKeyLength = 2056;
44+
public const int MaxPathLength = 2048;
45+
public static NpgsqlRestMetadata Build(NpgsqlRestOptions options, ILogger? logger, IApplicationBuilder? builder)
46+
{
47+
Dictionary<string, NpgsqlRestMetadataEntry> lookup = [];
48+
Dictionary<string, NpgsqlRestMetadataEntry> overloads = [];
49+
50+
var hasLogin = false;
51+
if (builder is not null)
52+
{
53+
foreach (var handler in options.EndpointCreateHandlers)
54+
{
55+
handler.Setup(builder, logger, options);
56+
}
57+
}
58+
59+
options.SourcesCreated(options.RoutineSources);
60+
61+
CommentsMode optionsCommentsMode = options.CommentsMode;
62+
foreach (var source in options.RoutineSources)
63+
{
64+
if (source.CommentsMode.HasValue)
65+
{
66+
options.CommentsMode = source.CommentsMode.Value;
67+
}
68+
else
69+
{
70+
options.CommentsMode = optionsCommentsMode;
71+
}
72+
foreach (var (routine, formatter) in source.Read(options, builder?.ApplicationServices))
73+
{
74+
RoutineEndpoint endpoint = DefaultEndpoint.Create(routine, options, logger)!;
75+
76+
if (endpoint is null)
77+
{
78+
continue;
79+
}
80+
81+
if (options.EndpointCreated is not null)
82+
{
83+
endpoint = options.EndpointCreated(routine, endpoint)!;
84+
}
85+
86+
if (endpoint is null)
87+
{
88+
continue;
89+
}
90+
91+
if (endpoint.Url.Length > MaxPathLength)
92+
{
93+
throw new ArgumentException($"URL path for URL {endpoint.Url}, routine {routine.Name} length exceeds {MaxPathLength} characters.");
94+
}
95+
96+
var method = endpoint.Method.ToString();
97+
if (endpoint.HasBodyParameter is true && endpoint.RequestParamType == RequestParamType.BodyJson)
98+
{
99+
endpoint.RequestParamType = RequestParamType.QueryString;
100+
logger?.EndpointTypeChanged(method, endpoint.Url, endpoint!.BodyParameterName ?? "");
101+
102+
}
103+
var key = string.Concat(method, endpoint?.Url);
104+
var value = new NpgsqlRestMetadataEntry(routine, endpoint!, formatter, key);
105+
if (lookup.TryGetValue(key, out var existing))
106+
{
107+
overloads[string.Concat(key, existing.Routine.ParamCount)] = existing;
108+
}
109+
lookup[key] = value;
110+
111+
if (builder is not null)
112+
{
113+
foreach (var handler in options.EndpointCreateHandlers)
114+
{
115+
handler.Handle(routine, endpoint!);
116+
}
117+
}
118+
119+
if (options.LogEndpointCreatedInfo)
120+
{
121+
logger?.EndpointCreated(method, endpoint!.Url);
122+
}
123+
124+
if (endpoint?.Login is true)
125+
{
126+
if (hasLogin is false)
127+
{
128+
hasLogin = true;
129+
}
130+
if (routine.IsVoid is true || routine.ReturnsUnnamedSet is true)
131+
{
132+
throw new ArgumentException($"{routine.Type.ToString().ToLowerInvariant()} {routine.Schema}.{routine.Name} is marked as login and it can't be void or returning unnamed data sets.");
133+
}
134+
}
135+
}
136+
}
137+
138+
if (hasLogin is true)
139+
{
140+
if (options.AuthenticationOptions.DefaultAuthenticationType is null)
141+
{
142+
string db = new NpgsqlConnectionStringBuilder(options.ConnectionString).Database ?? "NpgsqlRest";
143+
options.AuthenticationOptions.DefaultAuthenticationType = db;
144+
logger?.SetDefaultAuthenticationType(db);
145+
}
146+
}
147+
148+
if (options.EndpointsCreated is not null)
149+
{
150+
options.EndpointsCreated(lookup.Values.Select(x => (x.Routine, x.Endpoint)).ToArray());
151+
}
152+
153+
if (builder is not null)
154+
{
155+
(Routine routine, RoutineEndpoint endpoint)[]? array = null;
156+
foreach (var handler in options.EndpointCreateHandlers)
157+
{
158+
array ??= lookup.Values.Select(x => (x.Routine, x.Endpoint)).ToArray();
159+
handler.Cleanup(array);
160+
handler.Cleanup();
161+
}
162+
}
163+
164+
return new NpgsqlRestMetadata(lookup, overloads);
165+
}
166+
}

0 commit comments

Comments
 (0)