forked from npgsql/npgsql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNpgsqlTypeMapping.cs
More file actions
146 lines (129 loc) · 6.3 KB
/
NpgsqlTypeMapping.cs
File metadata and controls
146 lines (129 loc) · 6.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
using System;
using System.Data;
using Npgsql.TypeHandling;
using NpgsqlTypes;
namespace Npgsql.TypeMapping
{
/// <summary>
/// Builds instances of <see cref="NpgsqlTypeMapping"/> for addition into <see cref="INpgsqlTypeMapper"/>.
/// </summary>
public class NpgsqlTypeMappingBuilder
{
/// <summary>
/// The name of the PostgreSQL type name, as it appears in the pg_type catalog.
/// </summary>
/// <remarks>
/// 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.
/// </remarks>
public string PgTypeName { get; set; }
/// <summary>
/// The <see cref="NpgsqlDbType"/> that corresponds to this type. Setting an
/// <see cref="NpgsqlParameter"/>'s <see cref="NpgsqlParameter.NpgsqlDbType"/> property
/// to this value will make Npgsql write its value to PostgreSQL with this mapping.
/// </summary>
public NpgsqlDbType? NpgsqlDbType { get; set; }
/// <summary>
/// A set of <see cref="DbType"/>s that correspond to this type. Setting an
/// <see cref="NpgsqlParameter"/>'s <see cref="NpgsqlParameter.DbType"/> property
/// to one of these values will make Npgsql write its value to PostgreSQL with this mapping.
/// </summary>
public DbType[] DbTypes { get; set; }
/// <summary>
/// A set of CLR types that correspond to this type. Setting an
/// <see cref="NpgsqlParameter"/>'s <see cref="NpgsqlParameter.Value"/> property
/// to one of these types will make Npgsql write its value to PostgreSQL with this mapping.
/// </summary>
public Type[] ClrTypes { get; set; }
/// <summary>
/// Determines what is returned from <see cref="NpgsqlParameter.DbType"/> when this mapping
/// is used.
/// </summary>
public DbType? InferredDbType { get; set; }
/// <summary>
/// A factory for a type handler that will be used to read and write values for PostgreSQL type.
/// </summary>
public NpgsqlTypeHandlerFactory TypeHandlerFactory { get; set; }
/// <summary>
/// Builds an <see cref="NpgsqlTypeMapping"/> that can be added to an <see cref="INpgsqlTypeMapper"/>.
/// </summary>
/// <returns></returns>
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);
}
}
/// <summary>
/// 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.
/// </summary>
/// <seealso cref="NpgsqlConnection.GlobalTypeMapper"/>
/// <seealso cref="NpgsqlConnection.TypeMapper"/>
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;
}
/// <summary>
/// The name of the PostgreSQL type name, as it appears in the pg_type catalog.
/// </summary>
/// <remarks>
/// 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.
/// </remarks>
public string PgTypeName { get; }
/// <summary>
/// The <see cref="NpgsqlDbType"/> that corresponds to this type. Setting an
/// <see cref="NpgsqlParameter"/>'s <see cref="NpgsqlParameter.NpgsqlDbType"/> property
/// to this value will make Npgsql write its value to PostgreSQL with this mapping.
/// </summary>
public NpgsqlDbType? NpgsqlDbType { get; }
/// <summary>
/// A set of <see cref="DbType"/>s that correspond to this type. Setting an
/// <see cref="NpgsqlParameter"/>'s <see cref="NpgsqlParameter.DbType"/> property
/// to one of these values will make Npgsql write its value to PostgreSQL with this mapping.
/// </summary>
public DbType[] DbTypes { get; }
/// <summary>
/// A set of CLR types that correspond to this type. Setting an
/// <see cref="NpgsqlParameter"/>'s <see cref="NpgsqlParameter.Value"/> property
/// to one of these types will make Npgsql write its value to PostgreSQL with this mapping.
/// </summary>
public Type[] ClrTypes { get; }
/// <summary>
/// Determines what is returned from <see cref="NpgsqlParameter.DbType"/> when this mapping
/// is used.
/// </summary>
public DbType? InferredDbType { get; }
/// <summary>
/// A factory for a type handler that will be used to read and write values for PostgreSQL type.
/// </summary>
public NpgsqlTypeHandlerFactory TypeHandlerFactory { get; }
/// <summary>
/// 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).
/// </summary>
internal Type DefaultClrType => TypeHandlerFactory.DefaultValueType;
/// <summary>
/// Returns a string that represents the current object.
/// </summary>
public override string ToString() => $"{PgTypeName} => {TypeHandlerFactory.GetType().Name}";
static readonly DbType[] EmptyDbTypes = new DbType[0];
static readonly Type[] EmptyClrTypes = new Type[0];
}
}