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