-
Notifications
You must be signed in to change notification settings - Fork 874
Expand file tree
/
Copy pathNpgsqlDataSourceBuilder.cs
More file actions
664 lines (607 loc) · 35 KB
/
NpgsqlDataSourceBuilder.cs
File metadata and controls
664 lines (607 loc) · 35 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
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
using System;
using System.Diagnostics.CodeAnalysis;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Npgsql.Internal;
using Npgsql.Internal.ResolverFactories;
using Npgsql.NameTranslation;
using Npgsql.TypeMapping;
using NpgsqlTypes;
namespace Npgsql;
/// <summary>
/// Provides a simple API for configuring and creating an <see cref="NpgsqlDataSource" />, from which database connections can be obtained.
/// </summary>
public sealed class NpgsqlDataSourceBuilder : INpgsqlTypeMapper
{
static UnsupportedTypeInfoResolver<NpgsqlDataSourceBuilder> UnsupportedTypeInfoResolver { get; } = new();
readonly NpgsqlSlimDataSourceBuilder _internalBuilder;
/// <summary>
/// A diagnostics name used by Npgsql when generating tracing, logging and metrics.
/// </summary>
public string? Name
{
get => _internalBuilder.Name;
set => _internalBuilder.Name = value;
}
/// <inheritdoc />
public INpgsqlNameTranslator DefaultNameTranslator
{
get => _internalBuilder.DefaultNameTranslator;
set => _internalBuilder.DefaultNameTranslator = value;
}
/// <summary>
/// A connection string builder that can be used to configure the connection string on the builder.
/// </summary>
public NpgsqlConnectionStringBuilder ConnectionStringBuilder => _internalBuilder.ConnectionStringBuilder;
/// <summary>
/// Returns the connection string, as currently configured on the builder.
/// </summary>
public string ConnectionString => _internalBuilder.ConnectionString;
internal static void ResetGlobalMappings(bool overwrite)
=> GlobalTypeMapper.Instance.AddGlobalTypeMappingResolvers([
overwrite ? new AdoTypeInfoResolverFactory() : AdoTypeInfoResolverFactory.Instance,
new ExtraConversionResolverFactory(),
new JsonTypeInfoResolverFactory(),
new RecordTypeInfoResolverFactory(),
new FullTextSearchTypeInfoResolverFactory(),
new NetworkTypeInfoResolverFactory(),
new GeometricTypeInfoResolverFactory(),
new LTreeTypeInfoResolverFactory(),
new CubeTypeInfoResolverFactory()
], static () =>
{
var builder = new PgTypeInfoResolverChainBuilder();
builder.EnableRanges();
builder.EnableMultiranges();
builder.EnableArrays();
return builder;
}, overwrite);
static NpgsqlDataSourceBuilder()
=> ResetGlobalMappings(overwrite: false);
/// <summary>
/// Constructs a new <see cref="NpgsqlDataSourceBuilder" />, optionally starting out from the given <paramref name="connectionString"/>.
/// </summary>
public NpgsqlDataSourceBuilder(string? connectionString = null)
{
_internalBuilder = new(new NpgsqlConnectionStringBuilder(connectionString));
_internalBuilder.ConfigureDefaultFactories = static instance =>
{
instance.AppendDefaultFactories();
instance.AppendResolverFactory(new ExtraConversionResolverFactory());
instance.AppendResolverFactory(() => new JsonTypeInfoResolverFactory(instance.JsonSerializerOptions));
instance.AppendResolverFactory(new RecordTypeInfoResolverFactory());
instance.AppendResolverFactory(new FullTextSearchTypeInfoResolverFactory());
instance.AppendResolverFactory(new NetworkTypeInfoResolverFactory());
instance.AppendResolverFactory(new GeometricTypeInfoResolverFactory());
instance.AppendResolverFactory(new LTreeTypeInfoResolverFactory());
instance.AppendResolverFactory(new CubeTypeInfoResolverFactory());
};
_internalBuilder.ConfigureResolverChain = static chain => chain.Add(UnsupportedTypeInfoResolver);
_internalBuilder.EnableTransportSecurity();
_internalBuilder.EnableIntegratedSecurity();
_internalBuilder.EnableRanges();
_internalBuilder.EnableMultiranges();
_internalBuilder.EnableArrays();
}
/// <summary>
/// Sets the <see cref="ILoggerFactory" /> that will be used for logging.
/// </summary>
/// <param name="loggerFactory">The logger factory to be used.</param>
/// <returns>The same builder instance so that multiple calls can be chained.</returns>
public NpgsqlDataSourceBuilder UseLoggerFactory(ILoggerFactory? loggerFactory)
{
_internalBuilder.UseLoggerFactory(loggerFactory);
return this;
}
/// <summary>
/// Enables parameters to be included in logging. This includes potentially sensitive information from data sent to PostgreSQL.
/// You should only enable this flag in development, or if you have the appropriate security measures in place based on the
/// sensitivity of this data.
/// </summary>
/// <param name="parameterLoggingEnabled">If <see langword="true" />, then sensitive data is logged.</param>
/// <returns>The same builder instance so that multiple calls can be chained.</returns>
public NpgsqlDataSourceBuilder EnableParameterLogging(bool parameterLoggingEnabled = true)
{
_internalBuilder.EnableParameterLogging(parameterLoggingEnabled);
return this;
}
/// <summary>
/// Configures type loading options for the DataSource.
/// </summary>
public NpgsqlDataSourceBuilder ConfigureTypeLoading(Action<NpgsqlTypeLoadingOptionsBuilder> configureAction)
{
_internalBuilder.ConfigureTypeLoading(configureAction);
return this;
}
/// <summary>
/// Configures OpenTelemetry tracing options.
/// </summary>
/// <returns>The same builder instance so that multiple calls can be chained.</returns>
public NpgsqlDataSourceBuilder ConfigureTracing(Action<NpgsqlTracingOptionsBuilder> configureAction)
{
_internalBuilder.ConfigureTracing(configureAction);
return this;
}
/// <summary>
/// Configures the JSON serializer options used when reading and writing all System.Text.Json data.
/// </summary>
/// <param name="serializerOptions">Options to customize JSON serialization and deserialization.</param>
/// <returns>The same builder instance so that multiple calls can be chained.</returns>
public NpgsqlDataSourceBuilder ConfigureJsonOptions(JsonSerializerOptions serializerOptions)
{
_internalBuilder.ConfigureJsonOptions(serializerOptions);
return this;
}
/// <summary>
/// Sets up dynamic System.Text.Json mappings. This allows mapping arbitrary .NET types to PostgreSQL <c>json</c> and <c>jsonb</c>
/// types, as well as <see cref="JsonNode" /> and its derived types.
/// </summary>
/// <param name="jsonbClrTypes">
/// A list of CLR types to map to PostgreSQL <c>jsonb</c> (no need to specify <see cref="NpgsqlDbType.Jsonb" />).
/// </param>
/// <param name="jsonClrTypes">
/// A list of CLR types to map to PostgreSQL <c>json</c> (no need to specify <see cref="NpgsqlDbType.Json" />).
/// </param>
/// <remarks>
/// Due to the dynamic nature of these mappings, they are not compatible with NativeAOT or trimming.
/// </remarks>
[RequiresUnreferencedCode("Json serializer may perform reflection on trimmed types.")]
[RequiresDynamicCode("Serializing arbitrary types to json can require creating new generic types or methods, which requires creating code at runtime. This may not work when AOT compiling.")]
public NpgsqlDataSourceBuilder EnableDynamicJson(Type[]? jsonbClrTypes = null, Type[]? jsonClrTypes = null)
{
_internalBuilder.EnableDynamicJson(jsonbClrTypes, jsonClrTypes);
return this;
}
/// <summary>
/// Sets up mappings for the PostgreSQL <c>record</c> type as a .NET <see cref="ValueTuple" /> or <see cref="Tuple" />.
/// </summary>
/// <returns>The same builder instance so that multiple calls can be chained.</returns>
[RequiresUnreferencedCode("The mapping of PostgreSQL records as .NET tuples requires reflection usage which is incompatible with trimming.")]
[RequiresDynamicCode("The mapping of PostgreSQL records as .NET tuples requires dynamic code usage which is incompatible with NativeAOT.")]
public NpgsqlDataSourceBuilder EnableRecordsAsTuples()
{
AddTypeInfoResolverFactory(new TupledRecordTypeInfoResolverFactory());
return this;
}
/// <summary>
/// Sets up mappings allowing the use of unmapped enum, range and multirange types.
/// </summary>
/// <returns>The same builder instance so that multiple calls can be chained.</returns>
[RequiresUnreferencedCode("The use of unmapped enums, ranges or multiranges requires reflection usage which is incompatible with trimming.")]
[RequiresDynamicCode("The use of unmapped enums, ranges or multiranges requires dynamic code usage which is incompatible with NativeAOT.")]
public NpgsqlDataSourceBuilder EnableUnmappedTypes()
{
AddTypeInfoResolverFactory(new UnmappedTypeInfoResolverFactory());
return this;
}
#region Authentication
/// <summary>
/// When using SSL/TLS, this is a callback that allows customizing how the PostgreSQL-provided certificate is verified. This is an
/// advanced API, consider using <see cref="SslMode.VerifyFull" /> or <see cref="SslMode.VerifyCA" /> instead.
/// </summary>
/// <param name="userCertificateValidationCallback">The callback containing custom callback verification logic.</param>
/// <remarks>
/// <para>
/// Cannot be used in conjunction with <see cref="SslMode.Disable" />, <see cref="SslMode.VerifyCA" /> or
/// <see cref="SslMode.VerifyFull" />.
/// </para>
/// <para>
/// See <see href="https://msdn.microsoft.com/en-us/library/system.net.security.remotecertificatevalidationcallback(v=vs.110).aspx"/>.
/// </para>
/// </remarks>
/// <returns>The same builder instance so that multiple calls can be chained.</returns>
[Obsolete("Use UseSslClientAuthenticationOptionsCallback")]
public NpgsqlDataSourceBuilder UseUserCertificateValidationCallback(RemoteCertificateValidationCallback userCertificateValidationCallback)
{
_internalBuilder.UseUserCertificateValidationCallback(userCertificateValidationCallback);
return this;
}
/// <summary>
/// Specifies an SSL/TLS certificate which Npgsql will send to PostgreSQL for certificate-based authentication.
/// </summary>
/// <param name="clientCertificate">The client certificate to be sent to PostgreSQL when opening a connection.</param>
/// <returns>The same builder instance so that multiple calls can be chained.</returns>
[Obsolete("Use UseSslClientAuthenticationOptionsCallback")]
public NpgsqlDataSourceBuilder UseClientCertificate(X509Certificate? clientCertificate)
{
_internalBuilder.UseClientCertificate(clientCertificate);
return this;
}
/// <summary>
/// Specifies a collection of SSL/TLS certificates which Npgsql will send to PostgreSQL for certificate-based authentication.
/// </summary>
/// <param name="clientCertificates">The client certificate collection to be sent to PostgreSQL when opening a connection.</param>
/// <returns>The same builder instance so that multiple calls can be chained.</returns>
[Obsolete("Use UseSslClientAuthenticationOptionsCallback")]
public NpgsqlDataSourceBuilder UseClientCertificates(X509CertificateCollection? clientCertificates)
{
_internalBuilder.UseClientCertificates(clientCertificates);
return this;
}
/// <summary>
/// When using SSL/TLS, this is a callback that allows customizing SslStream's authentication options.
/// </summary>
/// <param name="sslClientAuthenticationOptionsCallback">The callback to customize SslStream's authentication options.</param>
/// <remarks>
/// <para>
/// See <see href="https://learn.microsoft.com/en-us/dotnet/api/system.net.security.sslclientauthenticationoptions"/>.
/// </para>
/// </remarks>
/// <returns>The same builder instance so that multiple calls can be chained.</returns>
public NpgsqlDataSourceBuilder UseSslClientAuthenticationOptionsCallback(Action<SslClientAuthenticationOptions>? sslClientAuthenticationOptionsCallback)
{
_internalBuilder.UseSslClientAuthenticationOptionsCallback(sslClientAuthenticationOptionsCallback);
return this;
}
/// <summary>
/// Specifies a callback to modify the collection of SSL/TLS client certificates which Npgsql will send to PostgreSQL for
/// certificate-based authentication. This is an advanced API, consider using <see cref="UseClientCertificate" /> or
/// <see cref="UseClientCertificates" /> instead.
/// </summary>
/// <param name="clientCertificatesCallback">The callback to modify the client certificate collection.</param>
/// <remarks>
/// <para>
/// The callback is invoked every time a physical connection is opened, and is therefore suitable for rotating short-lived client
/// certificates. Simply make sure the certificate collection argument has the up-to-date certificate(s).
/// </para>
/// <para>
/// The callback's collection argument already includes any client certificates specified via the connection string or environment
/// variables.
/// </para>
/// </remarks>
/// <returns>The same builder instance so that multiple calls can be chained.</returns>
[Obsolete("Use UseSslClientAuthenticationOptionsCallback")]
public NpgsqlDataSourceBuilder UseClientCertificatesCallback(Action<X509CertificateCollection>? clientCertificatesCallback)
{
_internalBuilder.UseClientCertificatesCallback(clientCertificatesCallback);
return this;
}
/// <summary>
/// Sets the <see cref="X509Certificate2" /> that will be used validate SSL certificate, received from the server.
/// </summary>
/// <param name="rootCertificate">The CA certificate.</param>
/// <returns>The same builder instance so that multiple calls can be chained.</returns>
public NpgsqlDataSourceBuilder UseRootCertificate(X509Certificate2? rootCertificate)
{
_internalBuilder.UseRootCertificate(rootCertificate);
return this;
}
/// <summary>
/// Sets the <see cref="X509Certificate2Collection" /> that will be used validate SSL certificate, received from the server.
/// </summary>
/// <param name="rootCertificates">The CA certificates.</param>
/// <returns>The same builder instance so that multiple calls can be chained.</returns>
public NpgsqlDataSourceBuilder UseRootCertificates(X509Certificate2Collection? rootCertificates)
{
_internalBuilder.UseRootCertificates(rootCertificates);
return this;
}
/// <summary>
/// Specifies a callback that will be used to validate SSL certificate, received from the server.
/// </summary>
/// <param name="rootCertificateCallback">The callback to get CA certificate.</param>
/// <returns>The same builder instance so that multiple calls can be chained.</returns>
/// <remarks>
/// This overload, which accepts a callback, is suitable for scenarios where the certificate rotates
/// and might change during the lifetime of the application.
/// When that's not the case, use the overload which directly accepts the certificate.
/// </remarks>
public NpgsqlDataSourceBuilder UseRootCertificateCallback(Func<X509Certificate2>? rootCertificateCallback)
{
_internalBuilder.UseRootCertificateCallback(rootCertificateCallback);
return this;
}
/// <summary>
/// Specifies a callback that will be used to validate SSL certificate, received from the server.
/// </summary>
/// <param name="rootCertificateCallback">The callback to get CA certificates.</param>
/// <returns>The same builder instance so that multiple calls can be chained.</returns>
/// <remarks>
/// This overload, which accepts a callback, is suitable for scenarios where the certificate rotates
/// and might change during the lifetime of the application.
/// When that's not the case, use the overload which directly accepts the certificate.
/// </remarks>
/// <returns>The same builder instance so that multiple calls can be chained.</returns>
public NpgsqlDataSourceBuilder UseRootCertificatesCallback(Func<X509Certificate2Collection>? rootCertificateCallback)
{
_internalBuilder.UseRootCertificatesCallback(rootCertificateCallback);
return this;
}
/// <summary>
/// Configures a periodic password provider, which is automatically called by the data source at some regular interval. This is the
/// recommended way to fetch a rotating access token.
/// </summary>
/// <param name="passwordProvider">A callback which returns the password to be sent to PostgreSQL.</param>
/// <param name="successRefreshInterval">How long to cache the password before re-invoking the callback.</param>
/// <param name="failureRefreshInterval">
/// If a password refresh attempt fails, it will be re-attempted with this interval.
/// This should typically be much lower than <paramref name="successRefreshInterval" />.
/// </param>
/// <returns>The same builder instance so that multiple calls can be chained.</returns>
/// <remarks>
/// <para>
/// The provided callback is invoked in a timer, and not when opening connections. It therefore doesn't affect opening time.
/// </para>
/// <para>
/// The provided cancellation token is only triggered when the entire data source is disposed. If you'd like to apply a timeout to the
/// token fetching, do so within the provided callback.
/// </para>
/// </remarks>
public NpgsqlDataSourceBuilder UsePeriodicPasswordProvider(
Func<NpgsqlConnectionStringBuilder, CancellationToken, ValueTask<string>>? passwordProvider,
TimeSpan successRefreshInterval,
TimeSpan failureRefreshInterval)
{
_internalBuilder.UsePeriodicPasswordProvider(passwordProvider, successRefreshInterval, failureRefreshInterval);
return this;
}
/// <summary>
/// Configures a password provider, which is called by the data source when opening connections.
/// </summary>
/// <param name="passwordProvider">
/// A callback that may be invoked during <see cref="NpgsqlConnection.Open()" /> which returns the password to be sent to PostgreSQL.
/// </param>
/// <param name="passwordProviderAsync">
/// A callback that may be invoked during <see cref="NpgsqlConnection.OpenAsync(CancellationToken)" /> which returns the password to be sent to PostgreSQL.
/// </param>
/// <returns>The same builder instance so that multiple calls can be chained.</returns>
/// <remarks>
/// <para>
/// The provided callback is invoked when opening connections. Therefore its important the callback internally depends on cached
/// data or returns quickly otherwise. Any unnecessary delay will affect connection opening time.
/// </para>
/// </remarks>
public NpgsqlDataSourceBuilder UsePasswordProvider(
Func<NpgsqlConnectionStringBuilder, string>? passwordProvider,
Func<NpgsqlConnectionStringBuilder, CancellationToken, ValueTask<string>>? passwordProviderAsync)
{
_internalBuilder.UsePasswordProvider(passwordProvider, passwordProviderAsync);
return this;
}
/// <summary>
/// When using Kerberos, this is a callback that allows customizing default settings for Kerberos authentication.
/// </summary>
/// <param name="negotiateOptionsCallback">The callback containing logic to customize Kerberos authentication settings.</param>
/// <remarks>
/// <para>
/// See <see href="https://learn.microsoft.com/en-us/dotnet/api/system.net.security.negotiateauthenticationclientoptions?view=net-7.0"/>.
/// </para>
/// </remarks>
/// <returns>The same builder instance so that multiple calls can be chained.</returns>
public NpgsqlDataSourceBuilder UseNegotiateOptionsCallback(Action<NegotiateAuthenticationClientOptions>? negotiateOptionsCallback)
{
_internalBuilder.UseNegotiateOptionsCallback(negotiateOptionsCallback);
return this;
}
#endregion Authentication
#region Type mapping
/// <inheritdoc />
void INpgsqlTypeMapper.AddDbTypeResolverFactory(DbTypeResolverFactory factory)
=> ((INpgsqlTypeMapper)_internalBuilder).AddDbTypeResolverFactory(factory);
/// <inheritdoc />
[Experimental(NpgsqlDiagnostics.ConvertersExperimental)]
public void AddTypeInfoResolverFactory(PgTypeInfoResolverFactory factory)
=> _internalBuilder.AddTypeInfoResolverFactory(factory);
/// <inheritdoc />
void INpgsqlTypeMapper.Reset() => ((INpgsqlTypeMapper)_internalBuilder).Reset();
/// <summary>
/// Maps a CLR enum to a PostgreSQL enum type.
/// </summary>
/// <remarks>
/// CLR enum labels are mapped by name to PostgreSQL enum labels.
/// The translation strategy can be controlled by the <paramref name="nameTranslator"/> parameter,
/// which defaults to <see cref="NpgsqlSnakeCaseNameTranslator"/>.
/// You can also use the <see cref="PgNameAttribute"/> on your enum fields to manually specify a PostgreSQL enum label.
/// If there is a discrepancy between the .NET and database labels while an enum is read or written,
/// an exception will be raised.
/// </remarks>
/// <param name="pgName">
/// A PostgreSQL type name for the corresponding enum type in the database.
/// If null, the name translator given in <paramref name="nameTranslator"/> will be used.
/// </param>
/// <param name="nameTranslator">
/// A component which will be used to translate CLR names (e.g. SomeClass) into database names (e.g. some_class).
/// Defaults to <see cref="DefaultNameTranslator" />.
/// </param>
/// <typeparam name="TEnum">The .NET enum type to be mapped</typeparam>
public NpgsqlDataSourceBuilder MapEnum<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields)] TEnum>(string? pgName = null, INpgsqlNameTranslator? nameTranslator = null)
where TEnum : struct, Enum
{
_internalBuilder.MapEnum<TEnum>(pgName, nameTranslator);
return this;
}
/// <inheritdoc />
public bool UnmapEnum<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields)] TEnum>(string? pgName = null, INpgsqlNameTranslator? nameTranslator = null)
where TEnum : struct, Enum
=> _internalBuilder.UnmapEnum<TEnum>(pgName, nameTranslator);
/// <summary>
/// Maps a CLR enum to a PostgreSQL enum type.
/// </summary>
/// <remarks>
/// CLR enum labels are mapped by name to PostgreSQL enum labels.
/// The translation strategy can be controlled by the <paramref name="nameTranslator"/> parameter,
/// which defaults to <see cref="NpgsqlSnakeCaseNameTranslator"/>.
/// You can also use the <see cref="PgNameAttribute"/> on your enum fields to manually specify a PostgreSQL enum label.
/// If there is a discrepancy between the .NET and database labels while an enum is read or written,
/// an exception will be raised.
/// </remarks>
/// <param name="clrType">The .NET enum type to be mapped</param>
/// <param name="pgName">
/// A PostgreSQL type name for the corresponding enum type in the database.
/// If null, the name translator given in <paramref name="nameTranslator"/> will be used.
/// </param>
/// <param name="nameTranslator">
/// A component which will be used to translate CLR names (e.g. SomeClass) into database names (e.g. some_class).
/// Defaults to <see cref="DefaultNameTranslator" />.
/// </param>
[RequiresDynamicCode("Calling MapEnum with a Type can require creating new generic types or methods. This may not work when AOT compiling.")]
public NpgsqlDataSourceBuilder MapEnum([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)]
Type clrType, string? pgName = null, INpgsqlNameTranslator? nameTranslator = null)
{
_internalBuilder.MapEnum(clrType, pgName, nameTranslator);
return this;
}
/// <inheritdoc />
public bool UnmapEnum([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)]
Type clrType, string? pgName = null, INpgsqlNameTranslator? nameTranslator = null)
=> _internalBuilder.UnmapEnum(clrType, pgName, nameTranslator);
/// <summary>
/// Maps a CLR type to a PostgreSQL composite type.
/// </summary>
/// <remarks>
/// CLR fields and properties by string to PostgreSQL names.
/// The translation strategy can be controlled by the <paramref name="nameTranslator"/> parameter,
/// which defaults to <see cref="NpgsqlSnakeCaseNameTranslator"/>.
/// You can also use the <see cref="PgNameAttribute"/> on your members to manually specify a PostgreSQL name.
/// If there is a discrepancy between the .NET type and database type while a composite is read or written,
/// an exception will be raised.
/// </remarks>
/// <param name="pgName">
/// A PostgreSQL type name for the corresponding composite type in the database.
/// If null, the name translator given in <paramref name="nameTranslator"/> will be used.
/// </param>
/// <param name="nameTranslator">
/// A component which will be used to translate CLR names (e.g. SomeClass) into database names (e.g. some_class).
/// Defaults to <see cref="DefaultNameTranslator" />.
/// </param>
/// <typeparam name="T">The .NET type to be mapped</typeparam>
[RequiresDynamicCode("Mapping composite types involves serializing arbitrary types which can require creating new generic types or methods. This is currently unsupported with NativeAOT, vote on issue #5303 if this is important to you.")]
public NpgsqlDataSourceBuilder MapComposite<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.PublicProperties | DynamicallyAccessedMemberTypes.PublicFields)] T>(
string? pgName = null, INpgsqlNameTranslator? nameTranslator = null)
{
_internalBuilder.MapComposite(typeof(T), pgName, nameTranslator);
return this;
}
/// <inheritdoc />
[RequiresDynamicCode("Mapping composite types involves serializing arbitrary types which can require creating new generic types or methods. This is currently unsupported with NativeAOT, vote on issue #5303 if this is important to you.")]
public bool UnmapComposite<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.PublicProperties | DynamicallyAccessedMemberTypes.PublicFields)] T>(
string? pgName = null, INpgsqlNameTranslator? nameTranslator = null)
=> _internalBuilder.UnmapComposite(typeof(T), pgName, nameTranslator);
/// <summary>
/// Maps a CLR type to a composite type.
/// </summary>
/// <remarks>
/// Maps CLR fields and properties by string to PostgreSQL names.
/// The translation strategy can be controlled by the <paramref name="nameTranslator"/> parameter,
/// which defaults to <see cref="DefaultNameTranslator" />.
/// If there is a discrepancy between the .NET type and database type while a composite is read or written,
/// an exception will be raised.
/// </remarks>
/// <param name="clrType">The .NET type to be mapped.</param>
/// <param name="pgName">
/// A PostgreSQL type name for the corresponding composite type in the database.
/// If null, the name translator given in <paramref name="nameTranslator"/> will be used.
/// </param>
/// <param name="nameTranslator">
/// A component which will be used to translate CLR names (e.g. SomeClass) into database names (e.g. some_class).
/// Defaults to <see cref="DefaultNameTranslator" />.
/// </param>
[RequiresDynamicCode("Mapping composite types involves serializing arbitrary types which can require creating new generic types or methods. This is currently unsupported with NativeAOT, vote on issue #5303 if this is important to you.")]
public NpgsqlDataSourceBuilder MapComposite([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.PublicProperties | DynamicallyAccessedMemberTypes.PublicFields)]
Type clrType, string? pgName = null, INpgsqlNameTranslator? nameTranslator = null)
{
_internalBuilder.MapComposite(clrType, pgName, nameTranslator);
return this;
}
/// <inheritdoc />
[RequiresDynamicCode("Mapping composite types involves serializing arbitrary types which can require creating new generic types or methods. This is currently unsupported with NativeAOT, vote on issue #5303 if this is important to you.")]
public bool UnmapComposite([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.PublicProperties | DynamicallyAccessedMemberTypes.PublicFields)]
Type clrType, string? pgName = null, INpgsqlNameTranslator? nameTranslator = null)
=> _internalBuilder.UnmapComposite(clrType, pgName, nameTranslator);
#endregion Type mapping
/// <summary>
/// Register a connection initializer, which allows executing arbitrary commands when a physical database connection is first opened.
/// </summary>
/// <param name="connectionInitializer">
/// A synchronous connection initialization lambda, which will be called from <see cref="NpgsqlConnection.Open()" /> when a new physical
/// connection is opened.
/// </param>
/// <param name="connectionInitializerAsync">
/// An asynchronous connection initialization lambda, which will be called from
/// <see cref="NpgsqlConnection.OpenAsync(CancellationToken)" /> when a new physical connection is opened.
/// </param>
/// <remarks>
/// If an initializer is registered, both sync and async versions must be provided. If you do not use sync APIs in your code, simply
/// throw <see cref="NotSupportedException" />, which would also catch accidental cases of sync opening.
/// </remarks>
/// <remarks>
/// Take care that the setting you apply in the initializer does not get reverted when the connection is returned to the pool, since
/// Npgsql sends <c>DISCARD ALL</c> by default. The <see cref="NpgsqlConnectionStringBuilder.NoResetOnClose" /> option can be used to
/// turn this off.
/// </remarks>
/// <returns>The same builder instance so that multiple calls can be chained.</returns>
public NpgsqlDataSourceBuilder UsePhysicalConnectionInitializer(
Action<NpgsqlConnection>? connectionInitializer,
Func<NpgsqlConnection, Task>? connectionInitializerAsync)
{
_internalBuilder.UsePhysicalConnectionInitializer(connectionInitializer, connectionInitializerAsync);
return this;
}
/// <summary>
/// Builds and returns an <see cref="NpgsqlDataSource" /> which is ready for use.
/// </summary>
public NpgsqlDataSource Build()
=> _internalBuilder.Build();
/// <summary>
/// Builds and returns a <see cref="NpgsqlMultiHostDataSource" /> which is ready for use for load-balancing and failover scenarios.
/// </summary>
public NpgsqlMultiHostDataSource BuildMultiHost()
=> _internalBuilder.BuildMultiHost();
// Used in testing.
internal (NpgsqlConnectionStringBuilder, NpgsqlDataSourceConfiguration) PrepareConfiguration()
=> _internalBuilder.PrepareConfiguration();
INpgsqlTypeMapper INpgsqlTypeMapper.ConfigureJsonOptions(JsonSerializerOptions serializerOptions)
=> ConfigureJsonOptions(serializerOptions);
[RequiresUnreferencedCode("Json serializer may perform reflection on trimmed types.")]
[RequiresDynamicCode(
"Serializing arbitrary types to json can require creating new generic types or methods, which requires creating code at runtime. This may not work when AOT compiling.")]
INpgsqlTypeMapper INpgsqlTypeMapper.EnableDynamicJson(Type[]? jsonbClrTypes, Type[]? jsonClrTypes)
=> EnableDynamicJson(jsonbClrTypes, jsonClrTypes);
[RequiresUnreferencedCode(
"The mapping of PostgreSQL records as .NET tuples requires reflection usage which is incompatible with trimming.")]
[RequiresDynamicCode(
"The mapping of PostgreSQL records as .NET tuples requires dynamic code usage which is incompatible with NativeAOT.")]
INpgsqlTypeMapper INpgsqlTypeMapper.EnableRecordsAsTuples()
=> EnableRecordsAsTuples();
[RequiresUnreferencedCode(
"The use of unmapped enums, ranges or multiranges requires reflection usage which is incompatible with trimming.")]
[RequiresDynamicCode(
"The use of unmapped enums, ranges or multiranges requires dynamic code usage which is incompatible with NativeAOT.")]
INpgsqlTypeMapper INpgsqlTypeMapper.EnableUnmappedTypes()
=> EnableUnmappedTypes();
/// <inheritdoc />
INpgsqlTypeMapper INpgsqlTypeMapper.MapEnum<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields)] TEnum>(string? pgName, INpgsqlNameTranslator? nameTranslator)
{
_internalBuilder.MapEnum<TEnum>(pgName, nameTranslator);
return this;
}
/// <inheritdoc />
[RequiresDynamicCode("Calling MapEnum with a Type can require creating new generic types or methods. This may not work when AOT compiling.")]
INpgsqlTypeMapper INpgsqlTypeMapper.MapEnum([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)]
Type clrType, string? pgName, INpgsqlNameTranslator? nameTranslator)
{
_internalBuilder.MapEnum(clrType, pgName, nameTranslator);
return this;
}
/// <inheritdoc />
[RequiresDynamicCode("Mapping composite types involves serializing arbitrary types which can require creating new generic types or methods. This is currently unsupported with NativeAOT, vote on issue #5303 if this is important to you.")]
INpgsqlTypeMapper INpgsqlTypeMapper.MapComposite<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.PublicProperties | DynamicallyAccessedMemberTypes.PublicFields)] T>(
string? pgName, INpgsqlNameTranslator? nameTranslator)
{
_internalBuilder.MapComposite(typeof(T), pgName, nameTranslator);
return this;
}
/// <inheritdoc />
[RequiresDynamicCode("Mapping composite types involves serializing arbitrary types which can require creating new generic types or methods. This is currently unsupported with NativeAOT, vote on issue #5303 if this is important to you.")]
INpgsqlTypeMapper INpgsqlTypeMapper.MapComposite([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.PublicProperties | DynamicallyAccessedMemberTypes.PublicFields)]
Type clrType, string? pgName, INpgsqlNameTranslator? nameTranslator)
{
_internalBuilder.MapComposite(clrType, pgName, nameTranslator);
return this;
}
}