Copy from 72246 to trunk
[mono.git] / mcs / class / System.Data / System.Data.SqlTypes / SqlBoolean.cs
1 //
2 // System.Data.SqlTypes.SqlBoolean
3 //
4 // Author:
5 //   Rodrigo Moya (rodrigo@ximian.com)
6 //   Daniel Morgan (danmorg@sc.rr.com)
7 //   Tim Coleman (tim@timcoleman.com)
8 //
9 // (C) Ximian, Inc. 2002
10 // (C) Copyright 2002 Tim Coleman
11 //
12
13 //
14 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
15 //
16 // Permission is hereby granted, free of charge, to any person obtaining
17 // a copy of this software and associated documentation files (the
18 // "Software"), to deal in the Software without restriction, including
19 // without limitation the rights to use, copy, modify, merge, publish,
20 // distribute, sublicense, and/or sell copies of the Software, and to
21 // permit persons to whom the Software is furnished to do so, subject to
22 // the following conditions:
23 // 
24 // The above copyright notice and this permission notice shall be
25 // included in all copies or substantial portions of the Software.
26 // 
27 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
28 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
29 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
30 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
31 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
32 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
33 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 //
35
36 using System;
37 using System.Globalization;
38
39 namespace System.Data.SqlTypes
40 {
41         /// <summary>
42         /// Represents an integer value that is either 1 or 0 
43         /// to be stored in or retrieved from a database.
44         /// </summary>
45         public struct SqlBoolean : INullable, IComparable 
46         {
47
48                 #region Fields
49
50                 byte value;
51                 
52                 // default is false
53                 private bool notNull;
54
55                 public static readonly SqlBoolean False = new SqlBoolean (false);
56                 public static readonly SqlBoolean Null;
57                 public static readonly SqlBoolean One = new SqlBoolean (1);
58                 public static readonly SqlBoolean True = new SqlBoolean (true);
59                 public static readonly SqlBoolean Zero = new SqlBoolean (0);
60
61                 #endregion // Fields
62
63                 #region Constructors
64
65                 public SqlBoolean (bool value) 
66                 {
67                         this.value = (byte) (value ? 1 : 0);
68                         notNull = true;
69                 }
70
71                 public SqlBoolean (int value) 
72                 {
73                         this.value = (byte) (value != 0 ? 1 : 0);
74                         notNull = true;
75                 }
76
77                 #endregion // Constructors
78
79                 #region Properties
80
81                 public byte ByteValue {
82                         get {
83                                 if (this.IsNull)
84                                         throw new SqlNullValueException(Locale.GetText("The property is set to null."));
85                                 else
86                                         return value;
87                         }
88                 }
89
90                 public bool IsFalse {
91                         get { 
92                                 if (this.IsNull) 
93                                         return false;
94                                 else 
95                                         return (value == 0);
96                         }
97                 }
98
99                 public bool IsNull {
100                         get { 
101                                 return !notNull;
102                         }
103                 }
104
105                 public bool IsTrue {
106                         get { 
107                                 if (this.IsNull) 
108                                         return false;
109                                 else    
110                                         return (value != 0);
111                         }
112                 }
113
114                 public bool Value {
115                         get { 
116                                 if (this.IsNull)
117                                         throw new SqlNullValueException(Locale.GetText("The property is set to null."));
118                                 else
119                                         return this.IsTrue;
120                         }
121                 }
122
123                 #endregion // Properties
124
125                 public static SqlBoolean And (SqlBoolean x, SqlBoolean y) 
126                 {
127                         return (x & y);
128                 }
129
130                 public int CompareTo (object value)
131                 {
132                         if (value == null)
133                                 return 1;
134                         if (!(value is SqlBoolean))
135                                 throw new ArgumentException (Locale.GetText ("Value is not a System.Data.SqlTypes.SqlBoolean"));
136
137                         return CompareTo ((SqlBoolean) value);
138                 }
139 #if NET_2_0
140                 public
141 #endif
142                 int CompareTo (SqlBoolean value) 
143                 {
144                         if (value.IsNull)
145                                 return 1;
146                         else
147                                 return this.value.CompareTo (value.ByteValue);
148                 }
149
150                 public override bool Equals(object value) 
151                 {
152                         if (!(value is SqlBoolean))
153                                 return false;
154                         if (this.IsNull && ((SqlBoolean)value).IsNull)
155                                 return true;
156                         else if (((SqlBoolean)value).IsNull)
157                                 return false;
158                         else
159                                 return (bool) (this == (SqlBoolean)value);
160                 }
161
162                 public static SqlBoolean Equals(SqlBoolean x, SqlBoolean y) 
163                 {
164                         return (x == y);
165                 }
166
167                 public override int GetHashCode() 
168                 {
169                         int hash;
170                         if (this.IsTrue)
171                                 hash = 1;
172                         else 
173                                 hash = 0;
174
175                         return hash;
176                 }
177
178                 public static SqlBoolean NotEquals(SqlBoolean x, SqlBoolean y) 
179                 {
180                         return (x != y);
181                 }
182
183                 public static SqlBoolean OnesComplement(SqlBoolean x) 
184                 {
185                         return ~x;
186                 }
187
188                 public static SqlBoolean Or(SqlBoolean x, SqlBoolean y) 
189                 {
190                         return (x | y);
191                 }
192
193                 public static SqlBoolean Parse(string s) 
194                 {
195                         switch (s) {
196                         case "0":
197                                 return new SqlBoolean (false);
198                         case "1":
199                                 return new SqlBoolean (true);
200                         }
201                         return new SqlBoolean (Boolean.Parse (s));
202                 }
203
204                 public SqlByte ToSqlByte() 
205                 {
206                         return new SqlByte (value);
207                 }
208
209                 // **************************************************
210                 // Conversion from SqlBoolean to other SqlTypes
211                 // **************************************************
212
213                 public SqlDecimal ToSqlDecimal() 
214                 {
215                         return ((SqlDecimal)this);
216                 }
217
218                 public SqlDouble ToSqlDouble() 
219                 {
220                         return ((SqlDouble)this);
221                 }
222
223                 public SqlInt16 ToSqlInt16() 
224                 {
225                         return ((SqlInt16)this);
226                 }
227
228                 public SqlInt32 ToSqlInt32() 
229                 {
230                         return ((SqlInt32)this);
231                 }
232
233                 public SqlInt64 ToSqlInt64() 
234                 {
235                         return ((SqlInt64)this);
236                 }
237
238                 public SqlMoney ToSqlMoney() 
239                 {
240                         return ((SqlMoney)this);
241                 }
242
243                 public SqlSingle ToSqlSingle() 
244                 {
245                         return ((SqlSingle)this);
246                 }
247
248                 public SqlString ToSqlString() 
249                 {
250                         if (this.IsNull)
251                                 return new SqlString ("Null");
252                         if (this.IsTrue)
253                                 return new SqlString ("True");
254                         else
255                                 return new SqlString ("False");
256                 }
257
258                 public override string ToString() 
259                 {
260                         if (this.IsNull)
261                                 return "Null";
262                         if (this.IsTrue)
263                                 return "True";
264                         else
265                                 return "False";
266                 }
267
268                 // Bitwise exclusive-OR (XOR)
269                 public static SqlBoolean Xor(SqlBoolean x, SqlBoolean y) 
270                 {
271                         return (x ^ y);
272                 }
273
274                 // **************************************************
275                 // Public Operators
276                 // **************************************************
277
278                 // Bitwise AND
279                 public static SqlBoolean operator & (SqlBoolean x, SqlBoolean y)
280                 {
281                         return new SqlBoolean (x.Value & y.Value);
282                 }
283
284                 // Bitwise OR
285                 public static SqlBoolean operator | (SqlBoolean x, SqlBoolean y)
286                 {
287                         return new SqlBoolean (x.Value | y.Value);
288
289                 }
290
291                 // Compares two instances for equality
292                 public static SqlBoolean operator == (SqlBoolean x, SqlBoolean y)
293                 {
294                         if (x.IsNull || y.IsNull) 
295                                 return SqlBoolean.Null;
296                         else
297                                 return new SqlBoolean (x.Value == y.Value);
298                 }
299                 
300                 // Bitwize exclusive-OR (XOR)
301                 public static SqlBoolean operator ^ (SqlBoolean x, SqlBoolean y) 
302                 {
303                         return new SqlBoolean (x.Value ^ y.Value);
304                 }
305
306                 // test Value of SqlBoolean to determine it is false.
307                 public static bool operator false (SqlBoolean x) 
308                 {
309                         return x.IsFalse;
310                 }
311
312                 // in-equality
313                 public static SqlBoolean operator != (SqlBoolean x, SqlBoolean y)
314                 {
315                         if (x.IsNull || y.IsNull) 
316                                 return SqlBoolean.Null;
317                         else
318                                 return new SqlBoolean (x.Value != y.Value);
319                 }
320
321                 // Logical NOT
322                 public static SqlBoolean operator ! (SqlBoolean x) 
323                 {
324                         if (x.IsNull)
325                                 return SqlBoolean.Null;
326                         else
327                                 return new SqlBoolean (!x.Value);
328                 }
329
330                 // One's Complement
331                 public static SqlBoolean operator ~ (SqlBoolean x) 
332                 {
333                         SqlBoolean b;
334                         if (x.IsTrue)
335                                 b = new SqlBoolean(false);
336                         else
337                                 b = new SqlBoolean(true);
338
339                         return b;
340                 }
341
342                 // test to see if value is true
343                 public static bool operator true (SqlBoolean x) 
344                 {
345                         return x.IsTrue;
346                 }
347
348                 // ****************************************
349                 // Type Conversion 
350                 // ****************************************
351
352                 
353                 // SqlBoolean to Boolean
354                 public static explicit operator bool (SqlBoolean x) 
355                 {
356                         return x.Value;
357                 }
358
359                 
360                 // SqlByte to SqlBoolean
361                 public static explicit operator SqlBoolean (SqlByte x) 
362                 {
363                         checked {
364                                 if (x.IsNull)
365                                         return Null;
366                                 else
367                                         return new SqlBoolean ((int)x.Value);
368                         }
369                 }
370
371                 // SqlDecimal to SqlBoolean
372                 public static explicit operator SqlBoolean (SqlDecimal x) 
373                 {
374                         checked {
375                                 if (x.IsNull)
376                                         return Null;
377                                 else
378                                         return new SqlBoolean ((int)x.Value);
379                         }
380                 }
381                 
382                 // SqlDouble to SqlBoolean
383                 public static explicit operator SqlBoolean (SqlDouble x) 
384                 {
385                         // FIXME
386                         //checked {
387                                 if (x.IsNull)
388                                         return Null;
389                                 else
390                                         return new SqlBoolean ((int)x.Value);
391                                 //}
392                 }
393
394                 // SqlInt16 to SqlBoolean
395                 public static explicit operator SqlBoolean (SqlInt16 x) 
396                 {
397                         checked {
398                                 if (x.IsNull)
399                                         return Null;
400                                 else
401                                         return new SqlBoolean ((int)x.Value);
402                         }
403                 }
404
405                 // SqlInt32 to SqlBoolean
406                 public static explicit operator SqlBoolean (SqlInt32 x) 
407                 {
408                         checked {
409                                 if (x.IsNull)
410                                         return Null;
411                                 else
412                                         return new SqlBoolean (x.Value);
413                         }
414                 }
415
416                 // SqlInt64 to SqlBoolean
417                 public static explicit operator SqlBoolean (SqlInt64 x) 
418                 {
419                         checked {
420                                 if (x.IsNull)
421                                         return Null;
422                                 else
423                                         return new SqlBoolean ((int)x.Value);
424                         }
425                 }
426
427                 // SqlMoney to SqlBoolean
428                 public static explicit operator SqlBoolean (SqlMoney x) 
429                 {
430                         checked {
431                                 if (x.IsNull)
432                                         return Null;
433                                 else
434                                         return new SqlBoolean ((int)x.Value);
435                         }
436                 }
437
438                 // SqlSingle to SqlBoolean
439                 public static explicit operator SqlBoolean (SqlSingle x) 
440                 {
441                         // FIXME
442                         //checked {
443                                 if (x.IsNull)
444                                         return Null;
445                                 else
446                                         return new SqlBoolean ((int)x.Value);
447                                 //}
448                 }
449
450                 // SqlString to SqlBoolean
451                 public static explicit operator SqlBoolean (SqlString x) 
452                 {
453                         checked {
454                                 if (x.IsNull)
455                                         return Null;
456                                 return SqlBoolean.Parse (x.Value);
457                         }
458                 }
459
460                 // Boolean to SqlBoolean
461                 public static implicit operator SqlBoolean (bool x) 
462                 {
463                         return new SqlBoolean (x);
464                 }
465         }
466 }