2 // System.Data.SqlTypes.SqlGuid
5 // Tim Coleman <tim@timcoleman.com>
6 // Ville Palo <vi64pa@koti.soon.fi>
8 // (C) Copyright 2002 Tim Coleman
12 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35 using System.Globalization;
37 namespace System.Data.SqlTypes
39 public struct SqlGuid : INullable, IComparable
47 public static readonly SqlGuid Null;
53 public SqlGuid (byte[] value)
55 this.value = new Guid (value);
59 public SqlGuid (Guid g)
65 public SqlGuid (string s)
67 this.value = new Guid (s);
71 public SqlGuid (int a, short b, short c, byte d, byte e, byte f, byte g, byte h, byte i, byte j, byte k)
73 this.value = new Guid (a, b, c, d, e, f, g, h, i, j, k);
82 get { return !notNull; }
88 throw new SqlNullValueException ("The property contains Null.");
94 private byte[] GetLastSixBytes()
96 byte [] lastSixBytes = new byte[6];
98 byte[] guidArray = value.ToByteArray();
99 lastSixBytes[0] = guidArray[10];
100 lastSixBytes[1] = guidArray[11];
101 lastSixBytes[2] = guidArray[12];
102 lastSixBytes[3] = guidArray[13];
103 lastSixBytes[4] = guidArray[14];
104 lastSixBytes[5] = guidArray[15];
113 public int CompareTo (object value)
117 else if (!(value is SqlGuid))
118 throw new ArgumentException (Locale.GetText ("Value is not a System.Data.SqlTypes.SqlGuid"));
119 else if (((SqlGuid)value).IsNull)
122 // LAMESPEC : ms.net implementation actually compares all the 16 bytes.
123 // This code is kept for future changes, if required.
126 //MSDN documentation says that CompareTo is different from
127 //Guid's CompareTo. It uses the SQL Server behavior where
128 //only the last 6 bytes of value are evaluated
129 byte[] compareValue = ((SqlGuid)value).GetLastSixBytes();
130 byte[] currentValue = GetLastSixBytes();
131 for (int i = 0; i < 6; i++)
133 if (currentValue[i] != compareValue[i]) {
134 return Compare(currentValue[i], compareValue[i]);
140 return this.value.CompareTo (((SqlGuid)value).Value);
145 private static int Compare (uint x, uint y)
156 public override bool Equals (object value)
158 if (!(value is SqlGuid))
160 else if (this.IsNull && ((SqlGuid)value).IsNull)
162 else if (((SqlGuid)value).IsNull)
165 return (bool) (this == (SqlGuid)value);
168 public static SqlBoolean Equals (SqlGuid x, SqlGuid y)
173 public override int GetHashCode ()
175 byte [] bytes = this.ToByteArray ();
178 foreach (byte b in bytes) {
179 result = 91 * result + b.GetHashCode ();
185 public static SqlBoolean GreaterThan (SqlGuid x, SqlGuid y)
190 public static SqlBoolean GreaterThanOrEqual (SqlGuid x, SqlGuid y)
195 public static SqlBoolean LessThan (SqlGuid x, SqlGuid y)
200 public static SqlBoolean LessThanOrEqual (SqlGuid x, SqlGuid y)
205 public static SqlBoolean NotEquals (SqlGuid x, SqlGuid y)
210 public static SqlGuid Parse (string s)
212 return new SqlGuid (s);
215 public byte[] ToByteArray()
217 return value.ToByteArray ();
220 public SqlBinary ToSqlBinary ()
222 return ((SqlBinary)this);
225 public SqlString ToSqlString ()
227 return ((SqlString)this);
230 public override string ToString ()
235 return value.ToString ();
238 public static SqlBoolean operator == (SqlGuid x, SqlGuid y)
240 if (x.IsNull || y.IsNull) return SqlBoolean.Null;
241 return new SqlBoolean (x.Value == y.Value);
244 public static SqlBoolean operator > (SqlGuid x, SqlGuid y)
246 if (x.IsNull || y.IsNull)
247 return SqlBoolean.Null;
249 if (x.Value.CompareTo (y.Value) > 0)
250 return new SqlBoolean (true);
252 return new SqlBoolean (false);
255 public static SqlBoolean operator >= (SqlGuid x, SqlGuid y)
257 if (x.IsNull || y.IsNull)
258 return SqlBoolean.Null;
260 if (x.Value.CompareTo (y.Value) >= 0)
261 return new SqlBoolean (true);
263 return new SqlBoolean (false);
267 public static SqlBoolean operator != (SqlGuid x, SqlGuid y)
269 if (x.IsNull || y.IsNull) return SqlBoolean.Null;
270 return new SqlBoolean (!(x.Value == y.Value));
273 public static SqlBoolean operator < (SqlGuid x, SqlGuid y)
275 if (x.IsNull || y.IsNull)
276 return SqlBoolean.Null;
278 if (x.Value.CompareTo (y.Value) < 0)
279 return new SqlBoolean (true);
281 return new SqlBoolean (false);
285 public static SqlBoolean operator <= (SqlGuid x, SqlGuid y)
287 if (x.IsNull || y.IsNull)
288 return SqlBoolean.Null;
290 if (x.Value.CompareTo (y.Value) <= 0)
291 return new SqlBoolean (true);
293 return new SqlBoolean (false);
296 public static explicit operator SqlGuid (SqlBinary x)
298 return new SqlGuid (x.Value);
301 public static explicit operator Guid (SqlGuid x)
306 public static explicit operator SqlGuid (SqlString x)
308 return new SqlGuid (x.Value);
311 public static implicit operator SqlGuid (Guid x)
313 return new SqlGuid (x);