forked from npgsql/npgsql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNpgsqlTypes.cs
More file actions
772 lines (644 loc) · 19.9 KB
/
NpgsqlTypes.cs
File metadata and controls
772 lines (644 loc) · 19.9 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
// 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 implementations of PostgreSQL specific data types that cannot
// be mapped to standard .NET classes.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Net;
using System.Net.NetworkInformation;
using Npgsql;
namespace NpgsqlTypes
{
/// <summary>
/// Represents a PostgreSQL Point type
/// </summary>
public struct NpgsqlPoint : IEquatable<NpgsqlPoint>
{
private Single _x;
private Single _y;
public NpgsqlPoint(Single x, Single y)
{
_x = x;
_y = y;
}
public Single X
{
get
{
return _x;
}
set
{
_x = value;
}
}
public Single Y
{
get
{
return _y;
}
set
{
_y = value;
}
}
public bool Equals(NpgsqlPoint other)
{
return X == other.X && Y == other.Y;
}
public override bool Equals(object obj)
{
return obj != null && obj is NpgsqlPoint && Equals((NpgsqlPoint) obj);
}
public static bool operator ==(NpgsqlPoint x, NpgsqlPoint y)
{
return x.Equals(y);
}
public static bool operator !=(NpgsqlPoint x, NpgsqlPoint y)
{
return !(x == y);
}
public override int GetHashCode()
{
return X.GetHashCode() ^ PGUtil.RotateShift(Y.GetHashCode(), sizeof (int)/2);
}
}
public struct NpgsqlBox : IEquatable<NpgsqlBox>
{
private NpgsqlPoint _upperRight;
private NpgsqlPoint _lowerLeft;
public NpgsqlBox(NpgsqlPoint upperRight, NpgsqlPoint lowerLeft)
{
_upperRight = upperRight;
_lowerLeft = lowerLeft;
}
public NpgsqlBox(float Top, float Right, float Bottom, float Left)
: this(new NpgsqlPoint(Right, Top), new NpgsqlPoint(Left, Bottom))
{
}
public NpgsqlPoint UpperRight
{
get
{
return _upperRight;
}
set
{
_upperRight = value;
}
}
public NpgsqlPoint LowerLeft
{
get
{
return _lowerLeft;
}
set
{
_lowerLeft = value;
}
}
public float Left
{
get { return LowerLeft.X; }
}
public float Right
{
get { return UpperRight.X; }
}
public float Bottom
{
get { return LowerLeft.Y; }
}
public float Top
{
get { return UpperRight.Y; }
}
public float Width
{
get { return Right - Left; }
}
public float Height
{
get { return Top - Bottom; }
}
public bool IsEmpty
{
get { return Width == 0 || Height == 0; }
}
public bool Equals(NpgsqlBox other)
{
return UpperRight == other.UpperRight && LowerLeft == other.LowerLeft;
}
public override bool Equals(object obj)
{
return obj != null && obj is NpgsqlBox && Equals((NpgsqlBox) obj);
}
public static bool operator ==(NpgsqlBox x, NpgsqlBox y)
{
return x.Equals(y);
}
public static bool operator !=(NpgsqlBox x, NpgsqlBox y)
{
return !(x == y);
}
public override int GetHashCode()
{
return
Top.GetHashCode() ^ PGUtil.RotateShift(Right.GetHashCode(), sizeof (int)/4) ^
PGUtil.RotateShift(Bottom.GetHashCode(), sizeof (int)/2) ^
PGUtil.RotateShift(LowerLeft.GetHashCode(), sizeof (int)*3/4);
}
}
/// <summary>
/// Represents a PostgreSQL Line Segment type.
/// </summary>
public struct NpgsqlLSeg : IEquatable<NpgsqlLSeg>
{
public NpgsqlPoint Start;
public NpgsqlPoint End;
public NpgsqlLSeg(NpgsqlPoint start, NpgsqlPoint end)
{
Start = start;
End = end;
}
public override String ToString()
{
return String.Format("({0}, {1})", Start, End);
}
public override int GetHashCode()
{
return
Start.X.GetHashCode() ^ PGUtil.RotateShift(Start.Y.GetHashCode(), sizeof (int)/4) ^
PGUtil.RotateShift(End.X.GetHashCode(), sizeof (int)/2) ^ PGUtil.RotateShift(End.Y.GetHashCode(), sizeof (int)*3/4);
}
public bool Equals(NpgsqlLSeg other)
{
return Start == other.Start && End == other.End;
}
public override bool Equals(object obj)
{
return obj != null && obj is NpgsqlLSeg && Equals((NpgsqlLSeg) obj);
}
public static bool operator ==(NpgsqlLSeg x, NpgsqlLSeg y)
{
return x.Equals(y);
}
public static bool operator !=(NpgsqlLSeg x, NpgsqlLSeg y)
{
return !(x == y);
}
}
/// <summary>
/// Represents a PostgreSQL Path type.
/// </summary>
public struct NpgsqlPath : IList<NpgsqlPoint>, IEquatable<NpgsqlPath>
{
private bool _open;
private readonly List<NpgsqlPoint> _points;
public NpgsqlPath(IEnumerable<NpgsqlPoint> points, bool open)
{
_points = new List<NpgsqlPoint>(points);
_open = open;
}
public NpgsqlPath(IEnumerable<NpgsqlPoint> points)
: this(points, false)
{
}
public NpgsqlPath(NpgsqlPoint[] points) : this((IEnumerable<NpgsqlPoint>)points, false)
{
}
public NpgsqlPath(bool open)
{
_points = new List<NpgsqlPoint>();
_open = open;
}
public NpgsqlPath(int capacity, bool open)
{
_points = new List<NpgsqlPoint>(capacity);
_open = open;
}
public NpgsqlPath(int capacity)
: this(capacity, false)
{
}
public bool Open
{
get
{
return _open;
}
set
{
_open = value;
}
}
public NpgsqlPoint this[int index]
{
get { return _points[index]; }
set { _points[index] = value; }
}
public int Count
{
get { return _points.Count; }
}
public bool IsReadOnly
{
get { return false; }
}
public int IndexOf(NpgsqlPoint item)
{
return _points.IndexOf(item);
}
public void Insert(int index, NpgsqlPoint item)
{
_points.Insert(index, item);
}
public void RemoveAt(int index)
{
_points.RemoveAt(index);
}
public void Add(NpgsqlPoint item)
{
_points.Add(item);
}
public void Clear()
{
_points.Clear();
}
public bool Contains(NpgsqlPoint item)
{
return _points.Contains(item);
}
public void CopyTo(NpgsqlPoint[] array, int arrayIndex)
{
_points.CopyTo(array, arrayIndex);
}
public bool Remove(NpgsqlPoint item)
{
return _points.Remove(item);
}
public IEnumerator<NpgsqlPoint> GetEnumerator()
{
return _points.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
public bool Equals(NpgsqlPath other)
{
if (Open != other.Open || Count != other.Count)
return false;
else if(ReferenceEquals(_points, other._points))//Short cut for shallow copies.
return true;
for (int i = 0; i != Count; ++i)
{
if (this[i] != other[i])
{
return false;
}
}
return true;
}
public override bool Equals(object obj)
{
return obj != null && obj is NpgsqlPath && Equals((NpgsqlPath) obj);
}
public static bool operator ==(NpgsqlPath x, NpgsqlPath y)
{
return x.Equals(y);
}
public static bool operator !=(NpgsqlPath x, NpgsqlPath y)
{
return !(x == y);
}
public override int GetHashCode()
{
int ret = 266370105;//seed with something other than zero to make paths of all zeros hash differently.
foreach (NpgsqlPoint point in this)
{
//The ideal amount to shift each value is one that would evenly spread it throughout
//the resultant bytes. Using the current result % 32 is essentially using a random value
//but one that will be the same on subsequent calls.
ret ^= PGUtil.RotateShift(point.GetHashCode(), ret%sizeof (int));
}
return Open ? ret : -ret;
}
}
/// <summary>
/// Represents a PostgreSQL Polygon type.
/// </summary>
public struct NpgsqlPolygon : IList<NpgsqlPoint>, IEquatable<NpgsqlPolygon>
{
private readonly List<NpgsqlPoint> _points;
public NpgsqlPolygon(IEnumerable<NpgsqlPoint> points)
{
_points = new List<NpgsqlPoint>(points);
}
public NpgsqlPolygon(NpgsqlPoint[] points) : this ((IEnumerable<NpgsqlPoint>) points)
{
}
public NpgsqlPoint this[int index]
{
get { return _points[index]; }
set { _points[index] = value; }
}
public int Count
{
get { return _points.Count; }
}
public bool IsReadOnly
{
get { return false; }
}
public int IndexOf(NpgsqlPoint item)
{
return _points.IndexOf(item);
}
public void Insert(int index, NpgsqlPoint item)
{
_points.Insert(index, item);
}
public void RemoveAt(int index)
{
_points.RemoveAt(index);
}
public void Add(NpgsqlPoint item)
{
_points.Add(item);
}
public void Clear()
{
_points.Clear();
}
public bool Contains(NpgsqlPoint item)
{
return _points.Contains(item);
}
public void CopyTo(NpgsqlPoint[] array, int arrayIndex)
{
_points.CopyTo(array, arrayIndex);
}
public bool Remove(NpgsqlPoint item)
{
return _points.Remove(item);
}
public IEnumerator<NpgsqlPoint> GetEnumerator()
{
return _points.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
public bool Equals(NpgsqlPolygon other)
{
if (Count != other.Count)
return false;
else if(ReferenceEquals(_points, _points))//Shortcut for copies of each other.
return true;
for (int i = 0; i != Count; ++i)
{
if (this[i] != other[i])
{
return false;
}
}
return true;
}
public override bool Equals(object obj)
{
return obj is NpgsqlPolygon && Equals((NpgsqlPolygon) obj);
}
public static bool operator ==(NpgsqlPolygon x, NpgsqlPolygon y)
{
return x.Equals(y);
}
public static bool operator !=(NpgsqlPolygon x, NpgsqlPolygon y)
{
return !(x == y);
}
public override int GetHashCode()
{
int ret = 266370105;//seed with something other than zero to make paths of all zeros hash differently.
foreach (NpgsqlPoint point in this)
{
//The ideal amount to shift each value is one that would evenly spread it throughout
//the resultant bytes. Using the current result % 32 is essentially using a random value
//but one that will be the same on subsequent calls.
ret ^= PGUtil.RotateShift(point.GetHashCode(), ret%sizeof (int));
}
return ret;
}
}
/// <summary>
/// Represents a PostgreSQL Circle type.
/// </summary>
public struct NpgsqlCircle : IEquatable<NpgsqlCircle>
{
public NpgsqlPoint Center;
public Double Radius;
public NpgsqlCircle(NpgsqlPoint center, Double radius)
{
Center = center;
Radius = radius;
}
public bool Equals(NpgsqlCircle other)
{
return Center == other.Center && Radius == other.Radius;
}
public override bool Equals(object obj)
{
return obj is NpgsqlCircle && Equals((NpgsqlCircle) obj);
}
public override String ToString()
{
return string.Format("({0}), {1}", Center, Radius);
}
public static bool operator ==(NpgsqlCircle x, NpgsqlCircle y)
{
return x.Equals(y);
}
public static bool operator !=(NpgsqlCircle x, NpgsqlCircle y)
{
return !(x == y);
}
public override int GetHashCode()
{
return
Center.X.GetHashCode() ^ PGUtil.RotateShift(Center.Y.GetHashCode(), sizeof (int)/4) ^
PGUtil.RotateShift(Radius.GetHashCode(), sizeof (int)/2);
}
}
/// <summary>
/// Represents a PostgreSQL inet type.
/// </summary>
public struct NpgsqlInet : IEquatable<NpgsqlInet>
{
public IPAddress addr;
public int mask;
public NpgsqlInet(IPAddress addr, int mask)
{
this.addr = addr;
this.mask = mask;
}
public NpgsqlInet(IPAddress addr)
{
this.addr = addr;
this.mask = 32;
}
public NpgsqlInet(string addr)
{
if (addr.IndexOf('/') > 0)
{
string[] addrbits = addr.Split('/');
if (addrbits.GetUpperBound(0) != 1)
{
throw new FormatException("Invalid number of parts in CIDR specification");
}
this.addr = IPAddress.Parse(addrbits[0]);
this.mask = int.Parse(addrbits[1]);
}
else
{
this.addr = IPAddress.Parse(addr);
this.mask = 32;
}
}
public override String ToString()
{
if (mask != 32)
{
return string.Format("{0}/{1}", addr, mask);
}
return addr.ToString();
}
public static explicit operator IPAddress(NpgsqlInet x)
{
if (x.mask != 32)
{
throw new InvalidCastException("Cannot cast CIDR network to address");
}
return x.addr;
}
public static implicit operator NpgsqlInet(IPAddress ipaddress)
{
return new NpgsqlInet(ipaddress);
}
public bool Equals(NpgsqlInet other)
{
return addr.Equals(other.addr) && mask == other.mask;
}
public override bool Equals(object obj)
{
return obj != null && obj is NpgsqlInet && Equals((NpgsqlInet) obj);
}
public override int GetHashCode()
{
return PGUtil.RotateShift(addr.GetHashCode(), mask%32);
}
public static bool operator ==(NpgsqlInet x, NpgsqlInet y)
{
return x.Equals(y);
}
public static bool operator !=(NpgsqlInet x, NpgsqlInet y)
{
return !(x == y);
}
}
/// <summary>
/// Represents a PostgreSQL MacAddress type.
/// </summary>
public struct NpgsqlMacAddress : IEquatable<NpgsqlMacAddress>
{
public PhysicalAddress macAddr;
public NpgsqlMacAddress(PhysicalAddress macAddr)
{
this.macAddr = macAddr;
}
/// <summary>
///
/// </summary>
/// <param name="macAddr">The macAddr parameter must contain a string that can only consist of numbers
/// and upper-case letters as hexadecimal digits. (See PhysicalAddress.Parse method on MSDN)</param>
public NpgsqlMacAddress(string macAddr)
{
if (!string.IsNullOrEmpty(macAddr))
{
string lowerMacAddr = macAddr.ToUpper();
System.Text.StringBuilder sb = new System.Text.StringBuilder();
foreach (char c in lowerMacAddr)
{
if ((c >= '0' && c <= '9') || (c >= 'A' && c <= 'F'))
{
sb.Append(c);
}
}
this.macAddr = PhysicalAddress.Parse(sb.ToString());
}
else
{
this.macAddr = PhysicalAddress.None;
}
}
public override String ToString()
{
return macAddr.ToString();
}
public static explicit operator PhysicalAddress(NpgsqlMacAddress x)
{
return x.macAddr;
}
public static implicit operator NpgsqlMacAddress(PhysicalAddress macAddr)
{
return new NpgsqlMacAddress(macAddr);
}
public bool Equals(NpgsqlMacAddress other)
{
return macAddr.Equals(other.macAddr);
}
public override bool Equals(object obj)
{
return obj != null && obj is NpgsqlMacAddress && Equals((NpgsqlMacAddress)obj);
}
public override int GetHashCode()
{
int ret = 266370105; //seed with something other than zero to make paths of all zeros hash differently.
return PGUtil.RotateShift(macAddr.GetHashCode(), ret);
}
public static bool operator ==(NpgsqlMacAddress x, NpgsqlMacAddress y)
{
return x.Equals(y);
}
public static bool operator !=(NpgsqlMacAddress x, NpgsqlMacAddress y)
{
return !(x == y);
}
}
}