2002-11-07 Ville Palo <vi64pa@koti.soon.fi>
[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                         return new SqlBoolean (Boolean.Parse (s));
165                 }
166
167                 public SqlByte ToSqlByte() 
168                 {
169                         return new SqlByte (value);
170                 }
171
172                 // **************************************************
173                 // Conversion from SqlBoolean to other SqlTypes
174                 // **************************************************
175
176                 public SqlDecimal ToSqlDecimal() 
177                 {
178                         return ((SqlDecimal)this);
179                 }
180
181                 public SqlDouble ToSqlDouble() 
182                 {
183                         return ((SqlDouble)this);
184                 }
185
186                 public SqlInt16 ToSqlInt16() 
187                 {
188                         return ((SqlInt16)this);
189                 }
190
191                 public SqlInt32 ToSqlInt32() 
192                 {
193                         return ((SqlInt32)this);
194                 }
195
196                 public SqlInt64 ToSqlInt64() 
197                 {
198                         return ((SqlInt64)this);
199                 }
200
201                 public SqlMoney ToSqlMoney() 
202                 {
203                         return ((SqlMoney)this);
204                 }
205
206                 public SqlSingle ToSqlSingle() 
207                 {
208                         return ((SqlSingle)this);
209                 }
210
211                 public SqlString ToSqlString() 
212                 {
213                         if (this.IsNull)
214                                 return new SqlString ("Null");
215                         if (this.IsTrue)
216                                 return new SqlString ("True");
217                         else
218                                 return new SqlString ("False");
219                 }
220
221                 public override string ToString() 
222                 {
223                         if (this.IsNull)
224                                 return "Null";
225                         if (this.IsTrue)
226                                 return "True";
227                         else
228                                 return "False";
229                 }
230
231                 // Bitwise exclusive-OR (XOR)
232                 public static SqlBoolean Xor(SqlBoolean x, SqlBoolean y) 
233                 {
234                         return (x ^ y);
235                 }
236
237                 // **************************************************
238                 // Public Operators
239                 // **************************************************
240
241                 // Bitwise AND
242                 public static SqlBoolean operator & (SqlBoolean x, SqlBoolean y)
243                 {
244                         return new SqlBoolean (x.Value & y.Value);
245                 }
246
247                 // Bitwise OR
248                 public static SqlBoolean operator | (SqlBoolean x, SqlBoolean y)
249                 {
250                         return new SqlBoolean (x.Value | y.Value);
251
252                 }
253
254                 // Compares two instances for equality
255                 public static SqlBoolean operator == (SqlBoolean x, SqlBoolean y)
256                 {
257                         if (x.IsNull || y.IsNull) 
258                                 return SqlBoolean.Null;
259                         else
260                                 return new SqlBoolean (x.Value == y.Value);
261                 }
262                 
263                 // Bitwize exclusive-OR (XOR)
264                 public static SqlBoolean operator ^ (SqlBoolean x, SqlBoolean y) 
265                 {
266                         return new SqlBoolean (x.Value ^ y.Value);
267                 }
268
269                 // test Value of SqlBoolean to determine it is false.
270                 public static bool operator false (SqlBoolean x) 
271                 {
272                         return x.IsFalse;
273                 }
274
275                 // in-equality
276                 public static SqlBoolean operator != (SqlBoolean x, SqlBoolean y)
277                 {
278                         if (x.IsNull || y.IsNull) 
279                                 return SqlBoolean.Null;
280                         else
281                                 return new SqlBoolean (x.Value != y.Value);
282                 }
283
284                 // Logical NOT
285                 public static SqlBoolean operator ! (SqlBoolean x) 
286                 {
287                         if (x.IsNull)
288                                 return SqlBoolean.Null;
289                         else
290                                 return new SqlBoolean (!x.Value);
291                 }
292
293                 // One's Complement
294                 public static SqlBoolean operator ~ (SqlBoolean x) 
295                 {
296                         SqlBoolean b;
297                         if (x.IsTrue)
298                                 b = new SqlBoolean(false);
299                         else
300                                 b = new SqlBoolean(true);
301
302                         return b;
303                 }
304
305                 // test to see if value is true
306                 public static bool operator true (SqlBoolean x) 
307                 {
308                         return x.IsTrue;
309                 }
310
311                 // ****************************************
312                 // Type Conversion 
313                 // ****************************************
314
315                 
316                 // SqlBoolean to Boolean
317                 public static explicit operator bool (SqlBoolean x) 
318                 {
319                         return x.Value;
320                 }
321
322                 
323                 // SqlByte to SqlBoolean
324                 public static explicit operator SqlBoolean (SqlByte x) 
325                 {
326                         checked {
327                                 if (x.IsNull)
328                                         return Null;
329                                 else
330                                         return new SqlBoolean ((int)x.Value);
331                         }
332                 }
333
334                 // SqlDecimal to SqlBoolean
335                 public static explicit operator SqlBoolean (SqlDecimal x) 
336                 {
337                         checked {
338                                 if (x.IsNull)
339                                         return Null;
340                                 else
341                                         return new SqlBoolean ((int)x.Value);
342                         }
343                 }
344                 
345                 // SqlDouble to SqlBoolean
346                 public static explicit operator SqlBoolean (SqlDouble x) 
347                 {
348                         // FIXME
349                         //checked {
350                                 if (x.IsNull)
351                                         return Null;
352                                 else
353                                         return new SqlBoolean ((int)x.Value);
354                                 //}
355                 }
356
357                 // SqlInt16 to SqlBoolean
358                 public static explicit operator SqlBoolean (SqlInt16 x) 
359                 {
360                         checked {
361                                 if (x.IsNull)
362                                         return Null;
363                                 else
364                                         return new SqlBoolean ((int)x.Value);
365                         }
366                 }
367
368                 // SqlInt32 to SqlBoolean
369                 public static explicit operator SqlBoolean (SqlInt32 x) 
370                 {
371                         checked {
372                                 if (x.IsNull)
373                                         return Null;
374                                 else
375                                         return new SqlBoolean (x.Value);
376                         }
377                 }
378
379                 // SqlInt64 to SqlBoolean
380                 public static explicit operator SqlBoolean (SqlInt64 x) 
381                 {
382                         checked {
383                                 if (x.IsNull)
384                                         return Null;
385                                 else
386                                         return new SqlBoolean ((int)x.Value);
387                         }
388                 }
389
390                 // SqlMoney to SqlBoolean
391                 public static explicit operator SqlBoolean (SqlMoney x) 
392                 {
393                         checked {
394                                 if (x.IsNull)
395                                         return Null;
396                                 else
397                                         return new SqlBoolean ((int)x.Value);
398                         }
399                 }
400
401                 // SqlSingle to SqlBoolean
402                 public static explicit operator SqlBoolean (SqlSingle x) 
403                 {
404                         // FIXME
405                         //checked {
406                                 if (x.IsNull)
407                                         return Null;
408                                 else
409                                         return new SqlBoolean ((int)x.Value);
410                                 //}
411                 }
412
413                 // SqlString to SqlBoolean
414                 public static explicit operator SqlBoolean (SqlString x) 
415                 {
416                         checked {
417                                 return SqlBoolean.Parse (x.Value);
418                         }
419                 }
420
421                 // Boolean to SqlBoolean
422                 public static implicit operator SqlBoolean (bool x) 
423                 {
424                         return new SqlBoolean (x);
425                 }
426         }
427 }