2007-01-08 Nagappan A <anagappan@novell.com>
[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.Globalization;
36
37 namespace System.Data.SqlTypes
38 {
39         public struct SqlGuid : INullable, IComparable
40         {
41                 #region Fields
42
43                 Guid value;
44
45                 private bool notNull;
46
47                 public static readonly SqlGuid Null;
48
49                 #endregion
50
51                 #region Constructors
52
53                 public SqlGuid (byte[] value) 
54                 {
55                         this.value = new Guid (value);
56                         notNull = true;
57                 }
58
59                 public SqlGuid (Guid g) 
60                 {
61                         this.value = g;
62                         notNull = true;
63                 }
64
65                 public SqlGuid (string s) 
66                 {
67                         this.value = new Guid (s);
68                         notNull = true;
69                 }
70
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) 
72                 {
73                         this.value = new Guid (a, b, c, d, e, f, g, h, i, j, k);
74                         notNull = true;
75                 }
76
77                 #endregion
78
79                 #region Properties
80
81                 public bool IsNull {
82                         get { return !notNull; }
83                 }
84
85                 public Guid Value { 
86                         get { 
87                                 if (this.IsNull) 
88                                         throw new SqlNullValueException ("The property contains Null.");
89                                 else 
90                                         return value; 
91                         }
92                 }
93
94                 private byte[] GetLastSixBytes()
95                 {
96                         byte [] lastSixBytes = new byte[6];
97                         
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];
105                 
106                         return lastSixBytes;
107                 }       
108
109                 #endregion
110
111                 #region Methods
112
113                 public int CompareTo (object value)
114                 {
115                         if (value == null)
116                                 return 1;
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)
120                                 return 1;
121                         else
122                                 // LAMESPEC : ms.net implementation actually compares all the 16 bytes.
123                                 // This code is kept for future changes, if required.
124                                 /*
125                                 {
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++)
132                                         {
133                                                 if (currentValue[i] != compareValue[i]) {
134                                                       return Compare(currentValue[i], compareValue[i]);
135                                                 }
136                                         }
137                                         return 0;
138                                 }
139                                 */
140                                 return this.value.CompareTo (((SqlGuid)value).Value);
141                                 
142                 }
143
144
145                 private static int Compare (uint x, uint y)
146                 {
147                         if (x < y) {
148                                 return -1;
149                         }
150                         else {
151                                 return 1;
152                         }
153                 }
154
155
156                 public override bool Equals (object value)
157                 {
158                         if (!(value is SqlGuid))
159                                 return false;
160                         else if (this.IsNull && ((SqlGuid)value).IsNull)
161                                 return true;
162                         else if (((SqlGuid)value).IsNull)
163                                 return false;
164                         else
165                                 return (bool) (this == (SqlGuid)value);
166                 }
167
168                 public static SqlBoolean Equals (SqlGuid x, SqlGuid y)
169                 {
170                         return (x == y);
171                 }
172
173                 public override int GetHashCode ()
174                 {
175                         byte [] bytes  = this.ToByteArray ();
176                         
177                         int result = 10;
178                         foreach (byte b in  bytes) {
179                                 result = 91 * result + b.GetHashCode ();
180                         }
181
182                         return result;
183                 }
184
185                 public static SqlBoolean GreaterThan (SqlGuid x, SqlGuid y)
186                 {
187                         return (x > y);
188                 }
189
190                 public static SqlBoolean GreaterThanOrEqual (SqlGuid x, SqlGuid y)
191                 {
192                         return (x >= y);
193                 }
194
195                 public static SqlBoolean LessThan (SqlGuid x, SqlGuid y)
196                 {
197                         return (x < y);
198                 }
199
200                 public static SqlBoolean LessThanOrEqual (SqlGuid x, SqlGuid y)
201                 {
202                         return (x <= y);
203                 }
204
205                 public static SqlBoolean NotEquals (SqlGuid x, SqlGuid y)
206                 {
207                         return (x != y);
208                 }
209
210                 public static SqlGuid Parse (string s)
211                 {
212                         return new SqlGuid (s);
213                 }
214
215                 public byte[] ToByteArray()
216                 {
217                         return value.ToByteArray ();
218                 }
219
220                 public SqlBinary ToSqlBinary ()
221                 {
222                         return ((SqlBinary)this);
223                 }
224
225                 public SqlString ToSqlString ()
226                 {
227                         return ((SqlString)this);
228                 }
229
230                 public override string ToString ()
231                 {
232                         if (!notNull)
233                                 return "Null";
234                         else
235                                 return value.ToString ();
236                 }
237
238                 public static SqlBoolean operator == (SqlGuid x, SqlGuid y)
239                 {
240                         if (x.IsNull || y.IsNull) return SqlBoolean.Null;
241                         return new SqlBoolean (x.Value == y.Value);
242                 }
243
244                 public static SqlBoolean operator > (SqlGuid x, SqlGuid y)
245                 {
246                         if (x.IsNull || y.IsNull)
247                                 return SqlBoolean.Null;
248
249                         if (x.Value.CompareTo (y.Value) > 0)
250                                 return new SqlBoolean (true);
251                         else
252                                 return new SqlBoolean (false);
253                 }
254
255                 public static SqlBoolean operator >= (SqlGuid x, SqlGuid y)
256                 {
257                         if (x.IsNull || y.IsNull)
258                                 return SqlBoolean.Null;
259                         
260                         if (x.Value.CompareTo (y.Value) >= 0)
261                                 return new SqlBoolean (true);
262                         else
263                                 return new SqlBoolean (false);
264
265                 }
266
267                 public static SqlBoolean operator != (SqlGuid x, SqlGuid y)
268                 {
269                         if (x.IsNull || y.IsNull) return SqlBoolean.Null;
270                         return new SqlBoolean (!(x.Value == y.Value));
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)
288                                 return SqlBoolean.Null;
289
290                         if (x.Value.CompareTo (y.Value) <= 0)
291                                 return new SqlBoolean (true);
292                         else
293                                 return new SqlBoolean (false);
294                 }
295
296                 public static explicit operator SqlGuid (SqlBinary x)
297                 {
298                         return new SqlGuid (x.Value);
299                 }
300
301                 public static explicit operator Guid (SqlGuid x)
302                 {
303                         return x.Value;
304                 }
305
306                 public static explicit operator SqlGuid (SqlString x)
307                 {
308                         return new SqlGuid (x.Value);
309                 }
310
311                 public static implicit operator SqlGuid (Guid x)
312                 {
313                         return new SqlGuid (x);
314                 }
315
316                 #endregion
317         }
318 }
319