X Tutup
using System; using System.Data; using Npgsql.TypeHandling; using NpgsqlTypes; namespace Npgsql.TypeMapping { /// /// Builds instances of for addition into . /// public class NpgsqlTypeMappingBuilder { /// /// The name of the PostgreSQL type name, as it appears in the pg_type catalog. /// /// /// This can a a partial name (without the schema), or a fully-qualified name /// (schema.typename) - the latter can be used if you have two types with the same /// name in different schemas. /// public string PgTypeName { get; set; } /// /// The that corresponds to this type. Setting an /// 's property /// to this value will make Npgsql write its value to PostgreSQL with this mapping. /// public NpgsqlDbType? NpgsqlDbType { get; set; } /// /// A set of s that correspond to this type. Setting an /// 's property /// to one of these values will make Npgsql write its value to PostgreSQL with this mapping. /// public DbType[] DbTypes { get; set; } /// /// A set of CLR types that correspond to this type. Setting an /// 's property /// to one of these types will make Npgsql write its value to PostgreSQL with this mapping. /// public Type[] ClrTypes { get; set; } /// /// Determines what is returned from when this mapping /// is used. /// public DbType? InferredDbType { get; set; } /// /// A factory for a type handler that will be used to read and write values for PostgreSQL type. /// public NpgsqlTypeHandlerFactory TypeHandlerFactory { get; set; } /// /// Builds an that can be added to an . /// /// public NpgsqlTypeMapping Build() { if (string.IsNullOrWhiteSpace(PgTypeName)) throw new ArgumentException($"{PgTypeName} must contain the name of a PostgreSQL data type"); if (TypeHandlerFactory == null) throw new ArgumentException($"{TypeHandlerFactory} must refer to a type handler factory"); return new NpgsqlTypeMapping(PgTypeName, NpgsqlDbType, DbTypes, ClrTypes, InferredDbType, TypeHandlerFactory); } } /// /// Represents a type mapping for a PostgreSQL data type, which can be added to a type mapper, /// managing when that data type will be read and written and how. /// /// /// public sealed class NpgsqlTypeMapping { internal NpgsqlTypeMapping( string pgTypeName, NpgsqlDbType? npgsqlDbType, DbType[] dbTypes, Type[] clrTypes, DbType? inferredDbType, NpgsqlTypeHandlerFactory typeHandlerFactory) { PgTypeName = pgTypeName; NpgsqlDbType = npgsqlDbType; DbTypes = dbTypes ?? EmptyDbTypes; ClrTypes = clrTypes ?? EmptyClrTypes; InferredDbType = inferredDbType; TypeHandlerFactory = typeHandlerFactory; } /// /// The name of the PostgreSQL type name, as it appears in the pg_type catalog. /// /// /// This can a a partial name (without the schema), or a fully-qualified name /// (schema.typename) - the latter can be used if you have two types with the same /// name in different schemas. /// public string PgTypeName { get; } /// /// The that corresponds to this type. Setting an /// 's property /// to this value will make Npgsql write its value to PostgreSQL with this mapping. /// public NpgsqlDbType? NpgsqlDbType { get; } /// /// A set of s that correspond to this type. Setting an /// 's property /// to one of these values will make Npgsql write its value to PostgreSQL with this mapping. /// public DbType[] DbTypes { get; } /// /// A set of CLR types that correspond to this type. Setting an /// 's property /// to one of these types will make Npgsql write its value to PostgreSQL with this mapping. /// public Type[] ClrTypes { get; } /// /// Determines what is returned from when this mapping /// is used. /// public DbType? InferredDbType { get; } /// /// A factory for a type handler that will be used to read and write values for PostgreSQL type. /// public NpgsqlTypeHandlerFactory TypeHandlerFactory { get; } /// /// The default CLR type that handlers produced by this factory will read and write. /// Used by the EF Core provider (and possibly others in the future). /// internal Type DefaultClrType => TypeHandlerFactory.DefaultValueType; /// /// Returns a string that represents the current object. /// public override string ToString() => $"{PgTypeName} => {TypeHandlerFactory.GetType().Name}"; static readonly DbType[] EmptyDbTypes = new DbType[0]; static readonly Type[] EmptyClrTypes = new Type[0]; } }
X Tutup