2 // System.Data.SqlTypes.SqlBoolean
5 // Rodrigo Moya (rodrigo@ximian.com)
6 // Daniel Morgan (danmorg@sc.rr.com)
7 // Tim Coleman (tim@timcoleman.com)
9 // (C) Ximian, Inc. 2002
10 // (C) Copyright 2002 Tim Coleman
14 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
16 // Permission is hereby granted, free of charge, to any person obtaining
17 // a copy of this software and associated documentation files (the
18 // "Software"), to deal in the Software without restriction, including
19 // without limitation the rights to use, copy, modify, merge, publish,
20 // distribute, sublicense, and/or sell copies of the Software, and to
21 // permit persons to whom the Software is furnished to do so, subject to
22 // the following conditions:
24 // The above copyright notice and this permission notice shall be
25 // included in all copies or substantial portions of the Software.
27 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
28 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
29 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
30 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
31 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
32 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
33 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
37 using System.Globalization;
39 namespace System.Data.SqlTypes
42 /// Represents an integer value that is either 1 or 0
43 /// to be stored in or retrieved from a database.
45 public struct SqlBoolean : INullable, IComparable
55 public static readonly SqlBoolean False = new SqlBoolean (false);
56 public static readonly SqlBoolean Null;
57 public static readonly SqlBoolean One = new SqlBoolean (1);
58 public static readonly SqlBoolean True = new SqlBoolean (true);
59 public static readonly SqlBoolean Zero = new SqlBoolean (0);
65 public SqlBoolean (bool value)
67 this.value = (byte) (value ? 1 : 0);
71 public SqlBoolean (int value)
73 this.value = (byte) (value != 0 ? 1 : 0);
77 #endregion // Constructors
81 public byte ByteValue {
84 throw new SqlNullValueException(Locale.GetText("The property is set to null."));
117 throw new SqlNullValueException(Locale.GetText("The property is set to null."));
123 #endregion // Properties
125 public static SqlBoolean And (SqlBoolean x, SqlBoolean y)
130 public int CompareTo (object value)
134 if (!(value is SqlBoolean))
135 throw new ArgumentException (Locale.GetText ("Value is not a System.Data.SqlTypes.SqlBoolean"));
137 return CompareTo ((SqlBoolean) value);
142 int CompareTo (SqlBoolean value)
147 return this.value.CompareTo (value.ByteValue);
150 public override bool Equals(object value)
152 if (!(value is SqlBoolean))
154 if (this.IsNull && ((SqlBoolean)value).IsNull)
156 else if (((SqlBoolean)value).IsNull)
159 return (bool) (this == (SqlBoolean)value);
162 public static SqlBoolean Equals(SqlBoolean x, SqlBoolean y)
167 public override int GetHashCode()
178 public static SqlBoolean NotEquals(SqlBoolean x, SqlBoolean y)
183 public static SqlBoolean OnesComplement(SqlBoolean x)
188 public static SqlBoolean Or(SqlBoolean x, SqlBoolean y)
193 public static SqlBoolean Parse(string s)
197 return new SqlBoolean (false);
199 return new SqlBoolean (true);
201 return new SqlBoolean (Boolean.Parse (s));
204 public SqlByte ToSqlByte()
206 return new SqlByte (value);
209 // **************************************************
210 // Conversion from SqlBoolean to other SqlTypes
211 // **************************************************
213 public SqlDecimal ToSqlDecimal()
215 return ((SqlDecimal)this);
218 public SqlDouble ToSqlDouble()
220 return ((SqlDouble)this);
223 public SqlInt16 ToSqlInt16()
225 return ((SqlInt16)this);
228 public SqlInt32 ToSqlInt32()
230 return ((SqlInt32)this);
233 public SqlInt64 ToSqlInt64()
235 return ((SqlInt64)this);
238 public SqlMoney ToSqlMoney()
240 return ((SqlMoney)this);
243 public SqlSingle ToSqlSingle()
245 return ((SqlSingle)this);
248 public SqlString ToSqlString()
251 return new SqlString ("Null");
253 return new SqlString ("True");
255 return new SqlString ("False");
258 public override string ToString()
268 // Bitwise exclusive-OR (XOR)
269 public static SqlBoolean Xor(SqlBoolean x, SqlBoolean y)
274 // **************************************************
276 // **************************************************
279 public static SqlBoolean operator & (SqlBoolean x, SqlBoolean y)
281 return new SqlBoolean (x.Value & y.Value);
285 public static SqlBoolean operator | (SqlBoolean x, SqlBoolean y)
287 return new SqlBoolean (x.Value | y.Value);
291 // Compares two instances for equality
292 public static SqlBoolean operator == (SqlBoolean x, SqlBoolean y)
294 if (x.IsNull || y.IsNull)
295 return SqlBoolean.Null;
297 return new SqlBoolean (x.Value == y.Value);
300 // Bitwize exclusive-OR (XOR)
301 public static SqlBoolean operator ^ (SqlBoolean x, SqlBoolean y)
303 return new SqlBoolean (x.Value ^ y.Value);
306 // test Value of SqlBoolean to determine it is false.
307 public static bool operator false (SqlBoolean x)
313 public static SqlBoolean operator != (SqlBoolean x, SqlBoolean y)
315 if (x.IsNull || y.IsNull)
316 return SqlBoolean.Null;
318 return new SqlBoolean (x.Value != y.Value);
322 public static SqlBoolean operator ! (SqlBoolean x)
325 return SqlBoolean.Null;
327 return new SqlBoolean (!x.Value);
331 public static SqlBoolean operator ~ (SqlBoolean x)
335 b = new SqlBoolean(false);
337 b = new SqlBoolean(true);
342 // test to see if value is true
343 public static bool operator true (SqlBoolean x)
348 // ****************************************
350 // ****************************************
353 // SqlBoolean to Boolean
354 public static explicit operator bool (SqlBoolean x)
360 // SqlByte to SqlBoolean
361 public static explicit operator SqlBoolean (SqlByte x)
367 return new SqlBoolean ((int)x.Value);
371 // SqlDecimal to SqlBoolean
372 public static explicit operator SqlBoolean (SqlDecimal x)
378 return new SqlBoolean ((int)x.Value);
382 // SqlDouble to SqlBoolean
383 public static explicit operator SqlBoolean (SqlDouble x)
390 return new SqlBoolean ((int)x.Value);
394 // SqlInt16 to SqlBoolean
395 public static explicit operator SqlBoolean (SqlInt16 x)
401 return new SqlBoolean ((int)x.Value);
405 // SqlInt32 to SqlBoolean
406 public static explicit operator SqlBoolean (SqlInt32 x)
412 return new SqlBoolean (x.Value);
416 // SqlInt64 to SqlBoolean
417 public static explicit operator SqlBoolean (SqlInt64 x)
423 return new SqlBoolean ((int)x.Value);
427 // SqlMoney to SqlBoolean
428 public static explicit operator SqlBoolean (SqlMoney x)
434 return new SqlBoolean ((int)x.Value);
438 // SqlSingle to SqlBoolean
439 public static explicit operator SqlBoolean (SqlSingle x)
446 return new SqlBoolean ((int)x.Value);
450 // SqlString to SqlBoolean
451 public static explicit operator SqlBoolean (SqlString x)
456 return SqlBoolean.Parse (x.Value);
460 // Boolean to SqlBoolean
461 public static implicit operator SqlBoolean (bool x)
463 return new SqlBoolean (x);