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