2010-04-06 Jb Evain <jbevain@novell.com>
[mcs.git] / class / Npgsql / NpgsqlTypes / NpgsqlTypes.cs
blob1eb660f89a79db9f61798be021db0634c5161f6e
1 // NpgsqlTypes.NpgsqlTypesHelper.cs
2 //
3 // Author:
4 // Glen Parker <glenebob@nwlink.com>
5 //
6 // Copyright (C) 2004 The Npgsql Development Team
7 // npgsql-general@gborg.postgresql.org
8 // http://gborg.postgresql.org/project/npgsql/projdisplay.php
9 //
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.
27 using System;
28 using System.Collections;
29 using System.Globalization;
30 using System.Data;
31 using System.Net;
32 using System.Text;
33 using System.IO;
34 using System.Resources;
36 namespace NpgsqlTypes
39 /// <summary>
40 /// Represents a PostgreSQL Point type
41 /// </summary>
43 public struct NpgsqlPoint
45 private Single _X;
46 private Single _Y;
48 public NpgsqlPoint(Single X, Single Y)
50 _X = X;
51 _Y = Y;
54 public Single X
56 get
58 return _X;
61 set
63 _X = value;
68 public Single Y
70 get
72 return _Y;
75 set
77 _Y = value;
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
96 get
98 return _UpperRight;
102 _UpperRight = value;
106 public NpgsqlPoint LowerLeft
110 return _LowerLeft;
114 _LowerLeft = value;
121 /// <summary>
122 /// Represents a PostgreSQL Line Segment type.
123 /// </summary>
124 public struct NpgsqlLSeg
126 public NpgsqlPoint Start;
127 public NpgsqlPoint End;
129 public NpgsqlLSeg(NpgsqlPoint Start, NpgsqlPoint End)
131 this.Start = Start;
132 this.End = End;
135 public override String ToString()
137 return String.Format("({0}, {1})", Start, End);
141 /// <summary>
142 /// Represents a PostgreSQL Path type.
143 /// </summary>
144 public struct NpgsqlPath
146 internal NpgsqlPoint[] Points;
148 internal Boolean IsOpen;
150 public NpgsqlPath(NpgsqlPoint[] Points)
152 this.Points = Points;
153 IsOpen = false;
156 public Int32 Count
157 { get
159 return Points.Length;
162 public NpgsqlPoint this [Int32 Index]
163 { get
165 return Points[Index];
168 public Boolean Open
172 return IsOpen;
177 /// <summary>
178 /// Represents a PostgreSQL Polygon type.
179 /// </summary>
180 public struct NpgsqlPolygon
182 internal NpgsqlPoint[] Points;
184 public NpgsqlPolygon(NpgsqlPoint[] Points)
186 this.Points = Points;
189 public Int32 Count
190 { get
192 return Points.Length;
195 public NpgsqlPoint this [Int32 Index]
196 { get
198 return Points[Index];
202 /// <summary>
203 /// Represents a PostgreSQL Circle type.
204 /// </summary>
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);
223 /// <summary>
224 /// Represents a PostgreSQL inet type.
225 /// </summary>
226 public struct NpgsqlInet
228 public IPAddress addr;
229 public int mask;
231 public NpgsqlInet(IPAddress addr, int mask)
233 this.addr = addr;
234 this.mask = mask;
237 public NpgsqlInet(IPAddress addr)
239 this.addr = addr;
240 this.mask = 32;
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]);
253 else
255 this.addr = IPAddress.Parse(addr);
256 this.mask = 32;
260 public override String ToString()
262 if (mask != 32)
263 return string.Format("{0}/{1}", addr.ToString(), mask);
264 else
265 return addr.ToString();
268 public static implicit operator IPAddress(NpgsqlInet x)
270 if (x.mask != 32)
271 throw new InvalidCastException("Cannot cast CIDR network to address");
272 else
273 return x.addr;