forked from npgsql/npgsql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNpgsqlTypesHelper.cs
More file actions
823 lines (656 loc) · 42.7 KB
/
NpgsqlTypesHelper.cs
File metadata and controls
823 lines (656 loc) · 42.7 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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
// NpgsqlTypes.NpgsqlTypesHelper.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>
/// This class contains helper methods for type conversion between
/// the .Net type system and postgresql.
/// </summary>
internal static class NpgsqlTypesHelper
{
// Logging related values
private static readonly String CLASSNAME = MethodBase.GetCurrentMethod().DeclaringType.Name;
// This is used by the test suite to test both text and binary encodings on version 3 connections.
// See NpgsqlTests.BaseClassTests.TestFixtureSetup() and InitBinaryBackendSuppression().
// If this field is changed or removed, some tests will become partially non-functional, and an error will be issued.
internal static bool SuppressBinaryBackendEncoding = false;
private struct MappingKey : IEquatable<MappingKey>
{
public readonly Version Version;
public readonly bool UseExtendedTypes;
public MappingKey(NpgsqlConnector conn)
{
Version = conn.ServerVersion;
UseExtendedTypes = conn.UseExtendedTypes;
}
public bool Equals(MappingKey other)
{
return UseExtendedTypes.Equals(other.UseExtendedTypes) && Version.Equals(other.Version);
}
public override bool Equals(object obj)
{
//Note that Dictionary<T, U> will call IEquatable<T>.Equals() when possible.
//This is included for completeness (that and second-guessing Mono while coding on .NET!).
return obj != null && obj is MappingKey && Equals((MappingKey) obj);
}
public override int GetHashCode()
{
return UseExtendedTypes ? ~Version.GetHashCode() : Version.GetHashCode();
}
}
/// <summary>
/// A cache of basic datatype mappings keyed by server version. This way we don't
/// have to load the basic type mappings for every connection.
/// </summary>
private static readonly Dictionary<MappingKey, NpgsqlBackendTypeMapping> BackendTypeMappingCache =
new Dictionary<MappingKey, NpgsqlBackendTypeMapping>();
private static readonly NpgsqlNativeTypeMapping NativeTypeMapping = PrepareDefaultTypesMap();
private static readonly Version Npgsql207 = new Version("2.0.7");
private static readonly Dictionary<string, NpgsqlBackendTypeInfo> DefaultBackendInfoMapping = PrepareDefaultBackendInfoMapping();
private static Dictionary<string, NpgsqlBackendTypeInfo> PrepareDefaultBackendInfoMapping()
{
Dictionary<string, NpgsqlBackendTypeInfo> NameIndex = new Dictionary<string, NpgsqlBackendTypeInfo>();
foreach (NpgsqlBackendTypeInfo TypeInfo in TypeInfoList(false, new Version("1000.0.0.0")))
{
NameIndex.Add(TypeInfo.Name, TypeInfo);
//do the same for the equivalent array type.
NameIndex.Add("_" + TypeInfo.Name, ArrayTypeInfo(TypeInfo));
}
return NameIndex;
}
/// <summary>
/// Find a NpgsqlNativeTypeInfo in the default types map that can handle objects
/// of the given NpgsqlDbType.
/// </summary>
public static bool TryGetBackendTypeInfo(String BackendTypeName, out NpgsqlBackendTypeInfo TypeInfo)
{
return DefaultBackendInfoMapping.TryGetValue(BackendTypeName, out TypeInfo);
}
/// <summary>
/// Find a NpgsqlNativeTypeInfo in the default types map that can handle objects
/// of the given NpgsqlDbType.
/// </summary>
public static bool TryGetNativeTypeInfo(NpgsqlDbType dbType, out NpgsqlNativeTypeInfo typeInfo)
{
return NativeTypeMapping.TryGetValue(dbType, out typeInfo);
}
/// <summary>
/// Find a NpgsqlNativeTypeInfo in the default types map that can handle objects
/// of the given DbType.
/// </summary>
public static bool TryGetNativeTypeInfo(DbType dbType, out NpgsqlNativeTypeInfo typeInfo)
{
return NativeTypeMapping.TryGetValue(dbType, out typeInfo);
}
public static NpgsqlNativeTypeInfo GetNativeTypeInfo(DbType DbType)
{
NpgsqlNativeTypeInfo ret = null;
return TryGetNativeTypeInfo(DbType, out ret) ? ret : null;
}
private static bool TestTypedEnumerator(Type type, out Type typeOut)
{
if (type.IsArray)
{
typeOut = type.GetElementType();
return true;
}
//We can only work out the element type for IEnumerable<T> not for IEnumerable
//so we are looking for IEnumerable<T> for any value of T.
//So we want to find an interface type where GetGenericTypeDefinition == typeof(IEnumerable<>);
//And we can only safely call GetGenericTypeDefinition() if IsGenericType is true, but if it's false
//then the interface clearly isn't an IEnumerable<T>.
foreach (Type iface in type.GetInterfaces())
{
if (iface.IsGenericType && iface.GetGenericTypeDefinition().Equals(typeof (IEnumerable<>)))
{
typeOut = iface.GetGenericArguments()[0];
return true;
}
}
typeOut = null;
return false;
}
/// <summary>
/// Find a NpgsqlNativeTypeInfo in the default types map that can handle objects
/// of the given System.Type.
/// </summary>
public static bool TryGetNativeTypeInfo(Type type, out NpgsqlNativeTypeInfo typeInfo)
{
if (NativeTypeMapping.TryGetValue(type, out typeInfo))
{
return true;
}
// At this point there is no direct mapping, so we see if we have an array or IEnumerable<T>.
// Note that we checked for a direct mapping first, so if there is a direct mapping of a class
// which implements IEnumerable<T> we will use that (currently this is only string, which
// implements IEnumerable<char>.
Type elementType = null;
NpgsqlNativeTypeInfo elementTypeInfo = null;
if (TestTypedEnumerator(type, out elementType) && TryGetNativeTypeInfo(elementType, out elementTypeInfo))
{
typeInfo = NpgsqlNativeTypeInfo.ArrayOf(elementTypeInfo);
return true;
}
return false;
}
public static NpgsqlNativeTypeInfo GetNativeTypeInfo(Type Type)
{
NpgsqlNativeTypeInfo ret = null;
return TryGetNativeTypeInfo(Type, out ret) ? ret : null;
}
public static bool DefinedType(Type type)
{
return NativeTypeMapping.ContainsType(type);
}
public static bool DefinedType(object item)
{
return DefinedType(item.GetType());
}
///<summary>
/// This method is responsible to convert the byte[] received from the backend
/// to the corresponding NpgsqlType.
/// The given TypeInfo is called upon to do the conversion.
/// If no TypeInfo object is provided, no conversion is performed.
/// </summary>
public static Object ConvertBackendBytesToSystemType(NpgsqlBackendTypeInfo TypeInfo, Byte[] data, Int32 fieldValueSize,
Int32 typeModifier)
{
NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "ConvertBackendBytesToStytemType");
if (TypeInfo != null)
{
return TypeInfo.ConvertBackendBinaryToNative(data, fieldValueSize, typeModifier);
}
else
{
return data;
}
}
///<summary>
/// This method is responsible to convert the string received from the backend
/// to the corresponding NpgsqlType.
/// The given TypeInfo is called upon to do the conversion.
/// If no TypeInfo object is provided, no conversion is performed.
/// </summary>
public static Object ConvertBackendStringToSystemType(NpgsqlBackendTypeInfo TypeInfo, Byte[] data, Int16 typeSize,
Int32 typeModifier)
{
NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "ConvertBackendStringToSystemType");
if (TypeInfo != null)
{
return TypeInfo.ConvertBackendTextToNative(data, typeSize, typeModifier);
}
else
{
return BackendEncoding.UTF8Encoding.GetString(data);
}
}
/// <summary>
/// Create the one and only native to backend type map.
/// This map is used when formatting native data
/// types to backend representations.
/// </summary>
private static NpgsqlNativeTypeMapping PrepareDefaultTypesMap()
{
NpgsqlNativeTypeMapping nativeTypeMapping = new NpgsqlNativeTypeMapping();
nativeTypeMapping.AddType("name", NpgsqlDbType.Name, DbType.String, true, null);
nativeTypeMapping.AddType("oidvector", NpgsqlDbType.Oidvector, DbType.String, true, null);
// Conflicting types should have mapped first the non default mappings.
// For example, char, varchar and text map to DbType.String. As the most
// common is to use text with string, it has to be the last mapped, in order
// to type mapping has the last entry, in this case, text, as the map value
// for DbType.String.
nativeTypeMapping.AddType("refcursor", NpgsqlDbType.Refcursor, DbType.String, true, null);
nativeTypeMapping.AddType("char", NpgsqlDbType.Char, DbType.String, false,
BasicNativeToBackendTypeConverter.StringToTextText,
BasicNativeToBackendTypeConverter.StringToTextBinary);
nativeTypeMapping.AddTypeAlias("char", typeof(Char));
nativeTypeMapping.AddType("varchar", NpgsqlDbType.Varchar, DbType.String, false,
BasicNativeToBackendTypeConverter.StringToTextText,
BasicNativeToBackendTypeConverter.StringToTextBinary);
// Dummy type that facilitates non-binary string conversions for types that are treated as
// text but which are not really text. Those types cause problems if they are encoded as binary.
// The mapping NpgsqlDbType.Text => text_nonbinary is removed when text is mapped.
// DBType.Object will be re-mapped to this type at the end.
nativeTypeMapping.AddType("unknown", NpgsqlDbType.Text, DbType.Object, true);
nativeTypeMapping.AddType("text", NpgsqlDbType.Text, DbType.String, false,
BasicNativeToBackendTypeConverter.StringToTextText,
BasicNativeToBackendTypeConverter.StringToTextBinary);
nativeTypeMapping.AddDbTypeAlias("text", DbType.StringFixedLength);
nativeTypeMapping.AddDbTypeAlias("text", DbType.AnsiString);
nativeTypeMapping.AddDbTypeAlias("text", DbType.AnsiStringFixedLength);
nativeTypeMapping.AddTypeAlias("text", typeof(String));
nativeTypeMapping.AddType("bytea", NpgsqlDbType.Bytea, DbType.Binary, false,
BasicNativeToBackendTypeConverter.ByteArrayToByteaText,
BasicNativeToBackendTypeConverter.ByteArrayToByteaBinary);
nativeTypeMapping.AddTypeAlias("bytea", typeof(Byte[]));
nativeTypeMapping.AddType("bit", NpgsqlDbType.Bit, DbType.Object, false,
BasicNativeToBackendTypeConverter.ToBit);
nativeTypeMapping.AddTypeAlias("bit", typeof(BitString));
nativeTypeMapping.AddType("bool", NpgsqlDbType.Boolean, DbType.Boolean, false,
BasicNativeToBackendTypeConverter.BooleanToBooleanText,
BasicNativeToBackendTypeConverter.BooleanToBooleanBinary);
nativeTypeMapping.AddTypeAlias("bool", typeof(Boolean));
nativeTypeMapping.AddType("int2", NpgsqlDbType.Smallint, DbType.Int16, false,
BasicNativeToBackendTypeConverter.ToBasicType<short>,
BasicNativeToBackendTypeConverter.Int16ToInt2Binary);
nativeTypeMapping.AddTypeAlias("int2", typeof(UInt16));
nativeTypeMapping.AddTypeAlias("int2", typeof(Int16));
nativeTypeMapping.AddDbTypeAlias("int2", DbType.Byte);
nativeTypeMapping.AddTypeAlias("int2", typeof(Byte));
nativeTypeMapping.AddType("int4", NpgsqlDbType.Integer, DbType.Int32, false,
BasicNativeToBackendTypeConverter.ToBasicType<int>,
BasicNativeToBackendTypeConverter.Int32ToInt4Binary);
nativeTypeMapping.AddTypeAlias("int4", typeof(Int32));
nativeTypeMapping.AddType("int8", NpgsqlDbType.Bigint, DbType.Int64, false,
BasicNativeToBackendTypeConverter.ToBasicType<long>,
BasicNativeToBackendTypeConverter.Int64ToInt8Binary);
nativeTypeMapping.AddTypeAlias("int8", typeof(Int64));
nativeTypeMapping.AddType("float4", NpgsqlDbType.Real, DbType.Single, false,
BasicNativeToBackendTypeConverter.SingleToFloat4Text,
BasicNativeToBackendTypeConverter.SingleToFloat4Binary);
nativeTypeMapping.AddTypeAlias("float4", typeof(Single));
nativeTypeMapping.AddType("float8", NpgsqlDbType.Double, DbType.Double, false,
BasicNativeToBackendTypeConverter.DoubleToFloat8Text,
BasicNativeToBackendTypeConverter.DoubleToFloat8Binary);
nativeTypeMapping.AddTypeAlias("float8", typeof(Double));
nativeTypeMapping.AddType("numeric", NpgsqlDbType.Numeric, DbType.Decimal, false,
BasicNativeToBackendTypeConverter.ToBasicType<decimal>);
nativeTypeMapping.AddTypeAlias("numeric", typeof (Decimal));
nativeTypeMapping.AddType("money", NpgsqlDbType.Money, DbType.Currency, true,
BasicNativeToBackendTypeConverter.ToMoney);
nativeTypeMapping.AddType("date", NpgsqlDbType.Date, DbType.Date, true,
BasicNativeToBackendTypeConverter.ToDate);
nativeTypeMapping.AddTypeAlias("date", typeof (NpgsqlDate));
nativeTypeMapping.AddType("timetz", NpgsqlDbType.TimeTZ, DbType.Time, true,
ExtendedNativeToBackendTypeConverter.ToTimeTZ);
nativeTypeMapping.AddTypeAlias("timetz", typeof (NpgsqlTimeTZ));
nativeTypeMapping.AddType("time", NpgsqlDbType.Time, DbType.Time, true,
BasicNativeToBackendTypeConverter.ToTime);
nativeTypeMapping.AddTypeAlias("time", typeof (NpgsqlTime));
nativeTypeMapping.AddType("timestamptz", NpgsqlDbType.TimestampTZ, DbType.DateTime, true,
ExtendedNativeToBackendTypeConverter.ToTimeStamp);
nativeTypeMapping.AddTypeAlias("timestamptz", typeof(NpgsqlTimeStampTZ));
nativeTypeMapping.AddDbTypeAlias("timestamptz", DbType.DateTimeOffset);
nativeTypeMapping.AddTypeAlias("timestamptz", typeof(DateTimeOffset));
nativeTypeMapping.AddType("abstime", NpgsqlDbType.Abstime, DbType.DateTime, true,
ExtendedNativeToBackendTypeConverter.ToTimeStamp);
nativeTypeMapping.AddType("timestamp", NpgsqlDbType.Timestamp, DbType.DateTime, true,
ExtendedNativeToBackendTypeConverter.ToTimeStamp);
nativeTypeMapping.AddTypeAlias("timestamp", typeof (DateTime));
nativeTypeMapping.AddTypeAlias("timestamp", typeof (NpgsqlTimeStamp));
nativeTypeMapping.AddType("point", NpgsqlDbType.Point, DbType.Object, true,
ExtendedNativeToBackendTypeConverter.ToPoint);
nativeTypeMapping.AddTypeAlias("point", typeof (NpgsqlPoint));
nativeTypeMapping.AddType("box", NpgsqlDbType.Box, DbType.Object, true,
ExtendedNativeToBackendTypeConverter.ToBox);
nativeTypeMapping.AddTypeAlias("box", typeof (NpgsqlBox));
nativeTypeMapping.AddType("lseg", NpgsqlDbType.LSeg, DbType.Object, true,
ExtendedNativeToBackendTypeConverter.ToLSeg);
nativeTypeMapping.AddTypeAlias("lseg", typeof (NpgsqlLSeg));
nativeTypeMapping.AddType("path", NpgsqlDbType.Path, DbType.Object, true,
ExtendedNativeToBackendTypeConverter.ToPath);
nativeTypeMapping.AddTypeAlias("path", typeof (NpgsqlPath));
nativeTypeMapping.AddType("polygon", NpgsqlDbType.Polygon, DbType.Object, true,
ExtendedNativeToBackendTypeConverter.ToPolygon);
nativeTypeMapping.AddTypeAlias("polygon", typeof (NpgsqlPolygon));
nativeTypeMapping.AddType("circle", NpgsqlDbType.Circle, DbType.Object, true,
ExtendedNativeToBackendTypeConverter.ToCircle);
nativeTypeMapping.AddTypeAlias("circle", typeof (NpgsqlCircle));
nativeTypeMapping.AddType("inet", NpgsqlDbType.Inet, DbType.Object, true,
ExtendedNativeToBackendTypeConverter.ToIPAddress);
nativeTypeMapping.AddTypeAlias("inet", typeof (IPAddress));
nativeTypeMapping.AddTypeAlias("inet", typeof (NpgsqlInet));
nativeTypeMapping.AddType("macaddr", NpgsqlDbType.MacAddr, DbType.Object, true,
ExtendedNativeToBackendTypeConverter.ToMacAddress);
nativeTypeMapping.AddTypeAlias("macaddr", typeof(PhysicalAddress));
nativeTypeMapping.AddTypeAlias("macaddr", typeof(NpgsqlMacAddress));
nativeTypeMapping.AddType("uuid", NpgsqlDbType.Uuid, DbType.Guid, true);
nativeTypeMapping.AddTypeAlias("uuid", typeof (Guid));
nativeTypeMapping.AddType("xml", NpgsqlDbType.Xml, DbType.Xml, false,
BasicNativeToBackendTypeConverter.StringToTextText,
BasicNativeToBackendTypeConverter.StringToTextBinary);
nativeTypeMapping.AddType("interval", NpgsqlDbType.Interval, DbType.Object, true,
ExtendedNativeToBackendTypeConverter.ToInterval);
nativeTypeMapping.AddTypeAlias("interval", typeof (NpgsqlInterval));
nativeTypeMapping.AddTypeAlias("interval", typeof (TimeSpan));
nativeTypeMapping.AddType("json", NpgsqlDbType.Json, DbType.Object, false,
BasicNativeToBackendTypeConverter.StringToTextText,
BasicNativeToBackendTypeConverter.StringToTextBinary);
nativeTypeMapping.AddType("jsonb", NpgsqlDbType.Jsonb, DbType.Object, false,
BasicNativeToBackendTypeConverter.StringToTextText,
BasicNativeToBackendTypeConverter.StringToTextBinary);
nativeTypeMapping.AddType("hstore", NpgsqlDbType.Hstore, DbType.Object, false,
BasicNativeToBackendTypeConverter.StringToTextText,
BasicNativeToBackendTypeConverter.StringToTextBinary);
nativeTypeMapping.AddDbTypeAlias("unknown", DbType.Object);
return nativeTypeMapping;
}
private static IEnumerable<NpgsqlBackendTypeInfo> TypeInfoList(bool useExtendedTypes, Version compat)
{
yield return new NpgsqlBackendTypeInfo(0, "oidvector", NpgsqlDbType.Text, DbType.String, typeof (String), null);
yield return new NpgsqlBackendTypeInfo(0, "unknown", NpgsqlDbType.Text, DbType.String, typeof (String),
null,
BasicBackendToNativeTypeConverter.TextBinaryToString);
yield return new NpgsqlBackendTypeInfo(0, "refcursor", NpgsqlDbType.Refcursor, DbType.String, typeof (String), null);
yield return new NpgsqlBackendTypeInfo(0, "char", NpgsqlDbType.Char, DbType.String, typeof(String),
null,
BasicBackendToNativeTypeConverter.TextBinaryToString);
yield return new NpgsqlBackendTypeInfo(0, "bpchar", NpgsqlDbType.Text, DbType.String, typeof(String),
null,
BasicBackendToNativeTypeConverter.TextBinaryToString);
yield return new NpgsqlBackendTypeInfo(0, "varchar", NpgsqlDbType.Varchar, DbType.String, typeof(String),
null,
BasicBackendToNativeTypeConverter.TextBinaryToString);
yield return new NpgsqlBackendTypeInfo(0, "text", NpgsqlDbType.Text, DbType.String, typeof(String),
null,
BasicBackendToNativeTypeConverter.TextBinaryToString);
yield return new NpgsqlBackendTypeInfo(0, "name", NpgsqlDbType.Name, DbType.String, typeof(String),
null,
BasicBackendToNativeTypeConverter.TextBinaryToString);
yield return
new NpgsqlBackendTypeInfo(0, "bytea", NpgsqlDbType.Bytea, DbType.Binary, typeof(Byte[]),
BasicBackendToNativeTypeConverter.ByteaTextToByteArray,
BasicBackendToNativeTypeConverter.ByteaBinaryToByteArray);
yield return
new NpgsqlBackendTypeInfo(0, "bit", NpgsqlDbType.Bit, DbType.Object, typeof (BitString),
BasicBackendToNativeTypeConverter.ToBit);
yield return
new NpgsqlBackendTypeInfo(0, "bool", NpgsqlDbType.Boolean, DbType.Boolean, typeof(Boolean),
BasicBackendToNativeTypeConverter.BooleanTextToBoolean,
BasicBackendToNativeTypeConverter.BooleanBinaryToBoolean);
yield return new NpgsqlBackendTypeInfo(0, "int2", NpgsqlDbType.Smallint, DbType.Int16, typeof (Int16),
null,
BasicBackendToNativeTypeConverter.IntBinaryToInt);
yield return new NpgsqlBackendTypeInfo(0, "int4", NpgsqlDbType.Integer, DbType.Int32, typeof (Int32),
null,
BasicBackendToNativeTypeConverter.IntBinaryToInt);
yield return new NpgsqlBackendTypeInfo(0, "int8", NpgsqlDbType.Bigint, DbType.Int64, typeof (Int64),
null,
BasicBackendToNativeTypeConverter.IntBinaryToInt);
yield return new NpgsqlBackendTypeInfo(0, "oid", NpgsqlDbType.Integer, DbType.Int32, typeof (Int32),
null,
BasicBackendToNativeTypeConverter.IntBinaryToInt);
yield return new NpgsqlBackendTypeInfo(0, "float4", NpgsqlDbType.Real, DbType.Single, typeof(Single),
null,
BasicBackendToNativeTypeConverter.Float4Float8BinaryToFloatDouble);
yield return new NpgsqlBackendTypeInfo(0, "float8", NpgsqlDbType.Double, DbType.Double, typeof(Double),
null,
BasicBackendToNativeTypeConverter.Float4Float8BinaryToFloatDouble);
yield return new NpgsqlBackendTypeInfo(0, "numeric", NpgsqlDbType.Numeric, DbType.Decimal, typeof (Decimal), null);
yield return
new NpgsqlBackendTypeInfo(0, "inet", NpgsqlDbType.Inet, DbType.Object, typeof (NpgsqlInet),
ExtendedBackendToNativeTypeConverter.ToInet,
typeof(IPAddress),
ipaddress => (IPAddress)(NpgsqlInet)ipaddress,
npgsqlinet => (npgsqlinet is IPAddress ? (NpgsqlInet)(IPAddress) npgsqlinet : npgsqlinet));
yield return
new NpgsqlBackendTypeInfo(0, "macaddr", NpgsqlDbType.MacAddr, DbType.Object, typeof(NpgsqlMacAddress),
ExtendedBackendToNativeTypeConverter.ToMacAddress,
typeof(PhysicalAddress),
macAddress => (PhysicalAddress)(NpgsqlMacAddress)macAddress,
npgsqlmacaddr => (npgsqlmacaddr is PhysicalAddress ? (NpgsqlMacAddress)(PhysicalAddress)npgsqlmacaddr : npgsqlmacaddr));
yield return
new NpgsqlBackendTypeInfo(0, "money", NpgsqlDbType.Money, DbType.Currency, typeof (Decimal),
BasicBackendToNativeTypeConverter.ToMoney);
yield return
new NpgsqlBackendTypeInfo(0, "point", NpgsqlDbType.Point, DbType.Object, typeof (NpgsqlPoint),
ExtendedBackendToNativeTypeConverter.ToPoint);
yield return
new NpgsqlBackendTypeInfo(0, "lseg", NpgsqlDbType.LSeg, DbType.Object, typeof (NpgsqlLSeg),
ExtendedBackendToNativeTypeConverter.ToLSeg);
yield return
new NpgsqlBackendTypeInfo(0, "path", NpgsqlDbType.Path, DbType.Object, typeof (NpgsqlPath),
ExtendedBackendToNativeTypeConverter.ToPath);
yield return
new NpgsqlBackendTypeInfo(0, "box", NpgsqlDbType.Box, DbType.Object, typeof (NpgsqlBox),
ExtendedBackendToNativeTypeConverter.ToBox);
yield return
new NpgsqlBackendTypeInfo(0, "circle", NpgsqlDbType.Circle, DbType.Object, typeof (NpgsqlCircle),
ExtendedBackendToNativeTypeConverter.ToCircle);
yield return
new NpgsqlBackendTypeInfo(0, "polygon", NpgsqlDbType.Polygon, DbType.Object, typeof (NpgsqlPolygon),
ExtendedBackendToNativeTypeConverter.ToPolygon);
yield return new NpgsqlBackendTypeInfo(0, "uuid", NpgsqlDbType.Uuid, DbType.Guid, typeof (Guid),
ExtendedBackendToNativeTypeConverter.ToGuid);
yield return new NpgsqlBackendTypeInfo(0, "xml", NpgsqlDbType.Xml, DbType.Xml, typeof (String), null);
yield return new NpgsqlBackendTypeInfo(0, "json", NpgsqlDbType.Json, DbType.Object, typeof(String),
null,
BasicBackendToNativeTypeConverter.TextBinaryToString);
yield return new NpgsqlBackendTypeInfo(0, "jsonb", NpgsqlDbType.Jsonb, DbType.Object, typeof(String),
null,
BasicBackendToNativeTypeConverter.TextBinaryToString);
yield return new NpgsqlBackendTypeInfo(0, "hstore", NpgsqlDbType.Hstore, DbType.Object, typeof(String),
null,
BasicBackendToNativeTypeConverter.TextBinaryToString);
if (useExtendedTypes)
{
yield return
new NpgsqlBackendTypeInfo(0, "interval", NpgsqlDbType.Interval, DbType.Object, typeof(NpgsqlInterval),
ExtendedBackendToNativeTypeConverter.ToInterval);
yield return
new NpgsqlBackendTypeInfo(0, "date", NpgsqlDbType.Date, DbType.Date, typeof(NpgsqlDate),
ExtendedBackendToNativeTypeConverter.ToDate);
yield return
new NpgsqlBackendTypeInfo(0, "time", NpgsqlDbType.Time, DbType.Time, typeof(NpgsqlTime),
ExtendedBackendToNativeTypeConverter.ToTime);
yield return
new NpgsqlBackendTypeInfo(0, "timetz", NpgsqlDbType.TimeTZ, DbType.Time, typeof(NpgsqlTimeTZ),
ExtendedBackendToNativeTypeConverter.ToTimeTZ);
yield return
new NpgsqlBackendTypeInfo(0, "timestamp", NpgsqlDbType.Timestamp, DbType.DateTime, typeof(NpgsqlTimeStamp),
ExtendedBackendToNativeTypeConverter.ToTimeStamp);
yield return
new NpgsqlBackendTypeInfo(0, "abstime", NpgsqlDbType.Abstime , DbType.DateTime, typeof(NpgsqlTimeStampTZ),
ExtendedBackendToNativeTypeConverter.ToTimeStampTZ);
yield return
new NpgsqlBackendTypeInfo(0, "timestamptz", NpgsqlDbType.TimestampTZ, DbType.DateTime, typeof(NpgsqlTimeStampTZ),
ExtendedBackendToNativeTypeConverter.ToTimeStampTZ);
}
else
{
if (compat <= Npgsql207)
{
// In 2.0.7 and earlier, intervals were returned as the native type.
// later versions return a CLR type and rely on provider specific api for NpgsqlInterval
yield return
new NpgsqlBackendTypeInfo(0, "interval", NpgsqlDbType.Interval, DbType.Object, typeof(NpgsqlInterval),
ExtendedBackendToNativeTypeConverter.ToInterval);
}
else
{
yield return
new NpgsqlBackendTypeInfo(0, "interval", NpgsqlDbType.Interval, DbType.Object, typeof(NpgsqlInterval),
ExtendedBackendToNativeTypeConverter.ToInterval,
typeof(TimeSpan),
interval => (TimeSpan)(NpgsqlInterval)interval,
intervalNpgsql => (intervalNpgsql is TimeSpan ? (NpgsqlInterval)(TimeSpan) intervalNpgsql : intervalNpgsql));
}
yield return
new NpgsqlBackendTypeInfo(0, "date", NpgsqlDbType.Date, DbType.Date, typeof (NpgsqlDate),
ExtendedBackendToNativeTypeConverter.ToDate,
typeof(DateTime),
date => (DateTime)(NpgsqlDate)date,
npgsqlDate => (npgsqlDate is DateTime ? (NpgsqlDate)(DateTime) npgsqlDate : npgsqlDate));
yield return
new NpgsqlBackendTypeInfo(0, "time", NpgsqlDbType.Time, DbType.Time, typeof (NpgsqlTime),
ExtendedBackendToNativeTypeConverter.ToTime,
typeof(DateTime),
time => time is DateTime ? time : (DateTime)(NpgsqlTime)time,
npgsqlTime => (npgsqlTime is TimeSpan ? (NpgsqlTime)(TimeSpan) npgsqlTime : npgsqlTime));
yield return
new NpgsqlBackendTypeInfo(0, "timetz", NpgsqlDbType.TimeTZ, DbType.Time, typeof (NpgsqlTimeTZ),
ExtendedBackendToNativeTypeConverter.ToTimeTZ,
typeof(DateTime),
timetz => (DateTime)(NpgsqlTimeTZ)timetz,
npgsqlTimetz => (npgsqlTimetz is TimeSpan ? (NpgsqlTimeTZ)(TimeSpan) npgsqlTimetz : npgsqlTimetz));
yield return
new NpgsqlBackendTypeInfo(0, "timestamp", NpgsqlDbType.Timestamp, DbType.DateTime, typeof (NpgsqlTimeStamp),
ExtendedBackendToNativeTypeConverter.ToTimeStamp,
typeof(DateTime),
timestamp => (DateTime)(NpgsqlTimeStamp)timestamp,
npgsqlTimestamp => (npgsqlTimestamp is DateTime ? (NpgsqlTimeStamp)(DateTime) npgsqlTimestamp : npgsqlTimestamp));
yield return
new NpgsqlBackendTypeInfo(0, "abstime", NpgsqlDbType.Abstime, DbType.DateTime, typeof(NpgsqlTimeStampTZ),
ExtendedBackendToNativeTypeConverter.ToTimeStampTZ,
typeof(DateTime),
timestamp => (DateTime)(NpgsqlTimeStampTZ)timestamp,
npgsqlTimestampTZ => (npgsqlTimestampTZ is DateTime ? (NpgsqlTimeStampTZ)(DateTime) npgsqlTimestampTZ : npgsqlTimestampTZ));
yield return
new NpgsqlBackendTypeInfo(0, "timestamptz", NpgsqlDbType.TimestampTZ, DbType.DateTime, typeof (NpgsqlTimeStampTZ),
ExtendedBackendToNativeTypeConverter.ToTimeStampTZ,
typeof(DateTime),
timestamptz => ((DateTime)(NpgsqlTimeStampTZ)timestamptz).ToLocalTime(),
npgsqlTimestampTZ => (npgsqlTimestampTZ is DateTime ? (NpgsqlTimeStampTZ)(DateTime)npgsqlTimestampTZ : npgsqlTimestampTZ is DateTimeOffset ? (NpgsqlTimeStampTZ)(DateTimeOffset)npgsqlTimestampTZ : npgsqlTimestampTZ));
}
}
///<summary>
/// This method creates (or retrieves from cache) a mapping between type and OID
/// of all natively supported postgresql data types.
/// This is needed as from one version to another, this mapping can be changed and
/// so we avoid hardcoding them.
/// </summary>
/// <returns>NpgsqlTypeMapping containing all known data types. The mapping must be
/// cloned before it is modified because it is cached; changes made by one connection may
/// effect another connection.
/// </returns>
public static NpgsqlBackendTypeMapping CreateAndLoadInitialTypesMapping(NpgsqlConnector conn)
{
NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "LoadTypesMapping");
MappingKey key = new MappingKey(conn);
// Check the cache for an initial types map.
NpgsqlBackendTypeMapping oidToNameMapping = null;
if(BackendTypeMappingCache.TryGetValue(key, out oidToNameMapping))
return oidToNameMapping;
// Not in cache, create a new one.
oidToNameMapping = new NpgsqlBackendTypeMapping();
// Create a list of all natively supported postgresql data types.
// Attempt to map each type info in the list to an OID on the backend and
// add each mapped type to the new type mapping object.
LoadTypesMappings(conn, oidToNameMapping, TypeInfoList(conn.UseExtendedTypes, conn.CompatVersion));
//We hold the lock for the least time possible on the least scope possible.
//We must lock on BackendTypeMappingCache because it will be updated by this operation,
//and we must not just add to it, but also check that another thread hasn't updated it
//in the meantime. Strictly just doing :
//return BackendTypeMappingCache[key] = oidToNameMapping;
//as the only call within the locked section should be safe and correct, but we'll assume
//there's some subtle problem with temporarily having two copies of the same mapping and
//ensure only one is called.
//It is of course wasteful that multiple threads could be creating mappings when only one
//will be used, but we aim for better overall concurrency at the risk of causing some
//threads the extra work.
NpgsqlBackendTypeMapping mappingCheck = null;
//First check without acquiring the lock; don't lock if we don't have to.
if(BackendTypeMappingCache.TryGetValue(key, out mappingCheck))//Another thread built the mapping in the meantime.
return mappingCheck;
lock(BackendTypeMappingCache)
{
//Final check. We have the lock now so if this fails it'll continue to fail.
if(BackendTypeMappingCache.TryGetValue(key, out mappingCheck))//Another thread built the mapping in the meantime.
return mappingCheck;
// Add this mapping to the per-server-version cache so we don't have to
// do these expensive queries on every connection startup.
BackendTypeMappingCache.Add(key, oidToNameMapping);
}
return oidToNameMapping;
}
//Take a NpgsqlBackendTypeInfo for a type and return the NpgsqlBackendTypeInfo for
//an array of that type.
private static NpgsqlBackendTypeInfo ArrayTypeInfo(NpgsqlBackendTypeInfo elementInfo)
{
ArrayBackendToNativeTypeConverter converter = new ArrayBackendToNativeTypeConverter(elementInfo);
if (elementInfo.SupportsBinaryBackendData)
{
return
new NpgsqlBackendTypeInfo(0, "_" + elementInfo.Name, NpgsqlDbType.Array | elementInfo.NpgsqlDbType, DbType.Object,
elementInfo.Type.MakeArrayType(),
converter.ArrayTextToArray,
converter.ArrayBinaryToArray);
}
else
{
return
new NpgsqlBackendTypeInfo(0, "_" + elementInfo.Name, NpgsqlDbType.Array | elementInfo.NpgsqlDbType, DbType.Object,
elementInfo.Type.MakeArrayType(),
converter.ArrayTextToArray);
}
}
/// <summary>
/// Attempt to map types by issuing a query against pg_type.
/// This function takes a list of NpgsqlTypeInfo and attempts to resolve the OID field
/// of each by querying pg_type. If the mapping is found, the type info object is
/// updated (OID) and added to the provided NpgsqlTypeMapping object.
/// </summary>
/// <param name="conn">NpgsqlConnector to send query through.</param>
/// <param name="TypeMappings">Mapping object to add types too.</param>
/// <param name="TypeInfoList">List of types that need to have OID's mapped.</param>
public static void LoadTypesMappings(NpgsqlConnector conn, NpgsqlBackendTypeMapping TypeMappings,
IEnumerable<NpgsqlBackendTypeInfo> TypeInfoList)
{
StringBuilder InList = new StringBuilder();
Dictionary<string, NpgsqlBackendTypeInfo> NameIndex = new Dictionary<string, NpgsqlBackendTypeInfo>();
// Build a clause for the SELECT statement.
// Build a name->typeinfo mapping so we can match the results of the query
// with the list of type objects efficiently.
foreach (NpgsqlBackendTypeInfo TypeInfo in TypeInfoList)
{
NameIndex.Add(TypeInfo.Name, TypeInfo);
InList.AppendFormat("{0}'{1}'", ((InList.Length > 0) ? ", " : ""), TypeInfo.Name);
//do the same for the equivalent array type.
NameIndex.Add("_" + TypeInfo.Name, ArrayTypeInfo(TypeInfo));
InList.Append(", '_").Append(TypeInfo.Name).Append('\'');
}
if (InList.Length == 0)
{
return;
}
using (
NpgsqlCommand command =
new NpgsqlCommand(string.Format("SELECT typname, oid FROM pg_type WHERE typname IN ({0})", InList), conn))
{
using (NpgsqlDataReader dr = command.GetReader(CommandBehavior.SequentialAccess | CommandBehavior.SingleResult))
{
while (dr.Read())
{
NpgsqlBackendTypeInfo TypeInfo = NameIndex[dr[0].ToString()];
TypeInfo._OID = Convert.ToInt32(dr[1]);
TypeMappings.AddType(TypeInfo);
}
}
}
}
}
}