forked from npgsql/npgsql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPostgresBaseType.cs
More file actions
165 lines (147 loc) · 6.51 KB
/
PostgresBaseType.cs
File metadata and controls
165 lines (147 loc) · 6.51 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#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 System.Diagnostics;
using System.Linq;
using System.Reflection;
using JetBrains.Annotations;
namespace Npgsql.PostgresTypes
{
/// <summary>
/// Represents a PostgreSQL base data type, which is a simple scalar value.
/// </summary>
public class PostgresBaseType : PostgresType
{
[CanBeNull]
readonly DbType[] _dbTypes;
[CanBeNull]
readonly Type[] _clrTypes;
[CanBeNull]
ConstructorInfo _ctorWithRegistry;
[CanBeNull]
ConstructorInfo _ctorWithoutRegistry;
/// <summary>
/// Constructs a representation of a PostgreSQL base data type.
/// </summary>
#pragma warning disable CA2222 // Do not decrease inherited member visibility
internal PostgresBaseType(string ns, string name, uint oid, Type handlerType, TypeMappingAttribute mapping)
#pragma warning restore CA2222 // Do not decrease inherited member visibility
: base(ns, name, oid)
{
HandlerType = handlerType;
NpgsqlDbType = mapping.NpgsqlDbType;
_dbTypes = mapping.DbTypes;
_clrTypes = mapping.ClrTypes;
}
/// <summary>
/// Constructs an unsupported base type (no handler exists in Npgsql for this type)
/// </summary>
protected internal PostgresBaseType(string ns, string name, uint oid) : base(ns, name, oid)
{
_dbTypes = new DbType[0];
_clrTypes = new Type[0];
}
internal override void AddTo(TypeHandlerRegistry.AvailablePostgresTypes types)
{
base.AddTo(types);
if (_dbTypes != null)
foreach (var dbType in _dbTypes)
types.ByDbType[dbType] = this;
if (_clrTypes != null)
foreach (var type in _clrTypes)
types.ByClrType[type] = this;
}
internal override TypeHandler Activate(TypeHandlerRegistry registry)
{
if (HandlerType == null)
{
registry.ByOID[OID] = registry.UnrecognizedTypeHandler;
return registry.UnrecognizedTypeHandler;
}
var handler = InstantiateHandler(registry);
registry.ByOID[OID] = handler;
if (NpgsqlDbType.HasValue)
{
var value = NpgsqlDbType.Value;
if (registry.ByNpgsqlDbType.ContainsKey(value))
throw new Exception($"Two type handlers registered on same NpgsqlDbType {NpgsqlDbType}: {registry.ByNpgsqlDbType[value].GetType().Name} and {HandlerType.Name}");
registry.ByNpgsqlDbType[NpgsqlDbType.Value] = handler;
}
if (_dbTypes != null)
{
foreach (var dbType in _dbTypes)
{
if (registry.ByDbType.ContainsKey(dbType))
throw new Exception($"Two type handlers registered on same DbType {dbType}: {registry.ByDbType[dbType].GetType().Name} and {HandlerType.Name}");
registry.ByDbType[dbType] = handler;
}
}
if (_clrTypes != null)
{
foreach (var type in _clrTypes)
{
if (registry.ByType.ContainsKey(type))
throw new Exception($"Two type handlers registered on same .NET type {type}: {registry.ByType[type].GetType().Name} and {HandlerType.Name}");
registry.ByType[type] = handler;
}
}
return handler;
}
/// <summary>
/// Instantiate the type handler. If it has a constructor that accepts a TypeHandlerRegistry, use that to allow
/// the handler to make connector-specific adjustments. Otherwise (the normal case), use the default constructor.
/// </summary>
/// <param name="registry"></param>
/// <returns></returns>
TypeHandler InstantiateHandler(TypeHandlerRegistry registry)
{
Debug.Assert(HandlerType != null);
if (_ctorWithRegistry == null && _ctorWithoutRegistry == null)
{
var ctors = HandlerType.GetConstructors(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
_ctorWithRegistry = (
from c in ctors
let p = c.GetParameters()
where p.Length == 2 && p[0].ParameterType == typeof(PostgresType) && p[1].ParameterType == typeof(TypeHandlerRegistry)
select c
).FirstOrDefault();
if (_ctorWithRegistry == null)
{
_ctorWithoutRegistry = (
from c in ctors
let p = c.GetParameters()
where p.Length == 1 && p[0].ParameterType == typeof(PostgresType)
select c
).FirstOrDefault();
if (_ctorWithoutRegistry == null)
throw new Exception($"Type handler type {HandlerType.Name} does not have an appropriate constructor");
}
}
if (_ctorWithRegistry != null)
return (TypeHandler)_ctorWithRegistry.Invoke(new object[] { this, registry });
Debug.Assert(_ctorWithoutRegistry != null);
return (TypeHandler)_ctorWithoutRegistry.Invoke(new object[] { this });
}
}
}