-
Notifications
You must be signed in to change notification settings - Fork 257
Expand file tree
/
Copy pathNpgsqlModelBuilderExtensions.cs
More file actions
801 lines (708 loc) · 33.6 KB
/
NpgsqlModelBuilderExtensions.cs
File metadata and controls
801 lines (708 loc) · 33.6 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
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata.Internal;
using Npgsql.NameTranslation;
// ReSharper disable once CheckNamespace
namespace Microsoft.EntityFrameworkCore;
/// <summary>
/// Npgsql-specific extension methods for <see cref="ModelBuilder" />.
/// </summary>
public static class NpgsqlModelBuilderExtensions
{
#region HiLo
/// <summary>
/// Configures the model to use a sequence-based hi-lo pattern to generate values for properties
/// marked as <see cref="ValueGenerated.OnAdd" />, when targeting PostgreSQL.
/// </summary>
/// <param name="modelBuilder">The model builder.</param>
/// <param name="name">The name of the sequence.</param>
/// <param name="schema">The schema of the sequence.</param>
/// <returns>The same builder instance so that multiple calls can be chained.</returns>
public static ModelBuilder UseHiLo(this ModelBuilder modelBuilder, string? name = null, string? schema = null)
{
Check.NotNull(modelBuilder, nameof(modelBuilder));
Check.NullButNotEmpty(name, nameof(name));
Check.NullButNotEmpty(schema, nameof(schema));
var model = modelBuilder.Model;
name ??= NpgsqlModelExtensions.DefaultHiLoSequenceName;
if (model.FindSequence(name, schema) is null)
{
modelBuilder.HasSequence(name, schema).IncrementsBy(10);
}
model.SetValueGenerationStrategy(NpgsqlValueGenerationStrategy.SequenceHiLo);
model.SetHiLoSequenceName(name);
model.SetHiLoSequenceSchema(schema);
model.SetSequenceNameSuffix(null);
model.SetSequenceSchema(null);
return modelBuilder;
}
/// <summary>
/// Configures the database sequence used for the hi-lo pattern to generate values for key properties
/// marked as <see cref="ValueGenerated.OnAdd" />, when targeting PostgreSQL.
/// </summary>
/// <param name="modelBuilder"> The model builder. </param>
/// <param name="name"> The name of the sequence. </param>
/// <param name="schema">The schema of the sequence. </param>
/// <param name="fromDataAnnotation"> Indicates whether the configuration was specified using a data annotation. </param>
/// <returns> A builder to further configure the sequence. </returns>
public static IConventionSequenceBuilder? HasHiLoSequence(
this IConventionModelBuilder modelBuilder,
string? name,
string? schema,
bool fromDataAnnotation = false)
{
if (!modelBuilder.CanSetHiLoSequence(name, schema))
{
return null;
}
modelBuilder.Metadata.SetHiLoSequenceName(name, fromDataAnnotation);
modelBuilder.Metadata.SetHiLoSequenceSchema(schema, fromDataAnnotation);
return name is null ? null : modelBuilder.HasSequence(name, schema, fromDataAnnotation);
}
/// <summary>
/// Returns a value indicating whether the given name and schema can be set for the hi-lo sequence.
/// </summary>
/// <param name="modelBuilder"> The model builder. </param>
/// <param name="name"> The name of the sequence. </param>
/// <param name="schema">The schema of the sequence. </param>
/// <param name="fromDataAnnotation"> Indicates whether the configuration was specified using a data annotation. </param>
/// <returns> <c>true</c> if the given name and schema can be set for the hi-lo sequence. </returns>
public static bool CanSetHiLoSequence(
this IConventionModelBuilder modelBuilder,
string? name,
string? schema,
bool fromDataAnnotation = false)
{
Check.NotNull(modelBuilder, nameof(modelBuilder));
Check.NullButNotEmpty(name, nameof(name));
Check.NullButNotEmpty(schema, nameof(schema));
return modelBuilder.CanSetAnnotation(NpgsqlAnnotationNames.HiLoSequenceName, name, fromDataAnnotation)
&& modelBuilder.CanSetAnnotation(NpgsqlAnnotationNames.HiLoSequenceSchema, schema, fromDataAnnotation);
}
#endregion HiLo
#region Serial
/// <summary>
/// <para>
/// Configures the model to use the PostgreSQL SERIAL feature to generate values for properties
/// marked as <see cref="ValueGenerated.OnAdd" />, when targeting PostgreSQL.
/// </para>
/// <para>
/// This option should be considered deprecated starting with PostgreSQL 10, consider using <see cref="UseIdentityColumns" /> instead.
/// </para>
/// </summary>
/// <param name="modelBuilder">The model builder.</param>
/// <returns>The same builder instance so that multiple calls can be chained.</returns>
public static ModelBuilder UseSerialColumns(
this ModelBuilder modelBuilder)
{
Check.NotNull(modelBuilder, nameof(modelBuilder));
var property = modelBuilder.Model;
property.SetValueGenerationStrategy(NpgsqlValueGenerationStrategy.SerialColumn);
property.SetHiLoSequenceName(null);
property.SetHiLoSequenceSchema(null);
return modelBuilder;
}
#endregion Serial
#region Identity
/// <summary>
/// <para>
/// Configures the model to use the PostgreSQL IDENTITY feature to generate values for properties
/// marked as <see cref="ValueGenerated.OnAdd" />, when targeting PostgreSQL. Values for these
/// columns will always be generated as identity, and the application will not be able to override
/// this behavior by providing a value.
/// </para>
/// <para>Available only starting PostgreSQL 10.</para>
/// </summary>
/// <param name="modelBuilder">The model builder.</param>
/// <returns>The same builder instance so that multiple calls can be chained.</returns>
public static ModelBuilder UseIdentityAlwaysColumns(this ModelBuilder modelBuilder)
{
Check.NotNull(modelBuilder, nameof(modelBuilder));
var model = modelBuilder.Model;
model.SetValueGenerationStrategy(NpgsqlValueGenerationStrategy.IdentityAlwaysColumn);
model.SetSequenceNameSuffix(null);
model.SetSequenceSchema(null);
model.SetHiLoSequenceName(null);
model.SetHiLoSequenceSchema(null);
return modelBuilder;
}
/// <summary>
/// <para>
/// Configures the model to use the PostgreSQL IDENTITY feature to generate values for properties
/// marked as <see cref="ValueGenerated.OnAdd" />, when targeting PostgreSQL. Values for these
/// columns will be generated as identity by default, but the application will be able to override
/// this behavior by providing a value.
/// </para>
/// <para>
/// This is the default behavior when targeting PostgreSQL. Available only starting PostgreSQL 10.
/// </para>
/// </summary>
/// <param name="modelBuilder">The model builder.</param>
/// <returns>The same builder instance so that multiple calls can be chained.</returns>
public static ModelBuilder UseIdentityByDefaultColumns(this ModelBuilder modelBuilder)
{
Check.NotNull(modelBuilder, nameof(modelBuilder));
var model = modelBuilder.Model;
model.SetValueGenerationStrategy(NpgsqlValueGenerationStrategy.IdentityByDefaultColumn);
model.SetSequenceNameSuffix(null);
model.SetSequenceSchema(null);
model.SetHiLoSequenceName(null);
model.SetHiLoSequenceSchema(null);
return modelBuilder;
}
/// <summary>
/// <para>
/// Configures the model to use the PostgreSQL IDENTITY feature to generate values for properties
/// marked as <see cref="ValueGenerated.OnAdd" />, when targeting PostgreSQL. Values for these
/// columns will be generated as identity by default, but the application will be able to override
/// this behavior by providing a value.
/// </para>
/// <para>
/// This is the default behavior when targeting PostgreSQL. Available only starting PostgreSQL 10.
/// </para>
/// </summary>
/// <param name="modelBuilder">The model builder.</param>
/// <returns>The same builder instance so that multiple calls can be chained.</returns>
public static ModelBuilder UseIdentityColumns(this ModelBuilder modelBuilder)
=> modelBuilder.UseIdentityByDefaultColumns();
/// <summary>
/// Configures the value generation strategy for the key property, when targeting PostgreSQL.
/// </summary>
/// <param name="modelBuilder">The builder for the property being configured.</param>
/// <param name="valueGenerationStrategy">The value generation strategy.</param>
/// <param name="fromDataAnnotation">Indicates whether the configuration was specified using a data annotation.</param>
/// <returns>
/// The same builder instance if the configuration was applied, <c>null</c> otherwise.
/// </returns>
public static IConventionModelBuilder? HasValueGenerationStrategy(
this IConventionModelBuilder modelBuilder,
NpgsqlValueGenerationStrategy? valueGenerationStrategy,
bool fromDataAnnotation = false)
{
if (modelBuilder.CanSetValueGenerationStrategy(valueGenerationStrategy, fromDataAnnotation))
{
modelBuilder.Metadata.SetValueGenerationStrategy(valueGenerationStrategy, fromDataAnnotation);
if (valueGenerationStrategy != NpgsqlValueGenerationStrategy.SequenceHiLo)
{
modelBuilder.HasHiLoSequence(null, null, fromDataAnnotation);
}
if (valueGenerationStrategy != NpgsqlValueGenerationStrategy.Sequence)
{
if (modelBuilder.CanSetAnnotation(NpgsqlAnnotationNames.SequenceNameSuffix, null)
&& modelBuilder.CanSetAnnotation(NpgsqlAnnotationNames.SequenceSchema, null))
{
modelBuilder.Metadata.SetSequenceNameSuffix(null, fromDataAnnotation);
modelBuilder.Metadata.SetSequenceSchema(null, fromDataAnnotation);
}
}
return modelBuilder;
}
return null;
}
/// <summary>
/// Returns a value indicating whether the given value can be set as the default value generation strategy.
/// </summary>
/// <param name="modelBuilder"> The model builder. </param>
/// <param name="valueGenerationStrategy"> The value generation strategy. </param>
/// <param name="fromDataAnnotation"> Indicates whether the configuration was specified using a data annotation. </param>
/// <returns> <c>true</c> if the given value can be set as the default value generation strategy. </returns>
public static bool CanSetValueGenerationStrategy(
this IConventionModelBuilder modelBuilder,
NpgsqlValueGenerationStrategy? valueGenerationStrategy,
bool fromDataAnnotation = false)
{
Check.NotNull(modelBuilder, nameof(modelBuilder));
return modelBuilder.CanSetAnnotation(
NpgsqlAnnotationNames.ValueGenerationStrategy, valueGenerationStrategy, fromDataAnnotation);
}
#endregion Identity
#region Sequence
/// <summary>
/// Configures the model to use a sequence per hierarchy to generate values for key properties marked as
/// <see cref="ValueGenerated.OnAdd" />, when targeting PostgreSQL.
/// </summary>
/// <param name="modelBuilder">The model builder.</param>
/// <param name="nameSuffix">The name that will suffix the table name for each sequence created automatically.</param>
/// <param name="schema">The schema of the sequence.</param>
/// <returns>The same builder instance so that multiple calls can be chained.</returns>
public static ModelBuilder UseKeySequences(
this ModelBuilder modelBuilder,
string? nameSuffix = null,
string? schema = null)
{
Check.NullButNotEmpty(nameSuffix, nameof(nameSuffix));
Check.NullButNotEmpty(schema, nameof(schema));
var model = modelBuilder.Model;
nameSuffix ??= NpgsqlModelExtensions.DefaultSequenceNameSuffix;
model.SetValueGenerationStrategy(NpgsqlValueGenerationStrategy.Sequence);
model.SetSequenceNameSuffix(nameSuffix);
model.SetSequenceSchema(schema);
model.SetHiLoSequenceName(null);
model.SetHiLoSequenceSchema(null);
return modelBuilder;
}
#endregion Sequence
#region Extensions
/// <summary>
/// Registers a PostgreSQL extension in the model.
/// </summary>
/// <param name="modelBuilder">The model builder in which to define the extension.</param>
/// <param name="schema">The schema in which to create the extension.</param>
/// <param name="name">The name of the extension to create.</param>
/// <param name="version">The version of the extension.</param>
/// <returns>The same builder instance so that multiple calls can be chained.</returns>
/// <remarks>
/// See: https://www.postgresql.org/docs/current/external-extensions.html
/// </remarks>
/// <exception cref="ArgumentNullException">
/// <paramref name="modelBuilder" />
/// </exception>
public static ModelBuilder HasPostgresExtension(
this ModelBuilder modelBuilder,
string? schema,
string name,
string? version = null)
{
Check.NotNull(modelBuilder, nameof(modelBuilder));
Check.NullButNotEmpty(schema, nameof(schema));
Check.NotEmpty(name, nameof(name));
modelBuilder.Model.GetOrAddPostgresExtension(schema, name, version);
return modelBuilder;
}
/// <summary>
/// Registers a PostgreSQL extension in the model.
/// </summary>
/// <param name="modelBuilder">The model builder in which to define the extension.</param>
/// <param name="name">The name of the extension to create.</param>
/// <returns>The same builder instance so that multiple calls can be chained.</returns>
/// <remarks>
/// See: https://www.postgresql.org/docs/current/external-extensions.html
/// </remarks>
/// <exception cref="ArgumentNullException">
/// <paramref name="modelBuilder" />
/// </exception>
public static ModelBuilder HasPostgresExtension(
this ModelBuilder modelBuilder,
string name)
=> modelBuilder.HasPostgresExtension(null, name);
/// <summary>
/// Registers a PostgreSQL extension in the model.
/// </summary>
/// <param name="modelBuilder">The model builder in which to define the extension.</param>
/// <param name="schema">The schema in which to create the extension.</param>
/// <param name="name">The name of the extension to create.</param>
/// <param name="version">The version of the extension.</param>
/// <param name="fromDataAnnotation">Indicates whether the configuration was specified using a data annotation.</param>
/// <returns>The same builder instance so that multiple calls can be chained.</returns>
/// <remarks>
/// See: https://www.postgresql.org/docs/current/external-extensions.html
/// </remarks>
/// <exception cref="ArgumentNullException">
/// <paramref name="modelBuilder" />
/// </exception>
public static IConventionModelBuilder? HasPostgresExtension(
this IConventionModelBuilder modelBuilder,
string? schema,
string name,
string? version = null,
bool fromDataAnnotation = false)
{
if (modelBuilder.CanSetPostgresExtension(schema, name, version, fromDataAnnotation))
{
modelBuilder.Metadata.GetOrAddPostgresExtension(schema, name, version);
return modelBuilder;
}
return null;
}
/// <summary>
/// Registers a PostgreSQL extension in the model.
/// </summary>
/// <param name="modelBuilder">The model builder in which to define the extension.</param>
/// <param name="name">The name of the extension to create.</param>
/// <param name="fromDataAnnotation">Indicates whether the configuration was specified using a data annotation.</param>
/// <returns>The same builder instance so that multiple calls can be chained.</returns>
/// <remarks>
/// See: https://www.postgresql.org/docs/current/external-extensions.html
/// </remarks>
/// <exception cref="ArgumentNullException">
/// <paramref name="modelBuilder" />
/// </exception>
public static IConventionModelBuilder? HasPostgresExtension(
this IConventionModelBuilder modelBuilder,
string name,
bool fromDataAnnotation = false)
=> modelBuilder.HasPostgresExtension(schema: null, name, version: null, fromDataAnnotation);
/// <summary>
/// Returns a value indicating whether the given PostgreSQL extension can be registered in the model.
/// </summary>
/// <remarks>
/// See <see href="https://aka.ms/efcore-docs-modeling">Modeling entity types and relationships</see>, and
/// <see href="https://aka.ms/efcore-docs-sqlserver">Accessing SQL Server and SQL Azure databases with EF Core</see>
/// for more information and examples.
/// </remarks>
/// <param name="modelBuilder">The model builder.</param>
/// <param name="schema">The schema in which to create the extension.</param>
/// <param name="name">The name of the extension to create.</param>
/// <param name="version">The version of the extension.</param>
/// <param name="fromDataAnnotation">Indicates whether the configuration was specified using a data annotation.</param>
/// <returns><see langword="true" /> if the given value can be set as the default increment for SQL Server IDENTITY.</returns>
public static bool CanSetPostgresExtension(
this IConventionModelBuilder modelBuilder,
string? schema,
string name,
string? version = null,
bool fromDataAnnotation = false)
{
var annotationName = PostgresExtension.BuildAnnotationName(schema, name);
return modelBuilder.CanSetAnnotation(annotationName, $"{schema},{name},{version}", fromDataAnnotation);
}
#endregion
#region Enums
/// <summary>
/// Registers a user-defined enum type in the model.
/// </summary>
/// <param name="modelBuilder">The model builder in which to create the enum type.</param>
/// <param name="schema">The schema in which to create the enum type.</param>
/// <param name="name">The name of the enum type to create.</param>
/// <param name="labels">The enum label values.</param>
/// <returns>
/// The updated <see cref="ModelBuilder" />.
/// </returns>
/// <remarks>
/// See: https://www.postgresql.org/docs/current/static/datatype-enum.html
/// </remarks>
/// <exception cref="ArgumentNullException">builder</exception>
public static ModelBuilder HasPostgresEnum(
this ModelBuilder modelBuilder,
string? schema,
string name,
string[] labels)
{
Check.NotNull(modelBuilder, nameof(modelBuilder));
Check.NotEmpty(name, nameof(name));
Check.NotNull(labels, nameof(labels));
modelBuilder.Model.GetOrAddPostgresEnum(schema, name, labels);
return modelBuilder;
}
/// <summary>
/// Registers a user-defined enum type in the model.
/// </summary>
/// <param name="modelBuilder">The model builder in which to create the enum type.</param>
/// <param name="schema">The schema in which to create the enum type.</param>
/// <param name="name">The name of the enum type to create.</param>
/// <param name="labels">The enum label values.</param>
/// <returns>
/// The updated <see cref="ModelBuilder" />.
/// </returns>
/// <remarks>
/// See: https://www.postgresql.org/docs/current/static/datatype-enum.html
/// </remarks>
/// <exception cref="ArgumentNullException">builder</exception>
public static IConventionModelBuilder HasPostgresEnum(
this IConventionModelBuilder modelBuilder,
string? schema,
string name,
string[] labels)
{
Check.NotNull(modelBuilder, nameof(modelBuilder));
Check.NotEmpty(name, nameof(name));
Check.NotNull(labels, nameof(labels));
if (modelBuilder.CanSetPostgresEnum(schema, name))
{
modelBuilder.Metadata.GetOrAddPostgresEnum(schema, name, labels);
}
return modelBuilder;
}
/// <summary>
/// Returns a value indicating whether the given PostgreSQL extension can be registered in the model.
/// </summary>
/// <remarks>
/// See <see href="https://aka.ms/efcore-docs-modeling">Modeling entity types and relationships</see>, and
/// <see href="https://aka.ms/efcore-docs-sqlserver">Accessing SQL Server and SQL Azure databases with EF Core</see>
/// for more information and examples.
/// </remarks>
/// <param name="modelBuilder">The model builder.</param>
/// <param name="schema">The schema in which to create the extension.</param>
/// <param name="name">The name of the extension to create.</param>
/// <param name="fromDataAnnotation">Indicates whether the configuration was specified using a data annotation.</param>
/// <returns><see langword="true" /> if the given value can be set as the default increment for SQL Server IDENTITY.</returns>
public static bool CanSetPostgresEnum(
this IConventionModelBuilder modelBuilder,
string? schema,
string name,
bool fromDataAnnotation = false)
{
var annotationName = PostgresExtension.BuildAnnotationName(schema, name);
return modelBuilder.CanSetAnnotation(annotationName, $"{schema},{name}", fromDataAnnotation);
}
/// <summary>
/// Registers a user-defined enum type in the model.
/// </summary>
/// <param name="modelBuilder">The model builder in which to create the enum type.</param>
/// <param name="name">The name of the enum type to create.</param>
/// <param name="labels">The enum label values.</param>
/// <returns>
/// The updated <see cref="ModelBuilder" />.
/// </returns>
/// <remarks>
/// See: https://www.postgresql.org/docs/current/static/datatype-enum.html
/// </remarks>
/// <exception cref="ArgumentNullException">builder</exception>
public static ModelBuilder HasPostgresEnum(
this ModelBuilder modelBuilder,
string name,
string[] labels)
=> modelBuilder.HasPostgresEnum(null, name, labels);
/// <summary>
/// Registers a user-defined enum type in the model.
/// </summary>
/// <param name="modelBuilder">The model builder in which to create the enum type.</param>
/// <param name="schema">The schema in which to create the enum type.</param>
/// <param name="name">The name of the enum type to create.</param>
/// <param name="nameTranslator">
/// The translator for name and label inference.
/// Defaults to <see cref="NpgsqlSnakeCaseNameTranslator" />.
/// </param>
/// <typeparam name="TEnum"></typeparam>
/// <returns>
/// The updated <see cref="ModelBuilder" />.
/// </returns>
/// <remarks>
/// See: https://www.postgresql.org/docs/current/static/datatype-enum.html
/// </remarks>
/// <exception cref="ArgumentNullException">builder</exception>
public static ModelBuilder HasPostgresEnum<TEnum>(
this ModelBuilder modelBuilder,
string? schema = null,
string? name = null,
INpgsqlNameTranslator? nameTranslator = null)
where TEnum : struct, Enum
{
#pragma warning disable CS0618 // NpgsqlConnection.GlobalTypeMapper is obsolete
nameTranslator ??= NpgsqlConnection.GlobalTypeMapper.DefaultNameTranslator;
#pragma warning restore CS0618
return modelBuilder.HasPostgresEnum(
schema,
name ?? GetTypePgName<TEnum>(nameTranslator),
GetMemberPgNames<TEnum>(nameTranslator));
}
#endregion
#region Templates
/// <summary>
/// Specifies the PostgreSQL database to use as a template when creating a new database for this model.
/// </summary>
public static ModelBuilder UseDatabaseTemplate(this ModelBuilder modelBuilder, string templateDatabaseName)
{
Check.NotNull(modelBuilder, nameof(modelBuilder));
Check.NotEmpty(templateDatabaseName, nameof(templateDatabaseName));
modelBuilder.Model.SetDatabaseTemplate(templateDatabaseName);
return modelBuilder;
}
#endregion
#region Ranges
/// <summary>
/// Registers a user-defined range type in the model.
/// </summary>
/// <param name="modelBuilder">The model builder on which to create the range type.</param>
/// <param name="schema">The schema in which to create the range type.</param>
/// <param name="name">The name of the range type to be created.</param>
/// <param name="subtype">The subtype (or element type) of the range</param>
/// <param name="canonicalFunction">
/// An optional PostgreSQL function which converts range values to a canonical form.
/// </param>
/// <param name="subtypeOpClass">Used to specify a non-default operator class.</param>
/// <param name="collation">Used to specify a non-default collation in the range's order.</param>
/// <param name="subtypeDiff">
/// An optional PostgreSQL function taking two values of the subtype type as argument, and return a double
/// precision value representing the difference between the two given values.
/// </param>
/// <remarks>
/// See https://www.postgresql.org/docs/current/static/rangetypes.html,
/// https://www.postgresql.org/docs/current/static/sql-createtype.html,
/// </remarks>
public static ModelBuilder HasPostgresRange(
this ModelBuilder modelBuilder,
string? schema,
string name,
string subtype,
string? canonicalFunction = null,
string? subtypeOpClass = null,
string? collation = null,
string? subtypeDiff = null)
{
Check.NotNull(modelBuilder, nameof(modelBuilder));
Check.NotEmpty(name, nameof(name));
Check.NotEmpty(subtype, nameof(subtype));
modelBuilder.Model.GetOrAddPostgresRange(
schema,
name,
subtype,
canonicalFunction,
subtypeOpClass,
collation,
subtypeDiff);
return modelBuilder;
}
/// <summary>
/// Registers a user-defined range type in the model.
/// </summary>
/// <param name="modelBuilder">The model builder on which to create the range type.</param>
/// <param name="name">The name of the range type to be created.</param>
/// <param name="subtype">The subtype (or element type) of the range</param>
/// <remarks>
/// See https://www.postgresql.org/docs/current/static/rangetypes.html,
/// https://www.postgresql.org/docs/current/static/sql-createtype.html,
/// </remarks>
public static ModelBuilder HasPostgresRange(
this ModelBuilder modelBuilder,
string name,
string subtype)
=> HasPostgresRange(modelBuilder, null, name, subtype);
#endregion
#region Tablespaces
/// <summary>
/// Specifies the PostgreSQL tablespace in which to place the new database created for this model.
/// </summary>
public static ModelBuilder UseTablespace(
this ModelBuilder modelBuilder,
string tablespace)
{
Check.NotNull(modelBuilder, nameof(modelBuilder));
Check.NotEmpty(tablespace, nameof(tablespace));
modelBuilder.Model.SetTablespace(tablespace);
return modelBuilder;
}
#endregion
#region Encoding
/// <summary>
/// Specifies the encoding to use when creating a new database for this model.
/// </summary>
public static ModelBuilder UseDatabaseEncoding(this ModelBuilder modelBuilder, string encoding)
{
Check.NotNull(modelBuilder, nameof(modelBuilder));
Check.NotEmpty(encoding, nameof(encoding));
modelBuilder.Model.SetDatabaseEncoding(encoding);
return modelBuilder;
}
#endregion
#region Collation management
/// <summary>
/// Creates a new collation in the database.
/// </summary>
/// <remarks>
/// See https://www.postgresql.org/docs/current/sql-createcollation.html.
/// </remarks>
/// <param name="modelBuilder">The model builder on which to create the collation.</param>
/// <param name="name">The name of the collation to create.</param>
/// <param name="locale">Sets LC_COLLATE and LC_CTYPE at once.</param>
/// <param name="provider">
/// Specifies the provider to use for locale services associated with this collation.
/// The available choices depend on the operating system and build options.
/// </param>
/// <param name="deterministic">
/// Specifies whether the collation should use deterministic comparisons.
/// Defaults to <c>true</c>.
/// </param>
/// <returns>The same builder instance so that multiple calls can be chained.</returns>
public static ModelBuilder HasCollation(
this ModelBuilder modelBuilder,
string name,
string locale,
string? provider = null,
bool? deterministic = null)
=> modelBuilder.HasCollation(schema: null, name, locale, provider: provider, deterministic: deterministic);
/// <summary>
/// Creates a new collation in the database.
/// </summary>
/// <remarks>
/// See https://www.postgresql.org/docs/current/sql-createcollation.html.
/// </remarks>
/// <param name="modelBuilder">The model builder on which to create the collation.</param>
/// <param name="schema">The schema in which to create the collation, or <c>null</c> for the default schema.</param>
/// <param name="name">The name of the collation to create.</param>
/// <param name="locale">Sets LC_COLLATE and LC_CTYPE at once.</param>
/// <param name="provider">
/// Specifies the provider to use for locale services associated with this collation.
/// The available choices depend on the operating system and build options.
/// </param>
/// <param name="deterministic">
/// Specifies whether the collation should use deterministic comparisons.
/// Defaults to <c>true</c>.
/// </param>
/// <returns>The same builder instance so that multiple calls can be chained.</returns>
public static ModelBuilder HasCollation(
this ModelBuilder modelBuilder,
string? schema,
string name,
string locale,
string? provider = null,
bool? deterministic = null)
{
Check.NotNull(modelBuilder, nameof(modelBuilder));
Check.NotEmpty(name, nameof(name));
Check.NotEmpty(locale, nameof(locale));
modelBuilder.Model.GetOrAddCollation(
schema,
name,
locale,
locale,
provider,
deterministic);
return modelBuilder;
}
/// <summary>
/// Creates a new collation in the database.
/// </summary>
/// <remarks>
/// See https://www.postgresql.org/docs/current/sql-createcollation.html.
/// </remarks>
/// <param name="modelBuilder">The model builder on which to create the collation.</param>
/// <param name="schema">The schema in which to create the collation, or <c>null</c> for the default schema.</param>
/// <param name="name">The name of the collation to create.</param>
/// <param name="lcCollate">Use the specified operating system locale for the LC_COLLATE locale category.</param>
/// <param name="lcCtype">Use the specified operating system locale for the LC_CTYPE locale category.</param>
/// <param name="provider">
/// Specifies the provider to use for locale services associated with this collation.
/// The available choices depend on the operating system and build options.
/// </param>
/// <param name="deterministic">
/// Specifies whether the collation should use deterministic comparisons.
/// Defaults to <c>true</c>.
/// </param>
/// <returns>The same builder instance so that multiple calls can be chained.</returns>
public static ModelBuilder HasCollation(
this ModelBuilder modelBuilder,
string? schema,
string name,
string lcCollate,
string lcCtype,
string? provider = null,
bool? deterministic = null)
{
Check.NotNull(modelBuilder, nameof(modelBuilder));
Check.NotEmpty(name, nameof(name));
Check.NotEmpty(lcCollate, nameof(lcCollate));
Check.NotEmpty(lcCtype, nameof(lcCtype));
modelBuilder.Model.GetOrAddCollation(
schema,
name,
lcCollate,
lcCtype,
provider,
deterministic);
return modelBuilder;
}
#endregion Collation management
#region Helpers
// See: https://github.com/npgsql/npgsql/blob/dev/src/Npgsql/TypeMapping/TypeMapperBase.cs#L132-L138
private static string GetTypePgName<TEnum>(INpgsqlNameTranslator nameTranslator)
where TEnum : struct, Enum
=> typeof(TEnum).GetCustomAttribute<PgNameAttribute>()?.PgName ?? nameTranslator.TranslateTypeName(typeof(TEnum).Name);
// See: https://github.com/npgsql/npgsql/blob/dev/src/Npgsql/TypeHandlers/EnumHandler.cs#L118-L129
private static string[] GetMemberPgNames<TEnum>(INpgsqlNameTranslator nameTranslator)
where TEnum : struct, Enum
=> typeof(TEnum)
.GetFields(BindingFlags.Static | BindingFlags.Public)
.Select(x => x.GetCustomAttribute<PgNameAttribute>()?.PgName ?? nameTranslator.TranslateMemberName(x.Name))
.ToArray();
#endregion
}