Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/Npgsql/PublicAPI.Shipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1170,10 +1170,12 @@ Npgsql.TypeMapping.INpgsqlTypeMapper.DefaultNameTranslator.set -> void
Npgsql.TypeMapping.INpgsqlTypeMapper.MapComposite(System.Type! clrType, string? pgName = null, Npgsql.INpgsqlNameTranslator? nameTranslator = null) -> Npgsql.TypeMapping.INpgsqlTypeMapper!
Npgsql.TypeMapping.INpgsqlTypeMapper.MapComposite<T>(string? pgName = null, Npgsql.INpgsqlNameTranslator? nameTranslator = null) -> Npgsql.TypeMapping.INpgsqlTypeMapper!
Npgsql.TypeMapping.INpgsqlTypeMapper.MapEnum<TEnum>(string? pgName = null, Npgsql.INpgsqlNameTranslator? nameTranslator = null) -> Npgsql.TypeMapping.INpgsqlTypeMapper!
Npgsql.TypeMapping.INpgsqlTypeMapper.MapEnum(System.Type! clrType, string? pgName = null, Npgsql.INpgsqlNameTranslator? nameTranslator = null) -> Npgsql.TypeMapping.INpgsqlTypeMapper!
Npgsql.TypeMapping.INpgsqlTypeMapper.Reset() -> void
Npgsql.TypeMapping.INpgsqlTypeMapper.UnmapComposite(System.Type! clrType, string? pgName = null, Npgsql.INpgsqlNameTranslator? nameTranslator = null) -> bool
Npgsql.TypeMapping.INpgsqlTypeMapper.UnmapComposite<T>(string? pgName = null, Npgsql.INpgsqlNameTranslator? nameTranslator = null) -> bool
Npgsql.TypeMapping.INpgsqlTypeMapper.UnmapEnum<TEnum>(string? pgName = null, Npgsql.INpgsqlNameTranslator? nameTranslator = null) -> bool
Npgsql.TypeMapping.INpgsqlTypeMapper.UnmapEnum(System.Type! clrType, string? pgName = null, Npgsql.INpgsqlNameTranslator? nameTranslator = null) -> bool
Npgsql.Util.NpgsqlTimeout
Npgsql.Util.NpgsqlTimeout.NpgsqlTimeout() -> void
NpgsqlTypes.NpgsqlBox
Expand Down
7 changes: 5 additions & 2 deletions src/Npgsql/TypeMapping/GlobalTypeMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,14 @@ public INpgsqlTypeMapper MapEnum<TEnum>(string? pgName = null, INpgsqlNameTransl
public bool UnmapEnum<TEnum>(string? pgName = null, INpgsqlNameTranslator? nameTranslator = null)
where TEnum : struct, Enum
{
if (!clrType.IsEnum)
throw new ArgumentException("clrType must be an Enum", nameof(clrType));

if (pgName != null && pgName.Trim() == "")
throw new ArgumentException("pgName can't be empty", nameof(pgName));

nameTranslator ??= DefaultNameTranslator;
pgName ??= GetPgName(typeof(TEnum), nameTranslator);
pgName ??= GetPgName(clrType, nameTranslator);

Lock.EnterWriteLock();
try
Expand Down Expand Up @@ -593,4 +596,4 @@ internal static DbType NpgsqlDbTypeToDbType(NpgsqlDbType npgsqlDbType)
};

#endregion Static translation tables
}
}
48 changes: 47 additions & 1 deletion src/Npgsql/TypeMapping/INpgsqlTypeMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,33 @@ INpgsqlTypeMapper MapEnum<TEnum>(
INpgsqlNameTranslator? nameTranslator = null)
where TEnum : struct, Enum;

/// <summary>
/// Maps a CLR enum to a PostgreSQL enum type.
/// </summary>
/// <remarks>
/// CLR enum labels are mapped by name to PostgreSQL enum labels.
/// The translation strategy can be controlled by the <paramref name="nameTranslator"/> parameter,
/// which defaults to <see cref="NpgsqlSnakeCaseNameTranslator"/>.
/// You can also use the <see cref="PgNameAttribute"/> on your enum fields to manually specify a PostgreSQL enum label.
/// If there is a discrepancy between the .NET and database labels while an enum is read or written,
/// an exception will be raised.
/// </remarks>
/// <param name="clrType">
/// The .NET enum type to be mapped
/// </param>
/// <param name="pgName">
/// A PostgreSQL type name for the corresponding enum type in the database.
/// If null, the name translator given in <paramref name="nameTranslator"/> will be used.
/// </param>
/// <param name="nameTranslator">
/// A component which will be used to translate CLR names (e.g. SomeClass) into database names (e.g. some_class).
/// Defaults to <see cref="NpgsqlSnakeCaseNameTranslator"/>
/// </param>
INpgsqlTypeMapper MapEnum(
Type clrType,
string? pgName = null,
INpgsqlNameTranslator? nameTranslator = null);

/// <summary>
/// Removes an existing enum mapping.
/// </summary>
Expand All @@ -62,6 +89,25 @@ bool UnmapEnum<TEnum>(
INpgsqlNameTranslator? nameTranslator = null)
where TEnum : struct, Enum;

/// <summary>
/// Removes an existing enum mapping.
/// </summary>
/// <param name="clrType">
/// The .NET enum type to be unmapped
/// </param>
/// <param name="pgName">
/// A PostgreSQL type name for the corresponding enum type in the database.
/// If null, the name translator given in <paramref name="nameTranslator"/> will be used.
/// </param>
/// <param name="nameTranslator">
/// A component which will be used to translate CLR names (e.g. SomeClass) into database names (e.g. some_class).
/// Defaults to <see cref="NpgsqlSnakeCaseNameTranslator"/>
/// </param>
bool UnmapEnum(
Type clrType,
string? pgName = null,
INpgsqlNameTranslator? nameTranslator = null);

/// <summary>
/// Maps a CLR type to a PostgreSQL composite type.
/// </summary>
Expand Down Expand Up @@ -157,4 +203,4 @@ bool UnmapComposite(
/// Resets all mapping changes performed on this type mapper and reverts it to its original, starting state.
/// </summary>
void Reset();
}
}