-
Notifications
You must be signed in to change notification settings - Fork 256
Expand file tree
/
Copy pathNpgsqlAnnotationProvider.cs
More file actions
245 lines (213 loc) · 11.1 KB
/
NpgsqlAnnotationProvider.cs
File metadata and controls
245 lines (213 loc) · 11.1 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
using Microsoft.EntityFrameworkCore.Metadata.Internal;
namespace Npgsql.EntityFrameworkCore.PostgreSQL.Metadata.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>
public class NpgsqlAnnotationProvider : RelationalAnnotationProvider
{
/// <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 NpgsqlAnnotationProvider(RelationalAnnotationProviderDependencies dependencies)
: base(dependencies)
{
}
/// <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 IEnumerable<IAnnotation> For(ITable table, bool designTime)
{
if (!designTime)
{
yield break;
}
// Model validation ensures that these facets are the same on all mapped entity types
var entityType = (IEntityType)table.EntityTypeMappings.First().TypeBase;
if (entityType.GetIsUnlogged())
{
yield return new Annotation(NpgsqlAnnotationNames.UnloggedTable, entityType.GetIsUnlogged());
}
if (entityType[CockroachDbAnnotationNames.InterleaveInParent] is not null)
{
yield return new Annotation(
CockroachDbAnnotationNames.InterleaveInParent, entityType[CockroachDbAnnotationNames.InterleaveInParent]);
}
foreach (var storageParamAnnotation in entityType.GetAnnotations()
.Where(a => a.Name.StartsWith(NpgsqlAnnotationNames.StorageParameterPrefix, StringComparison.Ordinal)))
{
yield return storageParamAnnotation;
}
}
/// <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 IEnumerable<IAnnotation> For(IColumn column, bool designTime)
{
if (!designTime)
{
yield break;
}
var table = StoreObjectIdentifier.Table(column.Table.Name, column.Table.Schema);
var valueGeneratedProperty = column.PropertyMappings.Where(
m => (m.TableMapping.IsSharedTablePrincipal ?? true)
&& m.TableMapping.TypeBase == m.Property.DeclaringType)
.Select(m => m.Property)
.FirstOrDefault(
p => p.GetValueGenerationStrategy(table) switch
{
NpgsqlValueGenerationStrategy.IdentityByDefaultColumn => true,
NpgsqlValueGenerationStrategy.IdentityAlwaysColumn => true,
NpgsqlValueGenerationStrategy.SerialColumn => true,
_ => false
});
if (valueGeneratedProperty is not null)
{
var valueGenerationStrategy = valueGeneratedProperty.GetValueGenerationStrategy();
yield return new Annotation(NpgsqlAnnotationNames.ValueGenerationStrategy, valueGenerationStrategy);
if (valueGenerationStrategy is NpgsqlValueGenerationStrategy.IdentityByDefaultColumn
or NpgsqlValueGenerationStrategy.IdentityAlwaysColumn)
{
if (valueGeneratedProperty[NpgsqlAnnotationNames.IdentityOptions] is string identityOptions)
{
yield return new Annotation(NpgsqlAnnotationNames.IdentityOptions, identityOptions);
}
}
}
if (column.PropertyMappings.Select(m => m.Property.GetTsVectorConfig())
.FirstOrDefault(c => c is not null) is { } tsVectorConfig)
{
yield return new Annotation(NpgsqlAnnotationNames.TsVectorConfig, tsVectorConfig);
}
valueGeneratedProperty = column.PropertyMappings.Select(m => m.Property)
.FirstOrDefault(p => p.GetTsVectorProperties() is not null);
if (valueGeneratedProperty is not null)
{
var tableIdentifier = StoreObjectIdentifier.Table(column.Table.Name, column.Table.Schema);
yield return new Annotation(
NpgsqlAnnotationNames.TsVectorProperties,
valueGeneratedProperty.GetTsVectorProperties()!
.Select(p2 => valueGeneratedProperty.DeclaringType.FindProperty(p2)!.GetColumnName(tableIdentifier))
.ToArray());
}
// JSON columns have no property mappings so all annotations that rely on property mappings should be skipped for them
if (column is not JsonColumn
&& column.PropertyMappings.FirstOrDefault()?.Property.GetCompressionMethod() is { } compressionMethod)
{
// Model validation ensures that these facets are the same on all mapped properties
yield return new Annotation(NpgsqlAnnotationNames.CompressionMethod, compressionMethod);
}
}
/// <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 IEnumerable<IAnnotation> For(ITableIndex index, bool designTime)
{
if (!designTime)
{
yield break;
}
// Model validation ensures that these facets are the same on all mapped indexes
var modelIndex = index.MappedIndexes.First();
if (modelIndex.GetCollation() is { } collation)
{
yield return new Annotation(RelationalAnnotationNames.Collation, collation);
}
if (modelIndex.GetMethod() is { } method)
{
yield return new Annotation(NpgsqlAnnotationNames.IndexMethod, method);
}
if (modelIndex.GetOperators() is { } operators)
{
yield return new Annotation(NpgsqlAnnotationNames.IndexOperators, operators);
}
if (modelIndex.GetNullSortOrder() is { } nullSortOrder)
{
yield return new Annotation(NpgsqlAnnotationNames.IndexNullSortOrder, nullSortOrder);
}
if (modelIndex.GetTsVectorConfig() is { } configName)
{
yield return new Annotation(NpgsqlAnnotationNames.TsVectorConfig, configName);
}
if (modelIndex.GetIncludeProperties() is { } includeProperties)
{
var tableIdentifier = StoreObjectIdentifier.Table(index.Table.Name, index.Table.Schema);
yield return new Annotation(
NpgsqlAnnotationNames.IndexInclude,
includeProperties
.Select(p => modelIndex.DeclaringEntityType.FindProperty(p)!.GetColumnName(tableIdentifier))
.ToArray());
}
if (modelIndex.IsCreatedConcurrently() is { } isCreatedConcurrently)
{
yield return new Annotation(NpgsqlAnnotationNames.CreatedConcurrently, isCreatedConcurrently);
}
if (modelIndex.GetAreNullsDistinct() is { } nullsDistinct)
{
yield return new Annotation(NpgsqlAnnotationNames.NullsDistinct, nullsDistinct);
}
foreach (var storageParamAnnotation in modelIndex.GetAnnotations()
.Where(a => a.Name.StartsWith(NpgsqlAnnotationNames.StorageParameterPrefix, StringComparison.Ordinal)))
{
yield return storageParamAnnotation;
}
// Support legacy annotation for index ordering
if (modelIndex[NpgsqlAnnotationNames.IndexSortOrder] is IReadOnlyList<SortOrder> legacySortOrder)
{
yield return new Annotation(NpgsqlAnnotationNames.IndexSortOrder, legacySortOrder);
}
}
/// <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 IEnumerable<IAnnotation> For(IForeignKeyConstraint foreignKey, bool designTime)
{
if (!designTime)
{
yield break;
}
foreach (var item in foreignKey.MappedForeignKeys)
{
if (item.GetMatchStrategy() is { } match)
{
yield return new Annotation(NpgsqlAnnotationNames.MatchStrategy, match);
}
}
}
/// <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 IEnumerable<IAnnotation> For(IRelationalModel model, bool designTime)
{
if (!designTime)
{
return [];
}
return model.Model.GetAnnotations().Where(
a =>
a.Name.StartsWith(NpgsqlAnnotationNames.PostgresExtensionPrefix, StringComparison.Ordinal)
|| a.Name.StartsWith(NpgsqlAnnotationNames.EnumPrefix, StringComparison.Ordinal)
|| a.Name.StartsWith(NpgsqlAnnotationNames.RangePrefix, StringComparison.Ordinal)
|| a.Name.StartsWith(NpgsqlAnnotationNames.CollationDefinitionPrefix, StringComparison.Ordinal));
}
}