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