2002-10-31 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 SqlByte))
122                                 return false;
123                         else
124                                 return (bool) (this == (SqlBoolean)value);
125                 }
126
127                 public static SqlBoolean Equals(SqlBoolean x, SqlBoolean y) 
128                 {
129                         return (x == y);
130                 }
131
132                 public override int GetHashCode() 
133                 {
134                         int hash;
135                         if (this.IsTrue)
136                                 hash = 1;
137                         else 
138                                 hash = 0;
139
140                         return hash;
141                 }
142
143                 public static SqlBoolean NotEquals(SqlBoolean x, SqlBoolean y) 
144                 {
145                         return (x != y);
146                 }
147
148                 public static SqlBoolean OnesComplement(SqlBoolean x) 
149                 {
150                         return ~x;
151                 }
152
153                 public static SqlBoolean Or(SqlBoolean x, SqlBoolean y) 
154                 {
155                         return (x | y);
156                 }
157
158                 public static SqlBoolean Parse(string s) 
159                 {
160                         return new SqlBoolean (Boolean.Parse (s));
161                 }
162
163                 public SqlByte ToSqlByte() 
164                 {
165                         return new SqlByte (value);
166                 }
167
168                 // **************************************************
169                 // Conversion from SqlBoolean to other SqlTypes
170                 // **************************************************
171
172                 public SqlDecimal ToSqlDecimal() 
173                 {
174                         return ((SqlDecimal)this);
175                 }
176
177                 public SqlDouble ToSqlDouble() 
178                 {
179                         return ((SqlDouble)this);
180                 }
181
182                 public SqlInt16 ToSqlInt16() 
183                 {
184                         return ((SqlInt16)this);
185                 }
186
187                 public SqlInt32 ToSqlInt32() 
188                 {
189                         return ((SqlInt32)this);
190                 }
191
192                 public SqlInt64 ToSqlInt64() 
193                 {
194                         return ((SqlInt64)this);
195                 }
196
197                 public SqlMoney ToSqlMoney() 
198                 {
199                         return ((SqlMoney)this);
200                 }
201
202                 public SqlSingle ToSqlSingle() 
203                 {
204                         return ((SqlSingle)this);
205                 }
206
207                 public SqlString ToSqlString() 
208                 {
209                         if (this.IsNull)
210                                 return new SqlString ("Null");
211                         if (this.IsTrue)
212                                 return new SqlString ("True");
213                         else
214                                 return new SqlString ("False");
215                 }
216
217                 public override string ToString() 
218                 {
219                         if (this.IsNull)
220                                 return "Null";
221                         if (this.IsTrue)
222                                 return "True";
223                         else
224                                 return "False";
225                 }
226
227                 // Bitwise exclusive-OR (XOR)
228                 public static SqlBoolean Xor(SqlBoolean x, SqlBoolean y) 
229                 {
230                         return (x ^ y);
231                 }
232
233                 // **************************************************
234                 // Public Operators
235                 // **************************************************
236
237                 // Bitwise AND
238                 public static SqlBoolean operator & (SqlBoolean x, SqlBoolean y)
239                 {
240                         return new SqlBoolean (x.Value & y.Value);
241                 }
242
243                 // Bitwise OR
244                 public static SqlBoolean operator | (SqlBoolean x, SqlBoolean y)
245                 {
246                         return new SqlBoolean (x.Value | y.Value);
247
248                 }
249
250                 // Compares two instances for 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                 // Bitwize exclusive-OR (XOR)
260                 public static SqlBoolean operator ^ (SqlBoolean x, SqlBoolean y) 
261                 {
262                         return new SqlBoolean (x.Value ^ y.Value);
263                 }
264
265                 // test Value of SqlBoolean to determine it is false.
266                 public static bool operator false (SqlBoolean x) 
267                 {
268                         return x.IsFalse;
269                 }
270
271                 // in-equality
272                 public static SqlBoolean operator != (SqlBoolean x, SqlBoolean y)
273                 {
274                         if (x.IsNull || y.IsNull) 
275                                 return SqlBoolean.Null;
276                         else
277                                 return new SqlBoolean (x.Value != y.Value);
278                 }
279
280                 // Logical NOT
281                 public static SqlBoolean operator ! (SqlBoolean x) 
282                 {
283                         if (x.IsNull)
284                                 return SqlBoolean.Null;
285                         else
286                                 return new SqlBoolean (!x.Value);
287                 }
288
289                 // One's Complement
290                 public static SqlBoolean operator ~ (SqlBoolean x) 
291                 {
292                         SqlBoolean b;
293                         if (x.IsTrue)
294                                 b = new SqlBoolean(false);
295                         else
296                                 b = new SqlBoolean(true);
297
298                         return b;
299                 }
300
301                 // test to see if value is true
302                 public static bool operator true (SqlBoolean x) 
303                 {
304                         return x.IsTrue;
305                 }
306
307                 // ****************************************
308                 // Type Conversion 
309                 // ****************************************
310
311                 
312                 // SqlBoolean to Boolean
313                 public static explicit operator bool (SqlBoolean x) 
314                 {
315                         return x.Value;
316                 }
317
318                 
319                 // SqlByte to SqlBoolean
320                 public static explicit operator SqlBoolean (SqlByte x) 
321                 {
322                         if (x.IsNull)
323                                 return Null;
324                         else
325                                 return new SqlBoolean ((int)x.Value);
326                 }
327
328                 // SqlDecimal to SqlBoolean
329                 public static explicit operator SqlBoolean (SqlDecimal x) 
330                 {
331                         if (x.IsNull)
332                                 return Null;
333                         else
334                                 return new SqlBoolean ((int)x.Value);
335                 }
336                 
337                 // SqlDouble to SqlBoolean
338                 public static explicit operator SqlBoolean (SqlDouble x) 
339                 {
340                         if (x.IsNull)
341                                 return Null;
342                         else
343                                 return new SqlBoolean ((int)x.Value);
344                 }
345
346                 // SqlInt16 to SqlBoolean
347                 public static explicit operator SqlBoolean (SqlInt16 x) 
348                 {
349                         if (x.IsNull)
350                                 return Null;
351                         else
352                                 return new SqlBoolean ((int)x.Value);
353                 }
354
355                 // SqlInt32 to SqlBoolean
356                 public static explicit operator SqlBoolean (SqlInt32 x) 
357                 {
358                         if (x.IsNull)
359                                 return Null;
360                         else
361                                 return new SqlBoolean (x.Value);
362                 }
363
364                 // SqlInt64 to SqlBoolean
365                 public static explicit operator SqlBoolean (SqlInt64 x) 
366                 {
367                         if (x.IsNull)
368                                 return Null;
369                         else
370                                 return new SqlBoolean ((int)x.Value);
371                 }
372
373                 // SqlMoney to SqlBoolean
374                 public static explicit operator SqlBoolean (SqlMoney x) 
375                 {
376                         if (x.IsNull)
377                                 return Null;
378                         else
379                                 return new SqlBoolean ((int)x.Value);
380                 }
381
382                 // SqlSingle to SqlBoolean
383                 public static explicit operator SqlBoolean (SqlSingle x) 
384                 {
385                         if (x.IsNull)
386                                 return Null;
387                         else
388                                 return new SqlBoolean ((int)x.Value);
389                 }
390
391                 // SqlString to SqlBoolean
392                 public static explicit operator SqlBoolean (SqlString x) 
393                 {
394                         return SqlBoolean.Parse (x.Value);
395                 }
396
397                 // Boolean to SqlBoolean
398                 public static implicit operator SqlBoolean (bool x) 
399                 {
400                         return new SqlBoolean (x);
401                 }
402         }
403 }