2002-10-23 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                 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                 public SqlString ToSqlString() 
195                 {
196                         if (this.IsNull)
197                                 throw new SqlNullValueException ("The value is null");
198                         if (this.IsTrue)
199                                 return new SqlString ("True");
200                         else
201                                 return new SqlString ("False");
202                 }
203
204                 public override string ToString() 
205                 {
206                         if (this.IsNull)
207                                 throw new SqlNullValueException ("The value is null");
208                         if (this.IsTrue)
209                                 return "True";
210                         else
211                                 return "False";
212                 }
213
214                 // Bitwise exclusive-OR (XOR)
215                 public static SqlBoolean Xor(SqlBoolean x, SqlBoolean y) 
216                 {
217                         return (x ^ y);
218                 }
219
220                 // **************************************************
221                 // Public Operators
222                 // **************************************************
223
224                 // Bitwise AND
225                 public static SqlBoolean operator & (SqlBoolean x, SqlBoolean y)
226                 {
227                         return new SqlBoolean (x.Value & y.Value);
228                 }
229
230                 // Bitwise OR
231                 public static SqlBoolean operator | (SqlBoolean x, SqlBoolean y)
232                 {
233                         return new SqlBoolean (x.Value | y.Value);
234
235                 }
236
237                 // Compares two instances for equality
238                 public static SqlBoolean operator == (SqlBoolean x, SqlBoolean y)
239                 {
240                         if (x.IsNull || y.IsNull) 
241                                 return SqlBoolean.Null;
242                         else
243                                 return new SqlBoolean (x.Value == y.Value);
244                 }
245                 
246                 // Bitwize exclusive-OR (XOR)
247                 public static SqlBoolean operator ^ (SqlBoolean x, SqlBoolean y) 
248                 {
249                         return new SqlBoolean (x.Value ^ y.Value);
250                 }
251
252                 // test Value of SqlBoolean to determine it is false.
253                 public static bool operator false (SqlBoolean x) 
254                 {
255                         return x.IsFalse;
256                 }
257
258                 // in-equality
259                 public static SqlBoolean operator != (SqlBoolean x, SqlBoolean y)
260                 {
261                         if (x.IsNull || y.IsNull) 
262                                 return SqlBoolean.Null;
263                         else
264                                 return new SqlBoolean (x.Value != y.Value);
265                 }
266
267                 // Logical NOT
268                 public static SqlBoolean operator ! (SqlBoolean x) 
269                 {
270                         if (x.IsNull)
271                                 return SqlBoolean.Null;
272                         else
273                                 return new SqlBoolean (!x.Value);
274                 }
275
276                 // One's Complement
277                 public static SqlBoolean operator ~ (SqlBoolean x) 
278                 {
279                         return new SqlBoolean (~x.ByteValue);
280                 }
281
282                 // test to see if value is true
283                 public static bool operator true (SqlBoolean x) 
284                 {
285                         return x.IsTrue;
286                 }
287
288                 // ****************************************
289                 // Type Conversion 
290                 // ****************************************
291
292                 
293                 // SqlBoolean to Boolean
294                 public static explicit operator bool (SqlBoolean x) 
295                 {
296                         return x.Value;
297                 }
298
299                 
300                 // SqlByte to SqlBoolean
301                 public static explicit operator SqlBoolean (SqlByte x) 
302                 {
303                         if (x.IsNull)
304                                 return Null;
305                         else
306                                 return new SqlBoolean ((int)x.Value);
307                 }
308
309                 // SqlDecimal to SqlBoolean
310                 public static explicit operator SqlBoolean (SqlDecimal x) 
311                 {
312                         if (x.IsNull)
313                                 return Null;
314                         else
315                                 return new SqlBoolean ((int)x.Value);
316                 }
317                 
318                 // SqlDouble to SqlBoolean
319                 public static explicit operator SqlBoolean (SqlDouble x) 
320                 {
321                         if (x.IsNull)
322                                 return Null;
323                         else
324                                 return new SqlBoolean ((int)x.Value);
325                 }
326
327                 // SqlInt16 to SqlBoolean
328                 public static explicit operator SqlBoolean (SqlInt16 x) 
329                 {
330                         if (x.IsNull)
331                                 return Null;
332                         else
333                                 return new SqlBoolean ((int)x.Value);
334                 }
335
336                 // SqlInt32 to SqlBoolean
337                 public static explicit operator SqlBoolean (SqlInt32 x) 
338                 {
339                         if (x.IsNull)
340                                 return Null;
341                         else
342                                 return new SqlBoolean (x.Value);
343                 }
344
345                 // SqlInt64 to SqlBoolean
346                 public static explicit operator SqlBoolean (SqlInt64 x) 
347                 {
348                         if (x.IsNull)
349                                 return Null;
350                         else
351                                 return new SqlBoolean ((int)x.Value);
352                 }
353
354                 // SqlMoney to SqlBoolean
355                 public static explicit operator SqlBoolean (SqlMoney x) 
356                 {
357                         if (x.IsNull)
358                                 return Null;
359                         else
360                                 return new SqlBoolean ((int)x.Value);
361                 }
362
363                 // SqlSingle to SqlBoolean
364                 public static explicit operator SqlBoolean (SqlSingle x) 
365                 {
366                         if (x.IsNull)
367                                 return Null;
368                         else
369                                 return new SqlBoolean ((int)x.Value);
370                 }
371
372                 // SqlString to SqlBoolean
373                 public static explicit operator SqlBoolean (SqlString x) 
374                 {
375                         return SqlBoolean.Parse (x.Value);
376                 }
377
378                 // Boolean to SqlBoolean
379                 public static implicit operator SqlBoolean (bool x) 
380                 {
381                         return new SqlBoolean (x);
382                 }
383         }
384 }