Esempio n. 1
0
        public bool UnmapComposite(Type clrType, string?pgName = null, INpgsqlNameTranslator?nameTranslator = null)
        {
            if (pgName != null && string.IsNullOrWhiteSpace(pgName))
            {
                throw new ArgumentException("pgName can't be empty.", nameof(pgName));
            }

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

            return(RemoveMapping(pgName));
        }
    /// <summary>
    /// Registers a user-defined enum type in the model.
    /// </summary>
    /// <param name="modelBuilder">The model builder in which to create the enum type.</param>
    /// <param name="schema">The schema in which to create the enum type.</param>
    /// <param name="name">The name of the enum type to create.</param>
    /// <param name="nameTranslator">
    /// The translator for name and label inference.
    /// Defaults to <see cref="NpgsqlSnakeCaseNameTranslator"/>.</param>
    /// <typeparam name="TEnum"></typeparam>
    /// <returns>
    /// The updated <see cref="ModelBuilder"/>.
    /// </returns>
    /// <remarks>
    /// See: https://www.postgresql.org/docs/current/static/datatype-enum.html
    /// </remarks>
    /// <exception cref="ArgumentNullException">builder</exception>
    public static ModelBuilder HasPostgresEnum <TEnum>(
        this ModelBuilder modelBuilder,
        string?schema = null,
        string?name   = null,
        INpgsqlNameTranslator?nameTranslator = null)
        where TEnum : struct, Enum
    {
        if (nameTranslator is null)
        {
            nameTranslator = NpgsqlConnection.GlobalTypeMapper.DefaultNameTranslator;
        }

        return(modelBuilder.HasPostgresEnum(
                   schema,
                   name ?? GetTypePgName <TEnum>(nameTranslator),
                   GetMemberPgNames <TEnum>(nameTranslator)));
    }
Esempio n. 3
0
        public bool UnmapComposite <T>(string?pgName = null, INpgsqlNameTranslator?nameTranslator = null)
        {
            if (pgName != null && pgName.Trim() == "")
            {
                throw new ArgumentException("pgName can't be empty", nameof(pgName));
            }

            if (nameTranslator == null)
            {
                nameTranslator = DefaultNameTranslator;
            }
            if (pgName == null)
            {
                pgName = GetPgName <T>(nameTranslator);
            }

            return(RemoveMapping(pgName));
        }
Esempio n. 4
0
        public bool UnmapEnum <TEnum>(string?pgName = null, INpgsqlNameTranslator?nameTranslator = null)
            where TEnum : struct, Enum
        {
            if (pgName != null && pgName.Trim() == "")
            {
                throw new ArgumentException("pgName can't be empty", nameof(pgName));
            }

            if (nameTranslator == null)
            {
                nameTranslator = DefaultNameTranslator;
            }
            if (pgName == null)
            {
                pgName = GetPgName(typeof(TEnum), nameTranslator);
            }

            return(RemoveMapping(pgName));
        }
        public NpgsqlEnumTypeMapping(
            string storeType,
            string?storeTypeSchema,
            Type enumType,
            ISqlGenerationHelper sqlGenerationHelper,
            INpgsqlNameTranslator?nameTranslator = null)
            : base(sqlGenerationHelper.DelimitIdentifier(storeType, storeTypeSchema), enumType)
        {
            if (!enumType.IsEnum || !enumType.IsValueType)
            {
                throw new ArgumentException($"Enum type mappings require a CLR enum. {enumType.FullName} is not an enum.");
            }

            nameTranslator ??= NpgsqlConnection.GlobalTypeMapper.DefaultNameTranslator;

            _nameTranslator      = nameTranslator;
            _sqlGenerationHelper = sqlGenerationHelper;
            _members             = CreateValueMapping(enumType, nameTranslator);
        }
Esempio n. 6
0
        INpgsqlTypeMapper MapComposite(string?pgName, INpgsqlNameTranslator?nameTranslator, Type type, Func <INpgsqlNameTranslator, NpgsqlTypeHandlerFactory> factory)
        {
            if (pgName != null && string.IsNullOrWhiteSpace(pgName))
            {
                throw new ArgumentException("pgName can't be empty.", nameof(pgName));
            }

            nameTranslator ??= DefaultNameTranslator;
            pgName ??= GetPgName(type, nameTranslator);

            return(AddMapping(
                       new NpgsqlTypeMappingBuilder
            {
                PgTypeName = pgName,
                ClrTypes = new[] { type },
                TypeHandlerFactory = factory(nameTranslator),
            }
                       .Build()));
        }
