|
| 1 | +using Microsoft.EntityFrameworkCore.Metadata.Builders; |
| 2 | +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata.Internal; |
| 3 | + |
| 4 | +// ReSharper disable once CheckNamespace |
| 5 | +namespace Microsoft.EntityFrameworkCore; |
| 6 | + |
| 7 | +/// <summary> |
| 8 | +/// Npgsql specific extension methods for relationship builders. |
| 9 | +/// </summary> |
| 10 | +public static class NpgsqlForeignKeyBuilderExtensions |
| 11 | +{ |
| 12 | + #region Period |
| 13 | + |
| 14 | + /// <summary> |
| 15 | + /// Configures the foreign key to use the PostgreSQL PERIOD feature for temporal foreign keys. |
| 16 | + /// The last column in the foreign key must be a PostgreSQL range type, and the referenced |
| 17 | + /// principal key must have WITHOUT OVERLAPS configured. |
| 18 | + /// </summary> |
| 19 | + /// <remarks> |
| 20 | + /// See https://www.postgresql.org/docs/current/sql-createtable.html for more information. |
| 21 | + /// </remarks> |
| 22 | + /// <param name="referenceCollectionBuilder">The builder being used to configure the relationship.</param> |
| 23 | + /// <param name="withPeriod">A value indicating whether to use PERIOD.</param> |
| 24 | + /// <returns>The same builder instance so that multiple calls can be chained.</returns> |
| 25 | + public static ReferenceCollectionBuilder WithPeriod( |
| 26 | + this ReferenceCollectionBuilder referenceCollectionBuilder, |
| 27 | + bool withPeriod = true) |
| 28 | + { |
| 29 | + Check.NotNull(referenceCollectionBuilder, nameof(referenceCollectionBuilder)); |
| 30 | + |
| 31 | + referenceCollectionBuilder.Metadata.SetPeriod(withPeriod); |
| 32 | + |
| 33 | + return referenceCollectionBuilder; |
| 34 | + } |
| 35 | + |
| 36 | + /// <summary> |
| 37 | + /// Configures the foreign key to use the PostgreSQL PERIOD feature for temporal foreign keys. |
| 38 | + /// The last column in the foreign key must be a PostgreSQL range type, and the referenced |
| 39 | + /// principal key must have WITHOUT OVERLAPS configured. |
| 40 | + /// </summary> |
| 41 | + /// <remarks> |
| 42 | + /// See https://www.postgresql.org/docs/current/sql-createtable.html for more information. |
| 43 | + /// </remarks> |
| 44 | + /// <param name="referenceCollectionBuilder">The builder being used to configure the relationship.</param> |
| 45 | + /// <param name="withPeriod">A value indicating whether to use PERIOD.</param> |
| 46 | + /// <typeparam name="TEntity">The principal entity type in this relationship.</typeparam> |
| 47 | + /// <typeparam name="TRelatedEntity">The dependent entity type in this relationship.</typeparam> |
| 48 | + /// <returns>The same builder instance so that multiple calls can be chained.</returns> |
| 49 | + public static ReferenceCollectionBuilder<TEntity, TRelatedEntity> WithPeriod<TEntity, TRelatedEntity>( |
| 50 | + this ReferenceCollectionBuilder<TEntity, TRelatedEntity> referenceCollectionBuilder, |
| 51 | + bool withPeriod = true) |
| 52 | + where TEntity : class |
| 53 | + where TRelatedEntity : class |
| 54 | + => (ReferenceCollectionBuilder<TEntity, TRelatedEntity>)WithPeriod( |
| 55 | + (ReferenceCollectionBuilder)referenceCollectionBuilder, withPeriod); |
| 56 | + |
| 57 | + /// <summary> |
| 58 | + /// Configures the foreign key to use the PostgreSQL PERIOD feature for temporal foreign keys. |
| 59 | + /// The last column in the foreign key must be a PostgreSQL range type, and the referenced |
| 60 | + /// principal key must have WITHOUT OVERLAPS configured. |
| 61 | + /// </summary> |
| 62 | + /// <remarks> |
| 63 | + /// See https://www.postgresql.org/docs/current/sql-createtable.html for more information. |
| 64 | + /// </remarks> |
| 65 | + /// <param name="referenceReferenceBuilder">The builder being used to configure the relationship.</param> |
| 66 | + /// <param name="withPeriod">A value indicating whether to use PERIOD.</param> |
| 67 | + /// <returns>The same builder instance so that multiple calls can be chained.</returns> |
| 68 | + public static ReferenceReferenceBuilder WithPeriod( |
| 69 | + this ReferenceReferenceBuilder referenceReferenceBuilder, |
| 70 | + bool withPeriod = true) |
| 71 | + { |
| 72 | + Check.NotNull(referenceReferenceBuilder, nameof(referenceReferenceBuilder)); |
| 73 | + |
| 74 | + referenceReferenceBuilder.Metadata.SetPeriod(withPeriod); |
| 75 | + |
| 76 | + return referenceReferenceBuilder; |
| 77 | + } |
| 78 | + |
| 79 | + /// <summary> |
| 80 | + /// Configures the foreign key to use the PostgreSQL PERIOD feature for temporal foreign keys. |
| 81 | + /// The last column in the foreign key must be a PostgreSQL range type, and the referenced |
| 82 | + /// principal key must have WITHOUT OVERLAPS configured. |
| 83 | + /// </summary> |
| 84 | + /// <remarks> |
| 85 | + /// See https://www.postgresql.org/docs/current/sql-createtable.html for more information. |
| 86 | + /// </remarks> |
| 87 | + /// <param name="referenceReferenceBuilder">The builder being used to configure the relationship.</param> |
| 88 | + /// <param name="withPeriod">A value indicating whether to use PERIOD.</param> |
| 89 | + /// <typeparam name="TEntity">The entity type on one end of the relationship.</typeparam> |
| 90 | + /// <typeparam name="TRelatedEntity">The entity type on the other end of the relationship.</typeparam> |
| 91 | + /// <returns>The same builder instance so that multiple calls can be chained.</returns> |
| 92 | + public static ReferenceReferenceBuilder<TEntity, TRelatedEntity> WithPeriod<TEntity, TRelatedEntity>( |
| 93 | + this ReferenceReferenceBuilder<TEntity, TRelatedEntity> referenceReferenceBuilder, |
| 94 | + bool withPeriod = true) |
| 95 | + where TEntity : class |
| 96 | + where TRelatedEntity : class |
| 97 | + => (ReferenceReferenceBuilder<TEntity, TRelatedEntity>)WithPeriod( |
| 98 | + (ReferenceReferenceBuilder)referenceReferenceBuilder, withPeriod); |
| 99 | + |
| 100 | + /// <summary> |
| 101 | + /// Configures the foreign key to use the PostgreSQL PERIOD feature for temporal foreign keys. |
| 102 | + /// The last column in the foreign key must be a PostgreSQL range type, and the referenced |
| 103 | + /// principal key must have WITHOUT OVERLAPS configured. |
| 104 | + /// </summary> |
| 105 | + /// <remarks> |
| 106 | + /// See https://www.postgresql.org/docs/current/sql-createtable.html for more information. |
| 107 | + /// </remarks> |
| 108 | + /// <param name="ownershipBuilder">The builder being used to configure the relationship.</param> |
| 109 | + /// <param name="withPeriod">A value indicating whether to use PERIOD.</param> |
| 110 | + /// <returns>The same builder instance so that multiple calls can be chained.</returns> |
| 111 | + public static OwnershipBuilder WithPeriod( |
| 112 | + this OwnershipBuilder ownershipBuilder, |
| 113 | + bool withPeriod = true) |
| 114 | + { |
| 115 | + Check.NotNull(ownershipBuilder, nameof(ownershipBuilder)); |
| 116 | + |
| 117 | + ownershipBuilder.Metadata.SetPeriod(withPeriod); |
| 118 | + |
| 119 | + return ownershipBuilder; |
| 120 | + } |
| 121 | + |
| 122 | + /// <summary> |
| 123 | + /// Configures the foreign key to use the PostgreSQL PERIOD feature for temporal foreign keys. |
| 124 | + /// The last column in the foreign key must be a PostgreSQL range type, and the referenced |
| 125 | + /// principal key must have WITHOUT OVERLAPS configured. |
| 126 | + /// </summary> |
| 127 | + /// <remarks> |
| 128 | + /// See https://www.postgresql.org/docs/current/sql-createtable.html for more information. |
| 129 | + /// </remarks> |
| 130 | + /// <param name="ownershipBuilder">The builder being used to configure the relationship.</param> |
| 131 | + /// <param name="withPeriod">A value indicating whether to use PERIOD.</param> |
| 132 | + /// <typeparam name="TEntity">The entity type on one end of the relationship.</typeparam> |
| 133 | + /// <typeparam name="TDependentEntity">The entity type on the other end of the relationship.</typeparam> |
| 134 | + /// <returns>The same builder instance so that multiple calls can be chained.</returns> |
| 135 | + public static OwnershipBuilder<TEntity, TDependentEntity> WithPeriod<TEntity, TDependentEntity>( |
| 136 | + this OwnershipBuilder<TEntity, TDependentEntity> ownershipBuilder, |
| 137 | + bool withPeriod = true) |
| 138 | + where TEntity : class |
| 139 | + where TDependentEntity : class |
| 140 | + => (OwnershipBuilder<TEntity, TDependentEntity>)WithPeriod( |
| 141 | + (OwnershipBuilder)ownershipBuilder, withPeriod); |
| 142 | + |
| 143 | + /// <summary> |
| 144 | + /// Configures the foreign key to use the PostgreSQL PERIOD feature for temporal foreign keys. |
| 145 | + /// The last column in the foreign key must be a PostgreSQL range type, and the referenced |
| 146 | + /// principal key must have WITHOUT OVERLAPS configured. |
| 147 | + /// </summary> |
| 148 | + /// <remarks> |
| 149 | + /// See https://www.postgresql.org/docs/current/sql-createtable.html for more information. |
| 150 | + /// </remarks> |
| 151 | + /// <param name="foreignKeyBuilder">The builder being used to configure the relationship.</param> |
| 152 | + /// <param name="withPeriod">A value indicating whether to use PERIOD.</param> |
| 153 | + /// <param name="fromDataAnnotation">Indicates whether the configuration was specified using a data annotation.</param> |
| 154 | + /// <returns> |
| 155 | + /// The same builder instance if the configuration was applied, |
| 156 | + /// <see langword="null" /> otherwise. |
| 157 | + /// </returns> |
| 158 | + public static IConventionForeignKeyBuilder? WithPeriod( |
| 159 | + this IConventionForeignKeyBuilder foreignKeyBuilder, |
| 160 | + bool? withPeriod = true, |
| 161 | + bool fromDataAnnotation = false) |
| 162 | + { |
| 163 | + if (foreignKeyBuilder.CanSetPeriod(withPeriod, fromDataAnnotation)) |
| 164 | + { |
| 165 | + foreignKeyBuilder.Metadata.SetPeriod(withPeriod, fromDataAnnotation); |
| 166 | + |
| 167 | + return foreignKeyBuilder; |
| 168 | + } |
| 169 | + |
| 170 | + return null; |
| 171 | + } |
| 172 | + |
| 173 | + /// <summary> |
| 174 | + /// Returns a value indicating whether PERIOD can be configured. |
| 175 | + /// </summary> |
| 176 | + /// <param name="foreignKeyBuilder">The builder being used to configure the relationship.</param> |
| 177 | + /// <param name="withPeriod">A value indicating whether to use PERIOD.</param> |
| 178 | + /// <param name="fromDataAnnotation">Indicates whether the configuration was specified using a data annotation.</param> |
| 179 | + /// <returns><see langword="true" /> if the foreign key can be configured with PERIOD.</returns> |
| 180 | + public static bool CanSetPeriod( |
| 181 | + this IConventionForeignKeyBuilder foreignKeyBuilder, |
| 182 | + bool? withPeriod = true, |
| 183 | + bool fromDataAnnotation = false) |
| 184 | + { |
| 185 | + Check.NotNull(foreignKeyBuilder, nameof(foreignKeyBuilder)); |
| 186 | + |
| 187 | + return foreignKeyBuilder.CanSetAnnotation(NpgsqlAnnotationNames.Period, withPeriod, fromDataAnnotation); |
| 188 | + } |
| 189 | + |
| 190 | + #endregion Period |
| 191 | +} |
0 commit comments