|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +using Microsoft.EntityFrameworkCore.Design.Internal; |
| 5 | +using Kdbndp.EntityFrameworkCore.KingbaseES.Metadata.Internal; |
| 6 | +using Kdbndp.EntityFrameworkCore.KingbaseES.Storage.Internal.Mapping; |
| 7 | + |
| 8 | +namespace Kdbndp.EntityFrameworkCore.KingbaseES.Design.Internal; |
| 9 | + |
| 10 | +/// <summary> |
| 11 | +/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to |
| 12 | +/// the same compatibility standards as public APIs. It may be changed or removed without notice in |
| 13 | +/// any release. You should only use it directly in your code with extreme caution and knowing that |
| 14 | +/// doing so can result in application failures when updating to a new Entity Framework Core release. |
| 15 | +/// </summary> |
| 16 | +#pragma warning disable EF1001 // Internal EF Core API usage. |
| 17 | +public class KdbndpCSharpRuntimeAnnotationCodeGenerator : RelationalCSharpRuntimeAnnotationCodeGenerator |
| 18 | +{ |
| 19 | + /// <summary> |
| 20 | + /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to |
| 21 | + /// the same compatibility standards as public APIs. It may be changed or removed without notice in |
| 22 | + /// any release. You should only use it directly in your code with extreme caution and knowing that |
| 23 | + /// doing so can result in application failures when updating to a new Entity Framework Core release. |
| 24 | + /// </summary> |
| 25 | + public KdbndpCSharpRuntimeAnnotationCodeGenerator( |
| 26 | + CSharpRuntimeAnnotationCodeGeneratorDependencies dependencies, |
| 27 | + RelationalCSharpRuntimeAnnotationCodeGeneratorDependencies relationalDependencies) |
| 28 | + : base(dependencies, relationalDependencies) |
| 29 | + { |
| 30 | + } |
| 31 | + |
| 32 | + /// <summary> |
| 33 | + /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to |
| 34 | + /// the same compatibility standards as public APIs. It may be changed or removed without notice in |
| 35 | + /// any release. You should only use it directly in your code with extreme caution and knowing that |
| 36 | + /// doing so can result in application failures when updating to a new Entity Framework Core release. |
| 37 | + /// </summary> |
| 38 | + public override bool Create( |
| 39 | + CoreTypeMapping typeMapping, |
| 40 | + CSharpRuntimeAnnotationCodeGeneratorParameters parameters, |
| 41 | + ValueComparer? valueComparer = null, |
| 42 | + ValueComparer? keyValueComparer = null, |
| 43 | + ValueComparer? providerValueComparer = null) |
| 44 | + { |
| 45 | + var result = base.Create(typeMapping, parameters, valueComparer, keyValueComparer, providerValueComparer); |
| 46 | + |
| 47 | + var mainBuilder = parameters.MainBuilder; |
| 48 | + |
| 49 | + var KdbndpDbTypeBasedDefaultInstance = typeMapping switch |
| 50 | + { |
| 51 | + KdbndpStringTypeMapping => KdbndpStringTypeMapping.Default, |
| 52 | + // KdbndpMultirangeTypeMapping => KdbndpMultirangeTypeMapping.Default, |
| 53 | + _ => (IKdbndpTypeMapping?)null |
| 54 | + }; |
| 55 | + |
| 56 | + if (KdbndpDbTypeBasedDefaultInstance is not null) |
| 57 | + { |
| 58 | + var KdbndpDbType = ((IKdbndpTypeMapping)typeMapping).KdbndpDbType; |
| 59 | + |
| 60 | + if (KdbndpDbType != KdbndpDbTypeBasedDefaultInstance.KdbndpDbType) |
| 61 | + { |
| 62 | + mainBuilder.AppendLine(";"); |
| 63 | + |
| 64 | + mainBuilder.Append( |
| 65 | + $"{parameters.TargetName}.TypeMapping = (({typeMapping.GetType().Name}){parameters.TargetName}.TypeMapping).Clone(KdbndpDbType: "); |
| 66 | + |
| 67 | + mainBuilder |
| 68 | + .Append(nameof(KdbndpTypes)) |
| 69 | + .Append(".") |
| 70 | + .Append(nameof(KdbndpDbType)) |
| 71 | + .Append(".") |
| 72 | + .Append(KdbndpDbType.ToString()); |
| 73 | + |
| 74 | + mainBuilder |
| 75 | + .Append(")") |
| 76 | + .DecrementIndent(); |
| 77 | + } |
| 78 | + |
| 79 | + } |
| 80 | + |
| 81 | + switch (typeMapping) |
| 82 | + { |
| 83 | +#pragma warning disable CS0618 // KdbndpConnection.GlobalTypeMapper is obsolete |
| 84 | + case KdbndpEnumTypeMapping enumTypeMapping: |
| 85 | + if (enumTypeMapping.NameTranslator != KdbndpConnection.GlobalTypeMapper.DefaultNameTranslator) |
| 86 | + { |
| 87 | + throw new NotSupportedException( |
| 88 | + "Mapped enums are only supported in the compiled model if they use the default name translator"); |
| 89 | + } |
| 90 | + break; |
| 91 | +#pragma warning restore CS0618 |
| 92 | + } |
| 93 | + |
| 94 | + return result; |
| 95 | + } |
| 96 | + |
| 97 | + /// <inheritdoc /> |
| 98 | + public override void Generate(IModel model, CSharpRuntimeAnnotationCodeGeneratorParameters parameters) |
| 99 | + { |
| 100 | + if (!parameters.IsRuntime) |
| 101 | + { |
| 102 | + var annotations = parameters.Annotations; |
| 103 | + annotations.Remove(KdbndpAnnotationNames.DatabaseTemplate); |
| 104 | + annotations.Remove(KdbndpAnnotationNames.Tablespace); |
| 105 | + annotations.Remove(KdbndpAnnotationNames.CollationDefinitionPrefix); |
| 106 | + |
| 107 | +#pragma warning disable CS0618 |
| 108 | + annotations.Remove(KdbndpAnnotationNames.DefaultColumnCollation); |
| 109 | +#pragma warning restore CS0618 |
| 110 | + |
| 111 | + foreach (var annotationName in annotations.Keys.Where( |
| 112 | + k => |
| 113 | + k.StartsWith(KdbndpAnnotationNames.PostgresExtensionPrefix, StringComparison.Ordinal) |
| 114 | + || k.StartsWith(KdbndpAnnotationNames.EnumPrefix, StringComparison.Ordinal) |
| 115 | + || k.StartsWith(KdbndpAnnotationNames.RangePrefix, StringComparison.Ordinal))) |
| 116 | + { |
| 117 | + annotations.Remove(annotationName); |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + base.Generate(model, parameters); |
| 122 | + } |
| 123 | + |
| 124 | + /// <inheritdoc /> |
| 125 | + public override void Generate(IRelationalModel model, CSharpRuntimeAnnotationCodeGeneratorParameters parameters) |
| 126 | + { |
| 127 | + if (!parameters.IsRuntime) |
| 128 | + { |
| 129 | + var annotations = parameters.Annotations; |
| 130 | + annotations.Remove(KdbndpAnnotationNames.DatabaseTemplate); |
| 131 | + annotations.Remove(KdbndpAnnotationNames.Tablespace); |
| 132 | + annotations.Remove(KdbndpAnnotationNames.CollationDefinitionPrefix); |
| 133 | + |
| 134 | +#pragma warning disable CS0618 |
| 135 | + annotations.Remove(KdbndpAnnotationNames.DefaultColumnCollation); |
| 136 | +#pragma warning restore CS0618 |
| 137 | + |
| 138 | + foreach (var annotationName in annotations.Keys.Where( |
| 139 | + k => |
| 140 | + k.StartsWith(KdbndpAnnotationNames.PostgresExtensionPrefix, StringComparison.Ordinal) |
| 141 | + || k.StartsWith(KdbndpAnnotationNames.EnumPrefix, StringComparison.Ordinal) |
| 142 | + || k.StartsWith(KdbndpAnnotationNames.RangePrefix, StringComparison.Ordinal))) |
| 143 | + { |
| 144 | + annotations.Remove(annotationName); |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + base.Generate(model, parameters); |
| 149 | + } |
| 150 | + |
| 151 | + /// <inheritdoc /> |
| 152 | + public override void Generate(IProperty property, CSharpRuntimeAnnotationCodeGeneratorParameters parameters) |
| 153 | + { |
| 154 | + if (!parameters.IsRuntime) |
| 155 | + { |
| 156 | + var annotations = parameters.Annotations; |
| 157 | + annotations.Remove(KdbndpAnnotationNames.IdentityOptions); |
| 158 | + annotations.Remove(KdbndpAnnotationNames.TsVectorConfig); |
| 159 | + annotations.Remove(KdbndpAnnotationNames.TsVectorProperties); |
| 160 | + annotations.Remove(KdbndpAnnotationNames.CompressionMethod); |
| 161 | + |
| 162 | + if (!annotations.ContainsKey(KdbndpAnnotationNames.ValueGenerationStrategy)) |
| 163 | + { |
| 164 | + annotations[KdbndpAnnotationNames.ValueGenerationStrategy] = property.GetValueGenerationStrategy(); |
| 165 | + } |
| 166 | + } |
| 167 | + |
| 168 | + base.Generate(property, parameters); |
| 169 | + } |
| 170 | + |
| 171 | + /// <inheritdoc /> |
| 172 | + public override void Generate(IColumn column, CSharpRuntimeAnnotationCodeGeneratorParameters parameters) |
| 173 | + { |
| 174 | + if (!parameters.IsRuntime) |
| 175 | + { |
| 176 | + var annotations = parameters.Annotations; |
| 177 | + |
| 178 | + annotations.Remove(KdbndpAnnotationNames.IdentityOptions); |
| 179 | + annotations.Remove(KdbndpAnnotationNames.TsVectorConfig); |
| 180 | + annotations.Remove(KdbndpAnnotationNames.TsVectorProperties); |
| 181 | + annotations.Remove(KdbndpAnnotationNames.CompressionMethod); |
| 182 | + } |
| 183 | + |
| 184 | + base.Generate(column, parameters); |
| 185 | + } |
| 186 | + |
| 187 | + /// <inheritdoc /> |
| 188 | + public override void Generate(IIndex index, CSharpRuntimeAnnotationCodeGeneratorParameters parameters) |
| 189 | + { |
| 190 | + if (!parameters.IsRuntime) |
| 191 | + { |
| 192 | + var annotations = parameters.Annotations; |
| 193 | + |
| 194 | + annotations.Remove(KdbndpAnnotationNames.IndexMethod); |
| 195 | + annotations.Remove(KdbndpAnnotationNames.IndexOperators); |
| 196 | + annotations.Remove(KdbndpAnnotationNames.IndexSortOrder); |
| 197 | + annotations.Remove(KdbndpAnnotationNames.IndexNullSortOrder); |
| 198 | + annotations.Remove(KdbndpAnnotationNames.IndexInclude); |
| 199 | + annotations.Remove(KdbndpAnnotationNames.CreatedConcurrently); |
| 200 | + annotations.Remove(KdbndpAnnotationNames.NullsDistinct); |
| 201 | + |
| 202 | + foreach (var annotationName in annotations.Keys.Where( |
| 203 | + k => k.StartsWith(KdbndpAnnotationNames.StorageParameterPrefix, StringComparison.Ordinal))) |
| 204 | + { |
| 205 | + annotations.Remove(annotationName); |
| 206 | + } |
| 207 | + } |
| 208 | + |
| 209 | + base.Generate(index, parameters); |
| 210 | + } |
| 211 | + |
| 212 | + /// <inheritdoc /> |
| 213 | + public override void Generate(ITableIndex index, CSharpRuntimeAnnotationCodeGeneratorParameters parameters) |
| 214 | + { |
| 215 | + if (!parameters.IsRuntime) |
| 216 | + { |
| 217 | + var annotations = parameters.Annotations; |
| 218 | + |
| 219 | + annotations.Remove(KdbndpAnnotationNames.IndexMethod); |
| 220 | + annotations.Remove(KdbndpAnnotationNames.IndexOperators); |
| 221 | + annotations.Remove(KdbndpAnnotationNames.IndexSortOrder); |
| 222 | + annotations.Remove(KdbndpAnnotationNames.IndexNullSortOrder); |
| 223 | + annotations.Remove(KdbndpAnnotationNames.IndexInclude); |
| 224 | + annotations.Remove(KdbndpAnnotationNames.CreatedConcurrently); |
| 225 | + annotations.Remove(KdbndpAnnotationNames.NullsDistinct); |
| 226 | + |
| 227 | + foreach (var annotationName in annotations.Keys.Where( |
| 228 | + k => k.StartsWith(KdbndpAnnotationNames.StorageParameterPrefix, StringComparison.Ordinal))) |
| 229 | + { |
| 230 | + annotations.Remove(annotationName); |
| 231 | + } |
| 232 | + } |
| 233 | + |
| 234 | + base.Generate(index, parameters); |
| 235 | + } |
| 236 | + |
| 237 | + /// <inheritdoc /> |
| 238 | + public override void Generate(IEntityType entityType, CSharpRuntimeAnnotationCodeGeneratorParameters parameters) |
| 239 | + { |
| 240 | + if (!parameters.IsRuntime) |
| 241 | + { |
| 242 | + var annotations = parameters.Annotations; |
| 243 | + |
| 244 | + annotations.Remove(KdbndpAnnotationNames.UnloggedTable); |
| 245 | + annotations.Remove(CockroachDbAnnotationNames.InterleaveInParent); |
| 246 | + |
| 247 | + foreach (var annotationName in annotations.Keys.Where( |
| 248 | + k => k.StartsWith(KdbndpAnnotationNames.StorageParameterPrefix, StringComparison.Ordinal))) |
| 249 | + { |
| 250 | + annotations.Remove(annotationName); |
| 251 | + } |
| 252 | + } |
| 253 | + |
| 254 | + base.Generate(entityType, parameters); |
| 255 | + } |
| 256 | + |
| 257 | + /// <inheritdoc /> |
| 258 | + public override void Generate(ITable table, CSharpRuntimeAnnotationCodeGeneratorParameters parameters) |
| 259 | + { |
| 260 | + if (!parameters.IsRuntime) |
| 261 | + { |
| 262 | + var annotations = parameters.Annotations; |
| 263 | + |
| 264 | + annotations.Remove(KdbndpAnnotationNames.UnloggedTable); |
| 265 | + annotations.Remove(CockroachDbAnnotationNames.InterleaveInParent); |
| 266 | + |
| 267 | + foreach (var annotationName in annotations.Keys.Where( |
| 268 | + k => k.StartsWith(KdbndpAnnotationNames.StorageParameterPrefix, StringComparison.Ordinal))) |
| 269 | + { |
| 270 | + annotations.Remove(annotationName); |
| 271 | + } |
| 272 | + } |
| 273 | + |
| 274 | + base.Generate(table, parameters); |
| 275 | + } |
| 276 | +} |
0 commit comments