Mark tests as not working under TARGET_JVM
[mono.git] / mcs / class / System.Data / System.Data.SqlTypes / SqlGuid.cs
1 //
2 // System.Data.SqlTypes.SqlGuid
3 //
4 // Author:
5 //   Tim Coleman <tim@timcoleman.com>
6 //   Ville Palo <vi64pa@koti.soon.fi>
7 //
8 // (C) Copyright 2002 Tim Coleman
9 //
10
11 //
12 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
13 //
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:
21 // 
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
24 // 
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.
32 //
33
34 using System;
35 using System.Xml;
36 using System.Xml.Schema;
37 using System.Globalization;
38 using System.Xml.Serialization;
39
40 namespace System.Data.SqlTypes
41 {
42 #if NET_2_0
43         [SerializableAttribute]
44         [XmlSchemaProvider ("GetXsdType")]
45 #endif
46         public struct SqlGuid : INullable, IComparable
47 #if NET_2_0
48                                 , IXmlSerializable
49 #endif
50         {
51                 #region Fields
52
53                 Guid value;
54
55                 private bool notNull;
56
57                 public static readonly SqlGuid Null;
58
59                 #endregion
60
61                 #region Constructors
62
63                 public SqlGuid (byte[] value) 
64                 {
65                         this.value = new Guid (value);
66                         notNull = true;
67                 }
68
69                 public SqlGuid (Guid g) 
70                 {
71                         this.value = g;
72                         notNull = true;
73                 }
74
75                 public SqlGuid (string s) 
76                 {
77                         this.value = new Guid (s);
78                         notNull = true;
79                 }
80
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) 
82                 {
83                         this.value = new Guid (a, b, c, d, e, f, g, h, i, j, k);
84                         notNull = true;
85                 }
86
87                 #endregion
88
89                 #region Properties
90
91                 public bool IsNull {
92                         get { return !notNull; }
93                 }
94
95                 public Guid Value { 
96                         get { 
97                                 if (this.IsNull) 
98                                         throw new SqlNullValueException ("The property contains Null.");
99                                 else 
100                                         return value; 
101                         }
102                 }
103
104                 private byte[] GetLastSixBytes()
105                 {
106                         byte [] lastSixBytes = new byte[6];
107                         
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];
115                 
116                         return lastSixBytes;
117                 }       
118
119                 #endregion
120
121                 #region Methods
122
123                 public int CompareTo (object value)
124                 {
125                         if (value == null)
126                                 return 1;
127                         if (!(value is SqlGuid))
128                                 throw new ArgumentException (Locale.GetText ("Value is not a System.Data.SqlTypes.SqlGuid"));
129
130                         return CompareTo ((SqlGuid) value);
131                 }
132 #if NET_2_0
133                 public
134 #endif
135                 int CompareTo (SqlGuid value)
136                 {
137                         if (value.IsNull)
138                                 return 1;
139                         else
140                                 // LAMESPEC : ms.net implementation actually compares all the 16 bytes.
141                                 // This code is kept for future changes, if required.
142                                 /*
143                                 {
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++)
150                                         {
151                                                 if (currentValue[i] != compareValue[i]) {
152                                                       return Compare(currentValue[i], compareValue[i]);
153                                                 }
154                                         }
155                                         return 0;
156                                 }
157                                 */
158                                 return this.value.CompareTo (value.Value);
159                                 
160                 }
161
162
163                 private static int Compare (uint x, uint y)
164                 {
165                         if (x < y) {
166                                 return -1;
167                         }
168                         else {
169                                 return 1;
170                         }
171                 }
172
173
174                 public override bool Equals (object value)
175                 {
176                         if (!(value is SqlGuid))
177                                 return false;
178                         else if (this.IsNull && ((SqlGuid)value).IsNull)
179                                 return true;
180                         else if (((SqlGuid)value).IsNull)
181                                 return false;
182                         else
183                                 return (bool) (this == (SqlGuid)value);
184                 }
185
186                 public static SqlBoolean Equals (SqlGuid x, SqlGuid y)
187                 {
188                         return (x == y);
189                 }
190
191                 public override int GetHashCode ()
192                 {
193                         byte [] bytes  = this.ToByteArray ();
194                         
195                         int result = 10;
196                         foreach (byte b in  bytes) {
197                                 result = 91 * result + b.GetHashCode ();
198                         }
199
200                         return result;
201                 }
202
203                 public static SqlBoolean GreaterThan (SqlGuid x, SqlGuid y)
204                 {
205                         return (x > y);
206                 }
207
208                 public static SqlBoolean GreaterThanOrEqual (SqlGuid x, SqlGuid y)
209                 {
210                         return (x >= y);
211                 }
212
213                 public static SqlBoolean LessThan (SqlGuid x, SqlGuid y)
214                 {
215                         return (x < y);
216                 }
217
218                 public static SqlBoolean LessThanOrEqual (SqlGuid x, SqlGuid y)
219                 {
220                         return (x <= y);
221                 }
222
223                 public static SqlBoolean NotEquals (SqlGuid x, SqlGuid y)
224                 {
225                         return (x != y);
226                 }
227
228                 public static SqlGuid Parse (string s)
229                 {
230                         return new SqlGuid (s);
231                 }
232
233                 public byte[] ToByteArray()
234                 {
235                         return value.ToByteArray ();
236                 }
237
238                 public SqlBinary ToSqlBinary ()
239                 {
240                         return ((SqlBinary)this);
241                 }
242
243                 public SqlString ToSqlString ()
244                 {
245                         return ((SqlString)this);
246                 }
247
248                 public override string ToString ()
249                 {
250                         if (!notNull)
251                                 return "Null";
252                         else
253                                 return value.ToString ();
254                 }
255
256                 public static SqlBoolean operator == (SqlGuid x, SqlGuid y)
257                 {
258                         if (x.IsNull || y.IsNull) return SqlBoolean.Null;
259                         return new SqlBoolean (x.Value == y.Value);
260                 }
261
262                 public static SqlBoolean operator > (SqlGuid x, SqlGuid y)
263                 {
264                         if (x.IsNull || y.IsNull)
265                                 return SqlBoolean.Null;
266
267                         if (x.Value.CompareTo (y.Value) > 0)
268                                 return new SqlBoolean (true);
269                         else
270                                 return new SqlBoolean (false);
271                 }
272
273                 public static SqlBoolean operator >= (SqlGuid x, SqlGuid y)
274                 {
275                         if (x.IsNull || y.IsNull)
276                                 return SqlBoolean.Null;
277                         
278                         if (x.Value.CompareTo (y.Value) >= 0)
279                                 return new SqlBoolean (true);
280                         else
281                                 return new SqlBoolean (false);
282
283                 }
284
285                 public static SqlBoolean operator != (SqlGuid x, SqlGuid y)
286                 {
287                         if (x.IsNull || y.IsNull) return SqlBoolean.Null;
288                         return new SqlBoolean (!(x.Value == y.Value));
289                 }
290
291                 public static SqlBoolean operator < (SqlGuid x, SqlGuid y)
292                 {
293                         if (x.IsNull || y.IsNull)
294                                 return SqlBoolean.Null;
295
296                         if (x.Value.CompareTo (y.Value) < 0)
297                                 return new SqlBoolean (true);
298                         else
299                                 return new SqlBoolean (false);
300
301                 }
302
303                 public static SqlBoolean operator <= (SqlGuid x, SqlGuid y)
304                 {
305                         if (x.IsNull || y.IsNull)
306                                 return SqlBoolean.Null;
307
308                         if (x.Value.CompareTo (y.Value) <= 0)
309                                 return new SqlBoolean (true);
310                         else
311                                 return new SqlBoolean (false);
312                 }
313
314                 public static explicit operator SqlGuid (SqlBinary x)
315                 {
316                         return new SqlGuid (x.Value);
317                 }
318
319                 public static explicit operator Guid (SqlGuid x)
320                 {
321                         return x.Value;
322                 }
323
324                 public static explicit operator SqlGuid (SqlString x)
325                 {
326                         return new SqlGuid (x.Value);
327                 }
328
329                 public static implicit operator SqlGuid (Guid x)
330                 {
331                         return new SqlGuid (x);
332                 }
333
334 #if NET_2_0
335                 public static XmlQualifiedName GetXsdType (XmlSchemaSet schemaSet)
336                 {
337                         XmlQualifiedName qualifiedName = new XmlQualifiedName ("string", "http://www.w3.org/2001/XMLSchema");
338                         return qualifiedName;
339                 }
340
341                 [MonoTODO]
342                 XmlSchema IXmlSerializable.GetSchema ()
343                 {
344                         throw new NotImplementedException ();
345                 }
346                 
347                 [MonoTODO]
348                 void IXmlSerializable.ReadXml (XmlReader reader)
349                 {
350                         throw new NotImplementedException ();
351                 }
352                 
353                 [MonoTODO]
354                 void IXmlSerializable.WriteXml (XmlWriter writer) 
355                 {
356                         throw new NotImplementedException ();
357                 }
358 #endif
359                 #endregion
360         }
361 }
362