1 // NpgsqlTypes.NpgsqlTypesHelper.cs
4 // Glen Parker <glenebob@nwlink.com>
6 // Copyright (C) 2004 The Npgsql Development Team
7 // npgsql-general@gborg.postgresql.org
8 // http://gborg.postgresql.org/project/npgsql/projdisplay.php
10 // This library is free software; you can redistribute it and/or
11 // modify it under the terms of the GNU Lesser General Public
12 // License as published by the Free Software Foundation; either
13 // version 2.1 of the License, or (at your option) any later version.
15 // This library is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 // Lesser General Public License for more details.
20 // You should have received a copy of the GNU Lesser General Public
21 // License along with this library; if not, write to the Free Software
22 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 // This file provides implementations of PostgreSQL specific data types that cannot
25 // be mapped to standard .NET classes.
28 using System
.Collections
;
29 using System
.Globalization
;
34 using System
.Resources
;
40 /// Represents a PostgreSQL Point type
43 public struct NpgsqlPoint
48 public NpgsqlPoint(Single X
, Single Y
)
82 public struct NpgsqlBox
84 private NpgsqlPoint _UpperRight
;
85 private NpgsqlPoint _LowerLeft
;
87 public NpgsqlBox(NpgsqlPoint UpperRight
, NpgsqlPoint LowerLeft
)
89 _UpperRight
= UpperRight
;
90 _LowerLeft
= LowerLeft
;
94 public NpgsqlPoint UpperRight
106 public NpgsqlPoint LowerLeft
122 /// Represents a PostgreSQL Line Segment type.
124 public struct NpgsqlLSeg
126 public NpgsqlPoint Start
;
127 public NpgsqlPoint End
;
129 public NpgsqlLSeg(NpgsqlPoint Start
, NpgsqlPoint End
)
135 public override String
ToString()
137 return String
.Format("({0}, {1})", Start
, End
);
142 /// Represents a PostgreSQL Path type.
144 public struct NpgsqlPath
146 internal NpgsqlPoint
[] Points
;
148 internal Boolean IsOpen
;
150 public NpgsqlPath(NpgsqlPoint
[] Points
)
152 this.Points
= Points
;
159 return Points
.Length
;
162 public NpgsqlPoint
this [Int32 Index
]
165 return Points
[Index
];
178 /// Represents a PostgreSQL Polygon type.
180 public struct NpgsqlPolygon
182 internal NpgsqlPoint
[] Points
;
184 public NpgsqlPolygon(NpgsqlPoint
[] Points
)
186 this.Points
= Points
;
192 return Points
.Length
;
195 public NpgsqlPoint
this [Int32 Index
]
198 return Points
[Index
];
203 /// Represents a PostgreSQL Circle type.
205 public struct NpgsqlCircle
207 public NpgsqlPoint Center
;
208 public Double Radius
;
210 public NpgsqlCircle(NpgsqlPoint Center
, Double Radius
)
212 this.Center
= Center
;
213 this.Radius
= Radius
;
216 public override String
ToString()
218 return string.Format("({0}), {1}", Center
, Radius
);
224 /// Represents a PostgreSQL inet type.
226 public struct NpgsqlInet
228 public IPAddress addr
;
231 public NpgsqlInet(IPAddress addr
, int mask
)
237 public NpgsqlInet(IPAddress addr
)
243 public NpgsqlInet(string addr
)
245 if (addr
.IndexOf('/') > 0)
247 string[] addrbits
= addr
.Split('/');
248 if (addrbits
.GetUpperBound(0) != 1)
249 throw new FormatException("Invalid number of parts in CIDR specification");
250 this.addr
= IPAddress
.Parse(addrbits
[0]);
251 this.mask
= int.Parse(addrbits
[1]);
255 this.addr
= IPAddress
.Parse(addr
);
260 public override String
ToString()
263 return string.Format("{0}/{1}", addr
.ToString(), mask
);
265 return addr
.ToString();
268 public static implicit operator IPAddress(NpgsqlInet x
)
271 throw new InvalidCastException("Cannot cast CIDR network to address");