forked from npgsql/npgsql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNpgsqlTypeInfoNative.cs
More file actions
384 lines (331 loc) · 14.4 KB
/
NpgsqlTypeInfoNative.cs
File metadata and controls
384 lines (331 loc) · 14.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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
// NpgsqlTypes.NpgsqlTypeInfoNative.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 native data to its backand representation.
/// </summary>
internal delegate byte[] ConvertNativeToBackendTextHandler(NpgsqlNativeTypeInfo TypeInfo, Object NativeData, Boolean forExtendedQuery, NativeToBackendTypeConverterOptions options, bool arrayElement);
internal delegate byte[] ConvertNativeToBackendBinaryHandler(NpgsqlNativeTypeInfo TypeInfo, Object NativeData, NativeToBackendTypeConverterOptions options);
internal delegate object ConvertProviderTypeToFrameworkTypeHander(object value);
internal delegate object ConvertFrameworkTypeToProviderTypeHander(object value);
/// <summary>
/// Represents a backend data type.
/// This class can be called upon to convert a native object to its backend field representation,
/// </summary>
internal class NpgsqlNativeTypeInfo
{
private static readonly NumberFormatInfo ni;
private readonly ConvertNativeToBackendTextHandler _ConvertNativeToBackendText;
private readonly ConvertNativeToBackendBinaryHandler _ConvertNativeToBackendBinary;
private readonly String _Name;
private readonly string _CastName;
private readonly NpgsqlDbType _NpgsqlDbType;
private readonly DbType _DbType;
private readonly Boolean _Quote;
private readonly Boolean _UseSize;
private Boolean _IsArray = false;
/// <summary>
/// Returns an NpgsqlNativeTypeInfo for an array where the elements are of the type
/// described by the NpgsqlNativeTypeInfo supplied.
/// </summary>
public static NpgsqlNativeTypeInfo ArrayOf(NpgsqlNativeTypeInfo elementType)
{
if (elementType._IsArray)
//we've an array of arrays. It's the inner most elements whose type we care about, so the type we have is fine.
{
return elementType;
}
NpgsqlNativeTypeInfo copy = null;
ArrayNativeToBackendTypeConverter converter = new ArrayNativeToBackendTypeConverter(elementType);
if (elementType._ConvertNativeToBackendBinary != null)
{
copy = new NpgsqlNativeTypeInfo("_" + elementType.Name, NpgsqlDbType.Array | elementType.NpgsqlDbType, elementType.DbType,
false,
converter.ArrayToArrayText,
converter.ArrayToArrayBinary);
}
else
{
copy = new NpgsqlNativeTypeInfo("_" + elementType.Name, NpgsqlDbType.Array | elementType.NpgsqlDbType, elementType.DbType,
false,
converter.ArrayToArrayText);
}
copy._IsArray = true;
return copy;
}
static NpgsqlNativeTypeInfo()
{
ni = (NumberFormatInfo) CultureInfo.InvariantCulture.NumberFormat.Clone();
ni.NumberDecimalDigits = 15;
}
internal static NumberFormatInfo NumberFormat
{
get { return ni; }
}
/// <summary>
/// Construct a new NpgsqlTypeInfo with the given attributes and conversion handlers.
/// </summary>
/// <param name="Name">Type name provided by the backend server.</param>
/// <param name="DbType">DbType</param>
/// <param name="Quote">Quote</param>
/// <param name="NpgsqlDbType">NpgsqlDbType</param>
/// <param name="ConvertNativeToBackendText">Data conversion handler for text backend encoding.</param>
/// <param name="ConvertNativeToBackendBinary">Data conversion handler for binary backend encoding (for extended queries).</param>
public NpgsqlNativeTypeInfo(String Name, NpgsqlDbType NpgsqlDbType, DbType DbType, Boolean Quote,
ConvertNativeToBackendTextHandler ConvertNativeToBackendText = null,
ConvertNativeToBackendBinaryHandler ConvertNativeToBackendBinary = null)
{
_Name = Name;
_CastName = Name.StartsWith("_") ? Name.Substring(1) + "[]" : Name;
_NpgsqlDbType = NpgsqlDbType;
_DbType = DbType;
_Quote = Quote;
_ConvertNativeToBackendText = ConvertNativeToBackendText;
_ConvertNativeToBackendBinary = ConvertNativeToBackendBinary;
// The only parameters types which use length currently supported are char and varchar. Check for them.
if ((NpgsqlDbType == NpgsqlDbType.Char) || (NpgsqlDbType == NpgsqlDbType.Varchar))
{
_UseSize = true;
}
else
{
_UseSize = false;
}
}
/// <summary>
/// Type name provided by the backend server.
/// </summary>
public String Name
{
get { return _Name; }
}
public string CastName
{
get { return _CastName; }
}
public bool IsArray
{
get { return _IsArray; }
}
/// <summary>
/// NpgsqlDbType.
/// </summary>
public NpgsqlDbType NpgsqlDbType
{
get { return _NpgsqlDbType; }
}
/// <summary>
/// DbType.
/// </summary>
public DbType DbType
{
get { return _DbType; }
}
/// <summary>
/// Apply quoting.
/// </summary>
public Boolean Quote
{
get { return _Quote; }
}
/// <summary>
/// Use parameter size information.
/// </summary>
public Boolean UseSize
{
get { return _UseSize; }
}
/// <summary>
/// Perform a data conversion from a native object to
/// a backend representation.
/// DBNull and null values are handled differently depending if a plain query is used
/// When
/// </summary>
/// <param name="NativeData">Native .NET object to be converted.</param>
/// <param name="forExtendedQuery">Specifies that the value should be formatted for the extended query syntax.</param>
/// <param name="options">Options to guide serialization. If null, a default options set is used.</param>
/// <param name="arrayElement">Specifies that the value should be formatted as an extended query array element.</param>
public byte[] ConvertToBackend(Object NativeData, Boolean forExtendedQuery, NativeToBackendTypeConverterOptions options = null, bool arrayElement = false)
{
if (options == null)
{
options = NativeToBackendTypeConverterOptions.Default;
}
if (forExtendedQuery)
{
return ConvertToBackendExtendedQuery(NativeData, options, arrayElement);
}
else
{
return ConvertToBackendPlainQuery(NativeData, options, arrayElement);
}
}
private byte[] ConvertToBackendPlainQuery(Object NativeData, NativeToBackendTypeConverterOptions options, bool arrayElement)
{
if ((NativeData == DBNull.Value) || (NativeData == null))
{
return ASCIIByteArrays.NULL; // Plain queries exptects null values as string NULL.
}
if (_ConvertNativeToBackendText != null)
{
byte[] backendSerialization;
// This path is responsible for escaping, and may also add quoting and the E prefix.
backendSerialization = (_ConvertNativeToBackendText(this, NativeData, false, options, arrayElement));
if (Quote)
{
backendSerialization = QuoteASCIIString(backendSerialization, false, arrayElement);
}
return backendSerialization;
}
else if (NativeData is Enum)
{
byte[] backendSerialization;
// Do a special handling of Enum values.
// Translate enum value to its underlying type.
backendSerialization =
BackendEncoding.UTF8Encoding.GetBytes(
(String)Convert.ChangeType(
Enum.Format(NativeData.GetType(), NativeData, "d"),
typeof(String), CultureInfo.InvariantCulture
)
);
backendSerialization = QuoteASCIIString(backendSerialization, false, arrayElement);
return backendSerialization;
}
else
{
byte[] backendSerialization;
if (NativeData is IFormattable)
{
backendSerialization = BackendEncoding.UTF8Encoding.GetBytes(((IFormattable)NativeData).ToString(null, ni));
}
else
{
backendSerialization = BackendEncoding.UTF8Encoding.GetBytes(NativeData.ToString());
}
if (Quote)
{
backendSerialization = QuoteASCIIString(backendSerialization, false, arrayElement);
}
return backendSerialization;
}
}
private byte[] ConvertToBackendExtendedQuery(Object NativeData, NativeToBackendTypeConverterOptions options, bool arrayElement)
{
if ((NativeData == DBNull.Value) || (NativeData == null))
{
return null; // Extended query expects null values be represented as null.
}
if (! NpgsqlTypesHelper.SuppressBinaryBackendEncoding && _ConvertNativeToBackendBinary != null)
{
return _ConvertNativeToBackendBinary(this, NativeData, options);
}
else if (_ConvertNativeToBackendText != null)
{
byte[] backendSerialization;
backendSerialization = _ConvertNativeToBackendText(this, NativeData, true, options, arrayElement);
if (Quote)
{
backendSerialization = QuoteASCIIString(backendSerialization, true, arrayElement);
}
return backendSerialization;
}
else
{
byte[] backendSerialization;
if (NativeData is Enum)
{
// Do a special handling of Enum values.
// Translate enum value to its underlying type.
backendSerialization = BackendEncoding.UTF8Encoding.GetBytes((String)
Convert.ChangeType(Enum.Format(NativeData.GetType(), NativeData, "d"), typeof (String),
CultureInfo.InvariantCulture));
}
else if (NativeData is IFormattable)
{
backendSerialization = BackendEncoding.UTF8Encoding.GetBytes(((IFormattable) NativeData).ToString(null, ni));
}
else
{
backendSerialization = BackendEncoding.UTF8Encoding.GetBytes(NativeData.ToString());
}
if (Quote)
{
backendSerialization = QuoteASCIIString(backendSerialization, true, arrayElement);
}
return backendSerialization;
}
}
private static byte[] QuoteASCIIString(byte[] src, bool forExtendedQuery, bool arrayElement)
{
byte[] ret = null;
if (arrayElement)
{
// Array elements always require double-quotes
ret = new byte[src.Length + 2];
ret[0] = (byte)ASCIIBytes.DoubleQuote;
src.CopyTo(ret, 1);
ret[ret.Length - 1] = (byte)ASCIIBytes.DoubleQuote;
}
else
{
if (forExtendedQuery)
{
// Non-array-element values sent via Bind are not quoted
ret = src;
}
else
{
// Non-array-element values sent via simple query require single-quotes
ret = new byte[src.Length + 2];
ret[0] = (byte)ASCIIBytes.SingleQuote;
src.CopyTo(ret, 1);
ret[ret.Length - 1] = (byte)ASCIIBytes.SingleQuote;
}
}
return ret;
}
/// <summary>
/// Reports whether a native to backend binary encoder is available for this type.
/// </summary>
public bool SupportsBinaryBackendData
{
get { return (! NpgsqlTypesHelper.SuppressBinaryBackendEncoding && _ConvertNativeToBackendBinary != null); }
}
}
}