-
Notifications
You must be signed in to change notification settings - Fork 257
Expand file tree
/
Copy pathNpgsqlModelExtensions.cs
More file actions
539 lines (461 loc) · 25.6 KB
/
NpgsqlModelExtensions.cs
File metadata and controls
539 lines (461 loc) · 25.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
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata.Internal;
// ReSharper disable once CheckNamespace
namespace Microsoft.EntityFrameworkCore;
/// <summary>
/// Model extension methods for Npgsql-specific metadata.
/// </summary>
/// <remarks>
/// See <see href="https://aka.ms/efcore-docs-modeling">Modeling entity types and relationships</see>.
/// </remarks>
public static class NpgsqlModelExtensions
{
/// <summary>
/// The default name for the hi-lo sequence.
/// </summary>
public const string DefaultHiLoSequenceName = "EntityFrameworkHiLoSequence";
/// <summary>
/// The default prefix for sequences applied to properties.
/// </summary>
public const string DefaultSequenceNameSuffix = "Sequence";
#region HiLo
/// <summary>
/// Returns the name to use for the default hi-lo sequence.
/// </summary>
/// <param name="model"> The model. </param>
/// <returns> The name to use for the default hi-lo sequence. </returns>
public static string GetHiLoSequenceName(this IReadOnlyModel model)
=> (string?)model[NpgsqlAnnotationNames.HiLoSequenceName]
?? DefaultHiLoSequenceName;
/// <summary>
/// Sets the name to use for the default hi-lo sequence.
/// </summary>
/// <param name="model"> The model. </param>
/// <param name="name"> The value to set. </param>
public static void SetHiLoSequenceName(this IMutableModel model, string? name)
{
Check.NullButNotEmpty(name, nameof(name));
model.SetOrRemoveAnnotation(NpgsqlAnnotationNames.HiLoSequenceName, name);
}
/// <summary>
/// Sets the name to use for the default hi-lo sequence.
/// </summary>
/// <param name="model"> The model. </param>
/// <param name="name"> The value to set. </param>
/// <param name="fromDataAnnotation"> Indicates whether the configuration was specified using a data annotation. </param>
public static string? SetHiLoSequenceName(
this IConventionModel model,
string? name,
bool fromDataAnnotation = false)
{
Check.NullButNotEmpty(name, nameof(name));
model.SetOrRemoveAnnotation(NpgsqlAnnotationNames.HiLoSequenceName, name, fromDataAnnotation);
return name;
}
/// <summary>
/// Returns the <see cref="ConfigurationSource" /> for the default hi-lo sequence name.
/// </summary>
/// <param name="model"> The model. </param>
/// <returns> The <see cref="ConfigurationSource" /> for the default hi-lo sequence name. </returns>
public static ConfigurationSource? GetHiLoSequenceNameConfigurationSource(this IConventionModel model)
=> model.FindAnnotation(NpgsqlAnnotationNames.HiLoSequenceName)?.GetConfigurationSource();
/// <summary>
/// Returns the schema to use for the default hi-lo sequence.
/// <see cref="NpgsqlPropertyBuilderExtensions.UseHiLo" />
/// </summary>
/// <param name="model"> The model. </param>
/// <returns> The schema to use for the default hi-lo sequence. </returns>
public static string? GetHiLoSequenceSchema(this IReadOnlyModel model)
=> (string?)model[NpgsqlAnnotationNames.HiLoSequenceSchema];
/// <summary>
/// Sets the schema to use for the default hi-lo sequence.
/// </summary>
/// <param name="model"> The model. </param>
/// <param name="value"> The value to set. </param>
public static void SetHiLoSequenceSchema(this IMutableModel model, string? value)
{
Check.NullButNotEmpty(value, nameof(value));
model.SetOrRemoveAnnotation(NpgsqlAnnotationNames.HiLoSequenceSchema, value);
}
/// <summary>
/// Sets the schema to use for the default hi-lo sequence.
/// </summary>
/// <param name="model"> The model. </param>
/// <param name="value"> The value to set. </param>
/// <param name="fromDataAnnotation"> Indicates whether the configuration was specified using a data annotation. </param>
public static string? SetHiLoSequenceSchema(
this IConventionModel model,
string? value,
bool fromDataAnnotation = false)
{
Check.NullButNotEmpty(value, nameof(value));
model.SetOrRemoveAnnotation(NpgsqlAnnotationNames.HiLoSequenceSchema, value, fromDataAnnotation);
return value;
}
/// <summary>
/// Returns the <see cref="ConfigurationSource" /> for the default hi-lo sequence schema.
/// </summary>
/// <param name="model"> The model. </param>
/// <returns> The <see cref="ConfigurationSource" /> for the default hi-lo sequence schema. </returns>
public static ConfigurationSource? GetHiLoSequenceSchemaConfigurationSource(this IConventionModel model)
=> model.FindAnnotation(NpgsqlAnnotationNames.HiLoSequenceSchema)?.GetConfigurationSource();
#endregion
#region Sequence
/// <summary>
/// Returns the suffix to append to the name of automatically created sequences.
/// </summary>
/// <param name="model">The model.</param>
/// <returns>The name to use for the default key value generation sequence.</returns>
public static string GetSequenceNameSuffix(this IReadOnlyModel model)
=> (string?)model[NpgsqlAnnotationNames.SequenceNameSuffix]
?? DefaultSequenceNameSuffix;
/// <summary>
/// Sets the suffix to append to the name of automatically created sequences.
/// </summary>
/// <param name="model">The model.</param>
/// <param name="name">The value to set.</param>
public static void SetSequenceNameSuffix(this IMutableModel model, string? name)
{
Check.NullButNotEmpty(name, nameof(name));
model.SetOrRemoveAnnotation(NpgsqlAnnotationNames.SequenceNameSuffix, name);
}
/// <summary>
/// Sets the suffix to append to the name of automatically created sequences.
/// </summary>
/// <param name="model">The model.</param>
/// <param name="name">The value to set.</param>
/// <param name="fromDataAnnotation">Indicates whether the configuration was specified using a data annotation.</param>
/// <returns>The configured value.</returns>
public static string? SetSequenceNameSuffix(
this IConventionModel model,
string? name,
bool fromDataAnnotation = false)
{
Check.NullButNotEmpty(name, nameof(name));
model.SetOrRemoveAnnotation(NpgsqlAnnotationNames.SequenceNameSuffix, name, fromDataAnnotation);
return name;
}
/// <summary>
/// Returns the <see cref="ConfigurationSource" /> for the default value generation sequence name suffix.
/// </summary>
/// <param name="model">The model.</param>
/// <returns>The <see cref="ConfigurationSource" /> for the default key value generation sequence name.</returns>
public static ConfigurationSource? GetSequenceNameSuffixConfigurationSource(this IConventionModel model)
=> model.FindAnnotation(NpgsqlAnnotationNames.SequenceNameSuffix)?.GetConfigurationSource();
/// <summary>
/// Returns the schema to use for the default value generation sequence.
/// <see cref="NpgsqlPropertyBuilderExtensions.UseSequence" />
/// </summary>
/// <param name="model">The model.</param>
/// <returns>The schema to use for the default key value generation sequence.</returns>
public static string? GetSequenceSchema(this IReadOnlyModel model)
=> (string?)model[NpgsqlAnnotationNames.SequenceSchema];
/// <summary>
/// Sets the schema to use for the default key value generation sequence.
/// </summary>
/// <param name="model">The model.</param>
/// <param name="value">The value to set.</param>
public static void SetSequenceSchema(this IMutableModel model, string? value)
{
Check.NullButNotEmpty(value, nameof(value));
model.SetOrRemoveAnnotation(NpgsqlAnnotationNames.SequenceSchema, value);
}
/// <summary>
/// Sets the schema to use for the default key value generation sequence.
/// </summary>
/// <param name="model">The model.</param>
/// <param name="value">The value to set.</param>
/// <param name="fromDataAnnotation">Indicates whether the configuration was specified using a data annotation.</param>
/// <returns>The configured value.</returns>
public static string? SetSequenceSchema(
this IConventionModel model,
string? value,
bool fromDataAnnotation = false)
{
Check.NullButNotEmpty(value, nameof(value));
model.SetOrRemoveAnnotation(NpgsqlAnnotationNames.SequenceSchema, value, fromDataAnnotation);
return value;
}
/// <summary>
/// Returns the <see cref="ConfigurationSource" /> for the default key value generation sequence schema.
/// </summary>
/// <param name="model">The model.</param>
/// <returns>The <see cref="ConfigurationSource" /> for the default key value generation sequence schema.</returns>
public static ConfigurationSource? GetSequenceSchemaConfigurationSource(this IConventionModel model)
=> model.FindAnnotation(NpgsqlAnnotationNames.SequenceSchema)?.GetConfigurationSource();
#endregion Sequence
#region Value Generation Strategy
/// <summary>
/// Returns the <see cref="NpgsqlValueGenerationStrategy" /> to use for properties
/// of keys in the model, unless the property has a strategy explicitly set.
/// </summary>
/// <param name="model"> The model. </param>
/// <returns> The default <see cref="NpgsqlValueGenerationStrategy" />. </returns>
public static NpgsqlValueGenerationStrategy? GetValueGenerationStrategy(this IReadOnlyModel model)
=> (NpgsqlValueGenerationStrategy?)model[NpgsqlAnnotationNames.ValueGenerationStrategy];
/// <summary>
/// Attempts to set the <see cref="NpgsqlValueGenerationStrategy" /> to use for properties
/// of keys in the model that don't have a strategy explicitly set.
/// </summary>
/// <param name="model"> The model. </param>
/// <param name="value"> The value to set. </param>
public static void SetValueGenerationStrategy(this IMutableModel model, NpgsqlValueGenerationStrategy? value)
=> model.SetOrRemoveAnnotation(NpgsqlAnnotationNames.ValueGenerationStrategy, value);
/// <summary>
/// Attempts to set the <see cref="NpgsqlValueGenerationStrategy" /> to use for properties
/// of keys in the model that don't have a strategy explicitly set.
/// </summary>
/// <param name="model"> The model. </param>
/// <param name="value"> The value to set. </param>
/// <param name="fromDataAnnotation"> Indicates whether the configuration was specified using a data annotation. </param>
public static NpgsqlValueGenerationStrategy? SetValueGenerationStrategy(
this IConventionModel model,
NpgsqlValueGenerationStrategy? value,
bool fromDataAnnotation = false)
{
model.SetOrRemoveAnnotation(NpgsqlAnnotationNames.ValueGenerationStrategy, value, fromDataAnnotation);
return value;
}
/// <summary>
/// Returns the <see cref="ConfigurationSource" /> for the default <see cref="NpgsqlValueGenerationStrategy" />.
/// </summary>
/// <param name="model"> The model. </param>
/// <returns> The <see cref="ConfigurationSource" /> for the default <see cref="NpgsqlValueGenerationStrategy" />. </returns>
public static ConfigurationSource? GetValueGenerationStrategyConfigurationSource(this IConventionModel model)
=> model.FindAnnotation(NpgsqlAnnotationNames.ValueGenerationStrategy)?.GetConfigurationSource();
#endregion
#region PostgreSQL Extensions
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public static PostgresExtension GetOrAddPostgresExtension(
this IMutableModel model,
string? schema,
string name,
string? version)
=> PostgresExtension.GetOrAddPostgresExtension(model, schema, name, version);
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public static IReadOnlyList<PostgresExtension> GetPostgresExtensions(this IReadOnlyModel model)
=> PostgresExtension.GetPostgresExtensions(model).ToArray();
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public static PostgresExtension GetOrAddPostgresExtension(
this IConventionModel model,
string? schema,
string name,
string? version)
=> PostgresExtension.GetOrAddPostgresExtension(model, schema, name, version);
#endregion
#region Enum types
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public static PostgresEnum GetOrAddPostgresEnum(
this IMutableModel model,
string? schema,
string name,
string[] labels)
=> PostgresEnum.GetOrAddPostgresEnum(model, schema, name, labels);
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public static PostgresEnum GetOrAddPostgresEnum(
this IConventionModel model,
string? schema,
string name,
string[] labels)
=> PostgresEnum.GetOrAddPostgresEnum(model, schema, name, labels);
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public static IReadOnlyList<PostgresEnum> GetPostgresEnums(this IReadOnlyModel model)
=> PostgresEnum.GetPostgresEnums(model).ToArray();
#endregion Enum types
#region Range types
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public static PostgresRange GetOrAddPostgresRange(
this IMutableModel model,
string? schema,
string name,
string subtype,
string? canonicalFunction = null,
string? subtypeOpClass = null,
string? collation = null,
string? subtypeDiff = null)
=> PostgresRange.GetOrAddPostgresRange(
model,
schema,
name,
subtype,
canonicalFunction,
subtypeOpClass,
collation,
subtypeDiff);
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public static IReadOnlyList<PostgresRange> PostgresRanges(this IReadOnlyModel model)
=> PostgresRange.GetPostgresRanges(model).ToArray();
#endregion Range types
#region Database Template
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public static string? GetDatabaseTemplate(this IReadOnlyModel model)
=> (string?)model[NpgsqlAnnotationNames.DatabaseTemplate];
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public static void SetDatabaseTemplate(this IMutableModel model, string? template)
=> model.SetOrRemoveAnnotation(NpgsqlAnnotationNames.DatabaseTemplate, template);
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public static string? SetDatabaseTemplate(
this IConventionModel model,
string? template,
bool fromDataAnnotation = false)
{
Check.NullButNotEmpty(template, nameof(template));
model.SetOrRemoveAnnotation(NpgsqlAnnotationNames.DatabaseTemplate, template, fromDataAnnotation);
return template;
}
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public static ConfigurationSource? GetDatabaseTemplateConfigurationSource(this IConventionModel model)
=> model.FindAnnotation(NpgsqlAnnotationNames.DatabaseTemplate)?.GetConfigurationSource();
#endregion
#region Tablespace
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public static string? GetTablespace(this IReadOnlyModel model)
=> (string?)model[NpgsqlAnnotationNames.Tablespace];
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public static void SetTablespace(this IMutableModel model, string? tablespace)
=> model.SetOrRemoveAnnotation(NpgsqlAnnotationNames.Tablespace, tablespace);
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public static string? SetTablespace(
this IConventionModel model,
string? tablespace,
bool fromDataAnnotation = false)
{
Check.NullButNotEmpty(tablespace, nameof(tablespace));
model.SetOrRemoveAnnotation(NpgsqlAnnotationNames.Tablespace, tablespace, fromDataAnnotation);
return tablespace;
}
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public static ConfigurationSource? GetTablespaceConfigurationSource(this IConventionModel model)
=> model.FindAnnotation(NpgsqlAnnotationNames.Tablespace)?.GetConfigurationSource();
#endregion
#region Collation management
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public static PostgresCollation GetOrAddCollation(
this IMutableModel model,
string? schema,
string name,
string lcCollate,
string lcCtype,
string? provider = null,
bool? deterministic = null)
=> PostgresCollation.GetOrAddCollation(
model,
schema,
name,
lcCollate,
lcCtype,
provider,
deterministic);
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public static IReadOnlyList<PostgresCollation> GetCollations(this IReadOnlyModel model)
=> PostgresCollation.GetCollations(model).ToArray();
#endregion Collation management
#region Encoding
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public static string? GetEncoding(this IReadOnlyModel model)
=> (string?)model[NpgsqlAnnotationNames.Encoding];
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public static void SetDatabaseEncoding(this IMutableModel model, string? encoding)
=> model.SetOrRemoveAnnotation(NpgsqlAnnotationNames.Encoding, encoding);
#endregion
}