This commit was manufactured by cvs2svn to create branch 'mono-1-0'.
[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                         else if (!(value is SqlBoolean))
135                                 throw new ArgumentException (Locale.GetText ("Value is not a System.Data.SqlTypes.SqlBoolean"));
136                         else if (((SqlBoolean)value).IsNull)
137                                 return 1;
138                         else
139                                 return this.value.CompareTo (((SqlBoolean)value).ByteValue);
140                 }
141
142                 public override bool Equals(object value) 
143                 {
144                         if (!(value is SqlBoolean))
145                                 return false;
146                         if (this.IsNull && ((SqlBoolean)value).IsNull)
147                                 return true;
148                         else if (((SqlBoolean)value).IsNull)
149                                 return false;
150                         else
151                                 return (bool) (this == (SqlBoolean)value);
152                 }
153
154                 public static SqlBoolean Equals(SqlBoolean x, SqlBoolean y) 
155                 {
156                         return (x == y);
157                 }
158
159                 public override int GetHashCode() 
160                 {
161                         int hash;
162                         if (this.IsTrue)
163                                 hash = 1;
164                         else 
165                                 hash = 0;
166
167                         return hash;
168                 }
169
170                 public static SqlBoolean NotEquals(SqlBoolean x, SqlBoolean y) 
171                 {
172                         return (x != y);
173                 }
174
175                 public static SqlBoolean OnesComplement(SqlBoolean x) 
176                 {
177                         return ~x;
178                 }
179
180                 public static SqlBoolean Or(SqlBoolean x, SqlBoolean y) 
181                 {
182                         return (x | y);
183                 }
184
185                 public static SqlBoolean Parse(string s) 
186                 {
187                         switch (s) {
188                         case "0":
189                                 return new SqlBoolean (false);
190                         case "1":
191                                 return new SqlBoolean (true);
192                         }
193                         return new SqlBoolean (Boolean.Parse (s));
194                 }
195
196                 public SqlByte ToSqlByte() 
197                 {
198                         return new SqlByte (value);
199                 }
200
201                 // **************************************************
202                 // Conversion from SqlBoolean to other SqlTypes
203                 // **************************************************
204
205                 public SqlDecimal ToSqlDecimal() 
206                 {
207                         return ((SqlDecimal)this);
208                 }
209
210                 public SqlDouble ToSqlDouble() 
211                 {
212                         return ((SqlDouble)this);
213                 }
214
215                 public SqlInt16 ToSqlInt16() 
216                 {
217                         return ((SqlInt16)this);
218                 }
219
220                 public SqlInt32 ToSqlInt32() 
221                 {
222                         return ((SqlInt32)this);
223                 }
224
225                 public SqlInt64 ToSqlInt64() 
226                 {
227                         return ((SqlInt64)this);
228                 }
229
230                 public SqlMoney ToSqlMoney() 
231                 {
232                         return ((SqlMoney)this);
233                 }
234
235                 public SqlSingle ToSqlSingle() 
236                 {
237                         return ((SqlSingle)this);
238                 }
239
240                 public SqlString ToSqlString() 
241                 {
242                         if (this.IsNull)
243                                 return new SqlString ("Null");
244                         if (this.IsTrue)
245                                 return new SqlString ("True");
246                         else
247                                 return new SqlString ("False");
248                 }
249
250                 public override string ToString() 
251                 {
252                         if (this.IsNull)
253                                 return "Null";
254                         if (this.IsTrue)
255                                 return "True";
256                         else
257                                 return "False";
258                 }
259
260                 // Bitwise exclusive-OR (XOR)
261                 public static SqlBoolean Xor(SqlBoolean x, SqlBoolean y) 
262                 {
263                         return (x ^ y);
264                 }
265
266                 // **************************************************
267                 // Public Operators
268                 // **************************************************
269
270                 // Bitwise AND
271                 public static SqlBoolean operator & (SqlBoolean x, SqlBoolean y)
272                 {
273                         return new SqlBoolean (x.Value & y.Value);
274                 }
275
276                 // Bitwise OR
277                 public static SqlBoolean operator | (SqlBoolean x, SqlBoolean y)
278                 {
279                         return new SqlBoolean (x.Value | y.Value);
280
281                 }
282
283                 // Compares two instances for equality
284                 public static SqlBoolean operator == (SqlBoolean x, SqlBoolean y)
285                 {
286                         if (x.IsNull || y.IsNull) 
287                                 return SqlBoolean.Null;
288                         else
289                                 return new SqlBoolean (x.Value == y.Value);
290                 }
291                 
292                 // Bitwize exclusive-OR (XOR)
293                 public static SqlBoolean operator ^ (SqlBoolean x, SqlBoolean y) 
294                 {
295                         return new SqlBoolean (x.Value ^ y.Value);
296                 }
297
298                 // test Value of SqlBoolean to determine it is false.
299                 public static bool operator false (SqlBoolean x) 
300                 {
301                         return x.IsFalse;
302                 }
303
304                 // in-equality
305                 public static SqlBoolean operator != (SqlBoolean x, SqlBoolean y)
306                 {
307                         if (x.IsNull || y.IsNull) 
308                                 return SqlBoolean.Null;
309                         else
310                                 return new SqlBoolean (x.Value != y.Value);
311                 }
312
313                 // Logical NOT
314                 public static SqlBoolean operator ! (SqlBoolean x) 
315                 {
316                         if (x.IsNull)
317                                 return SqlBoolean.Null;
318                         else
319                                 return new SqlBoolean (!x.Value);
320                 }
321
322                 // One's Complement
323                 public static SqlBoolean operator ~ (SqlBoolean x) 
324                 {
325                         SqlBoolean b;
326                         if (x.IsTrue)
327                                 b = new SqlBoolean(false);
328                         else
329                                 b = new SqlBoolean(true);
330
331                         return b;
332                 }
333
334                 // test to see if value is true
335                 public static bool operator true (SqlBoolean x) 
336                 {
337                         return x.IsTrue;
338                 }
339
340                 // ****************************************
341                 // Type Conversion 
342                 // ****************************************
343
344                 
345                 // SqlBoolean to Boolean
346                 public static explicit operator bool (SqlBoolean x) 
347                 {
348                         return x.Value;
349                 }
350
351                 
352                 // SqlByte to SqlBoolean
353                 public static explicit operator SqlBoolean (SqlByte x) 
354                 {
355                         checked {
356                                 if (x.IsNull)
357                                         return Null;
358                                 else
359                                         return new SqlBoolean ((int)x.Value);
360                         }
361                 }
362
363                 // SqlDecimal to SqlBoolean
364                 public static explicit operator SqlBoolean (SqlDecimal x) 
365                 {
366                         checked {
367                                 if (x.IsNull)
368                                         return Null;
369                                 else
370                                         return new SqlBoolean ((int)x.Value);
371                         }
372                 }
373                 
374                 // SqlDouble to SqlBoolean
375                 public static explicit operator SqlBoolean (SqlDouble x) 
376                 {
377                         // FIXME
378                         //checked {
379                                 if (x.IsNull)
380                                         return Null;
381                                 else
382                                         return new SqlBoolean ((int)x.Value);
383                                 //}
384                 }
385
386                 // SqlInt16 to SqlBoolean
387                 public static explicit operator SqlBoolean (SqlInt16 x) 
388                 {
389                         checked {
390                                 if (x.IsNull)
391                                         return Null;
392                                 else
393                                         return new SqlBoolean ((int)x.Value);
394                         }
395                 }
396
397                 // SqlInt32 to SqlBoolean
398                 public static explicit operator SqlBoolean (SqlInt32 x) 
399                 {
400                         checked {
401                                 if (x.IsNull)
402                                         return Null;
403                                 else
404                                         return new SqlBoolean (x.Value);
405                         }
406                 }
407
408                 // SqlInt64 to SqlBoolean
409                 public static explicit operator SqlBoolean (SqlInt64 x) 
410                 {
411                         checked {
412                                 if (x.IsNull)
413                                         return Null;
414                                 else
415                                         return new SqlBoolean ((int)x.Value);
416                         }
417                 }
418
419                 // SqlMoney to SqlBoolean
420                 public static explicit operator SqlBoolean (SqlMoney x) 
421                 {
422                         checked {
423                                 if (x.IsNull)
424                                         return Null;
425                                 else
426                                         return new SqlBoolean ((int)x.Value);
427                         }
428                 }
429
430                 // SqlSingle to SqlBoolean
431                 public static explicit operator SqlBoolean (SqlSingle x) 
432                 {
433                         // FIXME
434                         //checked {
435                                 if (x.IsNull)
436                                         return Null;
437                                 else
438                                         return new SqlBoolean ((int)x.Value);
439                                 //}
440                 }
441
442                 // SqlString to SqlBoolean
443                 public static explicit operator SqlBoolean (SqlString x) 
444                 {
445                         checked {
446                                 if (x.IsNull)
447                                         return Null;
448                                 return SqlBoolean.Parse (x.Value);
449                         }
450                 }
451
452                 // Boolean to SqlBoolean
453                 public static implicit operator SqlBoolean (bool x) 
454                 {
455                         return new SqlBoolean (x);
456                 }
457         }
458 }