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