-
Notifications
You must be signed in to change notification settings - Fork 257
Expand file tree
/
Copy pathNpgsqlCSharpRuntimeAnnotationCodeGenerator.cs
More file actions
380 lines (317 loc) · 15.4 KB
/
NpgsqlCSharpRuntimeAnnotationCodeGenerator.cs
File metadata and controls
380 lines (317 loc) · 15.4 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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using Microsoft.EntityFrameworkCore.Design.Internal;
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata.Internal;
using Npgsql.EntityFrameworkCore.PostgreSQL.Storage.Internal.Mapping;
namespace Npgsql.EntityFrameworkCore.PostgreSQL.Design.Internal;
/// <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>
#pragma warning disable EF1001 // Internal EF Core API usage.
public class NpgsqlCSharpRuntimeAnnotationCodeGenerator
: RelationalCSharpRuntimeAnnotationCodeGenerator, ICSharpRuntimeAnnotationCodeGenerator
{
private int _typeMappingNestingCount;
/// <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 NpgsqlCSharpRuntimeAnnotationCodeGenerator(
CSharpRuntimeAnnotationCodeGeneratorDependencies dependencies,
RelationalCSharpRuntimeAnnotationCodeGeneratorDependencies relationalDependencies)
: base(dependencies, relationalDependencies)
{
}
/// <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 override bool Create(
CoreTypeMapping typeMapping,
CSharpRuntimeAnnotationCodeGeneratorParameters parameters,
ValueComparer? valueComparer = null,
ValueComparer? keyValueComparer = null,
ValueComparer? providerValueComparer = null)
{
_typeMappingNestingCount++;
try
{
var result = base.Create(typeMapping, parameters, valueComparer, keyValueComparer, providerValueComparer);
AddNpgsqlTypeMappingTweaks(typeMapping, parameters);
return result;
}
finally
{
_typeMappingNestingCount--;
}
}
/// <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>
[EntityFrameworkInternal]
protected virtual void AddNpgsqlTypeMappingTweaks(
CoreTypeMapping typeMapping,
CSharpRuntimeAnnotationCodeGeneratorParameters parameters)
{
var mainBuilder = parameters.MainBuilder;
var npgsqlDbTypeBasedDefaultInstance = typeMapping switch
{
NpgsqlStringTypeMapping => NpgsqlStringTypeMapping.Default,
NpgsqlUIntTypeMapping => NpgsqlUIntTypeMapping.Default,
NpgsqlULongTypeMapping => NpgsqlULongTypeMapping.Default,
// NpgsqlMultirangeTypeMapping => NpgsqlMultirangeTypeMapping.Default,
_ => (INpgsqlTypeMapping?)null
};
if (npgsqlDbTypeBasedDefaultInstance is not null)
{
CheckElementTypeMapping();
var npgsqlDbType = ((INpgsqlTypeMapping)typeMapping).NpgsqlDbType;
if (npgsqlDbType != npgsqlDbTypeBasedDefaultInstance.NpgsqlDbType)
{
mainBuilder.AppendLine(";");
mainBuilder.Append(
$"{parameters.TargetName}.TypeMapping = (({typeMapping.GetType().Name}){parameters.TargetName}.TypeMapping).Clone(npgsqlDbType: ");
mainBuilder
.Append(nameof(NpgsqlTypes))
.Append(".")
.Append(nameof(NpgsqlDbType))
.Append(".")
.Append(npgsqlDbType.ToString());
mainBuilder
.Append(")")
.DecrementIndent();
}
}
switch (typeMapping)
{
case NpgsqlEnumTypeMapping enumTypeMapping:
CheckElementTypeMapping();
var code = Dependencies.CSharpHelper;
mainBuilder.AppendLine(";");
mainBuilder.AppendLine(
$"{parameters.TargetName}.TypeMapping = ((NpgsqlEnumTypeMapping){parameters.TargetName}.TypeMapping).Clone(")
.IncrementIndent();
mainBuilder
.Append("unquotedStoreType: ")
.Append(code.Literal(enumTypeMapping.UnquotedStoreType))
.AppendLine(",")
.AppendLine("labels: new Dictionary<object, string>()")
.AppendLine("{")
.IncrementIndent();
foreach (var (enumValue, label) in enumTypeMapping.Labels)
{
mainBuilder
.Append('[')
.Append(code.UnknownLiteral(enumValue))
.Append(']')
.Append(" = ")
.Append(code.Literal(label))
.AppendLine(",");
}
mainBuilder
.Append("}")
.DecrementIndent()
.Append(")")
.DecrementIndent();
break;
case NpgsqlRangeTypeMapping rangeTypeMapping:
{
CheckElementTypeMapping();
var defaultInstance = NpgsqlRangeTypeMapping.Default;
var npgsqlDbTypeDifferent = rangeTypeMapping.NpgsqlDbType != defaultInstance.NpgsqlDbType;
var subtypeTypeMappingIsDifferent = rangeTypeMapping.SubtypeMapping != defaultInstance.SubtypeMapping;
if (npgsqlDbTypeDifferent || subtypeTypeMappingIsDifferent)
{
mainBuilder.AppendLine(";");
mainBuilder.AppendLine(
$"{parameters.TargetName}.TypeMapping = ((NpgsqlRangeTypeMapping){parameters.TargetName}.TypeMapping).Clone(")
.IncrementIndent();
mainBuilder
.Append("npgsqlDbType: ")
.Append(nameof(NpgsqlTypes))
.Append(".")
.Append(nameof(NpgsqlDbType))
.Append(".")
.Append(rangeTypeMapping.NpgsqlDbType.ToString())
.AppendLine(",");
mainBuilder.Append("subtypeTypeMapping: ");
Create(rangeTypeMapping.SubtypeMapping, parameters);
mainBuilder
.Append(")")
.DecrementIndent();
}
break;
}
}
void CheckElementTypeMapping()
{
if (_typeMappingNestingCount > 1)
{
throw new NotSupportedException(
$"Non-default Npgsql type mappings ('{typeMapping.GetType().Name}' with store type '{(typeMapping as RelationalTypeMapping)?.StoreType}') aren't currently supported as element type mappings, see https://github.com/npgsql/efcore.pg/issues/3366.");
}
}
}
/// <inheritdoc />
public override void Generate(IModel model, CSharpRuntimeAnnotationCodeGeneratorParameters parameters)
{
if (!parameters.IsRuntime)
{
var annotations = parameters.Annotations;
annotations.Remove(NpgsqlAnnotationNames.DatabaseTemplate);
annotations.Remove(NpgsqlAnnotationNames.Tablespace);
annotations.Remove(NpgsqlAnnotationNames.CollationDefinitionPrefix);
annotations.Remove(NpgsqlAnnotationNames.Encoding);
#pragma warning disable CS0618
annotations.Remove(NpgsqlAnnotationNames.DefaultColumnCollation);
#pragma warning restore CS0618
foreach (var annotationName in annotations.Keys.Where(
k =>
k.StartsWith(NpgsqlAnnotationNames.PostgresExtensionPrefix, StringComparison.Ordinal)
|| k.StartsWith(NpgsqlAnnotationNames.EnumPrefix, StringComparison.Ordinal)
|| k.StartsWith(NpgsqlAnnotationNames.RangePrefix, StringComparison.Ordinal)))
{
annotations.Remove(annotationName);
}
}
base.Generate(model, parameters);
}
/// <inheritdoc />
public override void Generate(IRelationalModel model, CSharpRuntimeAnnotationCodeGeneratorParameters parameters)
{
if (!parameters.IsRuntime)
{
var annotations = parameters.Annotations;
annotations.Remove(NpgsqlAnnotationNames.DatabaseTemplate);
annotations.Remove(NpgsqlAnnotationNames.Tablespace);
annotations.Remove(NpgsqlAnnotationNames.CollationDefinitionPrefix);
annotations.Remove(NpgsqlAnnotationNames.Encoding);
#pragma warning disable CS0618
annotations.Remove(NpgsqlAnnotationNames.DefaultColumnCollation);
#pragma warning restore CS0618
foreach (var annotationName in annotations.Keys.Where(
k =>
k.StartsWith(NpgsqlAnnotationNames.PostgresExtensionPrefix, StringComparison.Ordinal)
|| k.StartsWith(NpgsqlAnnotationNames.EnumPrefix, StringComparison.Ordinal)
|| k.StartsWith(NpgsqlAnnotationNames.RangePrefix, StringComparison.Ordinal)))
{
annotations.Remove(annotationName);
}
}
base.Generate(model, parameters);
}
/// <inheritdoc />
public override void Generate(IProperty property, CSharpRuntimeAnnotationCodeGeneratorParameters parameters)
{
if (!parameters.IsRuntime)
{
var annotations = parameters.Annotations;
annotations.Remove(NpgsqlAnnotationNames.IdentityOptions);
annotations.Remove(NpgsqlAnnotationNames.TsVectorConfig);
annotations.Remove(NpgsqlAnnotationNames.TsVectorProperties);
annotations.Remove(NpgsqlAnnotationNames.CompressionMethod);
if (!annotations.ContainsKey(NpgsqlAnnotationNames.ValueGenerationStrategy))
{
annotations[NpgsqlAnnotationNames.ValueGenerationStrategy] = property.GetValueGenerationStrategy();
}
}
base.Generate(property, parameters);
}
/// <inheritdoc />
public override void Generate(IColumn column, CSharpRuntimeAnnotationCodeGeneratorParameters parameters)
{
if (!parameters.IsRuntime)
{
var annotations = parameters.Annotations;
annotations.Remove(NpgsqlAnnotationNames.IdentityOptions);
annotations.Remove(NpgsqlAnnotationNames.TsVectorConfig);
annotations.Remove(NpgsqlAnnotationNames.TsVectorProperties);
annotations.Remove(NpgsqlAnnotationNames.CompressionMethod);
}
base.Generate(column, parameters);
}
/// <inheritdoc />
public override void Generate(IIndex index, CSharpRuntimeAnnotationCodeGeneratorParameters parameters)
{
if (!parameters.IsRuntime)
{
var annotations = parameters.Annotations;
annotations.Remove(NpgsqlAnnotationNames.IndexMethod);
annotations.Remove(NpgsqlAnnotationNames.IndexOperators);
annotations.Remove(NpgsqlAnnotationNames.IndexSortOrder);
annotations.Remove(NpgsqlAnnotationNames.IndexNullSortOrder);
annotations.Remove(NpgsqlAnnotationNames.IndexInclude);
annotations.Remove(NpgsqlAnnotationNames.CreatedConcurrently);
annotations.Remove(NpgsqlAnnotationNames.NullsDistinct);
foreach (var annotationName in annotations.Keys.Where(
k => k.StartsWith(NpgsqlAnnotationNames.StorageParameterPrefix, StringComparison.Ordinal)))
{
annotations.Remove(annotationName);
}
}
base.Generate(index, parameters);
}
/// <inheritdoc />
public override void Generate(ITableIndex index, CSharpRuntimeAnnotationCodeGeneratorParameters parameters)
{
if (!parameters.IsRuntime)
{
var annotations = parameters.Annotations;
annotations.Remove(NpgsqlAnnotationNames.IndexMethod);
annotations.Remove(NpgsqlAnnotationNames.IndexOperators);
annotations.Remove(NpgsqlAnnotationNames.IndexSortOrder);
annotations.Remove(NpgsqlAnnotationNames.IndexNullSortOrder);
annotations.Remove(NpgsqlAnnotationNames.IndexInclude);
annotations.Remove(NpgsqlAnnotationNames.CreatedConcurrently);
annotations.Remove(NpgsqlAnnotationNames.NullsDistinct);
foreach (var annotationName in annotations.Keys.Where(
k => k.StartsWith(NpgsqlAnnotationNames.StorageParameterPrefix, StringComparison.Ordinal)))
{
annotations.Remove(annotationName);
}
}
base.Generate(index, parameters);
}
/// <inheritdoc />
public override void Generate(IEntityType entityType, CSharpRuntimeAnnotationCodeGeneratorParameters parameters)
{
if (!parameters.IsRuntime)
{
var annotations = parameters.Annotations;
annotations.Remove(NpgsqlAnnotationNames.UnloggedTable);
annotations.Remove(CockroachDbAnnotationNames.InterleaveInParent);
foreach (var annotationName in annotations.Keys.Where(
k => k.StartsWith(NpgsqlAnnotationNames.StorageParameterPrefix, StringComparison.Ordinal)))
{
annotations.Remove(annotationName);
}
}
base.Generate(entityType, parameters);
}
/// <inheritdoc />
public override void Generate(ITable table, CSharpRuntimeAnnotationCodeGeneratorParameters parameters)
{
if (!parameters.IsRuntime)
{
var annotations = parameters.Annotations;
annotations.Remove(NpgsqlAnnotationNames.UnloggedTable);
annotations.Remove(CockroachDbAnnotationNames.InterleaveInParent);
foreach (var annotationName in annotations.Keys.Where(
k => k.StartsWith(NpgsqlAnnotationNames.StorageParameterPrefix, StringComparison.Ordinal)))
{
annotations.Remove(annotationName);
}
}
base.Generate(table, parameters);
}
}