forked from danbarua/Npgsql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNpgsqlTypeInfoBackend.cs
More file actions
302 lines (279 loc) · 11.4 KB
/
NpgsqlTypeInfoBackend.cs
File metadata and controls
302 lines (279 loc) · 11.4 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
// NpgsqlTypes.NpgsqlTypeInfoBackend.cs
//
// Author:
// Francisco Jr. (fxjrlists@yahoo.com.br)
//
// Copyright (C) 2002 The Npgsql Development Team
// npgsql-general@gborg.postgresql.org
// http://gborg.postgresql.org/project/npgsql/projdisplay.php
//
// 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.
using System;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
using System.Net;
using System.Net.NetworkInformation;
using System.Reflection;
using System.Resources;
using System.Text;
using System.IO;
using Npgsql;
namespace NpgsqlTypes
{
/// <summary>
/// Delegate called to convert the given backend text data to its native representation.
/// </summary>
internal delegate Object ConvertBackendTextToNativeHandler(
NpgsqlBackendTypeInfo TypeInfo, byte[] BackendData, Int16 TypeSize, Int32 TypeModifier);
/// <summary>
/// Delegate called to convert the given backend binary data to its native representation.
/// </summary>
internal delegate Object ConvertBackendBinaryToNativeHandler(
NpgsqlBackendTypeInfo TypeInfo, byte[] BackendData, Int32 fieldValueSize, Int32 TypeModifier);
/// <summary>
/// Represents a backend data type.
/// This class can be called upon to convert a backend field representation to a native object.
/// </summary>
internal class NpgsqlBackendTypeInfo
{
private readonly ConvertBackendTextToNativeHandler _ConvertBackendTextToNative;
private readonly ConvertBackendBinaryToNativeHandler _ConvertBackendBinaryToNative;
private readonly ConvertProviderTypeToFrameworkTypeHander _convertProviderToFramework;
private readonly ConvertFrameworkTypeToProviderTypeHander _convertFrameworkToProvider;
internal Int32 _OID;
private readonly String _Name;
private readonly NpgsqlDbType _NpgsqlDbType;
private readonly DbType _DbType;
private readonly Type _Type;
private readonly Type _frameworkType;
/// <summary>
/// Construct a new NpgsqlTypeInfo with the given attributes and conversion handlers.
/// </summary>
/// <param name="OID">Type OID provided by the backend server.</param>
/// <param name="Name">Type name provided by the backend server.</param>
/// <param name="NpgsqlDbType">NpgsqlDbType</param>
/// <param name="DbType">DbType</param>
/// <param name="Type">System type to convert fields of this type to.</param>
/// <param name="ConvertBackendTextToNative">Data conversion handler for text encoding.</param>
/// <param name="ConvertBackendBinaryToNative">Data conversion handler for binary data.</param>
public NpgsqlBackendTypeInfo(Int32 OID, String Name, NpgsqlDbType NpgsqlDbType, DbType DbType, Type Type,
ConvertBackendTextToNativeHandler ConvertBackendTextToNative = null,
ConvertBackendBinaryToNativeHandler ConvertBackendBinaryToNative = null)
{
if (Type == null)
{
throw new ArgumentNullException("Type");
}
_OID = OID;
_Name = Name;
_NpgsqlDbType = NpgsqlDbType;
_DbType = DbType;
_Type = Type;
_frameworkType = Type;
_ConvertBackendTextToNative = ConvertBackendTextToNative;
_ConvertBackendBinaryToNative = ConvertBackendBinaryToNative;
}
public NpgsqlBackendTypeInfo(Int32 OID, String Name, NpgsqlDbType NpgsqlDbType, DbType DbType, Type Type,
ConvertBackendTextToNativeHandler ConvertBackendTextToNative,
ConvertBackendBinaryToNativeHandler ConvertBackendBinaryToNative,
Type frameworkType,
ConvertProviderTypeToFrameworkTypeHander convertProviderToFramework,
ConvertFrameworkTypeToProviderTypeHander convertFrameworkToProvider)
: this(OID, Name, NpgsqlDbType, DbType, Type, ConvertBackendTextToNative, ConvertBackendBinaryToNative)
{
_frameworkType = frameworkType;
_convertProviderToFramework = convertProviderToFramework;
_convertFrameworkToProvider = convertFrameworkToProvider;
}
public NpgsqlBackendTypeInfo(Int32 OID, String Name, NpgsqlDbType NpgsqlDbType, DbType DbType, Type Type,
ConvertBackendTextToNativeHandler ConvertBackendTextToNative,
Type frameworkType,
ConvertProviderTypeToFrameworkTypeHander convertProviderToFramework,
ConvertFrameworkTypeToProviderTypeHander convertFrameworkToProvider)
: this(OID, Name, NpgsqlDbType, DbType, Type, ConvertBackendTextToNative, null)
{
_frameworkType = frameworkType;
_convertProviderToFramework = convertProviderToFramework;
_convertFrameworkToProvider = convertFrameworkToProvider;
}
/// <summary>
/// Type OID provided by the backend server.
/// </summary>
public Int32 OID
{
get { return _OID; }
}
/// <summary>
/// Type name provided by the backend server.
/// </summary>
public String Name
{
get { return _Name; }
}
/// <summary>
/// NpgsqlDbType.
/// </summary>
public NpgsqlDbType NpgsqlDbType
{
get { return _NpgsqlDbType; }
}
/// <summary>
/// NpgsqlDbType.
/// </summary>
public DbType DbType
{
get { return _DbType; }
}
/// <summary>
/// Provider type to convert fields of this type to.
/// </summary>
public Type Type
{
get { return _Type; }
}
public Type GetType(int typeModifier, bool omitArray = false)
{
if (typeModifier == 1 && NpgsqlDbType == NpgsqlDbType.Bit)
{
return typeof(bool);
}
else if (typeModifier == 1 && NpgsqlDbType == (NpgsqlDbType.Array | NpgsqlDbType.Bit))
{
if (omitArray)
return typeof(bool);
else
return typeof(bool[]);
}
else
{
return _Type;
}
}
/// <summary>
/// System type to convert fields of this type to.
/// </summary>
public Type FrameworkType
{
get { return _frameworkType; }
}
public Type GetFrameworkType(int typeModifier, bool omitArray = false)
{
if (typeModifier == 1 && NpgsqlDbType == NpgsqlDbType.Bit)
{
return typeof(bool);
}
else if (typeModifier == 1 && NpgsqlDbType == (NpgsqlDbType.Array | NpgsqlDbType.Bit))
{
if (omitArray)
return typeof(bool);
else
return typeof(bool[]);
}
else
{
return _frameworkType;
}
}
/// <summary>
/// Reports whether a backend binary to native decoder is available for this type.
/// </summary>
public bool SupportsBinaryBackendData
{
get { return (! NpgsqlTypesHelper.SuppressBinaryBackendEncoding && _ConvertBackendBinaryToNative != null); }
}
/// <summary>
/// Perform a data conversion from a backend representation to
/// a native object.
/// </summary>
/// <param name="BackendData">Data sent from the backend.</param>
/// <param name="fieldValueSize">fieldValueSize</param>
/// <param name="TypeModifier">Type modifier field sent from the backend.</param>
public Object ConvertBackendBinaryToNative(Byte[] BackendData, Int32 fieldValueSize, Int32 TypeModifier)
{
if (! NpgsqlTypesHelper.SuppressBinaryBackendEncoding && _ConvertBackendBinaryToNative != null)
{
return _ConvertBackendBinaryToNative(this, BackendData, fieldValueSize, TypeModifier);
}
else
{
return BackendData;
}
}
/// <summary>
/// Perform a data conversion from a backend representation to
/// a native object.
/// </summary>
/// <param name="BackendData">Data sent from the backend.</param>
/// <param name="TypeSize">TypeSize</param>
/// <param name="TypeModifier">Type modifier field sent from the backend.</param>
public Object ConvertBackendTextToNative(Byte[] BackendData, Int16 TypeSize, Int32 TypeModifier)
{
if (_ConvertBackendTextToNative != null)
{
return _ConvertBackendTextToNative(this, BackendData, TypeSize, TypeModifier);
}
else
{
try
{
return Convert.ChangeType(BackendEncoding.UTF8Encoding.GetString(BackendData), Type, CultureInfo.InvariantCulture);
}
catch
{
return BackendData;
}
}
}
internal object ConvertToFrameworkType(object providerValue)
{
if (providerValue == DBNull.Value)
{
return providerValue;
}
else if (_convertProviderToFramework != null)
{
return _convertProviderToFramework(providerValue);
}
else if (Type != FrameworkType)
{
try
{
return Convert.ChangeType(providerValue, FrameworkType, CultureInfo.InvariantCulture);
}
catch
{
return providerValue;
}
}
return providerValue;
}
internal object ConvertToProviderType(object frameworkValue)
{
if (frameworkValue == DBNull.Value)
{
return frameworkValue;
}
else if (_convertFrameworkToProvider!= null)
{
return _convertFrameworkToProvider(frameworkValue);
}
return frameworkValue;
}
}
}