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
838 lines (715 loc) · 25.7 KB
/
NpgsqlTypes.cs
File metadata and controls
838 lines (715 loc) · 25.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
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
// 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.Diagnostics.Contracts;
using System.Globalization;
using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.Text;
using System.Text.RegularExpressions;
using Npgsql;
#pragma warning disable 1591
namespace NpgsqlTypes
{
/// <summary>
/// Represents a PostgreSQL point type.
/// </summary>
/// <remarks>
/// See http://www.postgresql.org/docs/current/static/datatype-geometric.html
/// </remarks>
public struct NpgsqlPoint : IEquatable<NpgsqlPoint>
{
static readonly Regex Regex = new Regex(@"\((-?\d+.?\d*),(-?\d+.?\d*)\)");
public double X { get; set; }
public double Y { get; set; }
public NpgsqlPoint(double x, double y)
: this()
{
X = x;
Y = y;
}
public bool Equals(NpgsqlPoint other)
{
return X == other.X && Y == other.Y;
}
public override bool Equals(object obj)
{
return 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 static NpgsqlPoint Parse(string s)
{
var m = Regex.Match(s);
if (!m.Success) {
throw new FormatException("Not a valid point: " + s);
}
return new NpgsqlPoint(Double.Parse(m.Groups[1].ToString(), NumberStyles.Any, CultureInfo.InvariantCulture.NumberFormat),
Double.Parse(m.Groups[2].ToString(), NumberStyles.Any, CultureInfo.InvariantCulture.NumberFormat));
}
public override string ToString()
{
return String.Format(CultureInfo.InvariantCulture, "({0},{1})", X, Y);
}
}
/// <summary>
/// Represents a PostgreSQL line type.
/// </summary>
/// <remarks>
/// See http://www.postgresql.org/docs/current/static/datatype-geometric.html
/// </remarks>
public struct NpgsqlLine : IEquatable<NpgsqlLine>
{
static readonly Regex Regex = new Regex(@"\{(-?\d+.?\d*),(-?\d+.?\d*),(-?\d+.?\d*)\}");
public double A { get; set; }
public double B { get; set; }
public double C { get; set; }
public NpgsqlLine(double a, double b, double c)
: this()
{
A = a;
B = b;
C = c;
}
public static NpgsqlLine Parse(string s)
{
var m = Regex.Match(s);
if (!m.Success) {
throw new FormatException("Not a valid line: " + s);
}
return new NpgsqlLine(
Double.Parse(m.Groups[1].ToString(), NumberStyles.Any, CultureInfo.InvariantCulture.NumberFormat),
Double.Parse(m.Groups[2].ToString(), NumberStyles.Any, CultureInfo.InvariantCulture.NumberFormat),
Double.Parse(m.Groups[3].ToString(), NumberStyles.Any, CultureInfo.InvariantCulture.NumberFormat)
);
}
public override String ToString()
{
return String.Format(CultureInfo.InvariantCulture, "{{{0},{1},{2}}}", A, B, C);
}
public override int GetHashCode()
{
return A.GetHashCode() * B.GetHashCode() * C.GetHashCode();
}
public bool Equals(NpgsqlLine other)
{
return A == other.A && B == other.B && C == other.C;
}
public override bool Equals(object obj)
{
return obj is NpgsqlLine && Equals((NpgsqlLine)obj);
}
public static bool operator ==(NpgsqlLine x, NpgsqlLine y)
{
return x.Equals(y);
}
public static bool operator !=(NpgsqlLine x, NpgsqlLine y)
{
return !(x == y);
}
}
/// <summary>
/// Represents a PostgreSQL Line Segment type.
/// </summary>
public struct NpgsqlLSeg : IEquatable<NpgsqlLSeg>
{
static readonly Regex Regex = new Regex(@"\[\((-?\d+.?\d*),(-?\d+.?\d*)\),\((-?\d+.?\d*),(-?\d+.?\d*)\)\]");
public NpgsqlPoint Start { get; set; }
public NpgsqlPoint End { get; set; }
public NpgsqlLSeg(NpgsqlPoint start, NpgsqlPoint end)
: this()
{
Start = start;
End = end;
}
public NpgsqlLSeg(double startx, double starty, double endx, double endy) : this()
{
Start = new NpgsqlPoint(startx, starty);
End = new NpgsqlPoint(endx, endy);
}
public static NpgsqlLSeg Parse(string s)
{
var m = Regex.Match(s);
if (!m.Success) {
throw new FormatException("Not a valid line: " + s);
}
return new NpgsqlLSeg(
Double.Parse(m.Groups[1].ToString(), NumberStyles.Any, CultureInfo.InvariantCulture.NumberFormat),
Double.Parse(m.Groups[2].ToString(), NumberStyles.Any, CultureInfo.InvariantCulture.NumberFormat),
Double.Parse(m.Groups[3].ToString(), NumberStyles.Any, CultureInfo.InvariantCulture.NumberFormat),
Double.Parse(m.Groups[4].ToString(), NumberStyles.Any, CultureInfo.InvariantCulture.NumberFormat)
);
}
public override String ToString()
{
return String.Format(CultureInfo.InvariantCulture, "[{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 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 box type.
/// </summary>
/// <remarks>
/// See http://www.postgresql.org/docs/current/static/datatype-geometric.html
/// </remarks>
public struct NpgsqlBox : IEquatable<NpgsqlBox>
{
static readonly Regex Regex = new Regex(@"\((-?\d+.?\d*),(-?\d+.?\d*)\),\((-?\d+.?\d*),(-?\d+.?\d*)\)");
public NpgsqlPoint UpperRight { get; set; }
public NpgsqlPoint LowerLeft { get; set; }
public NpgsqlBox(NpgsqlPoint upperRight, NpgsqlPoint lowerLeft) : this()
{
UpperRight = upperRight;
LowerLeft = lowerLeft;
}
public NpgsqlBox(double top, double right, double bottom, double left)
: this(new NpgsqlPoint(right, top), new NpgsqlPoint(left, bottom)) { }
public double Left { get { return LowerLeft.X; } }
public double Right { get { return UpperRight.X; } }
public double Bottom { get { return LowerLeft.Y; } }
public double Top { get { return UpperRight.Y; } }
public double Width { get { return Right - Left; } }
public double 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 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 string ToString()
{
return String.Format(CultureInfo.InvariantCulture, "{0},{1}", UpperRight, LowerLeft);
}
public static NpgsqlBox Parse(string s)
{
var m = Regex.Match(s);
return new NpgsqlBox(
new NpgsqlPoint(Double.Parse(m.Groups[1].ToString(), NumberStyles.Any, CultureInfo.InvariantCulture.NumberFormat),
Double.Parse(m.Groups[2].ToString(), NumberStyles.Any, CultureInfo.InvariantCulture.NumberFormat)),
new NpgsqlPoint(Double.Parse(m.Groups[3].ToString(), NumberStyles.Any, CultureInfo.InvariantCulture.NumberFormat),
Double.Parse(m.Groups[4].ToString(), NumberStyles.Any, CultureInfo.InvariantCulture.NumberFormat))
);
}
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 Path type.
/// </summary>
public struct NpgsqlPath : IList<NpgsqlPoint>, IEquatable<NpgsqlPath>
{
readonly List<NpgsqlPoint> _points;
public bool Open { get; set; }
public NpgsqlPath(IEnumerable<NpgsqlPoint> points, bool open) : this()
{
_points = new List<NpgsqlPoint>(points);
Open = open;
}
public NpgsqlPath(IEnumerable<NpgsqlPoint> points) : this(points, false) {}
public NpgsqlPath(params NpgsqlPoint[] points) : this(points, false) {}
public NpgsqlPath(bool open) : this()
{
_points = new List<NpgsqlPoint>();
Open = open;
}
public NpgsqlPath(int capacity, bool open) : this()
{
_points = new List<NpgsqlPoint>(capacity);
Open = open;
}
public NpgsqlPath(int capacity) : this(capacity, false) {}
public NpgsqlPoint this[int index]
{
get { return _points[index]; }
set { _points[index] = value; }
}
public int Capacity { get { return _points.Capacity; } }
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;
}
public override string ToString()
{
var sb = new StringBuilder();
sb.Append(Open ? '[' : '(');
int i;
for (i = 0; i < _points.Count; i++)
{
var p = _points[i];
sb.AppendFormat(CultureInfo.InvariantCulture, "({0},{1})", p.X, p.Y);
if (i < _points.Count - 1) {
sb.Append(",");
}
}
sb.Append(Open ? ']' : ')');
return sb.ToString();
}
public static NpgsqlPath Parse(string s)
{
bool open;
switch (s[0])
{
case '[':
open = true;
break;
case '(':
open = false;
break;
default:
throw new Exception("Invalid path string: " + s);
}
Contract.Assume(s[s.Length - 1] == (open ? ']' : ')'));
var result = new NpgsqlPath(open);
var i = 1;
while (true)
{
var i2 = s.IndexOf(')', i);
result.Add(NpgsqlPoint.Parse(s.Substring(i, i2 - i + 1)));
if (s[i2 + 1] != ',')
break;
i = i2 + 2;
}
return result;
}
}
/// <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(params NpgsqlPoint[] points) : this ((IEnumerable<NpgsqlPoint>) points) {}
public NpgsqlPolygon(int capacity)
{
_points = new List<NpgsqlPoint>(capacity);
}
public NpgsqlPoint this[int index]
{
get { return _points[index]; }
set { _points[index] = value; }
}
public int Capacity { get { return _points.Capacity; } }
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;
if (ReferenceEquals(_points, other._points))
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;
}
public static NpgsqlPolygon Parse(string s)
{
var points = new List<NpgsqlPoint>();
var i = 1;
while (true)
{
var i2 = s.IndexOf(')', i);
points.Add(NpgsqlPoint.Parse(s.Substring(i, i2 - i + 1)));
if (s[i2 + 1] != ',')
break;
i = i2 + 2;
}
return new NpgsqlPolygon(points);
}
public override string ToString()
{
var sb = new StringBuilder();
sb.Append('(');
int i;
for (i = 0; i < _points.Count; i++)
{
var p = _points[i];
sb.AppendFormat(CultureInfo.InvariantCulture, "({0},{1})", p.X, p.Y);
if (i < _points.Count - 1) {
sb.Append(",");
}
}
sb.Append(')');
return sb.ToString();
}
}
/// <summary>
/// Represents a PostgreSQL Circle type.
/// </summary>
public struct NpgsqlCircle : IEquatable<NpgsqlCircle>
{
static readonly Regex Regex = new Regex(@"<\((-?\d+.?\d*),(-?\d+.?\d*)\),(\d+.?\d*)>");
public double X { get; set; }
public double Y { get; set; }
public double Radius { get; set; }
public NpgsqlCircle(NpgsqlPoint center, double radius)
: this()
{
X = center.X;
Y = center.Y;
Radius = radius;
}
public NpgsqlCircle(double x, double y, double radius) : this()
{
X = x;
Y = y;
Radius = radius;
}
public NpgsqlPoint Center
{
get { return new NpgsqlPoint(X, Y); }
set
{
X = value.X;
Y = value.Y;
}
}
public bool Equals(NpgsqlCircle other)
{
return X == other.X && Y == other.Y && Radius == other.Radius;
}
public override bool Equals(object obj)
{
return obj is NpgsqlCircle && Equals((NpgsqlCircle) obj);
}
public static NpgsqlCircle Parse(string s)
{
var m = Regex.Match(s);
if (!m.Success) {
throw new FormatException("Not a valid circle: " + s);
}
return new NpgsqlCircle(
Double.Parse(m.Groups[1].ToString(), NumberStyles.Any, CultureInfo.InvariantCulture.NumberFormat),
Double.Parse(m.Groups[2].ToString(), NumberStyles.Any, CultureInfo.InvariantCulture.NumberFormat),
Double.Parse(m.Groups[3].ToString(), NumberStyles.Any, CultureInfo.InvariantCulture.NumberFormat)
);
}
public override String ToString()
{
return string.Format(CultureInfo.InvariantCulture, "<({0},{1}),{2}>", X, Y, 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 X.GetHashCode() * Y.GetHashCode() * Radius.GetHashCode();
}
}
/// <summary>
/// Represents a PostgreSQL inet type, which is a combination of an IPAddress and a
/// subnet mask.
/// </summary>
/// <remarks>
/// http://www.postgresql.org/docs/current/static/datatype-net-types.html
/// </remarks>
public struct NpgsqlInet : IEquatable<NpgsqlInet>
{
public IPAddress Address;
public int Netmask;
public NpgsqlInet(IPAddress address, int netmask)
{
if (address.AddressFamily != AddressFamily.InterNetwork && address.AddressFamily != AddressFamily.InterNetworkV6)
throw new ArgumentException("Only IPAddress of InterNetwork or InterNetworkV6 address families are accepted", "address");
Contract.EndContractBlock();
Address = address;
Netmask = netmask;
}
public NpgsqlInet(IPAddress address)
{
if (address.AddressFamily != AddressFamily.InterNetwork && address.AddressFamily != AddressFamily.InterNetworkV6)
throw new ArgumentException("Only IPAddress of InterNetwork or InterNetworkV6 address families are accepted", "address");
Contract.EndContractBlock();
Address = address;
Netmask = address.AddressFamily == AddressFamily.InterNetwork ? 32 : 128;
}
public NpgsqlInet(string addr)
{
if (addr.IndexOf('/') > 0)
{
var addrbits = addr.Split('/');
if (addrbits.GetUpperBound(0) != 1) {
throw new FormatException("Invalid number of parts in CIDR specification");
}
Address = IPAddress.Parse(addrbits[0]);
Netmask = int.Parse(addrbits[1]);
}
else
{
Address = IPAddress.Parse(addr);
Netmask = 32;
}
}
public override String ToString()
{
if (Netmask != 32) {
return string.Format("{0}/{1}", Address, Netmask);
}
return Address.ToString();
}
public static explicit operator IPAddress(NpgsqlInet x)
{
if (x.Netmask != 32) {
throw new InvalidCastException("Cannot cast CIDR network to address");
}
return x.Address;
}
public static implicit operator NpgsqlInet(IPAddress ipaddress)
{
return new NpgsqlInet(ipaddress);
}
public bool Equals(NpgsqlInet other)
{
return Address.Equals(other.Address) && Netmask == other.Netmask;
}
public override bool Equals(object obj)
{
return obj != null && obj is NpgsqlInet && Equals((NpgsqlInet) obj);
}
public override int GetHashCode()
{
return PGUtil.RotateShift(Address.GetHashCode(), Netmask%32);
}
public static bool operator ==(NpgsqlInet x, NpgsqlInet y)
{
return x.Equals(y);
}
public static bool operator !=(NpgsqlInet x, NpgsqlInet y)
{
return !(x == y);
}
}
}
#pragma warning restore 1591