|
| 1 | +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata.Internal; |
| 2 | + |
| 3 | +// ReSharper disable once CheckNamespace |
| 4 | +namespace Microsoft.EntityFrameworkCore; |
| 5 | + |
| 6 | +/// <summary> |
| 7 | +/// Npgsql specific extension methods for <see cref="KeyBuilder" />. |
| 8 | +/// </summary> |
| 9 | +public static class NpgsqlKeyBuilderExtensions |
| 10 | +{ |
| 11 | + #region WithoutOverlaps |
| 12 | + |
| 13 | + /// <summary> |
| 14 | + /// Configures the key to use the PostgreSQL WITHOUT OVERLAPS feature. |
| 15 | + /// The last property in the key must be a PostgreSQL range type. |
| 16 | + /// </summary> |
| 17 | + /// <remarks> |
| 18 | + /// See https://www.postgresql.org/docs/current/sql-createtable.html for more information. |
| 19 | + /// </remarks> |
| 20 | + /// <param name="keyBuilder">The builder for the key being configured.</param> |
| 21 | + /// <param name="withoutOverlaps">A value indicating whether to use WITHOUT OVERLAPS.</param> |
| 22 | + /// <returns>A builder to further configure the key.</returns> |
| 23 | + public static KeyBuilder WithoutOverlaps(this KeyBuilder keyBuilder, bool withoutOverlaps = true) |
| 24 | + { |
| 25 | + Check.NotNull(keyBuilder, nameof(keyBuilder)); |
| 26 | + |
| 27 | + keyBuilder.Metadata.SetWithoutOverlaps(withoutOverlaps); |
| 28 | + |
| 29 | + return keyBuilder; |
| 30 | + } |
| 31 | + |
| 32 | + /// <summary> |
| 33 | + /// Configures the key to use the PostgreSQL WITHOUT OVERLAPS feature. |
| 34 | + /// The last property in the key must be a PostgreSQL range type. |
| 35 | + /// </summary> |
| 36 | + /// <remarks> |
| 37 | + /// See https://www.postgresql.org/docs/current/sql-createtable.html for more information. |
| 38 | + /// </remarks> |
| 39 | + /// <param name="keyBuilder">The builder for the key being configured.</param> |
| 40 | + /// <param name="withoutOverlaps">A value indicating whether to use WITHOUT OVERLAPS.</param> |
| 41 | + /// <returns>A builder to further configure the key.</returns> |
| 42 | + public static KeyBuilder<TEntity> WithoutOverlaps<TEntity>(this KeyBuilder<TEntity> keyBuilder, bool withoutOverlaps = true) |
| 43 | + => (KeyBuilder<TEntity>)WithoutOverlaps((KeyBuilder)keyBuilder, withoutOverlaps); |
| 44 | + |
| 45 | + /// <summary> |
| 46 | + /// Configures the key to use the PostgreSQL WITHOUT OVERLAPS feature. |
| 47 | + /// The last property in the key must be a PostgreSQL range type. |
| 48 | + /// </summary> |
| 49 | + /// <remarks> |
| 50 | + /// See https://www.postgresql.org/docs/current/sql-createtable.html for more information. |
| 51 | + /// </remarks> |
| 52 | + /// <param name="keyBuilder">The builder for the key being configured.</param> |
| 53 | + /// <param name="withoutOverlaps">A value indicating whether to use WITHOUT OVERLAPS.</param> |
| 54 | + /// <param name="fromDataAnnotation">Indicates whether the configuration was specified using a data annotation.</param> |
| 55 | + /// <returns>A builder to further configure the key.</returns> |
| 56 | + public static IConventionKeyBuilder? WithoutOverlaps( |
| 57 | + this IConventionKeyBuilder keyBuilder, |
| 58 | + bool? withoutOverlaps = true, |
| 59 | + bool fromDataAnnotation = false) |
| 60 | + { |
| 61 | + if (keyBuilder.CanSetWithoutOverlaps(withoutOverlaps, fromDataAnnotation)) |
| 62 | + { |
| 63 | + keyBuilder.Metadata.SetWithoutOverlaps(withoutOverlaps, fromDataAnnotation); |
| 64 | + |
| 65 | + return keyBuilder; |
| 66 | + } |
| 67 | + |
| 68 | + return null; |
| 69 | + } |
| 70 | + |
| 71 | + /// <summary> |
| 72 | + /// Returns a value indicating whether WITHOUT OVERLAPS can be configured. |
| 73 | + /// </summary> |
| 74 | + /// <param name="keyBuilder">The builder for the key being configured.</param> |
| 75 | + /// <param name="withoutOverlaps">A value indicating whether to use WITHOUT OVERLAPS.</param> |
| 76 | + /// <param name="fromDataAnnotation">Indicates whether the configuration was specified using a data annotation.</param> |
| 77 | + /// <returns><see langword="true" /> if the key can be configured with WITHOUT OVERLAPS.</returns> |
| 78 | + public static bool CanSetWithoutOverlaps( |
| 79 | + this IConventionKeyBuilder keyBuilder, |
| 80 | + bool? withoutOverlaps = true, |
| 81 | + bool fromDataAnnotation = false) |
| 82 | + { |
| 83 | + Check.NotNull(keyBuilder, nameof(keyBuilder)); |
| 84 | + |
| 85 | + return keyBuilder.CanSetAnnotation(NpgsqlAnnotationNames.WithoutOverlaps, withoutOverlaps, fromDataAnnotation); |
| 86 | + } |
| 87 | + |
| 88 | + #endregion WithoutOverlaps |
| 89 | +} |
0 commit comments