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