#region License
// The PostgreSQL License
//
// Copyright (C) 2017 The Npgsql Development Team
//
// Permission to use, copy, modify, and distribute this software and its
// documentation for any purpose, without fee, and without a written
// agreement is hereby granted, provided that the above copyright notice
// and this paragraph and the following two paragraphs appear in all copies.
//
// IN NO EVENT SHALL THE NPGSQL DEVELOPMENT TEAM BE LIABLE TO ANY PARTY
// FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES,
// INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS
// DOCUMENTATION, EVEN IF THE NPGSQL DEVELOPMENT TEAM HAS BEEN ADVISED OF
// THE POSSIBILITY OF SUCH DAMAGE.
//
// THE NPGSQL DEVELOPMENT TEAM SPECIFICALLY DISCLAIMS ANY WARRANTIES,
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
// ON AN "AS IS" BASIS, AND THE NPGSQL DEVELOPMENT TEAM HAS NO OBLIGATIONS
// TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
#endregion
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];
}
}