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
1728 lines (1440 loc) · 69.8 KB
/
NpgsqlTypesHelper.cs
File metadata and controls
1728 lines (1440 loc) · 69.8 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
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// 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 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;
private static readonly ResourceManager resman = new ResourceManager(MethodBase.GetCurrentMethod().DeclaringType);
// 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("10.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());
}
// CHECKME
// Not sure what to do with this one. I don't believe we ever ask for a binary
// formatting, so this shouldn't even be used right now.
// At some point this will need to be merged into the type converter system somehow?
public static Object ConvertBackendBytesToSystemType(NpgsqlBackendTypeInfo TypeInfo, Byte[] data, Int32 fieldValueSize,
Int32 typeModifier)
{
NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "ConvertBackendBytesToStytemType");
if (TypeInfo != null)
{
return TypeInfo.ConvertToNative(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, String data, Int16 typeSize,
Int32 typeModifier)
{
NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "ConvertBackendStringToSystemType");
if (TypeInfo != null)
{
return TypeInfo.ConvertToNative(data, typeSize, typeModifier);
}
else
{
return 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, true, null);
nativeTypeMapping.AddTypeAlias("char", typeof (Char));
nativeTypeMapping.AddType("varchar", NpgsqlDbType.Varchar, DbType.String, true, null);
nativeTypeMapping.AddType("text", NpgsqlDbType.Text, DbType.String, true, null);
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, true,
new ConvertNativeToBackendTextHandler(BasicNativeToBackendTypeConverter.ByteArrayToByteaText),
new ConvertNativeToBackendBinaryHandler(BasicNativeToBackendTypeConverter.ByteArrayToByteaBinary));
nativeTypeMapping.AddTypeAlias("bytea", typeof (Byte[]));
nativeTypeMapping.AddType("bit", NpgsqlDbType.Bit, DbType.Object, false,
new ConvertNativeToBackendTextHandler(BasicNativeToBackendTypeConverter.ToBit));
nativeTypeMapping.AddTypeAlias("bit", typeof(BitString));
nativeTypeMapping.AddType("bool", NpgsqlDbType.Boolean, DbType.Boolean, false,
new ConvertNativeToBackendTextHandler(BasicNativeToBackendTypeConverter.BooleanToBooleanText),
new ConvertNativeToBackendBinaryHandler(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, true, new ConvertNativeToBackendTextHandler(BasicNativeToBackendTypeConverter.ToSingleDouble));
nativeTypeMapping.AddTypeAlias("float4", typeof (Single));
//nativeTypeMapping.AddType("float8", NpgsqlDbType.Double, DbType.Double, true, BasicNativeToBackendTypeConverter.ToBasicType<double>);
nativeTypeMapping.AddType("float8", NpgsqlDbType.Double, DbType.Double, true, new ConvertNativeToBackendTextHandler(BasicNativeToBackendTypeConverter.ToSingleDouble));
nativeTypeMapping.AddTypeAlias("float8", typeof (Double));
nativeTypeMapping.AddType("numeric", NpgsqlDbType.Numeric, DbType.Decimal, true, BasicNativeToBackendTypeConverter.ToBasicType<decimal>);
nativeTypeMapping.AddTypeAlias("numeric", typeof (Decimal));
nativeTypeMapping.AddType("money", NpgsqlDbType.Money, DbType.Currency, true,
new ConvertNativeToBackendTextHandler(BasicNativeToBackendTypeConverter.ToMoney));
nativeTypeMapping.AddType("date", NpgsqlDbType.Date, DbType.Date, true,
new ConvertNativeToBackendTextHandler(BasicNativeToBackendTypeConverter.ToDate));
nativeTypeMapping.AddTypeAlias("date", typeof (NpgsqlDate));
nativeTypeMapping.AddType("timetz", NpgsqlDbType.TimeTZ, DbType.Time, true,
new ConvertNativeToBackendTextHandler(ExtendedNativeToBackendTypeConverter.ToTimeTZ));
nativeTypeMapping.AddTypeAlias("timetz", typeof (NpgsqlTimeTZ));
nativeTypeMapping.AddType("time", NpgsqlDbType.Time, DbType.Time, true,
new ConvertNativeToBackendTextHandler(BasicNativeToBackendTypeConverter.ToTime));
nativeTypeMapping.AddTypeAlias("time", typeof (NpgsqlTime));
nativeTypeMapping.AddType("timestamptz", NpgsqlDbType.TimestampTZ, DbType.DateTime, true,
new ConvertNativeToBackendTextHandler(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,
new ConvertNativeToBackendTextHandler(ExtendedNativeToBackendTypeConverter.ToTimeStamp));
nativeTypeMapping.AddType("timestamp", NpgsqlDbType.Timestamp, DbType.DateTime, true,
new ConvertNativeToBackendTextHandler(ExtendedNativeToBackendTypeConverter.ToTimeStamp));
nativeTypeMapping.AddTypeAlias("timestamp", typeof (DateTime));
nativeTypeMapping.AddTypeAlias("timestamp", typeof (NpgsqlTimeStamp));
nativeTypeMapping.AddType("point", NpgsqlDbType.Point, DbType.Object, true,
new ConvertNativeToBackendTextHandler(ExtendedNativeToBackendTypeConverter.ToPoint));
nativeTypeMapping.AddTypeAlias("point", typeof (NpgsqlPoint));
nativeTypeMapping.AddType("box", NpgsqlDbType.Box, DbType.Object, true,
new ConvertNativeToBackendTextHandler(ExtendedNativeToBackendTypeConverter.ToBox));
nativeTypeMapping.AddTypeAlias("box", typeof (NpgsqlBox));
nativeTypeMapping.AddType("lseg", NpgsqlDbType.LSeg, DbType.Object, true,
new ConvertNativeToBackendTextHandler(ExtendedNativeToBackendTypeConverter.ToLSeg));
nativeTypeMapping.AddTypeAlias("lseg", typeof (NpgsqlLSeg));
nativeTypeMapping.AddType("path", NpgsqlDbType.Path, DbType.Object, true,
new ConvertNativeToBackendTextHandler(ExtendedNativeToBackendTypeConverter.ToPath));
nativeTypeMapping.AddTypeAlias("path", typeof (NpgsqlPath));
nativeTypeMapping.AddType("polygon", NpgsqlDbType.Polygon, DbType.Object, true,
new ConvertNativeToBackendTextHandler(ExtendedNativeToBackendTypeConverter.ToPolygon));
nativeTypeMapping.AddTypeAlias("polygon", typeof (NpgsqlPolygon));
nativeTypeMapping.AddType("circle", NpgsqlDbType.Circle, DbType.Object, true,
new ConvertNativeToBackendTextHandler(ExtendedNativeToBackendTypeConverter.ToCircle));
nativeTypeMapping.AddTypeAlias("circle", typeof (NpgsqlCircle));
nativeTypeMapping.AddType("inet", NpgsqlDbType.Inet, DbType.Object, true,
new ConvertNativeToBackendTextHandler(ExtendedNativeToBackendTypeConverter.ToIPAddress));
nativeTypeMapping.AddTypeAlias("inet", typeof (IPAddress));
nativeTypeMapping.AddTypeAlias("inet", typeof (NpgsqlInet));
nativeTypeMapping.AddType("macaddr", NpgsqlDbType.MacAddr, DbType.Object, true,
new ConvertNativeToBackendTextHandler(ExtendedNativeToBackendTypeConverter.ToMacAddress));
nativeTypeMapping.AddTypeAlias("macaddr", typeof(PhysicalAddress));
nativeTypeMapping.AddTypeAlias("macaddr", typeof(NpgsqlMacAddress));
nativeTypeMapping.AddType("uuid", NpgsqlDbType.Uuid, DbType.Guid, true, null);
nativeTypeMapping.AddTypeAlias("uuid", typeof (Guid));
nativeTypeMapping.AddType("xml", NpgsqlDbType.Xml, DbType.Xml, true, null);
nativeTypeMapping.AddType("interval", NpgsqlDbType.Interval, DbType.Object, true,
new ConvertNativeToBackendTextHandler(ExtendedNativeToBackendTypeConverter.ToInterval));
nativeTypeMapping.AddTypeAlias("interval", typeof (NpgsqlInterval));
nativeTypeMapping.AddTypeAlias("interval", typeof (TimeSpan));
nativeTypeMapping.AddDbTypeAlias("text", 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);
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);
yield return new NpgsqlBackendTypeInfo(0, "bpchar", NpgsqlDbType.Text, DbType.String, typeof (String), null);
yield return new NpgsqlBackendTypeInfo(0, "varchar", NpgsqlDbType.Varchar, DbType.String, typeof (String), null);
yield return new NpgsqlBackendTypeInfo(0, "text", NpgsqlDbType.Text, DbType.String, typeof (String), null);
yield return new NpgsqlBackendTypeInfo(0, "name", NpgsqlDbType.Name, DbType.String, typeof (String), null);
yield return
new NpgsqlBackendTypeInfo(0, "bytea", NpgsqlDbType.Bytea, DbType.Binary, typeof (Byte[]),
new ConvertBackendTextToNativeHandler(BasicBackendToNativeTypeConverter.ByteaTextToByteArray),
new ConvertBackendBinaryToNativeHandler(BasicBackendToNativeTypeConverter.ByteaBinaryToByteArray));
yield return
new NpgsqlBackendTypeInfo(0, "bit", NpgsqlDbType.Bit, DbType.Object, typeof (BitString),
new ConvertBackendTextToNativeHandler(BasicBackendToNativeTypeConverter.ToBit));
yield return
new NpgsqlBackendTypeInfo(0, "bool", NpgsqlDbType.Boolean, DbType.Boolean, typeof (Boolean),
new ConvertBackendTextToNativeHandler(BasicBackendToNativeTypeConverter.BooleanTextToBoolean),
new ConvertBackendBinaryToNativeHandler(BasicBackendToNativeTypeConverter.BooleanBinaryToBoolean));
yield return new NpgsqlBackendTypeInfo(0, "int2", NpgsqlDbType.Smallint, DbType.Int16, typeof (Int16),
null,
new ConvertBackendBinaryToNativeHandler(BasicBackendToNativeTypeConverter.IntBinaryToInt));
yield return new NpgsqlBackendTypeInfo(0, "int4", NpgsqlDbType.Integer, DbType.Int32, typeof (Int32),
null,
new ConvertBackendBinaryToNativeHandler(BasicBackendToNativeTypeConverter.IntBinaryToInt));
yield return new NpgsqlBackendTypeInfo(0, "int8", NpgsqlDbType.Bigint, DbType.Int64, typeof (Int64),
null,
new ConvertBackendBinaryToNativeHandler(BasicBackendToNativeTypeConverter.IntBinaryToInt));
yield return new NpgsqlBackendTypeInfo(0, "oid", NpgsqlDbType.Bigint, DbType.Int64, typeof (Int64), null);
yield return new NpgsqlBackendTypeInfo(0, "float4", NpgsqlDbType.Real, DbType.Single, typeof (Single), null);
yield return new NpgsqlBackendTypeInfo(0, "float8", NpgsqlDbType.Double, DbType.Double, typeof (Double), null);
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),
new ConvertBackendTextToNativeHandler(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),
new ConvertBackendTextToNativeHandler(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),
new ConvertBackendTextToNativeHandler(BasicBackendToNativeTypeConverter.ToMoney));
yield return
new NpgsqlBackendTypeInfo(0, "point", NpgsqlDbType.Point, DbType.Object, typeof (NpgsqlPoint),
new ConvertBackendTextToNativeHandler(ExtendedBackendToNativeTypeConverter.ToPoint));
yield return
new NpgsqlBackendTypeInfo(0, "lseg", NpgsqlDbType.LSeg, DbType.Object, typeof (NpgsqlLSeg),
new ConvertBackendTextToNativeHandler(ExtendedBackendToNativeTypeConverter.ToLSeg));
yield return
new NpgsqlBackendTypeInfo(0, "path", NpgsqlDbType.Path, DbType.Object, typeof (NpgsqlPath),
new ConvertBackendTextToNativeHandler(ExtendedBackendToNativeTypeConverter.ToPath));
yield return
new NpgsqlBackendTypeInfo(0, "box", NpgsqlDbType.Box, DbType.Object, typeof (NpgsqlBox),
new ConvertBackendTextToNativeHandler(ExtendedBackendToNativeTypeConverter.ToBox));
yield return
new NpgsqlBackendTypeInfo(0, "circle", NpgsqlDbType.Circle, DbType.Object, typeof (NpgsqlCircle),
new ConvertBackendTextToNativeHandler(ExtendedBackendToNativeTypeConverter.ToCircle));
yield return
new NpgsqlBackendTypeInfo(0, "polygon", NpgsqlDbType.Polygon, DbType.Object, typeof (NpgsqlPolygon),
new ConvertBackendTextToNativeHandler(ExtendedBackendToNativeTypeConverter.ToPolygon));
yield return new NpgsqlBackendTypeInfo(0, "uuid", NpgsqlDbType.Uuid, DbType.Guid, typeof (Guid), new
ConvertBackendTextToNativeHandler(ExtendedBackendToNativeTypeConverter.ToGuid));
yield return new NpgsqlBackendTypeInfo(0, "xml", NpgsqlDbType.Xml, DbType.Xml, typeof (String), null);
if (useExtendedTypes)
{
yield return
new NpgsqlBackendTypeInfo(0, "interval", NpgsqlDbType.Interval, DbType.Object, typeof(NpgsqlInterval),
new ConvertBackendTextToNativeHandler(ExtendedBackendToNativeTypeConverter.ToInterval));
yield return
new NpgsqlBackendTypeInfo(0, "date", NpgsqlDbType.Date, DbType.Date, typeof(NpgsqlDate),
new ConvertBackendTextToNativeHandler(ExtendedBackendToNativeTypeConverter.ToDate));
yield return
new NpgsqlBackendTypeInfo(0, "time", NpgsqlDbType.Time, DbType.Time, typeof(NpgsqlTime),
new ConvertBackendTextToNativeHandler(ExtendedBackendToNativeTypeConverter.ToTime));
yield return
new NpgsqlBackendTypeInfo(0, "timetz", NpgsqlDbType.TimeTZ, DbType.Time, typeof(NpgsqlTimeTZ),
new ConvertBackendTextToNativeHandler(ExtendedBackendToNativeTypeConverter.ToTimeTZ));
yield return
new NpgsqlBackendTypeInfo(0, "timestamp", NpgsqlDbType.Timestamp, DbType.DateTime, typeof(NpgsqlTimeStamp),
new ConvertBackendTextToNativeHandler(ExtendedBackendToNativeTypeConverter.ToTimeStamp));
yield return
new NpgsqlBackendTypeInfo(0, "abstime", NpgsqlDbType.Abstime , DbType.DateTime, typeof(NpgsqlTimeStampTZ),
new ConvertBackendTextToNativeHandler(ExtendedBackendToNativeTypeConverter.ToTimeStampTZ));
yield return
new NpgsqlBackendTypeInfo(0, "timestamptz", NpgsqlDbType.TimestampTZ, DbType.DateTime, typeof(NpgsqlTimeStampTZ),
new ConvertBackendTextToNativeHandler(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),
new ConvertBackendTextToNativeHandler(ExtendedBackendToNativeTypeConverter.ToInterval));
}
else
{
yield return
new NpgsqlBackendTypeInfo(0, "interval", NpgsqlDbType.Interval, DbType.Object, typeof(NpgsqlInterval),
new ConvertBackendTextToNativeHandler(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),
new ConvertBackendTextToNativeHandler(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),
new ConvertBackendTextToNativeHandler(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),
new ConvertBackendTextToNativeHandler(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),
new ConvertBackendTextToNativeHandler(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),
new ConvertBackendTextToNativeHandler(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),
new ConvertBackendTextToNativeHandler(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)
{
return
new NpgsqlBackendTypeInfo(0, "_" + elementInfo.Name, NpgsqlDbType.Array | elementInfo.NpgsqlDbType, DbType.Object,
elementInfo.Type.MakeArrayType(),
new ConvertBackendTextToNativeHandler(
new ArrayBackendToNativeTypeConverter(elementInfo).ToArray));
}
/// <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);
}
}
}
}
}
/// <summary>
/// Delegate called to convert the given backend text data to its native representation.
/// </summary>
internal delegate Object ConvertBackendTextToNativeHandler(
NpgsqlBackendTypeInfo TypeInfo, String 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>
/// Delegate called to convert the given native data to its backand representation.
/// </summary>
internal delegate String ConvertNativeToBackendTextHandler(NpgsqlNativeTypeInfo TypeInfo, Object NativeData, Boolean forExtendedQuery, NativeToBackendTypeConverterOptions options);
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 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; }
}
/// <summary>
/// System type to convert fields of this type to.
/// </summary>
public Type FrameworkType
{
get { 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 ConvertToNative(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 ConvertToNative(string BackendData, Int16 TypeSize, Int32 TypeModifier)
{
if (_ConvertBackendTextToNative != null)
{
return _ConvertBackendTextToNative(this, BackendData, TypeSize, TypeModifier);
}
else
{
try
{
return Convert.ChangeType(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;
}
}
/// <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;