Esempio n. 7
0
        public INpgsqlTypeMapper MapComposite <T>(string?pgName = null, INpgsqlNameTranslator?nameTranslator = null)
        {
            if (pgName != null && pgName.Trim() == "")
            {
                throw new ArgumentException("pgName can't be empty", nameof(pgName));
            }

            if (nameTranslator == null)
            {
                nameTranslator = DefaultNameTranslator;
            }
            if (pgName == null)
            {
                pgName = GetPgName <T>(nameTranslator);
            }

            return(AddMapping(new NpgsqlTypeMappingBuilder
            {
                PgTypeName = pgName,
                ClrTypes = new[] { typeof(T) },
                TypeHandlerFactory = new MappedCompositeTypeHandlerFactory <T>(nameTranslator)
            }.Build()));
        }
Esempio n. 8
0
        public INpgsqlTypeMapper MapEnum <TEnum>(string?pgName = null, INpgsqlNameTranslator?nameTranslator = null)
            where TEnum : struct, Enum
        {
            if (pgName != null && pgName.Trim() == "")
            {
                throw new ArgumentException("pgName can't be empty", nameof(pgName));
            }

            if (nameTranslator == null)
            {
                nameTranslator = DefaultNameTranslator;
            }
            if (pgName == null)
            {
                pgName = GetPgName(typeof(TEnum), nameTranslator);
            }

            return(AddMapping(new NpgsqlTypeMappingBuilder
            {
                PgTypeName = pgName,
                ClrTypes = new[] { typeof(TEnum) },
                TypeHandlerFactory = new EnumTypeHandlerFactory <TEnum>(nameTranslator)
            }.Build()));
        }
Esempio n. 9
0
 /// <inheritdoc />
 public abstract bool UnmapComposite(Type clrType, string?pgName = null, INpgsqlNameTranslator?nameTranslator = null);
Esempio n. 10
0
 /// <inheritdoc />
 public abstract bool UnmapComposite <T>(string?pgName = null, INpgsqlNameTranslator?nameTranslator = null);
Esempio n. 11
0
 public abstract INpgsqlTypeMapper MapComposite(Type clrType, string?pgName = null, INpgsqlNameTranslator?nameTranslator = null);
Esempio n. 12
0
 /// <inheritdoc />
 public abstract bool UnmapEnum <TEnum>(string?pgName = null, INpgsqlNameTranslator?nameTranslator = null)
     where TEnum : struct, Enum;
Esempio n. 13
0
 /// <inheritdoc />
 public abstract INpgsqlTypeMapper MapEnum <TEnum>(string?pgName = null, INpgsqlNameTranslator?nameTranslator = null)
     where TEnum : struct, Enum;
Esempio n. 14
0
 public INpgsqlTypeMapper MapComposite(Type clrType, string?pgName = null, INpgsqlNameTranslator?nameTranslator = null)
 => MapComposite(pgName, nameTranslator, clrType, t => (NpgsqlTypeHandlerFactory)
                 Activator.CreateInstance(typeof(CompositeTypeHandlerFactory <>).MakeGenericType(clrType), BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, new object[] { t }, null) !);
Esempio n. 15
0
 public INpgsqlTypeMapper MapComposite <T>(string?pgName = null, INpgsqlNameTranslator?nameTranslator = null)
 => MapComposite(pgName, nameTranslator, typeof(T), t => new CompositeTypeHandlerFactory <T>(t));
Esempio n. 16
0
 public INpgsqlTypeMapper MapComposite(Type clrType, string?pgName = null, INpgsqlNameTranslator?nameTranslator = null)
 => MapComposite(pgName, nameTranslator, clrType, t => (NpgsqlTypeHandlerFactory)
                 Activator.CreateInstance(typeof(CompositeTypeHandlerFactory <>).MakeGenericType(clrType), t) !);
Esempio n. 17
0
 public bool UnmapComposite <T>(string?pgName = null, INpgsqlNameTranslator?nameTranslator = null)
 => UnmapComposite(typeof(T), pgName, nameTranslator);