forked from npgsql/npgsql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNpgsqlTypeConverters.cs
More file actions
776 lines (684 loc) · 27.5 KB
/
NpgsqlTypeConverters.cs
File metadata and controls
776 lines (684 loc) · 27.5 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
// NpgsqlTypes.NpgsqlTypesHelper.cs
//
// Author:
// Glen Parker <glenebob@nwlink.com>
//
// Copyright (C) 2004 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.
// This file provides data type converters between PostgreSQL representations
// and .NET objects.
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
namespace NpgsqlTypes
{
/// <summary>
/// Provide event handlers to convert all native supported basic data types from their backend
/// text representation to a .NET object.
/// </summary>
internal abstract class BasicBackendToNativeTypeConverter
{
private static readonly String[] DateFormats = new String[] { "yyyy-MM-dd", };
private static readonly Regex EXCLUDE_DIGITS = new Regex("[^0-9\\-]", RegexOptions.Compiled | RegexOptions.CultureInvariant);
private static readonly String[] TimeFormats =
new String[]
{
"HH:mm:ss.ffffff", "HH:mm:ss", "HH:mm:ss.ffffffzz", "HH:mm:sszz", "HH:mm:ss.fffff", "HH:mm:ss.ffff", "HH:mm:ss.fff"
, "HH:mm:ss.ff", "HH:mm:ss.f", "HH:mm:ss.fffffzz", "HH:mm:ss.ffffzz", "HH:mm:ss.fffzz", "HH:mm:ss.ffzz",
"HH:mm:ss.fzz",
"HH:mm:ss.fffffzzz", "HH:mm:ss.ffffzzz", "HH:mm:ss.fffzzz", "HH:mm:ss.ffzzz",
"HH:mm:ss.fzzz", "HH:mm:sszzz",
};
private static readonly String[] DateTimeFormats =
new String[]
{
"yyyy-MM-dd HH:mm:ss.ffffff", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm:ss.ffffffzz", "yyyy-MM-dd HH:mm:sszz",
"yyyy-MM-dd HH:mm:ss.fffff", "yyyy-MM-dd HH:mm:ss.ffff", "yyyy-MM-dd HH:mm:ss.fff", "yyyy-MM-dd HH:mm:ss.ff",
"yyyy-MM-dd HH:mm:ss.f", "yyyy-MM-dd HH:mm:ss.fffffzz", "yyyy-MM-dd HH:mm:ss.ffffzz", "yyyy-MM-dd HH:mm:ss.fffzz",
"yyyy-MM-dd HH:mm:ss.ffzz", "yyyy-MM-dd HH:mm:ss.fzz",
"yyyy-MM-dd HH:mm:ss.fffffzzz", "yyyy-MM-dd HH:mm:ss.ffffzzz", "yyyy-MM-dd HH:mm:ss.fffzzz",
"yyyy-MM-dd HH:mm:ss.ffzzz", "yyyy-MM-dd HH:mm:ss.fzzz", "yyyy-MM-dd HH:mm:sszzz"
};
/// <summary>
/// Binary data.
/// </summary>
internal static Object ToBinary(NpgsqlBackendTypeInfo TypeInfo, String BackendData, Int16 TypeSize, Int32 TypeModifier)
{
Int32 octalValue = 0;
Int32 byteAPosition = 0;
Int32 byteAStringLength = BackendData.Length;
MemoryStream ms = new MemoryStream();
if (BackendData.StartsWith("\\x"))
{
// PostgreSQL 8.5+'s bytea_output=hex format
for (byteAPosition = 2; byteAPosition < byteAStringLength; byteAPosition += 2)
{
byte value = Convert.ToByte(BackendData.Substring(byteAPosition, 2), 16);
ms.WriteByte(value);
}
}
else
{
while (byteAPosition < byteAStringLength)
{
// The IsDigit is necessary in case we receive a \ as the octal value and not
// as the indicator of a following octal value in decimal format.
// i.e.: \201\301P\A
if (BackendData[byteAPosition] == '\\')
{
if (byteAPosition + 1 == byteAStringLength)
{
octalValue = '\\';
byteAPosition++;
}
else if (Char.IsDigit(BackendData[byteAPosition + 1]))
{
octalValue = Convert.ToByte(BackendData.Substring(byteAPosition + 1, 3), 8);
//octalValue = (Byte.Parse(BackendData[byteAPosition + 1].ToString()) << 6);
//octalValue |= (Byte.Parse(BackendData[byteAPosition + 2].ToString()) << 3);
//octalValue |= Byte.Parse(BackendData[byteAPosition + 3].ToString());
byteAPosition += 4;
}
else
{
octalValue = '\\';
byteAPosition += 2;
}
}
else
{
octalValue = (Byte)BackendData[byteAPosition];
byteAPosition++;
}
ms.WriteByte((Byte)octalValue);
}
}
return ms.ToArray();
}
/// <summary>
/// Convert a postgresql boolean to a System.Boolean.
/// </summary>
internal static Object ToBoolean(NpgsqlBackendTypeInfo TypeInfo, String BackendData, Int16 TypeSize,
Int32 TypeModifier)
{
return (BackendData.ToLower() == "t" ? true : false);
}
/// <summary>
/// Convert a postgresql bit to a System.Boolean.
/// </summary>
internal static Object ToBit(NpgsqlBackendTypeInfo TypeInfo, String BackendData, Int16 TypeSize, Int32 TypeModifier)
{
/// <summary>
/// Current tests seem to expect single-bit bitstrings to behave as boolean (why?)
///
/// To ensure compatibility we return a bool if the bitstring is single-length.
/// Maybe we don't need to do this (why do we?) or maybe people used to some other,
/// but taking a conservative approach here.
///
/// It means that IDataReader.GetValue() can't be used safely for bitstrings that
/// may be single-bit, but NpgsqlDataReader.GetBitString() can deal with the conversion
/// below by reversing it, so if GetBitString() is used, no harm is done.
BitString bs = BitString.Parse(BackendData);
return bs.Length == 1 ? (object)bs[0] : bs;
}
/// <summary>
/// Convert a postgresql datetime to a System.DateTime.
/// </summary>
internal static Object ToDateTime(NpgsqlBackendTypeInfo TypeInfo, String BackendData, Int16 TypeSize,
Int32 TypeModifier)
{
// Get the date time parsed in all expected formats for timestamp.
// First check for special values infinity and -infinity.
if (BackendData == "infinity")
{
return DateTime.MaxValue;
}
if (BackendData == "-infinity")
{
return DateTime.MinValue;
}
return
DateTime.ParseExact(BackendData, DateTimeFormats, DateTimeFormatInfo.InvariantInfo,
DateTimeStyles.NoCurrentDateDefault | DateTimeStyles.AllowWhiteSpaces);
}
/// <summary>
/// Convert a postgresql date to a System.DateTime.
/// </summary>
internal static Object ToDate(NpgsqlBackendTypeInfo TypeInfo, String BackendData, Int16 TypeSize, Int32 TypeModifier)
{
// First check for special values infinity and -infinity.
if (BackendData == "infinity")
{
return DateTime.MaxValue;
}
if (BackendData == "-infinity")
{
return DateTime.MinValue;
}
return
DateTime.ParseExact(BackendData, DateFormats, DateTimeFormatInfo.InvariantInfo, DateTimeStyles.AllowWhiteSpaces);
}
/// <summary>
/// Convert a postgresql time to a System.DateTime.
/// </summary>
internal static Object ToTime(NpgsqlBackendTypeInfo TypeInfo, String BackendData, Int16 TypeSize, Int32 TypeModifier)
{
return
DateTime.ParseExact(BackendData, TimeFormats, DateTimeFormatInfo.InvariantInfo,
DateTimeStyles.NoCurrentDateDefault | DateTimeStyles.AllowWhiteSpaces);
}
/// <summary>
/// Convert a postgresql money to a System.Decimal.
/// </summary>
internal static Object ToMoney(NpgsqlBackendTypeInfo TypeInfo, String BackendData, Int16 TypeSize, Int32 TypeModifier)
{
// Will vary according to currency symbol, position of symbol and decimal and thousands separators, but will
// alwasy be two-decimal precision number using Arabic (0-9) digits, so we can extract just those digits and
// divide by 100.
return Convert.ToDecimal(EXCLUDE_DIGITS.Replace(BackendData, string.Empty), CultureInfo.InvariantCulture) / 100m;
}
}
/// <summary>
/// Provide event handlers to convert the basic native supported data types from
/// native form to backend representation.
/// </summary>
internal abstract class BasicNativeToBackendTypeConverter
{
/// <summary>
/// Binary data.
/// </summary>
internal static String ToBinary(NpgsqlNativeTypeInfo TypeInfo, Object NativeData, Boolean ForExtendedQuery)
{
Byte[] byteArray = (Byte[])NativeData;
StringBuilder res = new StringBuilder(byteArray.Length * 5);
foreach(byte b in byteArray)
if(b >= 0x20 && b < 0x7F && b != 0x27 && b != 0x5C)
res.Append((char)b);
else
res.Append("\\\\")
.Append((char)('0' + (7 & (b >> 6))))
.Append((char)('0' + (7 & (b >> 3))))
.Append((char)('0' + (7 & b)));
return res.ToString();
}
/// <summary>
/// Convert to a postgresql boolean.
/// </summary>
internal static String ToBoolean(NpgsqlNativeTypeInfo TypeInfo, Object NativeData, Boolean ForExtendedQuery)
{
return ((bool)NativeData) ? "TRUE" : "FALSE";
}
/// <summary>
/// Convert to a postgresql bit.
/// </summary>
internal static String ToBit(NpgsqlNativeTypeInfo TypeInfo, Object NativeData, Boolean ForExtendedQuery)
{
if(NativeData is bool)
return ((bool)NativeData) ? "1" : "0";
// It may seem more sensible to just convert an integer to a BitString here and pass it on.
// However behaviour varies in terms of how this is interpretted if being passed to a bitstring
// value smaller than the int.
// Prior to Postgres 8.0, the behaviour would be the same either way. E.g. if 10 were passed to
// a bit(1) then the bits (1010) would be extracted from the left, so resulting in the bitstring B'1'.
// From 8.0 onwards though, if we cast 10 straight to a bit(1) then the right-most bit is taken,
// resulting in B'0'. If we cast it to the "natural" bitstring for it's size first (which is what would
// happen if we did that work here) then it would become B'1010' which would then be cast to bit(1) by
// taking the left-most bit resulting in B'1' (the behaviour one would expect from Postgres 7.x).
//
// Since we don't know what implicit casts (say by inserting into a table with a bitstring field of
// set size) may happen, we don't know how to ensure expected behaviour. While passing a bitstring
// literal would work as expected with Postgres before 8.0, it can fail with 8.0 and later.
else if(NativeData is int)
return NativeData.ToString();
else
return ((BitString)NativeData).ToString("E");
}
/// <summary>
/// Convert to a postgresql timestamp.
/// </summary>
internal static String ToDateTime(NpgsqlNativeTypeInfo TypeInfo, Object NativeData, Boolean ForExtendedQuery)
{
if (!(NativeData is DateTime))
{
return ExtendedNativeToBackendTypeConverter.ToTimeStamp(TypeInfo, NativeData, ForExtendedQuery);
}
if (DateTime.MaxValue.Equals(NativeData))
{
return "infinity";
}
if (DateTime.MinValue.Equals(NativeData))
{
return "-infinity";
}
return ((DateTime)NativeData).ToString("yyyy-MM-dd HH:mm:ss.ffffff", DateTimeFormatInfo.InvariantInfo);
}
/// <summary>
/// Convert to a postgresql date.
/// </summary>
internal static String ToDate(NpgsqlNativeTypeInfo TypeInfo, Object NativeData, Boolean ForExtendedQuery)
{
if (!(NativeData is DateTime))
{
return ExtendedNativeToBackendTypeConverter.ToDate(TypeInfo, NativeData, ForExtendedQuery);
}
return ((DateTime)NativeData).ToString("yyyy-MM-dd", DateTimeFormatInfo.InvariantInfo);
}
/// <summary>
/// Convert to a postgresql time.
/// </summary>
internal static String ToTime(NpgsqlNativeTypeInfo TypeInfo, Object NativeData, Boolean ForExtendedQuery)
{
if (!(NativeData is DateTime))
{
return ExtendedNativeToBackendTypeConverter.ToTime(TypeInfo, NativeData, ForExtendedQuery);
}
else
{
return ((DateTime)NativeData).ToString("HH:mm:ss.ffffff", DateTimeFormatInfo.InvariantInfo);
}
}
/// <summary>
/// Convert to a postgres money.
/// </summary>
internal static String ToMoney(NpgsqlNativeTypeInfo TypeInfo, Object NativeData, Boolean ForExtendedQuery)
{
//Formats accepted vary according to locale, but it always accepts a plain number (no currency or
//grouping symbols) passed as a string (with the appropriate cast appended, as UseCast will cause
//to happen.
return ((IFormattable)NativeData).ToString(null, CultureInfo.InvariantCulture.NumberFormat);
}
/// <summary>
/// Convert to a postgres double with maximum precision.
/// </summary>
internal static String ToSingleDouble(NpgsqlNativeTypeInfo TypeInfo, Object NativeData, Boolean ForExtendedQuery)
{
//Formats accepted vary according to locale, but it always accepts a plain number (no currency or
//grouping symbols) passed as a string (with the appropriate cast appended, as UseCast will cause
//to happen.
return ((IFormattable)NativeData).ToString("R", CultureInfo.InvariantCulture.NumberFormat);
}
internal static string ToBasicType<T>(NpgsqlNativeTypeInfo TypeInfo, object NativeData, Boolean ForExtendedQuery)
{
// This double cast is needed in order to get the enum type handled correctly (IConvertible)
// and the decimal separator always as "." regardless of culture (IFormattable)
return (((IFormattable)((IConvertible)NativeData).ToType(typeof(T), null)).ToString(null, CultureInfo.InvariantCulture.NumberFormat));
}
}
/// <summary>
/// Provide event handlers to convert extended native supported data types from their backend
/// text representation to a .NET object.
/// </summary>
internal abstract class ExtendedBackendToNativeTypeConverter
{
private static readonly Regex pointRegex = new Regex(@"\((-?\d+.?\d*),(-?\d+.?\d*)\)");
private static readonly Regex boxlsegRegex = new Regex(@"\((-?\d+.?\d*),(-?\d+.?\d*)\),\((-?\d+.?\d*),(-?\d+.?\d*)\)");
private static readonly Regex pathpolygonRegex = new Regex(@"\((-?\d+.?\d*),(-?\d+.?\d*)\)");
private static readonly Regex circleRegex = new Regex(@"<\((-?\d+.?\d*),(-?\d+.?\d*)\),(\d+.?\d*)>");
/// <summary>
/// Convert a postgresql point to a System.NpgsqlPoint.
/// </summary>
internal static Object ToPoint(NpgsqlBackendTypeInfo TypeInfo, String BackendData, Int16 TypeSize, Int32 TypeModifier)
{
Match m = pointRegex.Match(BackendData);
return
new NpgsqlPoint(Single.Parse(m.Groups[1].ToString(), NumberStyles.Any, CultureInfo.InvariantCulture.NumberFormat),
Single.Parse(m.Groups[2].ToString(), NumberStyles.Any, CultureInfo.InvariantCulture.NumberFormat));
}
/// <summary>
/// Convert a postgresql point to a System.RectangleF.
/// </summary>
internal static Object ToBox(NpgsqlBackendTypeInfo TypeInfo, String BackendData, Int16 TypeSize, Int32 TypeModifier)
{
Match m = boxlsegRegex.Match(BackendData);
return
new NpgsqlBox(
new NpgsqlPoint(Single.Parse(m.Groups[1].ToString(), NumberStyles.Any, CultureInfo.InvariantCulture.NumberFormat),
Single.Parse(m.Groups[2].ToString(), NumberStyles.Any, CultureInfo.InvariantCulture.NumberFormat)),
new NpgsqlPoint(Single.Parse(m.Groups[3].ToString(), NumberStyles.Any, CultureInfo.InvariantCulture.NumberFormat),
Single.Parse(m.Groups[4].ToString(), NumberStyles.Any, CultureInfo.InvariantCulture.NumberFormat)));
}
/// <summary>
/// LDeg.
/// </summary>
internal static Object ToLSeg(NpgsqlBackendTypeInfo TypeInfo, String BackendData, Int16 TypeSize, Int32 TypeModifier)
{
Match m = boxlsegRegex.Match(BackendData);
return
new NpgsqlLSeg(
new NpgsqlPoint(Single.Parse(m.Groups[1].ToString(), NumberStyles.Any, CultureInfo.InvariantCulture.NumberFormat),
Single.Parse(m.Groups[2].ToString(), NumberStyles.Any, CultureInfo.InvariantCulture.NumberFormat)),
new NpgsqlPoint(Single.Parse(m.Groups[3].ToString(), NumberStyles.Any, CultureInfo.InvariantCulture.NumberFormat),
Single.Parse(m.Groups[4].ToString(), NumberStyles.Any, CultureInfo.InvariantCulture.NumberFormat)));
}
/// <summary>
/// Path.
/// </summary>
internal static Object ToPath(NpgsqlBackendTypeInfo TypeInfo, String BackendData, Int16 TypeSize, Int32 TypeModifier)
{
Match m = pathpolygonRegex.Match(BackendData);
Boolean open = (BackendData[0] == '[');
List<NpgsqlPoint> points = new List<NpgsqlPoint>();
while (m.Success)
{
if (open)
{
points.Add(
new NpgsqlPoint(
Single.Parse(m.Groups[1].ToString(), NumberStyles.Any, CultureInfo.InvariantCulture.NumberFormat),
Single.Parse(m.Groups[2].ToString(), NumberStyles.Any, CultureInfo.InvariantCulture.NumberFormat)));
}
else
{
// Here we have to do a little hack, because as of 2004-08-11 mono cvs version, the last group is returned with
// a trailling ')' only when the last character of the string is a ')' which is the case for closed paths
// returned by backend. This gives parsing exception when converting to single.
// I still don't know if this is a bug in mono or in my regular expression.
// Check if there is this character and remove it.
String group2 = m.Groups[2].ToString();
if (group2.EndsWith(")"))
{
group2 = group2.Remove(group2.Length - 1, 1);
}
points.Add(
new NpgsqlPoint(
Single.Parse(m.Groups[1].ToString(), NumberStyles.Any, CultureInfo.InvariantCulture.NumberFormat),
Single.Parse(group2, NumberStyles.Any, CultureInfo.InvariantCulture.NumberFormat)));
}
m = m.NextMatch();
}
NpgsqlPath result = new NpgsqlPath(points.ToArray());
result.Open = open;
return result;
}
/// <summary>
/// Polygon.
/// </summary>
internal static Object ToPolygon(NpgsqlBackendTypeInfo TypeInfo, String BackendData, Int16 TypeSize,
Int32 TypeModifier)
{
Match m = pathpolygonRegex.Match(BackendData);
List<NpgsqlPoint> points = new List<NpgsqlPoint>();
while (m.Success)
{
// Here we have to do a little hack, because as of 2004-08-11 mono cvs version, the last group is returned with
// a trailling ')' only when the last character of the string is a ')' which is the case for closed paths
// returned by backend. This gives parsing exception when converting to single.
// I still don't know if this is a bug in mono or in my regular expression.
// Check if there is this character and remove it.
String group2 = m.Groups[2].ToString();
if (group2.EndsWith(")"))
{
group2 = group2.Remove(group2.Length - 1, 1);
}
points.Add(
new NpgsqlPoint(Single.Parse(m.Groups[1].ToString(), NumberStyles.Any, CultureInfo.InvariantCulture.NumberFormat),
Single.Parse(group2, NumberStyles.Any, CultureInfo.InvariantCulture.NumberFormat)));
m = m.NextMatch();
}
return new NpgsqlPolygon(points);
}
/// <summary>
/// Circle.
/// </summary>
internal static Object ToCircle(NpgsqlBackendTypeInfo TypeInfo, String BackendData, Int16 TypeSize, Int32 TypeModifier)
{
Match m = circleRegex.Match(BackendData);
return
new NpgsqlCircle(
new NpgsqlPoint(Single.Parse(m.Groups[1].ToString(), NumberStyles.Any, CultureInfo.InvariantCulture.NumberFormat),
Single.Parse(m.Groups[2].ToString(), NumberStyles.Any, CultureInfo.InvariantCulture.NumberFormat)),
Single.Parse(m.Groups[3].ToString(), NumberStyles.Any, CultureInfo.InvariantCulture.NumberFormat));
}
/// <summary>
/// Inet.
/// </summary>
internal static Object ToInet(NpgsqlBackendTypeInfo TypeInfo, String BackendData, Int16 TypeSize, Int32 TypeModifier)
{
return new NpgsqlInet(BackendData);
}
/// <summary>
/// MAC Address.
/// </summary>
internal static Object ToMacAddress(NpgsqlBackendTypeInfo TypeInfo, String BackendData, Int16 TypeSize, Int32 TypeModifier)
{
return new NpgsqlMacAddress(BackendData);
}
internal static Object ToGuid(NpgsqlBackendTypeInfo TypeInfo, String BackendData, Int16 TypeSize, Int32 TypeModifier)
{
return new Guid(BackendData);
}
/// <summary>
/// interval
/// </summary>
internal static object ToInterval(NpgsqlBackendTypeInfo typeInfo, String backendData, Int16 typeSize,
Int32 typeModifier)
{
return NpgsqlInterval.Parse(backendData);
}
internal static object ToTime(NpgsqlBackendTypeInfo typeInfo, String backendData, Int16 typeSize, Int32 typeModifier)
{
return NpgsqlTime.Parse(backendData);
}
internal static object ToTimeTZ(NpgsqlBackendTypeInfo typeInfo, String backendData, Int16 typeSize, Int32 typeModifier)
{
return NpgsqlTimeTZ.Parse(backendData);
}
internal static object ToDate(NpgsqlBackendTypeInfo typeInfo, String backendData, Int16 typeSize, Int32 typeModifier)
{
return NpgsqlDate.Parse(backendData);
}
internal static object ToTimeStamp(NpgsqlBackendTypeInfo typeInfo, String backendData, Int16 typeSize,
Int32 typeModifier)
{
return NpgsqlTimeStamp.Parse(backendData);
}
internal static object ToTimeStampTZ(NpgsqlBackendTypeInfo typeInfo, String backendData, Int16 typeSize,
Int32 typeModifier)
{
return NpgsqlTimeStampTZ.Parse(backendData);
}
}
/// <summary>
/// Provide event handlers to convert extended native supported data types from
/// native form to backend representation.
/// </summary>
internal abstract class ExtendedNativeToBackendTypeConverter
{
/// <summary>
/// Point.
/// </summary>
internal static String ToPoint(NpgsqlNativeTypeInfo TypeInfo, Object NativeData, Boolean ForExtendedQuery)
{
if (NativeData is NpgsqlPoint)
{
NpgsqlPoint P = (NpgsqlPoint)NativeData;
return String.Format(CultureInfo.InvariantCulture, "({0},{1})", P.X, P.Y);
}
else
{
throw new InvalidCastException("Unable to cast data to NpgsqlPoint type");
}
}
/// <summary>
/// Box.
/// </summary>
internal static String ToBox(NpgsqlNativeTypeInfo TypeInfo, Object NativeData, Boolean ForExtendedQuery)
{
/*if (NativeData.GetType() == typeof(Rectangle)) {
Rectangle R = (Rectangle)NativeData;
return String.Format(CultureInfo.InvariantCulture, "({0},{1}),({2},{3})", R.Left, R.Top, R.Left + R.Width, R.Top + R.Height);
} else if (NativeData.GetType() == typeof(RectangleF)) {
RectangleF R = (RectangleF)NativeData;
return String.Format(CultureInfo.InvariantCulture, "({0},{1}),({2},{3})", R.Left, R.Top, R.Left + R.Width, R.Top + R.Height);*/
if (NativeData is NpgsqlBox)
{
NpgsqlBox box = (NpgsqlBox)NativeData;
return
String.Format(CultureInfo.InvariantCulture, "({0},{1}),({2},{3})", box.LowerLeft.X, box.LowerLeft.Y,
box.UpperRight.X, box.UpperRight.Y);
}
else
{
throw new InvalidCastException("Unable to cast data to Rectangle type");
}
}
/// <summary>
/// LSeg.
/// </summary>
internal static String ToLSeg(NpgsqlNativeTypeInfo TypeInfo, Object NativeData, Boolean ForExtendedQuery)
{
NpgsqlLSeg S = (NpgsqlLSeg)NativeData;
return String.Format(CultureInfo.InvariantCulture, "{0},{1},{2},{3}", S.Start.X, S.Start.Y, S.End.X, S.End.Y);
}
/// <summary>
/// Open path.
/// </summary>
internal static String ToPath(NpgsqlNativeTypeInfo TypeInfo, Object NativeData, Boolean ForExtendedQuery)
{
StringBuilder B = null;
try
{
B =new StringBuilder();
foreach (NpgsqlPoint P in ((NpgsqlPath)NativeData))
{
B.AppendFormat(CultureInfo.InvariantCulture, "{0}({1},{2})", (B.Length > 0 ? "," : ""), P.X, P.Y);
}
return String.Format("[{0}]", B);
}
finally
{
B = null;
}
}
/// <summary>
/// Polygon.
/// </summary>
internal static String ToPolygon(NpgsqlNativeTypeInfo TypeInfo, Object NativeData, Boolean ForExtendedQuery)
{
StringBuilder B = new StringBuilder();
foreach (NpgsqlPoint P in ((NpgsqlPolygon)NativeData))
{
B.AppendFormat(CultureInfo.InvariantCulture, "{0}({1},{2})", (B.Length > 0 ? "," : ""), P.X, P.Y);
}
return String.Format("({0})", B);
}
/// <summary>
/// Convert to a postgres MAC Address.
/// </summary>
internal static String ToMacAddress(NpgsqlNativeTypeInfo TypeInfo, Object NativeData, Boolean ForExtendedQuery)
{
if (NativeData is NpgsqlMacAddress)
{
return ((NpgsqlMacAddress)NativeData).ToString();
}
return NativeData.ToString();
}
/// <summary>
/// Circle.
/// </summary>
internal static String ToCircle(NpgsqlNativeTypeInfo TypeInfo, Object NativeData, Boolean ForExtendedQuery)
{
NpgsqlCircle C = (NpgsqlCircle)NativeData;
return String.Format(CultureInfo.InvariantCulture, "{0},{1},{2}", C.Center.X, C.Center.Y, C.Radius);
}
/// <summary>
/// Convert to a postgres inet.
/// </summary>
internal static String ToIPAddress(NpgsqlNativeTypeInfo TypeInfo, Object NativeData, Boolean ForExtendedQuery)
{
if (NativeData is NpgsqlInet)
{
return ((NpgsqlInet)NativeData).ToString();
}
return NativeData.ToString();
}
/// <summary>
/// Convert to a postgres interval
/// </summary>
internal static String ToInterval(NpgsqlNativeTypeInfo TypeInfo, Object NativeData, Boolean ForExtendedQuery)
{
return
((NativeData is TimeSpan)
? ((NpgsqlInterval)(TimeSpan)NativeData).ToString()
: ((NpgsqlInterval)NativeData).ToString());
}
internal static string ToTime(NpgsqlNativeTypeInfo typeInfo, object nativeData, Boolean ForExtendedQuery)
{
if (nativeData is DateTime)
{
return BasicNativeToBackendTypeConverter.ToTime(typeInfo, nativeData, ForExtendedQuery);
}
NpgsqlTime time;
if (nativeData is TimeSpan)
{
time = (NpgsqlTime)(TimeSpan)nativeData;
}
else
{
time = (NpgsqlTime)nativeData;
}
return time.ToString();
}
internal static string ToTimeTZ(NpgsqlNativeTypeInfo typeInfo, object nativeData, Boolean ForExtendedQuery)
{
if (nativeData is DateTime)
{
return BasicNativeToBackendTypeConverter.ToTime(typeInfo, nativeData, ForExtendedQuery);
}
NpgsqlTimeTZ time;
if (nativeData is TimeSpan)
{
time = (NpgsqlTimeTZ)(TimeSpan)nativeData;
}
else
{
time = (NpgsqlTimeTZ)nativeData;
}
return time.ToString();
}
internal static string ToDate(NpgsqlNativeTypeInfo typeInfo, object nativeData, Boolean ForExtendedQuery)
{
if (nativeData is DateTime)
{
return BasicNativeToBackendTypeConverter.ToDate(typeInfo, nativeData, ForExtendedQuery);
}
else
{
return nativeData.ToString();
}
}
internal static string ToTimeStamp(NpgsqlNativeTypeInfo typeInfo, object nativeData, Boolean ForExtendedQuery)
{
if (nativeData is DateTime)
{
return BasicNativeToBackendTypeConverter.ToDateTime(typeInfo, nativeData, ForExtendedQuery);
}
else
{
return nativeData.ToString();
}
}
}
}