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.
36 using System.Xml.Schema;
37 using System.Globalization;
38 using System.Xml.Serialization;
40 namespace System.Data.SqlTypes
43 [SerializableAttribute]
44 [XmlSchemaProvider ("GetXsdType")]
46 public struct SqlGuid : INullable, IComparable
57 public static readonly SqlGuid Null;
63 public SqlGuid (byte[] value)
65 this.value = new Guid (value);
69 public SqlGuid (Guid g)
75 public SqlGuid (string s)
77 this.value = new Guid (s);
81 public SqlGuid (int a, short b, short c, byte d, byte e, byte f, byte g, byte h, byte i, byte j, byte k)
83 this.value = new Guid (a, b, c, d, e, f, g, h, i, j, k);
92 get { return !notNull; }
98 throw new SqlNullValueException ("The property contains Null.");
104 private byte[] GetLastSixBytes()
106 byte [] lastSixBytes = new byte[6];
108 byte[] guidArray = value.ToByteArray();
109 lastSixBytes[0] = guidArray[10];
110 lastSixBytes[1] = guidArray[11];
111 lastSixBytes[2] = guidArray[12];
112 lastSixBytes[3] = guidArray[13];
113 lastSixBytes[4] = guidArray[14];
114 lastSixBytes[5] = guidArray[15];
123 public int CompareTo (object value)
127 if (!(value is SqlGuid))
128 throw new ArgumentException (Locale.GetText ("Value is not a System.Data.SqlTypes.SqlGuid"));
130 return CompareTo ((SqlGuid) value);
135 int CompareTo (SqlGuid value)
140 // LAMESPEC : ms.net implementation actually compares all the 16 bytes.
141 // This code is kept for future changes, if required.
144 //MSDN documentation says that CompareTo is different from
145 //Guid's CompareTo. It uses the SQL Server behavior where
146 //only the last 6 bytes of value are evaluated
147 byte[] compareValue = ((SqlGuid)value).GetLastSixBytes();
148 byte[] currentValue = GetLastSixBytes();
149 for (int i = 0; i < 6; i++)
151 if (currentValue[i] != compareValue[i]) {
152 return Compare(currentValue[i], compareValue[i]);
158 return this.value.CompareTo (value.Value);
163 private static int Compare (uint x, uint y)
174 public override bool Equals (object value)
176 if (!(value is SqlGuid))
178 else if (this.IsNull && ((SqlGuid)value).IsNull)
180 else if (((SqlGuid)value).IsNull)
183 return (bool) (this == (SqlGuid)value);
186 public static SqlBoolean Equals (SqlGuid x, SqlGuid y)
191 public override int GetHashCode ()
193 byte [] bytes = this.ToByteArray ();
196 foreach (byte b in bytes) {
197 result = 91 * result + b.GetHashCode ();
203 public static SqlBoolean GreaterThan (SqlGuid x, SqlGuid y)
208 public static SqlBoolean GreaterThanOrEqual (SqlGuid x, SqlGuid y)
213 public static SqlBoolean LessThan (SqlGuid x, SqlGuid y)
218 public static SqlBoolean LessThanOrEqual (SqlGuid x, SqlGuid y)
223 public static SqlBoolean NotEquals (SqlGuid x, SqlGuid y)
228 public static SqlGuid Parse (string s)
230 return new SqlGuid (s);
233 public byte[] ToByteArray()
235 return value.ToByteArray ();
238 public SqlBinary ToSqlBinary ()
240 return ((SqlBinary)this);
243 public SqlString ToSqlString ()
245 return ((SqlString)this);
248 public override string ToString ()
253 return value.ToString ();
256 public static SqlBoolean operator == (SqlGuid x, SqlGuid y)
258 if (x.IsNull || y.IsNull) return SqlBoolean.Null;
259 return new SqlBoolean (x.Value == y.Value);
262 public static SqlBoolean operator > (SqlGuid x, SqlGuid y)
264 if (x.IsNull || y.IsNull)
265 return SqlBoolean.Null;
267 if (x.Value.CompareTo (y.Value) > 0)
268 return new SqlBoolean (true);
270 return new SqlBoolean (false);
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) return SqlBoolean.Null;
288 return new SqlBoolean (!(x.Value == y.Value));
291 public static SqlBoolean operator < (SqlGuid x, SqlGuid y)
293 if (x.IsNull || y.IsNull)
294 return SqlBoolean.Null;
296 if (x.Value.CompareTo (y.Value) < 0)
297 return new SqlBoolean (true);
299 return new SqlBoolean (false);
303 public static SqlBoolean operator <= (SqlGuid x, SqlGuid y)
305 if (x.IsNull || y.IsNull)
306 return SqlBoolean.Null;
308 if (x.Value.CompareTo (y.Value) <= 0)
309 return new SqlBoolean (true);
311 return new SqlBoolean (false);
314 public static explicit operator SqlGuid (SqlBinary x)
316 return new SqlGuid (x.Value);
319 public static explicit operator Guid (SqlGuid x)
324 public static explicit operator SqlGuid (SqlString x)
326 return new SqlGuid (x.Value);
329 public static implicit operator SqlGuid (Guid x)
331 return new SqlGuid (x);
335 public static XmlQualifiedName GetXsdType (XmlSchemaSet schemaSet)
337 XmlQualifiedName qualifiedName = new XmlQualifiedName ("string", "http://www.w3.org/2001/XMLSchema");
338 return qualifiedName;
342 XmlSchema IXmlSerializable.GetSchema ()
344 throw new NotImplementedException ();
348 void IXmlSerializable.ReadXml (XmlReader reader)
350 throw new NotImplementedException ();
354 void IXmlSerializable.WriteXml (XmlWriter writer)
356 throw new NotImplementedException ();