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.");
108 public int CompareTo (object value)
112 if (!(value is SqlGuid))
113 throw new ArgumentException (Locale.GetText ("Value is not a System.Data.SqlTypes.SqlGuid"));
115 return CompareTo ((SqlGuid) value);
120 int CompareTo (SqlGuid value)
125 // LAMESPEC : ms.net implementation actually compares all the 16 bytes.
126 // This code is kept for future changes, if required.
129 //MSDN documentation says that CompareTo is different from
130 //Guid's CompareTo. It uses the SQL Server behavior where
131 //only the last 6 bytes of value are evaluated
132 byte[] compareValue = ((SqlGuid)value).GetLastSixBytes();
133 byte[] currentValue = GetLastSixBytes();
134 for (int i = 0; i < 6; i++)
136 if (currentValue[i] != compareValue[i]) {
137 return Compare(currentValue[i], compareValue[i]);
143 return this.value.CompareTo (value.Value);
147 public override bool Equals (object value)
149 if (!(value is SqlGuid))
151 else if (this.IsNull)
152 return ((SqlGuid)value).IsNull;
153 else if (((SqlGuid)value).IsNull)
156 return (bool) (this == (SqlGuid)value);
159 public static SqlBoolean Equals (SqlGuid x, SqlGuid y)
164 public override int GetHashCode ()
166 byte [] bytes = this.ToByteArray ();
169 foreach (byte b in bytes) {
170 result = 91 * result + b.GetHashCode ();
176 public static SqlBoolean GreaterThan (SqlGuid x, SqlGuid y)
181 public static SqlBoolean GreaterThanOrEqual (SqlGuid x, SqlGuid y)
186 public static SqlBoolean LessThan (SqlGuid x, SqlGuid y)
191 public static SqlBoolean LessThanOrEqual (SqlGuid x, SqlGuid y)
196 public static SqlBoolean NotEquals (SqlGuid x, SqlGuid y)
201 public static SqlGuid Parse (string s)
203 return new SqlGuid (s);
206 public byte[] ToByteArray()
208 return value.ToByteArray ();
211 public SqlBinary ToSqlBinary ()
213 return ((SqlBinary)this);
216 public SqlString ToSqlString ()
218 return ((SqlString)this);
221 public override string ToString ()
226 return value.ToString ();
229 public static SqlBoolean operator == (SqlGuid x, SqlGuid y)
231 if (x.IsNull || y.IsNull) return SqlBoolean.Null;
232 return new SqlBoolean (x.Value == y.Value);
235 public static SqlBoolean operator > (SqlGuid x, SqlGuid y)
237 if (x.IsNull || y.IsNull)
238 return SqlBoolean.Null;
240 if (x.Value.CompareTo (y.Value) > 0)
241 return new SqlBoolean (true);
243 return new SqlBoolean (false);
246 public static SqlBoolean operator >= (SqlGuid x, SqlGuid y)
248 if (x.IsNull || y.IsNull)
249 return SqlBoolean.Null;
251 if (x.Value.CompareTo (y.Value) >= 0)
252 return new SqlBoolean (true);
254 return new SqlBoolean (false);
258 public static SqlBoolean operator != (SqlGuid x, SqlGuid y)
260 if (x.IsNull || y.IsNull) return SqlBoolean.Null;
261 return new SqlBoolean (!(x.Value == y.Value));
264 public static SqlBoolean operator < (SqlGuid x, SqlGuid y)
266 if (x.IsNull || y.IsNull)
267 return SqlBoolean.Null;
269 if (x.Value.CompareTo (y.Value) < 0)
270 return new SqlBoolean (true);
272 return new SqlBoolean (false);
276 public static SqlBoolean operator <= (SqlGuid x, SqlGuid y)
278 if (x.IsNull || y.IsNull)
279 return SqlBoolean.Null;
281 if (x.Value.CompareTo (y.Value) <= 0)
282 return new SqlBoolean (true);
284 return new SqlBoolean (false);
287 public static explicit operator SqlGuid (SqlBinary x)
289 return new SqlGuid (x.Value);
292 public static explicit operator Guid (SqlGuid x)
297 public static explicit operator SqlGuid (SqlString x)
299 return new SqlGuid (x.Value);
302 public static implicit operator SqlGuid (Guid x)
304 return new SqlGuid (x);
308 public static XmlQualifiedName GetXsdType (XmlSchemaSet schemaSet)
310 XmlQualifiedName qualifiedName = new XmlQualifiedName ("string", "http://www.w3.org/2001/XMLSchema");
311 return qualifiedName;
315 XmlSchema IXmlSerializable.GetSchema ()
317 throw new NotImplementedException ();
321 void IXmlSerializable.ReadXml (XmlReader reader)
323 throw new NotImplementedException ();
327 void IXmlSerializable.WriteXml (XmlWriter writer)
329 throw new NotImplementedException ();