2004-04-12 Atsushi Enomoto <atsushi@ximian.com>
[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 using System;
14 using System.Globalization;
15
16 namespace System.Data.SqlTypes
17 {
18         /// <summary>
19         /// Represents an integer value that is either 1 or 0 
20         /// to be stored in or retrieved from a database.
21         /// </summary>
22         public struct SqlBoolean : INullable, IComparable 
23         {
24
25                 #region Fields
26
27                 byte value;
28                 
29                 // default is false
30                 private bool notNull;
31
32                 public static readonly SqlBoolean False = new SqlBoolean (false);
33                 public static readonly SqlBoolean Null;
34                 public static readonly SqlBoolean One = new SqlBoolean (1);
35                 public static readonly SqlBoolean True = new SqlBoolean (true);
36                 public static readonly SqlBoolean Zero = new SqlBoolean (0);
37
38                 #endregion // Fields
39
40                 #region Constructors
41
42                 public SqlBoolean (bool value) 
43                 {
44                         this.value = (byte) (value ? 1 : 0);
45                         notNull = true;
46                 }
47
48                 public SqlBoolean (int value) 
49                 {
50                         this.value = (byte) (value != 0 ? 1 : 0);
51                         notNull = true;
52                 }
53
54                 #endregion // Constructors
55
56                 #region Properties
57
58                 public byte ByteValue {
59                         get {
60                                 if (this.IsNull)
61                                         throw new SqlNullValueException(Locale.GetText("The property is set to null."));
62                                 else
63                                         return value;
64                         }
65                 }
66
67                 public bool IsFalse {
68                         get { 
69                                 if (this.IsNull) 
70                                         return false;
71                                 else 
72                                         return (value == 0);
73                         }
74                 }
75
76                 public bool IsNull {
77                         get { 
78                                 return !notNull;
79                         }
80                 }
81
82                 public bool IsTrue {
83                         get { 
84                                 if (this.IsNull) 
85                                         return false;
86                                 else    
87                                         return (value != 0);
88                         }
89                 }
90
91                 public bool Value {
92                         get { 
93                                 if (this.IsNull)
94                                         throw new SqlNullValueException(Locale.GetText("The property is set to null."));
95                                 else
96                                         return this.IsTrue;
97                         }
98                 }
99
100                 #endregion // Properties
101
102                 public static SqlBoolean And (SqlBoolean x, SqlBoolean y) 
103                 {
104                         return (x & y);
105                 }
106
107                 public int CompareTo (object value) 
108                 {
109                         if (value == null)
110                                 return 1;
111                         else if (!(value is SqlBoolean))
112                                 throw new ArgumentException (Locale.GetText ("Value is not a System.Data.SqlTypes.SqlBoolean"));
113                         else if (((SqlBoolean)value).IsNull)
114                                 return 1;
115                         else
116                                 return this.value.CompareTo (((SqlBoolean)value).ByteValue);
117                 }
118
119                 public override bool Equals(object value) 
120                 {
121                         if (!(value is SqlBoolean))
122                                 return false;
123                         if (this.IsNull && ((SqlBoolean)value).IsNull)
124                                 return true;
125                         else if (((SqlBoolean)value).IsNull)
126                                 return false;
127                         else
128                                 return (bool) (this == (SqlBoolean)value);
129                 }
130
131                 public static SqlBoolean Equals(SqlBoolean x, SqlBoolean y) 
132                 {
133                         return (x == y);
134                 }
135
136                 public override int GetHashCode() 
137                 {
138                         int hash;
139                         if (this.IsTrue)
140                                 hash = 1;
141                         else 
142                                 hash = 0;
143
144                         return hash;
145                 }
146
147                 public static SqlBoolean NotEquals(SqlBoolean x, SqlBoolean y) 
148                 {
149                         return (x != y);
150                 }
151
152                 public static SqlBoolean OnesComplement(SqlBoolean x) 
153                 {
154                         return ~x;
155                 }
156
157                 public static SqlBoolean Or(SqlBoolean x, SqlBoolean y) 
158                 {
159                         return (x | y);
160                 }
161
162                 public static SqlBoolean Parse(string s) 
163                 {
164                         switch (s) {
165                         case "0":
166                                 return new SqlBoolean (false);
167                         case "1":
168                                 return new SqlBoolean (true);
169                         }
170                         return new SqlBoolean (Boolean.Parse (s));
171                 }
172
173                 public SqlByte ToSqlByte() 
174                 {
175                         return new SqlByte (value);
176                 }
177
178                 // **************************************************
179                 // Conversion from SqlBoolean to other SqlTypes
180                 // **************************************************
181
182                 public SqlDecimal ToSqlDecimal() 
183                 {
184                         return ((SqlDecimal)this);
185                 }
186
187                 public SqlDouble ToSqlDouble() 
188                 {
189                         return ((SqlDouble)this);
190                 }
191
192                 public SqlInt16 ToSqlInt16() 
193                 {
194                         return ((SqlInt16)this);
195                 }
196
197                 public SqlInt32 ToSqlInt32() 
198                 {
199                         return ((SqlInt32)this);
200                 }
201
202                 public SqlInt64 ToSqlInt64() 
203                 {
204                         return ((SqlInt64)this);
205                 }
206
207                 public SqlMoney ToSqlMoney() 
208                 {
209                         return ((SqlMoney)this);
210                 }
211
212                 public SqlSingle ToSqlSingle() 
213                 {
214                         return ((SqlSingle)this);
215                 }
216
217                 public SqlString ToSqlString() 
218                 {
219                         if (this.IsNull)
220                                 return new SqlString ("Null");
221                         if (this.IsTrue)
222                                 return new SqlString ("True");
223                         else
224                                 return new SqlString ("False");
225                 }
226
227                 public override string ToString() 
228                 {
229                         if (this.IsNull)
230                                 return "Null";
231                         if (this.IsTrue)
232                                 return "True";
233                         else
234                                 return "False";
235                 }
236
237                 // Bitwise exclusive-OR (XOR)
238                 public static SqlBoolean Xor(SqlBoolean x, SqlBoolean y) 
239                 {
240                         return (x ^ y);
241                 }
242
243                 // **************************************************
244                 // Public Operators
245                 // **************************************************
246
247                 // Bitwise AND
248                 public static SqlBoolean operator & (SqlBoolean x, SqlBoolean y)
249                 {
250                         return new SqlBoolean (x.Value & y.Value);
251                 }
252
253                 // Bitwise OR
254                 public static SqlBoolean operator | (SqlBoolean x, SqlBoolean y)
255                 {
256                         return new SqlBoolean (x.Value | y.Value);
257
258                 }
259
260                 // Compares two instances for equality
261                 public static SqlBoolean operator == (SqlBoolean x, SqlBoolean y)
262                 {
263                         if (x.IsNull || y.IsNull) 
264                                 return SqlBoolean.Null;
265                         else
266                                 return new SqlBoolean (x.Value == y.Value);
267                 }
268                 
269                 // Bitwize exclusive-OR (XOR)
270                 public static SqlBoolean operator ^ (SqlBoolean x, SqlBoolean y) 
271                 {
272                         return new SqlBoolean (x.Value ^ y.Value);
273                 }
274
275                 // test Value of SqlBoolean to determine it is false.
276                 public static bool operator false (SqlBoolean x) 
277                 {
278                         return x.IsFalse;
279                 }
280
281                 // in-equality
282                 public static SqlBoolean operator != (SqlBoolean x, SqlBoolean y)
283                 {
284                         if (x.IsNull || y.IsNull) 
285                                 return SqlBoolean.Null;
286                         else
287                                 return new SqlBoolean (x.Value != y.Value);
288                 }
289
290                 // Logical NOT
291                 public static SqlBoolean operator ! (SqlBoolean x) 
292                 {
293                         if (x.IsNull)
294                                 return SqlBoolean.Null;
295                         else
296                                 return new SqlBoolean (!x.Value);
297                 }
298
299                 // One's Complement
300                 public static SqlBoolean operator ~ (SqlBoolean x) 
301                 {
302                         SqlBoolean b;
303                         if (x.IsTrue)
304                                 b = new SqlBoolean(false);
305                         else
306                                 b = new SqlBoolean(true);
307
308                         return b;
309                 }
310
311                 // test to see if value is true
312                 public static bool operator true (SqlBoolean x) 
313                 {
314                         return x.IsTrue;
315                 }
316
317                 // ****************************************
318                 // Type Conversion 
319                 // ****************************************
320
321                 
322                 // SqlBoolean to Boolean
323                 public static explicit operator bool (SqlBoolean x) 
324                 {
325                         return x.Value;
326                 }
327
328                 
329                 // SqlByte to SqlBoolean
330                 public static explicit operator SqlBoolean (SqlByte x) 
331                 {
332                         checked {
333                                 if (x.IsNull)
334                                         return Null;
335                                 else
336                                         return new SqlBoolean ((int)x.Value);
337                         }
338                 }
339
340                 // SqlDecimal to SqlBoolean
341                 public static explicit operator SqlBoolean (SqlDecimal x) 
342                 {
343                         checked {
344                                 if (x.IsNull)
345                                         return Null;
346                                 else
347                                         return new SqlBoolean ((int)x.Value);
348                         }
349                 }
350                 
351                 // SqlDouble to SqlBoolean
352                 public static explicit operator SqlBoolean (SqlDouble x) 
353                 {
354                         // FIXME
355                         //checked {
356                                 if (x.IsNull)
357                                         return Null;
358                                 else
359                                         return new SqlBoolean ((int)x.Value);
360                                 //}
361                 }
362
363                 // SqlInt16 to SqlBoolean
364                 public static explicit operator SqlBoolean (SqlInt16 x) 
365                 {
366                         checked {
367                                 if (x.IsNull)
368                                         return Null;
369                                 else
370                                         return new SqlBoolean ((int)x.Value);
371                         }
372                 }
373
374                 // SqlInt32 to SqlBoolean
375                 public static explicit operator SqlBoolean (SqlInt32 x) 
376                 {
377                         checked {
378                                 if (x.IsNull)
379                                         return Null;
380                                 else
381                                         return new SqlBoolean (x.Value);
382                         }
383                 }
384
385                 // SqlInt64 to SqlBoolean
386                 public static explicit operator SqlBoolean (SqlInt64 x) 
387                 {
388                         checked {
389                                 if (x.IsNull)
390                                         return Null;
391                                 else
392                                         return new SqlBoolean ((int)x.Value);
393                         }
394                 }
395
396                 // SqlMoney to SqlBoolean
397                 public static explicit operator SqlBoolean (SqlMoney x) 
398                 {
399                         checked {
400                                 if (x.IsNull)
401                                         return Null;
402                                 else
403                                         return new SqlBoolean ((int)x.Value);
404                         }
405                 }
406
407                 // SqlSingle to SqlBoolean
408                 public static explicit operator SqlBoolean (SqlSingle x) 
409                 {
410                         // FIXME
411                         //checked {
412                                 if (x.IsNull)
413                                         return Null;
414                                 else
415                                         return new SqlBoolean ((int)x.Value);
416                                 //}
417                 }
418
419                 // SqlString to SqlBoolean
420                 public static explicit operator SqlBoolean (SqlString x) 
421                 {
422                         checked {
423                                 if (x.IsNull)
424                                         return Null;
425                                 return SqlBoolean.Parse (x.Value);
426                         }
427                 }
428
429                 // Boolean to SqlBoolean
430                 public static implicit operator SqlBoolean (bool x) 
431                 {
432                         return new SqlBoolean (x);
433                 }
434         }
435 }